Add and use base::make_weak_unique() helper.

This commit is contained in:
John Preston 2017-08-15 12:35:01 +03:00
parent 0880c01a20
commit 782114d644
5 changed files with 23 additions and 13 deletions

View file

@ -114,13 +114,23 @@ inline bool operator!=(std::nullptr_t, const weak_unique_ptr<T> &pointer) {
return !(pointer == nullptr);
}
template <typename T>
weak_unique_ptr<T> make_weak_unique(T *value) {
return weak_unique_ptr<T>(value);
}
template <typename T>
weak_unique_ptr<T> make_weak_unique(const std::unique_ptr<T> &value) {
return weak_unique_ptr<T>(value);
}
} // namespace base
#ifdef QT_VERSION
template <typename Lambda>
inline void InvokeQueued(base::enable_weak_from_this *context, Lambda &&lambda) {
QObject proxy;
QObject::connect(&proxy, &QObject::destroyed, QCoreApplication::instance(), [guard = base::weak_unique_ptr<base::enable_weak_from_this>(context), lambda = std::forward<Lambda>(lambda)] {
QObject::connect(&proxy, &QObject::destroyed, QCoreApplication::instance(), [guard = base::make_weak_unique(context), lambda = std::forward<Lambda>(lambda)] {
if (guard) {
lambda();
}

View file

@ -134,7 +134,7 @@ void Instance::createCall(gsl::not_null<UserData*> user, Call::Type type) {
void Instance::refreshDhConfig() {
Expects(_currentCall != nullptr);
request(MTPmessages_GetDhConfig(MTP_int(_dhConfig.version), MTP_int(Call::kRandomPowerSize))).done([this, call = base::weak_unique_ptr<Call>(_currentCall)](const MTPmessages_DhConfig &result) {
request(MTPmessages_GetDhConfig(MTP_int(_dhConfig.version), MTP_int(Call::kRandomPowerSize))).done([this, call = base::make_weak_unique(_currentCall)](const MTPmessages_DhConfig &result) {
auto random = base::const_byte_span();
switch (result.type()) {
case mtpc_messages_dhConfig: {
@ -170,7 +170,7 @@ void Instance::refreshDhConfig() {
if (call) {
call->start(random);
}
}).fail([this, call = base::weak_unique_ptr<Call>(_currentCall)](const RPCError &error) {
}).fail([this, call = base::make_weak_unique(_currentCall)](const RPCError &error) {
if (!call) {
DEBUG_LOG(("API Warning: call was destroyed before got dhConfig."));
return;

View file

@ -244,7 +244,7 @@ void CloudManager::switchToLanguage(QString id) {
void CloudManager::performSwitchToCustom() {
auto filter = qsl("Language files (*.strings)");
auto title = qsl("Choose language .strings file");
FileDialog::GetOpenPath(title, filter, [weak = base::weak_unique_ptr<CloudManager>(this)](const FileDialog::OpenResult &result) {
FileDialog::GetOpenPath(title, filter, [weak = base::make_weak_unique(this)](const FileDialog::OpenResult &result) {
if (!weak || result.paths.isEmpty()) {
return;
}

View file

@ -142,7 +142,7 @@ void ConfigLoader::sendSpecialRequest() {
return;
}
auto weak = base::weak_unique_ptr<ConfigLoader>(this);
auto weak = base::make_weak_unique(this);
auto index = rand_value<uint32>() % uint32(_specialEndpoints.size());
auto endpoint = _specialEndpoints.begin() + index;
_specialEnumCurrent = specialToRealDcId(endpoint->dcId);

View file

@ -98,7 +98,7 @@ void ParticipantsBoxController::addNewItem() {
}
return;
}
auto weak = base::weak_unique_ptr<ParticipantsBoxController>(this);
auto weak = base::make_weak_unique(this);
_addBox = Ui::show(Box<PeerListBox>(std::make_unique<AddParticipantBoxController>(_channel, _role, [weak](gsl::not_null<UserData*> user, const MTPChannelAdminRights &rights) {
if (weak) {
weak->editAdminDone(user, rights);
@ -337,7 +337,7 @@ void ParticipantsBoxController::showAdmin(gsl::not_null<UserData*> user) {
auto currentRights = isCreator
? MTP_channelAdminRights(MTP_flags(~MTPDchannelAdminRights::Flag::f_add_admins | MTPDchannelAdminRights::Flag::f_add_admins))
: notAdmin ? MTP_channelAdminRights(MTP_flags(0)) : it->second;
auto weak = base::weak_unique_ptr<ParticipantsBoxController>(this);
auto weak = base::make_weak_unique(this);
auto box = Box<EditAdminBox>(_channel, user, currentRights);
auto canEdit = (_additional.adminCanEdit.find(user) != _additional.adminCanEdit.end());
auto canSave = notAdmin ? _channel->canAddAdmins() : canEdit;
@ -392,7 +392,7 @@ void ParticipantsBoxController::showRestricted(gsl::not_null<UserData*> user) {
if (it == _additional.restrictedRights.cend()) {
return;
}
auto weak = base::weak_unique_ptr<ParticipantsBoxController>(this);
auto weak = base::make_weak_unique(this);
auto hasAdminRights = false;
auto box = Box<EditRestrictedBox>(_channel, user, hasAdminRights, it->second);
if (_channel->canBanMembers()) {
@ -453,7 +453,7 @@ void ParticipantsBoxController::editRestrictedDone(gsl::not_null<UserData*> user
void ParticipantsBoxController::kickMember(gsl::not_null<UserData*> user) {
auto text = (_channel->isMegagroup() ? lng_profile_sure_kick : lng_profile_sure_kick_channel)(lt_user, user->firstName);
auto weak = base::weak_unique_ptr<ParticipantsBoxController>(this);
auto weak = base::make_weak_unique(this);
_editBox = Ui::show(Box<ConfirmBox>(text, lang(lng_box_remove), [weak, user] {
if (weak) {
weak->kickMemberSure(user);
@ -804,7 +804,7 @@ void AddParticipantBoxController::showAdmin(gsl::not_null<UserData*> user, bool
}
// Check restrictions.
auto weak = base::weak_unique_ptr<AddParticipantBoxController>(this);
auto weak = base::make_weak_unique(this);
auto alreadyIt = _additional.adminRights.find(user);
auto currentRights = (_additional.creator == user)
? MTP_channelAdminRights(MTP_flags(~MTPDchannelAdminRights::Flag::f_add_admins | MTPDchannelAdminRights::Flag::f_add_admins))
@ -930,7 +930,7 @@ void AddParticipantBoxController::showRestricted(gsl::not_null<UserData*> user,
}
// Check restrictions.
auto weak = base::weak_unique_ptr<AddParticipantBoxController>(this);
auto weak = base::make_weak_unique(this);
auto alreadyIt = _additional.restrictedRights.find(user);
auto currentRights = MTP_channelBannedRights(MTP_flags(0), MTP_int(0));
auto hasAdminRights = false;
@ -966,7 +966,7 @@ void AddParticipantBoxController::showRestricted(gsl::not_null<UserData*> user,
}
void AddParticipantBoxController::restrictUserSure(gsl::not_null<UserData*> user, const MTPChannelBannedRights &oldRights, const MTPChannelBannedRights &newRights) {
auto weak = base::weak_unique_ptr<AddParticipantBoxController>(this);
auto weak = base::make_weak_unique(this);
MTP::send(MTPchannels_EditBanned(_channel->inputChannel, user->inputUser, newRights), rpcDone([megagroup = _channel.get(), user, weak, oldRights, newRights](const MTPUpdates &result) {
Auth().api().applyUpdates(result);
megagroup->applyEditBanned(user, oldRights, newRights);
@ -1005,7 +1005,7 @@ void AddParticipantBoxController::kickUser(gsl::not_null<UserData*> user, bool s
}
// Check restrictions.
auto weak = base::weak_unique_ptr<AddParticipantBoxController>(this);
auto weak = base::make_weak_unique(this);
if (_additional.adminRights.find(user) != _additional.adminRights.end() || _additional.creator == user) {
// The user is an admin or creator.
if (_additional.adminCanEdit.find(user) != _additional.adminCanEdit.end()) {