From 460e2ec0acd6454975cb587b90bda87392ca924c Mon Sep 17 00:00:00 2001 From: John Preston Date: Sun, 10 Apr 2016 23:20:48 +0400 Subject: [PATCH] UniquePointer > std_::unique_ptr, MakeUnique > std_::make_unique. --- Telegram/SourceFiles/basic_types.h | 147 +++++++++++------- Telegram/SourceFiles/boxes/contactsbox.cpp | 8 +- Telegram/SourceFiles/boxes/contactsbox.h | 2 +- Telegram/SourceFiles/dialogs/dialogs_list.cpp | 8 +- Telegram/SourceFiles/dialogs/dialogs_list.h | 2 +- Telegram/SourceFiles/dialogswidget.cpp | 22 +-- Telegram/SourceFiles/dialogswidget.h | 2 +- Telegram/SourceFiles/dropdown.cpp | 4 +- Telegram/SourceFiles/dropdown.h | 2 +- Telegram/SourceFiles/history.cpp | 10 +- Telegram/SourceFiles/history.h | 30 ++-- Telegram/SourceFiles/historywidget.cpp | 30 ++-- Telegram/SourceFiles/historywidget.h | 6 +- .../inline_bot_layout_internal.cpp | 2 +- .../inline_bots/inline_bot_layout_internal.h | 2 +- .../inline_bots/inline_bot_layout_item.cpp | 22 +-- .../inline_bots/inline_bot_layout_item.h | 4 +- .../inline_bots/inline_bot_result.cpp | 26 ++-- .../inline_bots/inline_bot_result.h | 8 +- Telegram/SourceFiles/localstorage.cpp | 4 +- Telegram/SourceFiles/mainwidget.cpp | 4 +- Telegram/SourceFiles/structs.cpp | 10 +- Telegram/SourceFiles/structs.h | 14 +- Telegram/SourceFiles/ui/toast/toast.cpp | 8 +- Telegram/SourceFiles/ui/toast/toast.h | 2 +- .../SourceFiles/ui/toast/toast_manager.cpp | 4 +- Telegram/SourceFiles/ui/toast/toast_manager.h | 2 +- Telegram/SourceFiles/window.cpp | 4 +- Telegram/SourceFiles/window.h | 2 +- 29 files changed, 214 insertions(+), 177 deletions(-) diff --git a/Telegram/SourceFiles/basic_types.h b/Telegram/SourceFiles/basic_types.h index 62c9c4056..05fe2f9c7 100644 --- a/Telegram/SourceFiles/basic_types.h +++ b/Telegram/SourceFiles/basic_types.h @@ -315,6 +315,98 @@ constexpr add_const_t &as_const(T& t) noexcept { template void as_const(const T&&) = delete; +// This is not full unique_ptr, but at least with std interface. +template +class unique_ptr { +public: + constexpr unique_ptr() noexcept = default; + unique_ptr(const unique_ptr &) = delete; + unique_ptr &operator=(const unique_ptr &) = delete; + + constexpr unique_ptr(nullptr_t) { + } + unique_ptr &operator=(nullptr_t) noexcept { + reset(); + return (*this); + } + + explicit unique_ptr(T *p) noexcept : _p(p) { + } + + template + unique_ptr(unique_ptr &&other) noexcept : _p(other.release()) { + } + template + unique_ptr &operator=(unique_ptr &&other) noexcept { + reset(other.release()); + return (*this); + } + unique_ptr &operator=(unique_ptr &&other) noexcept { + if (this != &other) { + reset(other.release()); + } + return (*this); + } + + void swap(unique_ptr &other) noexcept { + std::swap(_p, other._p); + } + ~unique_ptr() noexcept { + delete _p; + } + + T &operator*() const { + return (*get()); + } + T *operator->() const noexcept { + return get(); + } + T *get() const noexcept { + return _p; + } + explicit operator bool() const noexcept { + return get() != nullptr; + } + + T *release() noexcept { + return getPointerAndReset(_p); + } + + void reset(T *p = nullptr) noexcept { + T *old = _p; + _p = p; + if (old) { + delete old; + } + } + +private: + T *_p = nullptr; + +}; + +template +inline unique_ptr make_unique(Args&&... args) { + return unique_ptr(new T(forward(args)...)); +} + +template +inline bool operator==(const unique_ptr &a, nullptr_t) noexcept { + return !a; +} +template +inline bool operator==(nullptr_t, const unique_ptr &b) noexcept { + return !b; +} +template +inline bool operator!=(const unique_ptr &a, nullptr_t b) noexcept { + return !(a == b); +} +template +inline bool operator!=(nullptr_t a, const unique_ptr &b) noexcept { + return !(a == b); +} + } // namespace std_ #include "logs.h" @@ -747,61 +839,6 @@ inline RefPairImplementation RefPairCreator(T1 &first, T2 &second) { #define RefPair(Type1, Name1, Type2, Name2) Type1 Name1; Type2 Name2; RefPairCreator(Name1, Name2) -template -class UniquePointer { -public: - explicit UniquePointer(T *p = nullptr) : _p(p) { - } - UniquePointer(const UniquePointer &other) = delete; - UniquePointer &operator=(const UniquePointer &other) = delete; - UniquePointer(UniquePointer &&other) : _p(other.release()) { - } - UniquePointer &operator=(UniquePointer &&other) { - std::swap(_p, other._p); - return *this; - } - template - UniquePointer(UniquePointer &&other) : _p(other.release()) { - } - T *data() const { - return _p; - } - T *release() { - return getPointerAndReset(_p); - } - void reset(T *p = nullptr) { - *this = UniquePointer(p); - } - bool isNull() const { - return data() == nullptr; - } - - void clear() { - reset(); - } - T *operator->() const { - return data(); - } - T &operator*() const { - t_assert(!isNull()); - return *data(); - } - explicit operator bool() const { - return !isNull(); - } - ~UniquePointer() { - delete data(); - } - -private: - T *_p; - -}; -template -inline UniquePointer MakeUnique(Args&&... args) { - return UniquePointer(new T(std_::forward(args)...)); -} - template inline QSharedPointer MakeShared(Args&&... args) { return QSharedPointer(new T(std_::forward(args)...)); diff --git a/Telegram/SourceFiles/boxes/contactsbox.cpp b/Telegram/SourceFiles/boxes/contactsbox.cpp index 31ff4de2e..b00b68f8a 100644 --- a/Telegram/SourceFiles/boxes/contactsbox.cpp +++ b/Telegram/SourceFiles/boxes/contactsbox.cpp @@ -72,8 +72,8 @@ ContactsInner::ContactsInner(ChatData *chat, MembersFilter membersFilter) : TWid , _aboutWidth(st::boxWideWidth - st::contactsPadding.left() - st::contactsPadding.right() - st::contactsCheckPosition.x() * 2 - st::contactsCheckIcon.pxWidth()) , _aboutAllAdmins(st::boxTextFont, lang(lng_chat_about_all_admins), _defaultOptions, _aboutWidth) , _aboutAdmins(st::boxTextFont, lang(lng_chat_about_admins), _defaultOptions, _aboutWidth) -, _customList((membersFilter == MembersFilterRecent) ? UniquePointer() : MakeUnique(Dialogs::SortMode::Add)) -, _contacts((membersFilter == MembersFilterRecent) ? App::main()->contactsList() : _customList.data()) +, _customList((membersFilter == MembersFilterRecent) ? std_::unique_ptr() : std_::make_unique(Dialogs::SortMode::Add)) +, _contacts((membersFilter == MembersFilterRecent) ? App::main()->contactsList() : _customList.get()) , _addContactLnk(this, lang(lng_add_contact_button)) { initList(); if (membersFilter == MembersFilterAdmins) { @@ -89,8 +89,8 @@ ContactsInner::ContactsInner(UserData *bot) : TWidget() , _rowHeight(st::contactsPadding.top() + st::contactsPhotoSize + st::contactsPadding.bottom()) , _bot(bot) , _allAdmins(this, lang(lng_chat_all_members_admins), false, st::contactsAdminCheckbox) -, _customList(MakeUnique(Dialogs::SortMode::Add)) -, _contacts(_customList.data()) +, _customList(std_::make_unique(Dialogs::SortMode::Add)) +, _contacts(_customList.get()) , _addContactLnk(this, lang(lng_add_contact_button)) { auto v = App::main()->dialogsList(); for_const (auto row, *v) { diff --git a/Telegram/SourceFiles/boxes/contactsbox.h b/Telegram/SourceFiles/boxes/contactsbox.h index fc3b3f8a6..e1bb53673 100644 --- a/Telegram/SourceFiles/boxes/contactsbox.h +++ b/Telegram/SourceFiles/boxes/contactsbox.h @@ -147,7 +147,7 @@ private: int32 _time; - UniquePointer _customList; + std_::unique_ptr _customList; Dialogs::IndexedList *_contacts = nullptr; Dialogs::Row *_sel = nullptr; QString _filter; diff --git a/Telegram/SourceFiles/dialogs/dialogs_list.cpp b/Telegram/SourceFiles/dialogs/dialogs_list.cpp index f6e73d2d0..12591987a 100644 --- a/Telegram/SourceFiles/dialogs/dialogs_list.cpp +++ b/Telegram/SourceFiles/dialogs/dialogs_list.cpp @@ -27,11 +27,11 @@ Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org namespace Dialogs { List::List(SortMode sortMode) -: _last(MakeUnique(nullptr, nullptr, nullptr, 0)) -, _begin(_last.data()) -, _end(_last.data()) +: _last(std_::make_unique(nullptr, nullptr, nullptr, 0)) +, _begin(_last.get()) +, _end(_last.get()) , _sortMode(sortMode) -, _current(_last.data()) { +, _current(_last.get()) { } void List::adjustCurrent(int32 y, int32 h) const { diff --git a/Telegram/SourceFiles/dialogs/dialogs_list.h b/Telegram/SourceFiles/dialogs/dialogs_list.h index bd281d2f7..422eeb203 100644 --- a/Telegram/SourceFiles/dialogs/dialogs_list.h +++ b/Telegram/SourceFiles/dialogs/dialogs_list.h @@ -121,7 +121,7 @@ private: return row->_prev; } - UniquePointer _last; + std_::unique_ptr _last; Row *_begin; Row *_end; SortMode _sortMode; diff --git a/Telegram/SourceFiles/dialogswidget.cpp b/Telegram/SourceFiles/dialogswidget.cpp index 44e25e097..81274388d 100644 --- a/Telegram/SourceFiles/dialogswidget.cpp +++ b/Telegram/SourceFiles/dialogswidget.cpp @@ -35,9 +35,9 @@ Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org #include "localstorage.h" DialogsInner::DialogsInner(QWidget *parent, MainWidget *main) : SplittedWidget(parent) -, dialogs(MakeUnique(Dialogs::SortMode::Date)) -, contactsNoDialogs(MakeUnique(Dialogs::SortMode::Name)) -, contacts(MakeUnique(Dialogs::SortMode::Name)) +, dialogs(std_::make_unique(Dialogs::SortMode::Date)) +, contactsNoDialogs(std_::make_unique(Dialogs::SortMode::Name)) +, contacts(std_::make_unique(Dialogs::SortMode::Name)) , _addContactLnk(this, lang(lng_add_contact_button)) , _cancelSearchInPeer(this, st::btnCancelSearch) { connect(App::wnd(), SIGNAL(imageLoaded()), this, SLOT(update())); @@ -419,10 +419,10 @@ void DialogsInner::onDialogRowReplaced(Dialogs::Row *oldRow, Dialogs::Row *newRo void DialogsInner::createDialog(History *history) { bool creating = !history->inChatList(); if (creating) { - Dialogs::Row *mainRow = history->addToChatList(dialogs.data()); + Dialogs::Row *mainRow = history->addToChatList(dialogs.get()); contactsNoDialogs->del(history->peer, mainRow); } - RefPair(int32, movedFrom, int32, movedTo) = history->adjustByPosInChatsList(dialogs.data()); + RefPair(int32, movedFrom, int32, movedTo) = history->adjustByPosInChatsList(dialogs.get()); emit dialogMoved(movedFrom, movedTo); @@ -441,7 +441,7 @@ void DialogsInner::removeDialog(History *history) { if (sel && sel->history() == history) { sel = nullptr; } - history->removeFromChatList(dialogs.data()); + history->removeFromChatList(dialogs.get()); history->clearNotifications(); if (App::wnd()) App::wnd()->notifyClear(history); if (contacts->contains(history->peer->id)) { @@ -1450,9 +1450,9 @@ void DialogsInner::destroyData() { _filter.clear(); _searchedSel = _peopleSel = -1; clearSearchResults(); - contacts.clear(); - contactsNoDialogs.clear(); - dialogs.clear(); + contacts = nullptr; + contactsNoDialogs = nullptr; + dialogs = nullptr; } void DialogsInner::peerBefore(const PeerData *inPeer, MsgId inMsg, PeerData *&outPeer, MsgId &outMsg) const { @@ -1593,11 +1593,11 @@ void DialogsInner::peerAfter(const PeerData *inPeer, MsgId inMsg, PeerData *&out } Dialogs::IndexedList *DialogsInner::contactsList() { - return contacts.data(); + return contacts.get(); } Dialogs::IndexedList *DialogsInner::dialogsList() { - return dialogs.data(); + return dialogs.get(); } DialogsInner::FilteredDialogs &DialogsInner::filteredList() { diff --git a/Telegram/SourceFiles/dialogswidget.h b/Telegram/SourceFiles/dialogswidget.h index 3460e8b69..4def9e106 100644 --- a/Telegram/SourceFiles/dialogswidget.h +++ b/Telegram/SourceFiles/dialogswidget.h @@ -170,7 +170,7 @@ private: bool menuPeerMuted(); void contextBlockDone(QPair data, const MTPBool &result); - using DialogsList = UniquePointer; + using DialogsList = std_::unique_ptr; DialogsList dialogs; DialogsList contactsNoDialogs; DialogsList contacts; diff --git a/Telegram/SourceFiles/dropdown.cpp b/Telegram/SourceFiles/dropdown.cpp index 53d1460b2..59be24664 100644 --- a/Telegram/SourceFiles/dropdown.cpp +++ b/Telegram/SourceFiles/dropdown.cpp @@ -1917,10 +1917,10 @@ void StickerPanInner::refreshSwitchPmButton(const InlineCacheEntry *entry) { _switchPmStartToken.clear(); } else { if (!_switchPmButton) { - _switchPmButton = MakeUnique(this, QString(), st::switchPmButton); + _switchPmButton = std_::make_unique(this, QString(), st::switchPmButton); _switchPmButton->show(); _switchPmButton->move(st::inlineResultsLeft, st::emojiPanHeader); - connect(_switchPmButton.data(), SIGNAL(clicked()), this, SLOT(onSwitchPm())); + connect(_switchPmButton.get(), SIGNAL(clicked()), this, SLOT(onSwitchPm())); } _switchPmButton->setText(entry->switchPmText); // doesn't perform text.toUpper() _switchPmStartToken = entry->switchPmStartToken; diff --git a/Telegram/SourceFiles/dropdown.h b/Telegram/SourceFiles/dropdown.h index 10c51449b..ba8eb213e 100644 --- a/Telegram/SourceFiles/dropdown.h +++ b/Telegram/SourceFiles/dropdown.h @@ -463,7 +463,7 @@ private: QTimer _updateInlineItems; bool _inlineWithThumb; - UniquePointer _switchPmButton; + std_::unique_ptr _switchPmButton; QString _switchPmStartToken; typedef QVector InlineItems; diff --git a/Telegram/SourceFiles/history.cpp b/Telegram/SourceFiles/history.cpp index e4e28b669..acd26701a 100644 --- a/Telegram/SourceFiles/history.cpp +++ b/Telegram/SourceFiles/history.cpp @@ -1969,7 +1969,7 @@ HistoryBlock *History::finishBuildingFrontBlock() { } } - _buildingFrontBlock.clear(); + _buildingFrontBlock = nullptr; return block; } @@ -2641,7 +2641,7 @@ int ReplyKeyboard::naturalHeight() const { } void ReplyKeyboard::paint(Painter &p, const QRect &clip) const { - t_assert(!_st.isNull()); + t_assert(_st != nullptr); t_assert(_width > 0); _st->startPaint(p); @@ -2815,7 +2815,7 @@ void HistoryMessageReplyMarkup::createFromButtonRows(const QVectorinlineKeyboard) { - markup->inlineKeyboard.reset(new ReplyKeyboard(this, MakeUnique(st::msgBotKbButton))); + markup->inlineKeyboard.reset(new ReplyKeyboard(this, std_::make_unique(st::msgBotKbButton))); } // if we have a text bubble we can resize it to fit the keyboard diff --git a/Telegram/SourceFiles/history.h b/Telegram/SourceFiles/history.h index 623035380..b37a51961 100644 --- a/Telegram/SourceFiles/history.h +++ b/Telegram/SourceFiles/history.h @@ -350,12 +350,12 @@ public: NotifyQueue notifies; HistoryDraft *msgDraft() { - return _msgDraft.data(); + return _msgDraft.get(); } HistoryEditDraft *editDraft() { - return _editDraft.data(); + return _editDraft.get(); } - void setMsgDraft(UniquePointer &&draft) { + void setMsgDraft(std_::unique_ptr &&draft) { _msgDraft = std_::move(draft); } void takeMsgDraft(History *from) { @@ -367,14 +367,14 @@ public: from->clearMsgDraft(); } } - void setEditDraft(UniquePointer &&draft) { + void setEditDraft(std_::unique_ptr &&draft) { _editDraft = std_::move(draft); } void clearMsgDraft() { - _msgDraft.clear(); + _msgDraft = nullptr; } void clearEditDraft() { - _editDraft.clear(); + _editDraft = nullptr; } HistoryDraft *draft() { return _editDraft ? editDraft() : msgDraft(); @@ -513,7 +513,7 @@ protected: void startBuildingFrontBlock(int expectedItemsCount = 1); HistoryBlock *finishBuildingFrontBlock(); // Returns the built block or nullptr if nothing was added. bool isBuildingFrontBlock() const { - return !_buildingFrontBlock.isNull(); + return _buildingFrontBlock != nullptr; } private: @@ -553,14 +553,14 @@ private: int expectedItemsCount = 0; // optimization for block->items.reserve() call HistoryBlock *block = nullptr; }; - UniquePointer _buildingFrontBlock; + std_::unique_ptr _buildingFrontBlock; // Creates if necessary a new block for adding item. // Depending on isBuildingFrontBlock() gets front or back block. HistoryBlock *prepareBlockForAddingItem(); - UniquePointer _msgDraft; - UniquePointer _editDraft; + std_::unique_ptr _msgDraft; + std_::unique_ptr _editDraft; }; @@ -782,7 +782,7 @@ struct HistoryMessageReply : public BaseComponent { ~HistoryMessageReply() { // clearData() should be called by holder t_assert(replyToMsg == nullptr); - t_assert(_replyToVia.data() == nullptr); + t_assert(_replyToVia == nullptr); } bool updateData(HistoryMessage *holder, bool force = false); @@ -816,7 +816,7 @@ struct HistoryMessageReply : public BaseComponent { mutable Text replyToName, replyToText; mutable int replyToVersion = 0; mutable int _maxReplyWidth = 0; - UniquePointer _replyToVia; + std_::unique_ptr _replyToVia; int toWidth = 0; }; Q_DECLARE_OPERATORS_FOR_FLAGS(HistoryMessageReply::PaintFlags); @@ -849,7 +849,7 @@ struct HistoryMessageReplyMarkup : public BaseComponent inlineKeyboard; + std_::unique_ptr inlineKeyboard; // If >= 0 it holds the y coord of the inlineKeyboard before the last edition. int oldTop = -1; @@ -900,7 +900,7 @@ public: friend class ReplyKeyboard; }; - typedef UniquePointer