From 9a988d89e3efd048c3ada13b6b016d3cb19280d2 Mon Sep 17 00:00:00 2001 From: John Preston Date: Wed, 25 Oct 2017 11:38:55 +0300 Subject: [PATCH] Remove std::any dependency (for now). Xcode 9 still doesn't have std::any :( --- Telegram/SourceFiles/boxes/peer_list_box.cpp | 14 +++++- Telegram/SourceFiles/boxes/peer_list_box.h | 48 ++++++++++--------- .../info_profile_members_controllers.cpp | 11 +++-- .../profile/profile_channel_controllers.cpp | 41 ++++++++-------- .../profile/profile_channel_controllers.h | 14 +++--- Telegram/SourceFiles/stdafx.h | 1 - 6 files changed, 75 insertions(+), 54 deletions(-) diff --git a/Telegram/SourceFiles/boxes/peer_list_box.cpp b/Telegram/SourceFiles/boxes/peer_list_box.cpp index 4b8a16c612..56c899f4d0 100644 --- a/Telegram/SourceFiles/boxes/peer_list_box.cpp +++ b/Telegram/SourceFiles/boxes/peer_list_box.cpp @@ -260,6 +260,15 @@ void PeerListController::setSearchNoResultsText(const QString &text) { } } +std::unique_ptr PeerListController::saveState() { + return delegate()->peerListSaveState(); +} + +void PeerListController::restoreState( + std::unique_ptr state) { + delegate()->peerListRestoreState(std::move(state)); +} + void PeerListBox::addSelectItem(not_null peer, PeerListRow::SetStyle style) { if (!_select) { createMultiSelect(); @@ -1230,7 +1239,8 @@ void PeerListContent::searchQueryChanged(QString query) { std::unique_ptr PeerListContent::saveState() const { auto result = std::make_unique(); - result->controllerState = EmptyControllerState(); + result->controllerState + = std::make_unique(); result->list.reserve(_rows.size()); for (auto &row : _rows) { result->list.push_back(row->peer()); @@ -1245,7 +1255,7 @@ std::unique_ptr PeerListContent::saveState() const { void PeerListContent::restoreState( std::unique_ptr state) { - if (!state || !state->controllerState.has_value()) { + if (!state || !state->controllerState) { return; } diff --git a/Telegram/SourceFiles/boxes/peer_list_box.h b/Telegram/SourceFiles/boxes/peer_list_box.h index 2a5029a4f1..22192e01bf 100644 --- a/Telegram/SourceFiles/boxes/peer_list_box.h +++ b/Telegram/SourceFiles/boxes/peer_list_box.h @@ -215,16 +215,7 @@ enum class PeerListSearchMode { Enabled, }; -struct PeerListState { - PeerListState() = default; - PeerListState(PeerListState &&other) = delete; - PeerListState &operator=(PeerListState &&other) = delete; - - base::unique_any controllerState; - std::vector> list; - std::vector> filterResults; - QString searchQuery; -}; +struct PeerListState; class PeerListDelegate { public: @@ -284,6 +275,10 @@ public: class PeerListSearchController { public: + struct SavedStateBase { + virtual ~SavedStateBase() = default; + }; + virtual void searchQuery(const QString &query) = 0; virtual bool isLoading() = 0; virtual bool loadMoreRows() = 0; @@ -293,10 +288,11 @@ public: _delegate = delegate; } - virtual base::unique_any saveState() { - return {}; + virtual std::unique_ptr saveState() { + return nullptr; } - virtual void restoreState(base::unique_any &&state) { + virtual void restoreState( + std::unique_ptr state) { } protected: @@ -311,6 +307,10 @@ private: class PeerListController : public PeerListSearchDelegate { public: + struct SavedStateBase { + virtual ~SavedStateBase() = default; + }; + // Search works only with RowId == peer->id. PeerListController(std::unique_ptr searchController = nullptr); @@ -343,13 +343,9 @@ public: return nullptr; } - virtual std::unique_ptr saveState() { - return delegate()->peerListSaveState(); - } + virtual std::unique_ptr saveState(); virtual void restoreState( - std::unique_ptr state) { - delegate()->peerListRestoreState(std::move(state)); - } + std::unique_ptr state); bool isRowSelected(not_null peer) { return delegate()->peerListIsRowSelected(peer); @@ -401,6 +397,17 @@ private: }; +struct PeerListState { + PeerListState() = default; + PeerListState(PeerListState &&other) = delete; + PeerListState &operator=(PeerListState &&other) = delete; + + std::unique_ptr controllerState; + std::vector> list; + std::vector> filterResults; + QString searchQuery; +}; + class PeerListContent : public Ui::RpWidget , private base::Subscriber { @@ -517,9 +524,6 @@ private: Selected old; }; - struct EmptyControllerState { - }; - void setSelected(Selected selected); void setPressed(Selected pressed); void setContexted(Selected contexted); diff --git a/Telegram/SourceFiles/info/profile/info_profile_members_controllers.cpp b/Telegram/SourceFiles/info/profile/info_profile_members_controllers.cpp index 7412da98aa..8d5d7550e3 100644 --- a/Telegram/SourceFiles/info/profile/info_profile_members_controllers.cpp +++ b/Telegram/SourceFiles/info/profile/info_profile_members_controllers.cpp @@ -63,6 +63,9 @@ public: void restoreState(std::unique_ptr state) override; private: + struct SavedState : SavedStateBase { + rpl::lifetime lifetime; + }; void rebuildRows(); void refreshOnlineCount(); std::unique_ptr createRow(not_null user); @@ -133,13 +136,13 @@ void ChatMembersController::sortByOnline() { std::unique_ptr ChatMembersController::saveState() { auto result = PeerListController::saveState(); - auto lifetime = rpl::lifetime(); + auto my = std::make_unique(); using Flag = Notify::PeerUpdate::Flag; Notify::PeerUpdateViewer(_chat, Flag::MembersChanged) | rpl::start_with_next([state = result.get()](auto update) { - state->controllerState = base::unique_any{}; - }, lifetime); - result->controllerState = std::move(lifetime); + state->controllerState = nullptr; + }, my->lifetime); + result->controllerState = std::move(my); return result; } diff --git a/Telegram/SourceFiles/profile/profile_channel_controllers.cpp b/Telegram/SourceFiles/profile/profile_channel_controllers.cpp index 3cd0a00874..986481a76f 100644 --- a/Telegram/SourceFiles/profile/profile_channel_controllers.cpp +++ b/Telegram/SourceFiles/profile/profile_channel_controllers.cpp @@ -251,16 +251,16 @@ std::unique_ptr ParticipantsBoxController::saveState() { auto result = PeerListController::saveState(); - auto my = SavedState(); - my.additional = std::move(_additional); - my.offset = _offset; - my.allLoaded = _allLoaded; + auto my = std::make_unique(); + my->additional = std::move(_additional); + my->offset = _offset; + my->allLoaded = _allLoaded; if (auto requestId = base::take(_loadRequestId)) { request(requestId).cancel(); - my.wasLoading = true; + my->wasLoading = true; } if (auto search = searchController()) { - my.searchState = search->saveState(); + my->searchState = search->saveState(); } auto weak = result.get(); @@ -278,7 +278,7 @@ std::unique_ptr ParticipantsBoxController::saveState() { base::stable_partition(weak->list, [user](not_null peer) { return (peer == user); }); - }, my.lifetime); + }, my->lifetime); Auth().data().megagroupParticipantRemoved(_channel) | rpl::start_with_next([weak](not_null user) { weak->list.erase(std::remove( @@ -289,7 +289,7 @@ std::unique_ptr ParticipantsBoxController::saveState() { weak->filterResults.begin(), weak->filterResults.end(), user), weak->filterResults.end()); - }, my.lifetime); + }, my->lifetime); result->controllerState = std::move(my); return result; @@ -297,8 +297,10 @@ std::unique_ptr ParticipantsBoxController::saveState() { void ParticipantsBoxController::restoreState( std::unique_ptr state) { - auto typeErasedState = &state->controllerState; - if (auto my = base::any_cast(typeErasedState)) { + auto typeErasedState = state + ? state->controllerState.get() + : nullptr; + if (auto my = dynamic_cast(typeErasedState)) { if (auto requestId = base::take(_loadRequestId)) { request(requestId).cancel(); } @@ -851,21 +853,22 @@ void ParticipantsBoxSearchController::searchQuery(const QString &query) { } } -base::unique_any ParticipantsBoxSearchController::saveState() { - auto result = SavedState(); - result.query = _query; - result.offset = _offset; - result.allLoaded = _allLoaded; +auto ParticipantsBoxSearchController::saveState() +-> std::unique_ptr { + auto result = std::make_unique(); + result->query = _query; + result->offset = _offset; + result->allLoaded = _allLoaded; if (auto requestId = base::take(_requestId)) { request(requestId).cancel(); - result.wasLoading = true; + result->wasLoading = true; } - return result; + return std::move(result); } void ParticipantsBoxSearchController::restoreState( - base::unique_any &&state) { - if (auto my = base::any_cast(&state)) { + std::unique_ptr state) { + if (auto my = dynamic_cast(state.get())) { if (auto requestId = base::take(_requestId)) { request(requestId).cancel(); } diff --git a/Telegram/SourceFiles/profile/profile_channel_controllers.h b/Telegram/SourceFiles/profile/profile_channel_controllers.h index 11b7a67e1e..174eb51b10 100644 --- a/Telegram/SourceFiles/profile/profile_channel_controllers.h +++ b/Telegram/SourceFiles/profile/profile_channel_controllers.h @@ -102,8 +102,8 @@ protected: virtual std::unique_ptr createRow(not_null user) const; private: - struct SavedState { - base::unique_any searchState; + struct SavedState : SavedStateBase { + std::unique_ptr searchState; int offset = 0; bool allLoaded = false; bool wasLoading = false; @@ -150,7 +150,9 @@ private: }; // Members, banned and restricted users server side search. -class ParticipantsBoxSearchController : public PeerListSearchController, private MTP::Sender { +class ParticipantsBoxSearchController + : public PeerListSearchController + , private MTP::Sender { public: using Role = ParticipantsBoxController::Role; using Additional = ParticipantsBoxController::Additional; @@ -161,11 +163,11 @@ public: bool isLoading() override; bool loadMoreRows() override; - base::unique_any saveState() override; - void restoreState(base::unique_any &&state) override; + std::unique_ptr saveState() override; + void restoreState(std::unique_ptr state) override; private: - struct SavedState { + struct SavedState : SavedStateBase { QString query; int offset = 0; bool allLoaded = false; diff --git a/Telegram/SourceFiles/stdafx.h b/Telegram/SourceFiles/stdafx.h index d8a4746b3e..4f793d1110 100644 --- a/Telegram/SourceFiles/stdafx.h +++ b/Telegram/SourceFiles/stdafx.h @@ -84,7 +84,6 @@ namespace func = base::functors; #include "base/flat_set.h" #include "base/flat_map.h" -#include "base/unique_any.h" #include "core/basic_types.h" #include "logs.h"