Optimize and rename base::weak_unique_ptr.

Rename base::enable_weak_from_this -> base::has_weak_ptr.
Rename base::weak_unique_ptr -> base::weak_ptr.
Rename base::make_weak_unique -> base::make_weak.
Rename base/weak_unique_ptr.h -> base/weak_ptr.h
This commit is contained in:
John Preston 2017-11-30 21:33:27 +04:00
parent 0bf854bf18
commit 2432845df2
26 changed files with 388 additions and 230 deletions

View File

@ -21,9 +21,9 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
#pragma once #pragma once
#include <QPointer> #include <QPointer>
#include "base/weak_unique_ptr.h" #include "base/weak_ptr.h"
// Guard lambda call by QObject* or enable_weak_from_this* pointers. // Guard lambda call by QObject* or has_weak_ptr* pointers.
namespace base { namespace base {
namespace lambda_internal { namespace lambda_internal {
@ -66,9 +66,9 @@ class guard_with_weak {
public: public:
template <typename OtherLambda> template <typename OtherLambda>
guard_with_weak( guard_with_weak(
const base::enable_weak_from_this *object, const base::has_weak_ptr *object,
OtherLambda &&other) OtherLambda &&other)
: _guard(base::make_weak_unique(object)) : _guard(base::make_weak(object))
, _callable(std::forward<OtherLambda>(other)) { , _callable(std::forward<OtherLambda>(other)) {
} }
@ -91,7 +91,7 @@ public:
} }
private: private:
base::weak_unique_ptr<const base::enable_weak_from_this> _guard; base::weak_ptr<const base::has_weak_ptr> _guard;
Lambda _callable; Lambda _callable;
}; };
@ -117,7 +117,7 @@ inline auto lambda_guarded(const QObject *object, Lambda &&lambda) {
template <typename Lambda> template <typename Lambda>
inline auto lambda_guarded( inline auto lambda_guarded(
const base::enable_weak_from_this *object, const base::has_weak_ptr *object,
Lambda &&lambda) { Lambda &&lambda) {
using Guarded = lambda_internal::guard_with_weak< using Guarded = lambda_internal::guard_with_weak<
std::decay_t<Lambda>>; std::decay_t<Lambda>>;

View File

@ -0,0 +1,302 @@
/*
This file is part of Telegram Desktop,
the official desktop version of Telegram messaging app, see https://telegram.org
Telegram Desktop is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
It is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
In addition, as a special exception, the copyright holders give permission
to link the code of portions of this program with the OpenSSL library.
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
*/
#pragma once
#include <atomic>
namespace base {
namespace details {
struct alive_tracker {
std::atomic<int> counter = 1;
std::atomic<bool> dead = false;
};
inline alive_tracker *check_and_increment(alive_tracker *tracker) noexcept {
if (tracker) {
++tracker->counter;
}
return tracker;
}
inline void decrement(alive_tracker *tracker) noexcept {
if (tracker->counter.fetch_sub(1) == 0) {
delete tracker;
}
}
} // namespace details
class has_weak_ptr;
template <typename T>
class weak_ptr;
class has_weak_ptr {
public:
has_weak_ptr() = default;
has_weak_ptr(const has_weak_ptr &other) noexcept {
}
has_weak_ptr(has_weak_ptr &&other) noexcept {
}
has_weak_ptr &operator=(const has_weak_ptr &other) noexcept {
return *this;
}
has_weak_ptr &operator=(has_weak_ptr &&other) noexcept {
return *this;
}
~has_weak_ptr() {
if (auto alive = _alive.load()) {
alive->dead.store(true);
details::decrement(alive);
}
}
private:
template <typename Child>
friend class weak_ptr;
details::alive_tracker *incrementAliveTracker() const {
auto current = _alive.load();
if (!current) {
auto alive = std::make_unique<details::alive_tracker>();
if (_alive.compare_exchange_strong(current, alive.get())) {
return alive.release();
}
}
++current->counter;
return current;
}
mutable std::atomic<details::alive_tracker*> _alive = nullptr;
};
template <typename T>
class weak_ptr {
public:
weak_ptr() = default;
weak_ptr(T *value)
: _alive(value ? value->incrementAliveTracker() : nullptr)
, _value(value) {
}
weak_ptr(const std::unique_ptr<T> &value)
: weak_ptr(value.get()) {
}
weak_ptr(const std::shared_ptr<T> &value)
: weak_ptr(value.get()) {
}
weak_ptr(const std::weak_ptr<T> &value)
: weak_ptr(value.lock().get()) {
}
weak_ptr(const weak_ptr &other) noexcept
: _alive(details::check_and_increment(other._alive))
, _value(other._value) {
}
weak_ptr(weak_ptr &&other) noexcept
: _alive(std::exchange(other._alive, nullptr))
, _value(std::exchange(other._value, nullptr)) {
}
template <
typename Other,
typename = std::enable_if_t<
std::is_base_of_v<T, Other> && !std::is_same_v<T, Other>>>
weak_ptr(const weak_ptr<Other> &other) noexcept
: _alive(details::check_and_increment(other._alive))
, _value(other._value) {
}
template <
typename Other,
typename = std::enable_if_t<
std::is_base_of_v<T, Other> && !std::is_same_v<T, Other>>>
weak_ptr(weak_ptr<Other> &&other) noexcept
: _alive(std::exchange(other._alive, nullptr))
, _value(std::exchange(other._value, nullptr)) {
}
weak_ptr &operator=(T *value) {
reset(value);
return *this;
}
weak_ptr &operator=(const std::unique_ptr<T> &value) {
reset(value.get());
return *this;
}
weak_ptr &operator=(const std::shared_ptr<T> &value) {
reset(value.get());
return *this;
}
weak_ptr &operator=(const std::weak_ptr<T> &value) {
reset(value.lock().get());
return *this;
}
weak_ptr &operator=(const weak_ptr &other) noexcept {
if (_value != other._value) {
destroy();
_alive = details::check_and_increment(other._alive);
_value = other._value;
}
return *this;
}
weak_ptr &operator=(weak_ptr &&other) noexcept {
if (_value != other._value) {
destroy();
_alive = std::exchange(other._alive, nullptr);
_value = std::exchange(other._value, nullptr);
}
return *this;
}
template <
typename Other,
typename = std::enable_if_t<
std::is_base_of_v<T, Other> && !std::is_same_v<T, Other>>>
weak_ptr &operator=(const weak_ptr<Other> &other) noexcept {
if (_value != other._value) {
destroy();
_alive = details::check_and_increment(other._alive);
_value = other._value;
}
return *this;
}
template <
typename Other,
typename = std::enable_if_t<
std::is_base_of_v<T, Other> && !std::is_same_v<T, Other>>>
weak_ptr &operator=(weak_ptr<Other> &&other) noexcept {
if (_value != other._value) {
destroy();
_alive = std::exchange(other._alive, nullptr);
_value = std::exchange(other._value, nullptr);
}
return *this;
}
~weak_ptr() {
destroy();
}
T *get() const noexcept {
return (_alive && !_alive->dead) ? _value : nullptr;
}
explicit operator bool() const noexcept {
return (_alive && !_alive->dead);
}
T &operator*() const noexcept {
return *get();
}
T *operator->() const noexcept {
return get();
}
void reset(T *value = nullptr) {
if (_value != value) {
destroy();
_alive = value ? value->incrementAliveTracker() : nullptr;
_value = value;
}
}
private:
void destroy() noexcept {
if (_alive) {
details::decrement(_alive);
}
}
details::alive_tracker *_alive = nullptr;
T *_value = nullptr;
template <typename Other>
friend class weak_ptr;
};
template <typename T>
inline bool operator==(const weak_ptr<T> &pointer, std::nullptr_t) noexcept {
return (pointer.get() == nullptr);
}
template <typename T>
inline bool operator==(std::nullptr_t, const weak_ptr<T> &pointer) noexcept {
return (pointer == nullptr);
}
template <typename T>
inline bool operator!=(const weak_ptr<T> &pointer, std::nullptr_t) noexcept {
return !(pointer == nullptr);
}
template <typename T>
inline bool operator!=(std::nullptr_t, const weak_ptr<T> &pointer) noexcept {
return !(pointer == nullptr);
}
template <
typename T,
typename = std::enable_if_t<std::is_base_of_v<has_weak_ptr, T>>>
weak_ptr<T> make_weak(T *value) {
return value;
}
template <
typename T,
typename = std::enable_if_t<std::is_base_of_v<has_weak_ptr, T>>>
weak_ptr<T> make_weak(const std::unique_ptr<T> &value) {
return value;
}
template <
typename T,
typename = std::enable_if_t<std::is_base_of_v<has_weak_ptr, T>>>
weak_ptr<T> make_weak(const std::shared_ptr<T> &value) {
return value;
}
template <
typename T,
typename = std::enable_if_t<std::is_base_of_v<has_weak_ptr, T>>>
weak_ptr<T> make_weak(const std::weak_ptr<T> &value) {
return value;
}
} // namespace base
#ifdef QT_VERSION
template <typename Lambda>
inline void InvokeQueued(const base::has_weak_ptr *context, Lambda &&lambda) {
auto callback = [
guard = base::make_weak(context),
lambda = std::forward<Lambda>(lambda)
] {
if (guard) {
lambda();
}
};
QObject proxy;
QObject::connect(
&proxy,
&QObject::destroyed,
QCoreApplication::instance(),
std::move(callback),
Qt::QueuedConnection);
}
#endif // QT_VERSION

View File

@ -1,147 +0,0 @@
/*
This file is part of Telegram Desktop,
the official desktop version of Telegram messaging app, see https://telegram.org
Telegram Desktop is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
It is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
In addition, as a special exception, the copyright holders give permission
to link the code of portions of this program with the OpenSSL library.
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
*/
#pragma once
namespace base {
class enable_weak_from_this;
template <typename T>
class weak_unique_ptr;
class enable_weak_from_this {
public:
enable_weak_from_this() = default;
enable_weak_from_this(const enable_weak_from_this &other) noexcept {
}
enable_weak_from_this(enable_weak_from_this &&other) noexcept {
}
enable_weak_from_this &operator=(const enable_weak_from_this &other) noexcept {
return *this;
}
enable_weak_from_this &operator=(enable_weak_from_this &&other) noexcept {
return *this;
}
private:
template <typename Child>
friend class weak_unique_ptr;
std::shared_ptr<enable_weak_from_this*> getGuarded() const {
if (!_guarded) {
_guarded = std::make_shared<enable_weak_from_this*>(
const_cast<enable_weak_from_this*>(
static_cast<const enable_weak_from_this*>(this)));
}
return _guarded;
}
mutable std::shared_ptr<enable_weak_from_this*> _guarded;
};
template <typename T>
class weak_unique_ptr {
public:
weak_unique_ptr() = default;
weak_unique_ptr(T *value)
: _guarded(value
? value->getGuarded()
: std::shared_ptr<enable_weak_from_this*>()) {
}
weak_unique_ptr(const std::unique_ptr<T> &value)
: weak_unique_ptr(value.get()) {
}
weak_unique_ptr &operator=(T *value) {
_guarded = value
? value->getGuarded()
: std::shared_ptr<enable_weak_from_this*>();
return *this;
}
weak_unique_ptr &operator=(const std::unique_ptr<T> &value) {
return (*this = value.get());
}
T *get() const noexcept {
if (auto shared = _guarded.lock()) {
return static_cast<T*>(*shared);
}
return nullptr;
}
explicit operator bool() const noexcept {
return !!_guarded.lock();
}
T &operator*() const noexcept {
return *get();
}
T *operator->() const noexcept {
return get();
}
private:
std::weak_ptr<enable_weak_from_this*> _guarded;
};
template <typename T>
inline bool operator==(const weak_unique_ptr<T> &pointer, std::nullptr_t) {
return (pointer.get() == nullptr);
}
template <typename T>
inline bool operator==(std::nullptr_t, const weak_unique_ptr<T> &pointer) {
return (pointer == nullptr);
}
template <typename T>
inline bool operator!=(const weak_unique_ptr<T> &pointer, std::nullptr_t) {
return !(pointer == nullptr);
}
template <typename T>
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::make_weak_unique(context), lambda = std::forward<Lambda>(lambda)] {
if (guard) {
lambda();
}
}, Qt::QueuedConnection);
}
#endif // QT_VERSION

View File

@ -725,8 +725,7 @@ void AddBotToGroupBoxController::rowClicked(not_null<PeerListRow*> row) {
} }
void AddBotToGroupBoxController::shareBotGame(not_null<PeerData*> chat) { void AddBotToGroupBoxController::shareBotGame(not_null<PeerData*> chat) {
auto weak = base::make_weak_unique(this); auto send = [weak = base::make_weak(this), bot = _bot, chat] {
auto send = [weak, bot = _bot, chat] {
if (!weak) { if (!weak) {
return; return;
} }
@ -773,8 +772,7 @@ void AddBotToGroupBoxController::addBotToGroup(not_null<PeerData*> chat) {
return; return;
} }
} }
auto weak = base::make_weak_unique(this); auto send = [weak = base::make_weak(this), bot = _bot, chat] {
auto send = [weak, bot = _bot, chat] {
if (!weak) { if (!weak) {
return; return;
} }

View File

@ -22,7 +22,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
#include "boxes/peer_list_box.h" #include "boxes/peer_list_box.h"
#include "base/flat_set.h" #include "base/flat_set.h"
#include "base/weak_unique_ptr.h" #include "base/weak_ptr.h"
// Not used for now. // Not used for now.
// //
@ -207,7 +207,7 @@ private:
}; };
class AddBotToGroupBoxController : public ChatsListBoxController, public base::enable_weak_from_this { class AddBotToGroupBoxController : public ChatsListBoxController, public base::has_weak_ptr {
public: public:
static void Start(not_null<UserData*> bot); static void Start(not_null<UserData*> bot);

View File

@ -52,7 +52,7 @@ constexpr auto kUsernameCheckTimeout = TimeMs(200);
class Controller class Controller
: private MTP::Sender : private MTP::Sender
, private base::enable_weak_from_this { , private base::has_weak_ptr {
public: public:
Controller( Controller(
not_null<BoxContent*> box, not_null<BoxContent*> box,

View File

@ -20,7 +20,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
*/ */
#pragma once #pragma once
#include "base/weak_unique_ptr.h" #include "base/weak_ptr.h"
#include "base/timer.h" #include "base/timer.h"
#include "mtproto/sender.h" #include "mtproto/sender.h"
#include "mtproto/auth_key.h" #include "mtproto/auth_key.h"
@ -43,7 +43,7 @@ struct DhConfig {
std::vector<gsl::byte> p; std::vector<gsl::byte> p;
}; };
class Call : public base::enable_weak_from_this, private MTP::Sender { class Call : public base::has_weak_ptr, private MTP::Sender {
public: public:
class Delegate { class Delegate {
public: public:

View File

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

View File

@ -20,7 +20,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
*/ */
#pragma once #pragma once
#include "base/weak_unique_ptr.h" #include "base/weak_ptr.h"
#include "base/timer.h" #include "base/timer.h"
#include "calls/calls_call.h" #include "calls/calls_call.h"
#include "ui/widgets/tooltip.h" #include "ui/widgets/tooltip.h"

View File

@ -39,7 +39,7 @@ constexpr auto kUpdateDebugTimeoutMs = TimeMs(500);
class DebugInfoBox : public BoxContent { class DebugInfoBox : public BoxContent {
public: public:
DebugInfoBox(QWidget*, base::weak_unique_ptr<Call> call); DebugInfoBox(QWidget*, base::weak_ptr<Call> call);
protected: protected:
void prepare() override; void prepare() override;
@ -47,13 +47,14 @@ protected:
private: private:
void updateText(); void updateText();
base::weak_unique_ptr<Call> _call; base::weak_ptr<Call> _call;
QPointer<Ui::FlatLabel> _text; QPointer<Ui::FlatLabel> _text;
base::Timer _updateTextTimer; base::Timer _updateTextTimer;
}; };
DebugInfoBox::DebugInfoBox(QWidget*, base::weak_unique_ptr<Call> call) : _call(call) { DebugInfoBox::DebugInfoBox(QWidget*, base::weak_ptr<Call> call)
: _call(call) {
} }
void DebugInfoBox::prepare() { void DebugInfoBox::prepare() {
@ -82,7 +83,7 @@ void DebugInfoBox::updateText() {
TopBar::TopBar( TopBar::TopBar(
QWidget *parent, QWidget *parent,
const base::weak_unique_ptr<Call> &call) const base::weak_ptr<Call> &call)
: RpWidget(parent) : RpWidget(parent)
, _call(call) , _call(call)
, _durationLabel(this, st::callBarLabel) , _durationLabel(this, st::callBarLabel)

View File

@ -20,7 +20,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
*/ */
#pragma once #pragma once
#include "base/weak_unique_ptr.h" #include "base/weak_ptr.h"
#include "base/timer.h" #include "base/timer.h"
#include "ui/rp_widget.h" #include "ui/rp_widget.h"
@ -37,7 +37,7 @@ class Call;
class TopBar : public Ui::RpWidget, private base::Subscriber { class TopBar : public Ui::RpWidget, private base::Subscriber {
public: public:
TopBar(QWidget *parent, const base::weak_unique_ptr<Call> &call); TopBar(QWidget *parent, const base::weak_ptr<Call> &call);
~TopBar(); ~TopBar();
@ -54,7 +54,7 @@ private:
void startDurationUpdateTimer(TimeMs currentDuration); void startDurationUpdateTimer(TimeMs currentDuration);
void setMuted(bool mute); void setMuted(bool mute);
base::weak_unique_ptr<Call> _call; base::weak_ptr<Call> _call;
bool _muted = false; bool _muted = false;
object_ptr<Ui::LabelSimple> _durationLabel; object_ptr<Ui::LabelSimple> _durationLabel;

View File

@ -21,7 +21,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
#pragma once #pragma once
#include "storage/storage_shared_media.h" #include "storage/storage_shared_media.h"
#include "base/weak_unique_ptr.h" #include "base/weak_ptr.h"
#include "data/data_sparse_ids.h" #include "data/data_sparse_ids.h"
base::optional<Storage::SharedMediaType> SharedMediaOverviewType( base::optional<Storage::SharedMediaType> SharedMediaOverviewType(

View File

@ -21,7 +21,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
#pragma once #pragma once
#include "storage/storage_user_photos.h" #include "storage/storage_user_photos.h"
#include "base/weak_unique_ptr.h" #include "base/weak_ptr.h"
class UserPhotosSlice { class UserPhotosSlice {
public: public:

View File

@ -69,7 +69,7 @@ inline void CallDelayed(
template <typename Lambda> template <typename Lambda>
inline void CallDelayed( inline void CallDelayed(
int duration, int duration,
const base::enable_weak_from_this *object, const base::has_weak_ptr *object,
Lambda &&lambda) { Lambda &&lambda) {
return internal::CallDelayed( return internal::CallDelayed(
duration, duration,
@ -93,7 +93,7 @@ inline auto LambdaDelayed(
template <typename Lambda> template <typename Lambda>
inline auto LambdaDelayed( inline auto LambdaDelayed(
int duration, int duration,
const base::enable_weak_from_this *object, const base::has_weak_ptr *object,
Lambda &&lambda) { Lambda &&lambda) {
auto guarded = base::lambda_guarded( auto guarded = base::lambda_guarded(
object, object,
@ -120,7 +120,7 @@ inline auto LambdaDelayedOnce(
template <typename Lambda> template <typename Lambda>
inline auto LambdaDelayedOnce( inline auto LambdaDelayedOnce(
int duration, int duration,
const base::enable_weak_from_this *object, const base::has_weak_ptr *object,
Lambda &&lambda) { Lambda &&lambda) {
auto guarded = base::lambda_guarded( auto guarded = base::lambda_guarded(
object, object,

View File

@ -21,7 +21,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
#include "info/profile/info_profile_members_controllers.h" #include "info/profile/info_profile_members_controllers.h"
#include <rpl/variable.h> #include <rpl/variable.h>
#include "base/weak_unique_ptr.h" #include "base/weak_ptr.h"
#include "profile/profile_channel_controllers.h" #include "profile/profile_channel_controllers.h"
#include "ui/widgets/popup_menu.h" #include "ui/widgets/popup_menu.h"
#include "lang/lang_keys.h" #include "lang/lang_keys.h"
@ -42,7 +42,7 @@ constexpr auto kSortByOnlineDelay = TimeMs(1000);
class ChatMembersController class ChatMembersController
: public PeerListController : public PeerListController
, private base::Subscriber , private base::Subscriber
, public base::enable_weak_from_this { , public base::has_weak_ptr {
public: public:
ChatMembersController( ChatMembersController(
not_null<Window::Controller*> window, not_null<Window::Controller*> window,
@ -286,7 +286,7 @@ Ui::PopupMenu *ChatMembersController::rowContextMenu(
auto result = new Ui::PopupMenu(nullptr); auto result = new Ui::PopupMenu(nullptr);
result->addAction( result->addAction(
lang(lng_context_view_profile), lang(lng_context_view_profile),
[weak = base::make_weak_unique(this), user] { [weak = base::make_weak(this), user] {
if (weak) { if (weak) {
weak->_window->showPeerInfo(user); weak->_window->showPeerInfo(user);
} }
@ -294,7 +294,7 @@ Ui::PopupMenu *ChatMembersController::rowContextMenu(
if (canRemoveMember) { if (canRemoveMember) {
result->addAction( result->addAction(
lang(lng_context_remove_from_group), lang(lng_context_remove_from_group),
[weak = base::make_weak_unique(this), user] { [weak = base::make_weak(this), user] {
if (weak) { if (weak) {
weak->removeMember(user); weak->removeMember(user);
} }

View File

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

View File

@ -21,7 +21,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
#pragma once #pragma once
#include "mtproto/sender.h" #include "mtproto/sender.h"
#include "base/weak_unique_ptr.h" #include "base/weak_ptr.h"
namespace MTP { namespace MTP {
class Instance; class Instance;
@ -31,7 +31,7 @@ namespace Lang {
class Instance; class Instance;
class CloudManager : public base::enable_weak_from_this, private MTP::Sender, private base::Subscriber { class CloudManager : public base::has_weak_ptr, private MTP::Sender, private base::Subscriber {
public: public:
CloudManager(Instance &langpack, not_null<MTP::Instance*> mtproto); CloudManager(Instance &langpack, not_null<MTP::Instance*> mtproto);

View File

@ -22,7 +22,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
#include <rpl/producer.h> #include <rpl/producer.h>
#include "lang_auto.h" #include "lang_auto.h"
#include "base/weak_unique_ptr.h" #include "base/weak_ptr.h"
namespace Lang { namespace Lang {

View File

@ -23,7 +23,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
#include "storage/localimageloader.h" #include "storage/localimageloader.h"
#include "history/history_common.h" #include "history/history_common.h"
#include "core/single_timer.h" #include "core/single_timer.h"
#include "base/weak_unique_ptr.h" #include "base/weak_ptr.h"
#include "ui/rp_widget.h" #include "ui/rp_widget.h"
namespace Notify { namespace Notify {
@ -583,7 +583,7 @@ private:
object_ptr<Window::SectionWidget> _thirdSection = { nullptr }; object_ptr<Window::SectionWidget> _thirdSection = { nullptr };
std::unique_ptr<Window::SectionMemento> _thirdSectionFromStack; std::unique_ptr<Window::SectionMemento> _thirdSectionFromStack;
base::weak_unique_ptr<Calls::Call> _currentCall; base::weak_ptr<Calls::Call> _currentCall;
object_ptr<Ui::SlideWrap<Calls::TopBar>> _callTopBar = { nullptr }; object_ptr<Ui::SlideWrap<Calls::TopBar>> _callTopBar = { nullptr };
object_ptr<Window::PlayerWrapWidget> _player = { nullptr }; object_ptr<Window::PlayerWrapWidget> _player = { nullptr };

View File

@ -142,16 +142,15 @@ void ConfigLoader::sendSpecialRequest() {
return; return;
} }
auto weak = base::make_weak_unique(this); auto weak = base::make_weak(this);
auto index = rand_value<uint32>() % uint32(_specialEndpoints.size()); auto index = rand_value<uint32>() % uint32(_specialEndpoints.size());
auto endpoint = _specialEndpoints.begin() + index; auto endpoint = _specialEndpoints.begin() + index;
_specialEnumCurrent = specialToRealDcId(endpoint->dcId); _specialEnumCurrent = specialToRealDcId(endpoint->dcId);
_instance->dcOptions()->constructAddOne(_specialEnumCurrent, MTPDdcOption::Flag::f_tcpo_only, endpoint->ip, endpoint->port); _instance->dcOptions()->constructAddOne(_specialEnumCurrent, MTPDdcOption::Flag::f_tcpo_only, endpoint->ip, endpoint->port);
_specialEnumRequest = _instance->send(MTPhelp_GetConfig(), rpcDone([weak](const MTPConfig &result) { _specialEnumRequest = _instance->send(MTPhelp_GetConfig(), rpcDone([weak](const MTPConfig &result) {
if (!weak) { if (const auto strong = weak.get()) {
return; strong->specialConfigLoaded(result);
} }
weak->specialConfigLoaded(result);
}), _failHandler, _specialEnumCurrent); }), _failHandler, _specialEnumCurrent);
_triedSpecialEndpoints.push_back(*endpoint); _triedSpecialEndpoints.push_back(*endpoint);
_specialEndpoints.erase(endpoint); _specialEndpoints.erase(endpoint);

View File

@ -21,7 +21,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
#pragma once #pragma once
#include "base/timer.h" #include "base/timer.h"
#include "base/weak_unique_ptr.h" #include "base/weak_ptr.h"
#include "mtproto/rpc_sender.h" #include "mtproto/rpc_sender.h"
namespace MTP { namespace MTP {
@ -31,7 +31,7 @@ class Instance;
namespace internal { namespace internal {
class ConfigLoader : public base::enable_weak_from_this { class ConfigLoader : public base::has_weak_ptr {
public: public:
ConfigLoader(not_null<Instance*> instance, RPCDoneHandlerPtr onDone, RPCFailHandlerPtr onFail); ConfigLoader(not_null<Instance*> instance, RPCDoneHandlerPtr onDone, RPCFailHandlerPtr onFail);
~ConfigLoader(); ~ConfigLoader();

View File

@ -21,7 +21,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
#pragma once #pragma once
#include "platform/platform_notifications_manager.h" #include "platform/platform_notifications_manager.h"
#include "base/weak_unique_ptr.h" #include "base/weak_ptr.h"
namespace Platform { namespace Platform {
namespace Notifications { namespace Notifications {
@ -29,7 +29,7 @@ namespace Notifications {
bool SkipAudio(); bool SkipAudio();
bool SkipToast(); bool SkipToast();
class Manager : public Window::Notifications::NativeManager, public base::enable_weak_from_this { class Manager : public Window::Notifications::NativeManager, public base::has_weak_ptr {
public: public:
Manager(Window::Notifications::System *system); Manager(Window::Notifications::System *system);
~Manager(); ~Manager();

View File

@ -61,19 +61,19 @@ NSImage *qt_mac_create_nsimage(const QPixmap &pm);
@interface NotificationDelegate : NSObject<NSUserNotificationCenterDelegate> { @interface NotificationDelegate : NSObject<NSUserNotificationCenterDelegate> {
} }
- (id) initWithManager:(base::weak_unique_ptr<Manager>)manager managerId:(uint64)managerId; - (id) initWithManager:(base::weak_ptr<Manager>)manager managerId:(uint64)managerId;
- (void) userNotificationCenter:(NSUserNotificationCenter*)center didActivateNotification:(NSUserNotification*)notification; - (void) userNotificationCenter:(NSUserNotificationCenter*)center didActivateNotification:(NSUserNotification*)notification;
- (BOOL) userNotificationCenter:(NSUserNotificationCenter*)center shouldPresentNotification:(NSUserNotification*)notification; - (BOOL) userNotificationCenter:(NSUserNotificationCenter*)center shouldPresentNotification:(NSUserNotification*)notification;
@end // @interface NotificationDelegate @end // @interface NotificationDelegate
@implementation NotificationDelegate { @implementation NotificationDelegate {
base::weak_unique_ptr<Manager> _manager; base::weak_ptr<Manager> _manager;
uint64 _managerId; uint64 _managerId;
} }
- (id) initWithManager:(base::weak_unique_ptr<Manager>)manager managerId:(uint64)managerId { - (id) initWithManager:(base::weak_ptr<Manager>)manager managerId:(uint64)managerId {
if (self = [super init]) { if (self = [super init]) {
_manager = manager; _manager = manager;
_managerId = managerId; _managerId = managerId;

View File

@ -216,14 +216,14 @@ void ParticipantsBoxController::addNewItem() {
} }
return; return;
} }
auto weak = base::make_weak_unique(this); auto weak = base::make_weak(this);
_addBox = Ui::show(Box<PeerListBox>(std::make_unique<AddParticipantBoxController>(_channel, _role, [weak](not_null<UserData*> user, const MTPChannelAdminRights &rights) { _addBox = Ui::show(Box<PeerListBox>(std::make_unique<AddParticipantBoxController>(_channel, _role, [weak](not_null<UserData*> user, const MTPChannelAdminRights &rights) {
if (weak) { if (const auto strong = weak.get()) {
weak->editAdminDone(user, rights); strong->editAdminDone(user, rights);
} }
}, [weak](not_null<UserData*> user, const MTPChannelBannedRights &rights) { }, [weak](not_null<UserData*> user, const MTPChannelBannedRights &rights) {
if (weak) { if (const auto strong = weak.get()) {
weak->editRestrictedDone(user, rights); strong->editRestrictedDone(user, rights);
} }
}), [](not_null<PeerListBox*> box) { }), [](not_null<PeerListBox*> box) {
box->addButton(langFactory(lng_cancel), [box] { box->closeBox(); }); box->addButton(langFactory(lng_cancel), [box] { box->closeBox(); });
@ -640,9 +640,9 @@ Ui::PopupMenu *ParticipantsBoxController::rowContextMenu(
auto result = new Ui::PopupMenu(nullptr); auto result = new Ui::PopupMenu(nullptr);
result->addAction( result->addAction(
lang(lng_context_view_profile), lang(lng_context_view_profile),
[weak = base::make_weak_unique(this), user] { [weak = base::make_weak(this), user] {
if (weak) { if (const auto strong = weak.get()) {
weak->_window->showPeerInfo(user); strong->_window->showPeerInfo(user);
} }
}); });
if (canEditAdmin(user)) { if (canEditAdmin(user)) {
@ -654,9 +654,9 @@ Ui::PopupMenu *ParticipantsBoxController::rowContextMenu(
: lng_context_edit_permissions); : lng_context_edit_permissions);
result->addAction( result->addAction(
label, label,
[weak = base::make_weak_unique(this), user] { [weak = base::make_weak(this), user] {
if (weak) { if (const auto strong = weak.get()) {
weak->showAdmin(user); strong->showAdmin(user);
} }
}); });
} }
@ -665,9 +665,9 @@ Ui::PopupMenu *ParticipantsBoxController::rowContextMenu(
if (isGroup) { if (isGroup) {
result->addAction( result->addAction(
lang(lng_context_restrict_user), lang(lng_context_restrict_user),
[weak = base::make_weak_unique(this), user]{ [weak = base::make_weak(this), user]{
if (weak) { if (const auto strong = weak.get()) {
weak->showRestricted(user); strong->showRestricted(user);
} }
}); });
} }
@ -675,9 +675,9 @@ Ui::PopupMenu *ParticipantsBoxController::rowContextMenu(
lang(isGroup lang(isGroup
? lng_context_remove_from_group ? lng_context_remove_from_group
: lng_profile_kick), : lng_profile_kick),
[weak = base::make_weak_unique(this), user] { [weak = base::make_weak(this), user] {
if (weak) { if (auto strong = weak.get()) {
weak->kickMember(user); strong->kickMember(user);
} }
}); });
} }
@ -691,7 +691,7 @@ void ParticipantsBoxController::showAdmin(not_null<UserData*> user) {
auto currentRights = isCreator auto currentRights = isCreator
? MTP_channelAdminRights(MTP_flags(~MTPDchannelAdminRights::Flag::f_add_admins | MTPDchannelAdminRights::Flag::f_add_admins)) ? MTP_channelAdminRights(MTP_flags(~MTPDchannelAdminRights::Flag::f_add_admins | MTPDchannelAdminRights::Flag::f_add_admins))
: notAdmin ? MTP_channelAdminRights(MTP_flags(0)) : it->second; : notAdmin ? MTP_channelAdminRights(MTP_flags(0)) : it->second;
auto weak = base::make_weak_unique(this); auto weak = base::make_weak(this);
auto box = Box<EditAdminBox>(_channel, user, currentRights); auto box = Box<EditAdminBox>(_channel, user, currentRights);
auto canEdit = (_additional.adminCanEdit.find(user) != _additional.adminCanEdit.end()); auto canEdit = (_additional.adminCanEdit.find(user) != _additional.adminCanEdit.end());
auto canSave = notAdmin ? _channel->canAddAdmins() : canEdit; auto canSave = notAdmin ? _channel->canAddAdmins() : canEdit;
@ -749,7 +749,7 @@ void ParticipantsBoxController::showRestricted(not_null<UserData*> user) {
auto restrictedRights = (it == _additional.restrictedRights.cend()) auto restrictedRights = (it == _additional.restrictedRights.cend())
? MTP_channelBannedRights(MTP_flags(0), MTP_int(0)) ? MTP_channelBannedRights(MTP_flags(0), MTP_int(0))
: it->second; : it->second;
auto weak = base::make_weak_unique(this); auto weak = base::make_weak(this);
auto hasAdminRights = false; auto hasAdminRights = false;
auto box = Box<EditRestrictedBox>(_channel, user, hasAdminRights, restrictedRights); auto box = Box<EditRestrictedBox>(_channel, user, hasAdminRights, restrictedRights);
if (_channel->canBanMembers()) { if (_channel->canBanMembers()) {
@ -815,10 +815,10 @@ void ParticipantsBoxController::editRestrictedDone(not_null<UserData*> user, con
void ParticipantsBoxController::kickMember(not_null<UserData*> user) { void ParticipantsBoxController::kickMember(not_null<UserData*> user) {
auto text = (_channel->isMegagroup() ? lng_profile_sure_kick : lng_profile_sure_kick_channel)(lt_user, user->firstName); auto text = (_channel->isMegagroup() ? lng_profile_sure_kick : lng_profile_sure_kick_channel)(lt_user, user->firstName);
auto weak = base::make_weak_unique(this); auto weak = base::make_weak(this);
_editBox = Ui::show(Box<ConfirmBox>(text, lang(lng_box_remove), [weak, user] { _editBox = Ui::show(Box<ConfirmBox>(text, lang(lng_box_remove), [weak, user] {
if (weak) { if (const auto strong = weak.get()) {
weak->kickMemberSure(user); strong->kickMemberSure(user);
} }
}), LayerOption::KeepOther); }), LayerOption::KeepOther);
} }
@ -1255,7 +1255,7 @@ void AddParticipantBoxController::showAdmin(not_null<UserData*> user, bool sure)
} }
// Check restrictions. // Check restrictions.
auto weak = base::make_weak_unique(this); auto weak = base::make_weak(this);
auto alreadyIt = _additional.adminRights.find(user); auto alreadyIt = _additional.adminRights.find(user);
auto currentRights = (_additional.creator == user) auto currentRights = (_additional.creator == user)
? MTP_channelAdminRights(MTP_flags(~MTPDchannelAdminRights::Flag::f_add_admins | MTPDchannelAdminRights::Flag::f_add_admins)) ? MTP_channelAdminRights(MTP_flags(~MTPDchannelAdminRights::Flag::f_add_admins | MTPDchannelAdminRights::Flag::f_add_admins))
@ -1398,7 +1398,7 @@ void AddParticipantBoxController::showRestricted(not_null<UserData*> user, bool
} }
// Check restrictions. // Check restrictions.
auto weak = base::make_weak_unique(this); auto weak = base::make_weak(this);
auto alreadyIt = _additional.restrictedRights.find(user); auto alreadyIt = _additional.restrictedRights.find(user);
auto currentRights = MTP_channelBannedRights(MTP_flags(0), MTP_int(0)); auto currentRights = MTP_channelBannedRights(MTP_flags(0), MTP_int(0));
auto hasAdminRights = false; auto hasAdminRights = false;
@ -1436,12 +1436,12 @@ void AddParticipantBoxController::showRestricted(not_null<UserData*> user, bool
} }
void AddParticipantBoxController::restrictUserSure(not_null<UserData*> user, const MTPChannelBannedRights &oldRights, const MTPChannelBannedRights &newRights) { void AddParticipantBoxController::restrictUserSure(not_null<UserData*> user, const MTPChannelBannedRights &oldRights, const MTPChannelBannedRights &newRights) {
auto weak = base::make_weak_unique(this); auto weak = base::make_weak(this);
MTP::send(MTPchannels_EditBanned(_channel->inputChannel, user->inputUser, newRights), rpcDone([megagroup = _channel.get(), user, weak, oldRights, newRights](const MTPUpdates &result) { MTP::send(MTPchannels_EditBanned(_channel->inputChannel, user->inputUser, newRights), rpcDone([megagroup = _channel.get(), user, weak, oldRights, newRights](const MTPUpdates &result) {
Auth().api().applyUpdates(result); Auth().api().applyUpdates(result);
megagroup->applyEditBanned(user, oldRights, newRights); megagroup->applyEditBanned(user, oldRights, newRights);
if (weak) { if (const auto strong = weak.get()) {
weak->editRestrictedDone(user, newRights); strong->editRestrictedDone(user, newRights);
} }
})); }));
} }
@ -1477,7 +1477,7 @@ void AddParticipantBoxController::kickUser(not_null<UserData*> user, bool sure)
} }
// Check restrictions. // Check restrictions.
auto weak = base::make_weak_unique(this); auto weak = base::make_weak(this);
if (_additional.adminRights.find(user) != _additional.adminRights.end() || _additional.creator == user) { if (_additional.adminRights.find(user) != _additional.adminRights.end() || _additional.creator == user) {
// The user is an admin or creator. // The user is an admin or creator.
if (_additional.adminCanEdit.find(user) != _additional.adminCanEdit.end()) { if (_additional.adminCanEdit.find(user) != _additional.adminCanEdit.end()) {

View File

@ -24,7 +24,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
#include "boxes/peer_list_box.h" #include "boxes/peer_list_box.h"
#include "mtproto/sender.h" #include "mtproto/sender.h"
#include "base/timer.h" #include "base/timer.h"
#include "base/weak_unique_ptr.h" #include "base/weak_ptr.h"
#include "info/profile/info_profile_members_controllers.h" #include "info/profile/info_profile_members_controllers.h"
namespace Window { namespace Window {
@ -38,7 +38,7 @@ class ParticipantsBoxController
: public PeerListController : public PeerListController
, private base::Subscriber , private base::Subscriber
, private MTP::Sender , private MTP::Sender
, public base::enable_weak_from_this { , public base::has_weak_ptr {
public: public:
enum class Role { enum class Role {
Profile, Profile,
@ -212,7 +212,7 @@ private:
}; };
// Adding an admin, banned or restricted user from channel members with search + contacts search + global search. // Adding an admin, banned or restricted user from channel members with search + contacts search + global search.
class AddParticipantBoxController : public PeerListController, private base::Subscriber, private MTP::Sender, public base::enable_weak_from_this { class AddParticipantBoxController : public PeerListController, private base::Subscriber, private MTP::Sender, public base::has_weak_ptr {
public: public:
using Role = ParticipantsBoxController::Role; using Role = ParticipantsBoxController::Role;
using Additional = ParticipantsBoxController::Additional; using Additional = ParticipantsBoxController::Additional;

View File

@ -33,7 +33,7 @@
<(src_loc)/base/value_ordering.h <(src_loc)/base/value_ordering.h
<(src_loc)/base/variant.h <(src_loc)/base/variant.h
<(src_loc)/base/virtual_method.h <(src_loc)/base/virtual_method.h
<(src_loc)/base/weak_unique_ptr.h <(src_loc)/base/weak_ptr.h
<(src_loc)/base/zlib_help.h <(src_loc)/base/zlib_help.h
<(src_loc)/boxes/peers/edit_peer_info_box.cpp <(src_loc)/boxes/peers/edit_peer_info_box.cpp
<(src_loc)/boxes/peers/edit_peer_info_box.h <(src_loc)/boxes/peers/edit_peer_info_box.h