From 14034c255e209c23e7ba616e0058015a4d7d50f3 Mon Sep 17 00:00:00 2001 From: John Preston Date: Mon, 18 Dec 2017 13:07:18 +0400 Subject: [PATCH] Replace QSharedPointer with std::shared_ptr. --- Telegram/SourceFiles/base/observer.h | 24 ++++++---- Telegram/SourceFiles/boxes/about_box.cpp | 2 +- .../chat_helpers/gifs_list_widget.cpp | 2 +- .../chat_helpers/stickers_list_widget.cpp | 16 ++++--- .../chat_helpers/stickers_list_widget.h | 4 +- Telegram/SourceFiles/core/click_handler.cpp | 8 ++-- Telegram/SourceFiles/core/click_handler.h | 15 ++++--- .../SourceFiles/core/click_handler_types.h | 6 ++- Telegram/SourceFiles/core/utils.h | 5 --- Telegram/SourceFiles/data/data_peer.cpp | 2 +- .../history/history_admin_log_inner.cpp | 14 +++--- .../history/history_admin_log_item.cpp | 2 +- .../history/history_inner_widget.cpp | 18 ++++---- Telegram/SourceFiles/history/history_item.cpp | 6 +-- Telegram/SourceFiles/history/history_item.h | 4 +- .../history/history_media_types.cpp | 44 +++++++++---------- .../SourceFiles/history/history_media_types.h | 14 +++--- .../SourceFiles/history/history_message.cpp | 6 +-- .../SourceFiles/history/history_service.cpp | 2 +- .../SourceFiles/history/history_widget.cpp | 8 ++-- .../info/media/info_media_list_widget.cpp | 10 ++--- .../info/profile/info_profile_cover.cpp | 2 +- .../inline_bot_layout_internal.cpp | 8 ++-- .../inline_bots/inline_bot_layout_item.cpp | 4 +- .../inline_bots/inline_results_widget.cpp | 2 +- Telegram/SourceFiles/intro/introphone.cpp | 4 +- Telegram/SourceFiles/logs.cpp | 18 +++++--- Telegram/SourceFiles/mediaview.cpp | 2 +- Telegram/SourceFiles/mtproto/core_types.cpp | 12 +++-- Telegram/SourceFiles/mtproto/core_types.h | 5 ++- Telegram/SourceFiles/mtproto/rpc_sender.h | 4 +- Telegram/SourceFiles/mtproto/sender.h | 8 ++-- .../SourceFiles/overview/overview_layout.cpp | 28 ++++++------ .../linux/notifications_manager_linux.cpp | 10 ++++- .../settings/settings_info_widget.cpp | 2 +- Telegram/SourceFiles/storage/file_upload.h | 2 +- .../SourceFiles/storage/localimageloader.cpp | 2 +- .../SourceFiles/storage/localimageloader.h | 4 +- Telegram/SourceFiles/storage/localstorage.cpp | 10 ++--- Telegram/SourceFiles/ui/images.cpp | 15 ++++--- Telegram/SourceFiles/ui/images.h | 4 +- Telegram/SourceFiles/ui/text/text.cpp | 28 ++++++------ .../ui/widgets/discrete_sliders.cpp | 2 +- .../SourceFiles/ui/widgets/discrete_sliders.h | 2 +- Telegram/SourceFiles/ui/widgets/labels.cpp | 4 +- .../SourceFiles/window/window_main_menu.cpp | 6 +-- 46 files changed, 215 insertions(+), 185 deletions(-) diff --git a/Telegram/SourceFiles/base/observer.h b/Telegram/SourceFiles/base/observer.h index d22c37498..982f507e6 100644 --- a/Telegram/SourceFiles/base/observer.h +++ b/Telegram/SourceFiles/base/observer.h @@ -46,7 +46,6 @@ struct SubscriptionHandlerHelper { template using SubscriptionHandler = typename SubscriptionHandlerHelper::type; -// Required because QShared/WeakPointer can't point to void. class BaseObservableData { }; @@ -84,14 +83,17 @@ public: private: struct Node { - Node(const QSharedPointer &observable) : observable(observable) { + Node(const std::shared_ptr &observable) + : observable(observable) { } Node *next = nullptr; Node *prev = nullptr; - QWeakPointer observable; + std::weak_ptr observable; }; using RemoveAndDestroyMethod = void(*)(Node*); - Subscription(Node *node, RemoveAndDestroyMethod removeAndDestroyMethod) : _node(node), _removeAndDestroyMethod(removeAndDestroyMethod) { + Subscription(Node *node, RemoveAndDestroyMethod removeAndDestroyMethod) + : _node(node) + , _removeAndDestroyMethod(removeAndDestroyMethod) { } Node *_node = nullptr; @@ -115,13 +117,13 @@ class CommonObservable { public: Subscription add_subscription(Handler &&handler) { if (!_data) { - _data = MakeShared>(this); + _data = std::make_shared>(this); } return _data->append(std::move(handler)); } private: - QSharedPointer> _data; + std::shared_ptr> _data; friend class CommonObservableData; friend class BaseObservable::is_fast_copy_type::value>; @@ -184,7 +186,11 @@ public: private: struct Node : public Subscription::Node { - Node(const QSharedPointer &observer, Handler &&handler) : Subscription::Node(observer), handler(std::move(handler)) { + Node( + const std::shared_ptr &observer, + Handler &&handler) + : Subscription::Node(observer) + , handler(std::move(handler)) { } Handler handler; }; @@ -210,8 +216,8 @@ private: } static void removeAndDestroyNode(Subscription::Node *node) { - if (auto that = node->observable.toStrongRef()) { - static_cast(that.data())->remove(node); + if (const auto that = node->observable.lock()) { + static_cast(that.get())->remove(node); } delete static_cast(node); } diff --git a/Telegram/SourceFiles/boxes/about_box.cpp b/Telegram/SourceFiles/boxes/about_box.cpp index bb5b27edc..504299162 100644 --- a/Telegram/SourceFiles/boxes/about_box.cpp +++ b/Telegram/SourceFiles/boxes/about_box.cpp @@ -45,7 +45,7 @@ void AboutBox::prepare() { addButton(langFactory(lng_close), [this] { closeBox(); }); const auto linkHook = [](const ClickHandlerPtr &link, auto button) { - if (const auto url = dynamic_cast(link.data())) { + if (const auto url = dynamic_cast(link.get())) { url->UrlClickHandler::onClick(button); return false; } diff --git a/Telegram/SourceFiles/chat_helpers/gifs_list_widget.cpp b/Telegram/SourceFiles/chat_helpers/gifs_list_widget.cpp index 4a7509c8a..7357bed90 100644 --- a/Telegram/SourceFiles/chat_helpers/gifs_list_widget.cpp +++ b/Telegram/SourceFiles/chat_helpers/gifs_list_widget.cpp @@ -335,7 +335,7 @@ void GifsListWidget::mouseReleaseEvent(QMouseEvent *e) { return; } - if (dynamic_cast(activated.data())) { + if (dynamic_cast(activated.get())) { int row = _selected / MatrixRowShift, column = _selected % MatrixRowShift; selectInlineResult(row, column); } else { diff --git a/Telegram/SourceFiles/chat_helpers/stickers_list_widget.cpp b/Telegram/SourceFiles/chat_helpers/stickers_list_widget.cpp index 614feb64b..ec9d10a87 100644 --- a/Telegram/SourceFiles/chat_helpers/stickers_list_widget.cpp +++ b/Telegram/SourceFiles/chat_helpers/stickers_list_widget.cpp @@ -1005,19 +1005,21 @@ QRect StickersListWidget::megagroupSetButtonRectFinal() const { return result; } -QSharedPointer StickersListWidget::createButtonRipple(int section) { +std::shared_ptr StickersListWidget::createButtonRipple(int section) { if (_section == Section::Featured) { auto maskSize = QSize(_addWidth - st::stickersTrendingAdd.width, st::stickersTrendingAdd.height); auto mask = Ui::RippleAnimation::roundRectMask(maskSize, st::buttonRadius); - return MakeShared(st::stickersTrendingAdd.ripple, std::move(mask), [this, section] { - rtlupdate(featuredAddRect(section)); - }); + return std::make_shared( + st::stickersTrendingAdd.ripple, + std::move(mask), + [this, section] { rtlupdate(featuredAddRect(section)); }); } auto maskSize = QSize(st::stickerPanRemoveSet.rippleAreaSize, st::stickerPanRemoveSet.rippleAreaSize); auto mask = Ui::RippleAnimation::ellipseMask(maskSize); - return MakeShared(st::stickerPanRemoveSet.ripple, std::move(mask), [this, section] { - rtlupdate(removeButtonRect(section)); - }); + return std::make_shared( + st::stickerPanRemoveSet.ripple, + std::move(mask), + [this, section] { rtlupdate(removeButtonRect(section)); }); } QPoint StickersListWidget::buttonRippleTopLeft(int section) const { diff --git a/Telegram/SourceFiles/chat_helpers/stickers_list_widget.h b/Telegram/SourceFiles/chat_helpers/stickers_list_widget.h index 593b15b4b..1bf997642 100644 --- a/Telegram/SourceFiles/chat_helpers/stickers_list_widget.h +++ b/Telegram/SourceFiles/chat_helpers/stickers_list_widget.h @@ -144,7 +144,7 @@ private: MTPDstickerSet::Flags flags; QString title; Stickers::Pack pack; - QSharedPointer ripple; + std::shared_ptr ripple; }; using Sets = QList; @@ -172,7 +172,7 @@ private: void updateSelected(); void setSelected(OverState newSelected); void setPressed(OverState newPressed); - QSharedPointer createButtonRipple(int section); + std::shared_ptr createButtonRipple(int section); QPoint buttonRippleTopLeft(int section) const; enum class ValidateIconAnimations { diff --git a/Telegram/SourceFiles/core/click_handler.cpp b/Telegram/SourceFiles/core/click_handler.cpp index 46e74f3c2..1f5bef7fc 100644 --- a/Telegram/SourceFiles/core/click_handler.cpp +++ b/Telegram/SourceFiles/core/click_handler.cpp @@ -38,9 +38,11 @@ bool ClickHandler::setActive(const ClickHandlerPtr &p, ClickHandlerHost *host) { // other pressed click handler currently, if there is // this method will be called when it is unpressed if (_active && *_active) { - auto emitClickHandlerActiveChanged = (!_pressed || !*_pressed || *_pressed == *_active); - auto wasactive = *_active; - (*_active).clear(); + const auto emitClickHandlerActiveChanged = false + || !_pressed + || !*_pressed + || (*_pressed == *_active); + const auto wasactive = base::take(*_active); if (_activeHost) { if (emitClickHandlerActiveChanged) { _activeHost->clickHandlerActiveChanged(wasactive, false); diff --git a/Telegram/SourceFiles/core/click_handler.h b/Telegram/SourceFiles/core/click_handler.h index 734c98c79..c9a2bd976 100644 --- a/Telegram/SourceFiles/core/click_handler.h +++ b/Telegram/SourceFiles/core/click_handler.h @@ -21,7 +21,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org #pragma once class ClickHandler; -using ClickHandlerPtr = QSharedPointer; +using ClickHandlerPtr = std::shared_ptr; enum ExpandLinksMode { ExpandLinksNone, @@ -103,9 +103,8 @@ public: // The activated click handler (if any) is returned. static ClickHandlerPtr unpressed() { if (_pressed && *_pressed) { - bool activated = (_active && *_active == *_pressed); - ClickHandlerPtr waspressed = *_pressed; - (*_pressed).clear(); + const auto activated = (_active && *_active == *_pressed); + const auto waspressed = base::take(*_pressed); if (_pressedHost) { _pressedHost->clickHandlerPressedChanged(waspressed, false); _pressedHost = nullptr; @@ -144,11 +143,15 @@ public: } static void hostDestroyed(ClickHandlerHost *host) { if (_activeHost == host) { - if (_active) (*_active).clear(); + if (_active) { + *_active = nullptr; + } _activeHost = nullptr; } if (_pressedHost == host) { - if (_pressed) (*_pressed).clear(); + if (_pressed) { + *_pressed = nullptr; + } _pressedHost = nullptr; } } diff --git a/Telegram/SourceFiles/core/click_handler_types.h b/Telegram/SourceFiles/core/click_handler_types.h index 04f7ddb8f..8c7067e27 100644 --- a/Telegram/SourceFiles/core/click_handler_types.h +++ b/Telegram/SourceFiles/core/click_handler_types.h @@ -97,14 +97,16 @@ private: QString _originalUrl, _readable; }; -typedef QSharedPointer TextClickHandlerPtr; +using TextClickHandlerPtr = std::shared_ptr; class HiddenUrlClickHandler : public UrlClickHandler { public: HiddenUrlClickHandler(QString url) : UrlClickHandler(url, false) { } QString copyToClipboardContextItemText() const override { - return url().isEmpty() ? QString() : UrlClickHandler::copyToClipboardContextItemText(); + return url().isEmpty() + ? QString() + : UrlClickHandler::copyToClipboardContextItemText(); } static void doOpen(QString url); diff --git a/Telegram/SourceFiles/core/utils.h b/Telegram/SourceFiles/core/utils.h index 90ac2c3d8..0c17b6c6b 100644 --- a/Telegram/SourceFiles/core/utils.h +++ b/Telegram/SourceFiles/core/utils.h @@ -545,11 +545,6 @@ static int32 QuarterArcLength = (FullArcLength / 4); static int32 MinArcLength = (FullArcLength / 360); static int32 AlmostFullArcLength = (FullArcLength - MinArcLength); -template -inline QSharedPointer MakeShared(Args&&... args) { - return QSharedPointer(new T(std::forward(args)...)); -} - // This pointer is used for global non-POD variables that are allocated // on demand by createIfNull(lambda) and are never automatically freed. template diff --git a/Telegram/SourceFiles/data/data_peer.cpp b/Telegram/SourceFiles/data/data_peer.cpp index 5dd8cc272..b12a1ba97 100644 --- a/Telegram/SourceFiles/data/data_peer.cpp +++ b/Telegram/SourceFiles/data/data_peer.cpp @@ -164,7 +164,7 @@ void PeerData::refreshEmptyUserpic() const { } ClickHandlerPtr PeerData::createOpenLink() { - return MakeShared(this); + return std::make_shared(this); } void PeerData::setUserpic( diff --git a/Telegram/SourceFiles/history/history_admin_log_inner.cpp b/Telegram/SourceFiles/history/history_admin_log_inner.cpp index 32371db1c..7f9b250e5 100644 --- a/Telegram/SourceFiles/history/history_admin_log_inner.cpp +++ b/Telegram/SourceFiles/history/history_admin_log_inner.cpp @@ -821,9 +821,9 @@ void InnerWidget::showContextMenu(QContextMenuEvent *e, bool showFromTouch) { _contextMenuLink = ClickHandler::getActive(); auto item = App::hoveredItem() ? App::hoveredItem() : App::hoveredLinkItem(); - auto lnkPhoto = dynamic_cast(_contextMenuLink.data()); - auto lnkDocument = dynamic_cast(_contextMenuLink.data()); - auto lnkPeer = dynamic_cast(_contextMenuLink.data()); + auto lnkPhoto = dynamic_cast(_contextMenuLink.get()); + auto lnkDocument = dynamic_cast(_contextMenuLink.get()); + auto lnkPeer = dynamic_cast(_contextMenuLink.get()); auto lnkIsVideo = lnkDocument ? lnkDocument->document()->isVideoFile() : false; auto lnkIsVoice = lnkDocument ? lnkDocument->document()->isVoiceMessage() : false; auto lnkIsAudio = lnkDocument ? lnkDocument->document()->isAudioFile() : false; @@ -979,7 +979,7 @@ void InnerWidget::showStickerPackInfo() { } void InnerWidget::cancelContextDownload() { - if (auto lnkDocument = dynamic_cast(_contextMenuLink.data())) { + if (auto lnkDocument = dynamic_cast(_contextMenuLink.get())) { lnkDocument->document()->cancel(); } else if (auto item = App::contextItem()) { if (auto media = item->getMedia()) { @@ -992,7 +992,7 @@ void InnerWidget::cancelContextDownload() { void InnerWidget::showContextInFolder() { QString filepath; - if (auto lnkDocument = dynamic_cast(_contextMenuLink.data())) { + if (auto lnkDocument = dynamic_cast(_contextMenuLink.get())) { filepath = lnkDocument->document()->filepath(DocumentData::FilePathResolveChecked); } else if (auto item = App::contextItem()) { if (auto media = item->getMedia()) { @@ -1240,9 +1240,9 @@ void InnerWidget::mouseActionCancel() { void InnerWidget::mouseActionFinish(const QPoint &screenPos, Qt::MouseButton button) { mouseActionUpdate(screenPos); - ClickHandlerPtr activated = ClickHandler::unpressed(); + auto activated = ClickHandler::unpressed(); if (_mouseAction == MouseAction::Dragging) { - activated.clear(); + activated = nullptr; } if (App::pressedItem()) { repaintItem(App::pressedItem()); diff --git a/Telegram/SourceFiles/history/history_admin_log_item.cpp b/Telegram/SourceFiles/history/history_admin_log_item.cpp index e5fadfdab..496f6e11b 100644 --- a/Telegram/SourceFiles/history/history_admin_log_item.cpp +++ b/Telegram/SourceFiles/history/history_admin_log_item.cpp @@ -488,7 +488,7 @@ void GenerateItems(not_null history, LocalIdManager &idManager, const fromLinkText, lt_sticker_set, textcmdLink(2, lang(lng_admin_log_changed_stickers_set))); - auto setLink = MakeShared([set] { + auto setLink = std::make_shared([set] { Ui::show(Box(set)); }); auto message = HistoryService::PreparedText { text }; diff --git a/Telegram/SourceFiles/history/history_inner_widget.cpp b/Telegram/SourceFiles/history/history_inner_widget.cpp index 10997d5ed..b98f47e6f 100644 --- a/Telegram/SourceFiles/history/history_inner_widget.cpp +++ b/Telegram/SourceFiles/history/history_inner_widget.cpp @@ -1031,7 +1031,7 @@ void HistoryInner::performDrag() { } auto pressedHandler = ClickHandler::getPressed(); - if (dynamic_cast(pressedHandler.data())) { + if (dynamic_cast(pressedHandler.get())) { return; } @@ -1127,14 +1127,14 @@ void HistoryInner::mouseActionFinish(const QPoint &screenPos, Qt::MouseButton bu auto activated = ClickHandler::unpressed(); if (_mouseAction == MouseAction::Dragging) { - activated.clear(); + activated = nullptr; } else if (_mouseActionItem) { // if we are in selecting items mode perhaps we want to // toggle selection instead of activating the pressed link if (_mouseAction == MouseAction::PrepareDrag && !_pressWasInactive && !_selected.empty() && _selected.cbegin()->second == FullSelection && button != Qt::RightButton) { if (auto media = _mouseActionItem->getMedia()) { if (media->toggleSelectionByHandlerClick(activated)) { - activated.clear(); + activated = nullptr; } } } @@ -1291,8 +1291,8 @@ void HistoryInner::showContextMenu(QContextMenuEvent *e, bool showFromTouch) { _menu = new Ui::PopupMenu(nullptr); _contextMenuLink = ClickHandler::getActive(); - auto lnkPhoto = dynamic_cast(_contextMenuLink.data()); - auto lnkDocument = dynamic_cast(_contextMenuLink.data()); + auto lnkPhoto = dynamic_cast(_contextMenuLink.get()); + auto lnkDocument = dynamic_cast(_contextMenuLink.get()); auto lnkIsVideo = lnkDocument ? lnkDocument->document()->isVideoFile() : false; auto lnkIsVoice = lnkDocument ? lnkDocument->document()->isVoiceMessage() : false; auto lnkIsAudio = lnkDocument ? lnkDocument->document()->isAudioFile() : false; @@ -1594,7 +1594,7 @@ void HistoryInner::toggleFavedSticker(DocumentData *document) { } void HistoryInner::cancelContextDownload() { - if (auto lnkDocument = dynamic_cast(_contextMenuLink.data())) { + if (auto lnkDocument = dynamic_cast(_contextMenuLink.get())) { lnkDocument->document()->cancel(); } else if (auto item = App::contextItem()) { if (auto media = item->getMedia()) { @@ -1607,7 +1607,7 @@ void HistoryInner::cancelContextDownload() { void HistoryInner::showContextInFolder() { QString filepath; - if (auto lnkDocument = dynamic_cast(_contextMenuLink.data())) { + if (auto lnkDocument = dynamic_cast(_contextMenuLink.get())) { filepath = lnkDocument->document()->filepath(DocumentData::FilePathResolveChecked); } else if (auto item = App::contextItem()) { if (auto media = item->getMedia()) { @@ -2286,9 +2286,9 @@ void HistoryInner::onUpdateSelected() { if (point.x() >= dateLeft && point.x() < dateLeft + dateWidth) { if (!_scrollDateLink) { - _scrollDateLink = MakeShared(item->history()->peer, item->date.date()); + _scrollDateLink = std::make_shared(item->history()->peer, item->date.date()); } else { - static_cast(_scrollDateLink.data())->setDate(item->date.date()); + static_cast(_scrollDateLink.get())->setDate(item->date.date()); } dragState = HistoryTextState( nullptr, diff --git a/Telegram/SourceFiles/history/history_item.cpp b/Telegram/SourceFiles/history/history_item.cpp index 0ff81dfc6..2f0e35a29 100644 --- a/Telegram/SourceFiles/history/history_item.cpp +++ b/Telegram/SourceFiles/history/history_item.cpp @@ -142,7 +142,7 @@ ReplyKeyboard::ReplyKeyboard(const HistoryItem *item, StylePtr &&s) auto &button = newRow[j]; auto str = row.at(j).text; button.type = row.at(j).type; - button.link = MakeShared(item, i, j); + button.link = std::make_shared(item, i, j); button.text.setText(_st->textStyle(), TextUtilities::SingleLine(str), _textPlainOptions); button.characters = str.isEmpty() ? 1 : str.size(); } @@ -324,7 +324,7 @@ void ReplyKeyboard::clickHandlerPressedChanged(const ClickHandlerPtr &p, bool pr if (pressed) { if (!button.ripple) { auto mask = Ui::RippleAnimation::roundRectMask(button.rect.size(), _st->buttonRadius()); - button.ripple = MakeShared(_st->_st->ripple, std::move(mask), [this] { _st->repaint(_item); }); + button.ripple = std::make_shared(_st->_st->ripple, std::move(mask), [this] { _st->repaint(_item); }); } button.ripple->add(_savedCoords - button.rect.topLeft()); } else { @@ -1424,7 +1424,7 @@ HistoryItem::~HistoryItem() { } ClickHandlerPtr goToMessageClickHandler(PeerData *peer, MsgId msgId) { - return MakeShared([peer, msgId] { + return std::make_shared([peer, msgId] { if (App::main()) { auto current = App::mousedItem(); if (current && current->history()->peer == peer) { diff --git a/Telegram/SourceFiles/history/history_item.h b/Telegram/SourceFiles/history/history_item.h index 4c387f357..c1f79ace9 100644 --- a/Telegram/SourceFiles/history/history_item.h +++ b/Telegram/SourceFiles/history/history_item.h @@ -372,7 +372,7 @@ private: void startAnimation(int i, int j, int direction); friend class Style; - using ReplyMarkupClickHandlerPtr = QSharedPointer; + using ReplyMarkupClickHandlerPtr = std::shared_ptr; struct Button { Text text = { 1 }; QRect rect; @@ -380,7 +380,7 @@ private: float64 howMuchOver = 0.; HistoryMessageReplyMarkup::Button::Type type; ReplyMarkupClickHandlerPtr link; - mutable QSharedPointer ripple; + mutable std::shared_ptr ripple; }; using ButtonRow = QVector