From 782114d6444d370be7da5b6ed9d817c52711db65 Mon Sep 17 00:00:00 2001 From: John Preston Date: Tue, 15 Aug 2017 12:35:01 +0300 Subject: [PATCH] Add and use base::make_weak_unique() helper. --- Telegram/SourceFiles/base/weak_unique_ptr.h | 12 +++++++++++- Telegram/SourceFiles/calls/calls_instance.cpp | 4 ++-- Telegram/SourceFiles/lang/lang_cloud_manager.cpp | 2 +- Telegram/SourceFiles/mtproto/config_loader.cpp | 2 +- .../profile/profile_channel_controllers.cpp | 16 ++++++++-------- 5 files changed, 23 insertions(+), 13 deletions(-) diff --git a/Telegram/SourceFiles/base/weak_unique_ptr.h b/Telegram/SourceFiles/base/weak_unique_ptr.h index 9e57be4af..dd74f6d43 100644 --- a/Telegram/SourceFiles/base/weak_unique_ptr.h +++ b/Telegram/SourceFiles/base/weak_unique_ptr.h @@ -114,13 +114,23 @@ inline bool operator!=(std::nullptr_t, const weak_unique_ptr &pointer) { return !(pointer == nullptr); } +template +weak_unique_ptr make_weak_unique(T *value) { + return weak_unique_ptr(value); +} + +template +weak_unique_ptr make_weak_unique(const std::unique_ptr &value) { + return weak_unique_ptr(value); +} + } // namespace base #ifdef QT_VERSION template 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(context), lambda = std::forward(lambda)] { + QObject::connect(&proxy, &QObject::destroyed, QCoreApplication::instance(), [guard = base::make_weak_unique(context), lambda = std::forward(lambda)] { if (guard) { lambda(); } diff --git a/Telegram/SourceFiles/calls/calls_instance.cpp b/Telegram/SourceFiles/calls/calls_instance.cpp index 1dd56af3d..7275897f7 100644 --- a/Telegram/SourceFiles/calls/calls_instance.cpp +++ b/Telegram/SourceFiles/calls/calls_instance.cpp @@ -134,7 +134,7 @@ void Instance::createCall(gsl::not_null 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(_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(_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; diff --git a/Telegram/SourceFiles/lang/lang_cloud_manager.cpp b/Telegram/SourceFiles/lang/lang_cloud_manager.cpp index 7627394d3..5f7bfae0a 100644 --- a/Telegram/SourceFiles/lang/lang_cloud_manager.cpp +++ b/Telegram/SourceFiles/lang/lang_cloud_manager.cpp @@ -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(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; } diff --git a/Telegram/SourceFiles/mtproto/config_loader.cpp b/Telegram/SourceFiles/mtproto/config_loader.cpp index cd66d61ae..c507f038c 100644 --- a/Telegram/SourceFiles/mtproto/config_loader.cpp +++ b/Telegram/SourceFiles/mtproto/config_loader.cpp @@ -142,7 +142,7 @@ void ConfigLoader::sendSpecialRequest() { return; } - auto weak = base::weak_unique_ptr(this); + auto weak = base::make_weak_unique(this); auto index = rand_value() % uint32(_specialEndpoints.size()); auto endpoint = _specialEndpoints.begin() + index; _specialEnumCurrent = specialToRealDcId(endpoint->dcId); diff --git a/Telegram/SourceFiles/profile/profile_channel_controllers.cpp b/Telegram/SourceFiles/profile/profile_channel_controllers.cpp index 0192d11fc..5f9ff268d 100644 --- a/Telegram/SourceFiles/profile/profile_channel_controllers.cpp +++ b/Telegram/SourceFiles/profile/profile_channel_controllers.cpp @@ -98,7 +98,7 @@ void ParticipantsBoxController::addNewItem() { } return; } - auto weak = base::weak_unique_ptr(this); + auto weak = base::make_weak_unique(this); _addBox = Ui::show(Box(std::make_unique(_channel, _role, [weak](gsl::not_null user, const MTPChannelAdminRights &rights) { if (weak) { weak->editAdminDone(user, rights); @@ -337,7 +337,7 @@ void ParticipantsBoxController::showAdmin(gsl::not_null 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(this); + auto weak = base::make_weak_unique(this); auto box = Box(_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 user) { if (it == _additional.restrictedRights.cend()) { return; } - auto weak = base::weak_unique_ptr(this); + auto weak = base::make_weak_unique(this); auto hasAdminRights = false; auto box = Box(_channel, user, hasAdminRights, it->second); if (_channel->canBanMembers()) { @@ -453,7 +453,7 @@ void ParticipantsBoxController::editRestrictedDone(gsl::not_null user void ParticipantsBoxController::kickMember(gsl::not_null user) { auto text = (_channel->isMegagroup() ? lng_profile_sure_kick : lng_profile_sure_kick_channel)(lt_user, user->firstName); - auto weak = base::weak_unique_ptr(this); + auto weak = base::make_weak_unique(this); _editBox = Ui::show(Box(text, lang(lng_box_remove), [weak, user] { if (weak) { weak->kickMemberSure(user); @@ -804,7 +804,7 @@ void AddParticipantBoxController::showAdmin(gsl::not_null user, bool } // Check restrictions. - auto weak = base::weak_unique_ptr(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 user, } // Check restrictions. - auto weak = base::weak_unique_ptr(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 user, } void AddParticipantBoxController::restrictUserSure(gsl::not_null user, const MTPChannelBannedRights &oldRights, const MTPChannelBannedRights &newRights) { - auto weak = base::weak_unique_ptr(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 user, bool s } // Check restrictions. - auto weak = base::weak_unique_ptr(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()) {