diff --git a/Telegram/SourceFiles/app.cpp b/Telegram/SourceFiles/app.cpp index 407ea84e9..b35fc9abc 100644 --- a/Telegram/SourceFiles/app.cpp +++ b/Telegram/SourceFiles/app.cpp @@ -1794,17 +1794,17 @@ namespace App { MsgsData *data = fetchMsgsData(item->channelId(), false); if (!data) return; - MsgsData::iterator i = data->find(item->id); + auto i = data->find(item->id); if (i != data->cend()) { if (i.value() == item) { data->erase(i); } } historyItemDetached(item); - DependentItems::iterator j = ::dependentItems.find(item); + auto j = ::dependentItems.find(item); if (j != ::dependentItems.cend()) { - for (OrderedSet::const_iterator k = j.value().cbegin(), e = j.value().cend(); k != e; ++k) { - k.key()->dependencyItemRemoved(item); + for_const (HistoryItem *dependent, j.value()) { + dependent->dependencyItemRemoved(item); } ::dependentItems.erase(j); } @@ -1816,8 +1816,8 @@ namespace App { void historyUpdateDependent(HistoryItem *item) { DependentItems::iterator j = ::dependentItems.find(item); if (j != ::dependentItems.cend()) { - for (OrderedSet::const_iterator k = j.value().cbegin(), e = j.value().cend(); k != e; ++k) { - k.key()->updateDependencyItem(); + for_const (HistoryItem *dependent, j.value()) { + dependent->updateDependencyItem(); } } if (App::main()) { @@ -1829,15 +1829,15 @@ namespace App { ::dependentItems.clear(); QVector toDelete; - for (MsgsData::const_iterator i = msgsData.cbegin(), e = msgsData.cend(); i != e; ++i) { - if ((*i)->detached()) { - toDelete.push_back(*i); + for_const (HistoryItem *item, msgsData) { + if (item->detached()) { + toDelete.push_back(item); } } - for (ChannelMsgsData::const_iterator j = channelMsgsData.cbegin(), end = channelMsgsData.cend(); j != end; ++j) { - for (MsgsData::const_iterator i = j->cbegin(), e = j->cend(); i != e; ++i) { - if ((*i)->detached()) { - toDelete.push_back(*i); + for_const (const MsgsData &chMsgsData, channelMsgsData) { + for_const (HistoryItem *item, chMsgsData) { + if (item->detached()) { + toDelete.push_back(item); } } } diff --git a/Telegram/SourceFiles/boxes/contactsbox.cpp b/Telegram/SourceFiles/boxes/contactsbox.cpp index 0d23989d5..3e0cf9212 100644 --- a/Telegram/SourceFiles/boxes/contactsbox.cpp +++ b/Telegram/SourceFiles/boxes/contactsbox.cpp @@ -1670,12 +1670,15 @@ void ContactsBox::getAdminsDone(const MTPmessages_ChatFull &result) { } } _saveRequestId = 0; - for (ChatData::Admins::const_iterator i = curadmins.cbegin(), e = curadmins.cend(); i != e; ++i) { - MTP::send(MTPmessages_EditChatAdmin(_inner.chat()->inputChat, i.key()->inputUser, MTP_boolFalse()), rpcDone(&ContactsBox::removeAdminDone, i.key()), rpcFail(&ContactsBox::editAdminFail), 0, (appoint.isEmpty() && i + 1 == e) ? 0 : 10); + + for_const (UserData *user, curadmins) { + MTP::send(MTPmessages_EditChatAdmin(_inner.chat()->inputChat, user->inputUser, MTP_boolFalse()), rpcDone(&ContactsBox::removeAdminDone, user), rpcFail(&ContactsBox::editAdminFail), 0, 10); } - for (int32 i = 0, l = appoint.size(); i < l; ++i) { - MTP::send(MTPmessages_EditChatAdmin(_inner.chat()->inputChat, appoint.at(i)->inputUser, MTP_boolTrue()), rpcDone(&ContactsBox::setAdminDone, appoint.at(i)), rpcFail(&ContactsBox::editAdminFail), 0, (i + 1 == l) ? 0 : 10); + for_const (UserData *user, appoint) { + MTP::send(MTPmessages_EditChatAdmin(_inner.chat()->inputChat, user->inputUser, MTP_boolTrue()), rpcDone(&ContactsBox::setAdminDone, user), rpcFail(&ContactsBox::editAdminFail), 0, 10); } + MTP::sendAnything(); + _saveRequestId = curadmins.size() + appoint.size(); if (!_saveRequestId) { onClose(); diff --git a/Telegram/SourceFiles/dropdown.cpp b/Telegram/SourceFiles/dropdown.cpp index 44d9d2653..ee475348b 100644 --- a/Telegram/SourceFiles/dropdown.cpp +++ b/Telegram/SourceFiles/dropdown.cpp @@ -4501,8 +4501,7 @@ void MentionsDropdown::updateFiltered(bool resetScroll) { if (_channel->mgInfo->bots.isEmpty()) { if (!_channel->mgInfo->botStatus && App::api()) App::api()->requestBots(_channel); } else { - for (MegagroupInfo::Bots::const_iterator i = _channel->mgInfo->bots.cbegin(), e = _channel->mgInfo->bots.cend(); i != e; ++i) { - UserData *user = i.key(); + for_const (auto *user, _channel->mgInfo->bots) { if (!user->botInfo) continue; if (!user->botInfo->inited && App::api()) App::api()->requestFullPeer(user); if (user->botInfo->commands.isEmpty()) continue; diff --git a/Telegram/SourceFiles/facades.cpp b/Telegram/SourceFiles/facades.cpp index 378d807f9..0b9557e3d 100644 --- a/Telegram/SourceFiles/facades.cpp +++ b/Telegram/SourceFiles/facades.cpp @@ -216,8 +216,8 @@ namespace Notify { if (MainWidget *m = App::main()) { m->notify_handlePendingHistoryUpdate(); } - for (auto i = Global::PendingRepaintItems().cbegin(), e = Global::PendingRepaintItems().cend(); i != e; ++i) { - Ui::repaintHistoryItem(i.key()); + for_const (HistoryItem *item, Global::PendingRepaintItems()) { + Ui::repaintHistoryItem(item); } Global::RefPendingRepaintItems().clear(); } diff --git a/Telegram/SourceFiles/history.cpp b/Telegram/SourceFiles/history.cpp index b04fb0b06..4110b299a 100644 --- a/Telegram/SourceFiles/history.cpp +++ b/Telegram/SourceFiles/history.cpp @@ -6090,7 +6090,11 @@ void HistoryMessageReply::resize(int width) const { void HistoryMessageReply::itemRemoved(HistoryMessage *holder, HistoryItem *removed) { if (replyToMsg == removed) { - clearData(holder); + delete _replyToVia; + _replyToVia = nullptr; + + replyToMsg = nullptr; + replyToMsgId = 0; holder->setPendingInitDimensions(); } } diff --git a/Telegram/SourceFiles/mtproto/dcenter.cpp b/Telegram/SourceFiles/mtproto/dcenter.cpp index 62e6bdaf7..6fbdeac24 100644 --- a/Telegram/SourceFiles/mtproto/dcenter.cpp +++ b/Telegram/SourceFiles/mtproto/dcenter.cpp @@ -269,11 +269,11 @@ void ConfigLoader::enumDC() { dcs.insert(MTP::bareDcId(i.key())); } } - OrderedSet::const_iterator i = dcs.constFind(_enumCurrent); + auto i = dcs.constFind(_enumCurrent); if (i == dcs.cend() || (++i) == dcs.cend()) { - _enumCurrent = dcs.cbegin().key(); + _enumCurrent = *dcs.cbegin(); } else { - _enumCurrent = i.key(); + _enumCurrent = *i; } _enumRequest = MTP::send(MTPhelp_GetConfig(), rpcDone(configLoaded), rpcFail(configFailed), MTP::cfgDcId(_enumCurrent)); diff --git a/Telegram/SourceFiles/mtproto/facade.cpp b/Telegram/SourceFiles/mtproto/facade.cpp index eb3b23f2f..ed02dadb9 100644 --- a/Telegram/SourceFiles/mtproto/facade.cpp +++ b/Telegram/SourceFiles/mtproto/facade.cpp @@ -837,9 +837,9 @@ void finish() { sessions.clear(); mainSession = nullptr; - for (MTPQuittingConnections::const_iterator i = quittingConnections.cbegin(), e = quittingConnections.cend(); i != e; ++i) { - i.key()->waitTillFinish(); - delete i.key(); + for_const (internal::Connection *connection, quittingConnections) { + connection->waitTillFinish(); + delete connection; } quittingConnections.clear(); diff --git a/Telegram/SourceFiles/mtproto/file_download.cpp b/Telegram/SourceFiles/mtproto/file_download.cpp index aa8251de4..b7333812b 100644 --- a/Telegram/SourceFiles/mtproto/file_download.cpp +++ b/Telegram/SourceFiles/mtproto/file_download.cpp @@ -985,27 +985,27 @@ void WebLoadManager::process() { i.value() = 0; } } - for (Loaders::iterator i = _loaders.begin(), e = _loaders.end(); i != e;) { - LoaderPointers::iterator it = _loaderPointers.find(i.key()->_interface); - if (it != _loaderPointers.cend() && it.key()->_private != i.key()) { + for (auto i = _loaders.begin(), e = _loaders.end(); i != e;) { + LoaderPointers::iterator it = _loaderPointers.find((*i)->_interface); + if (it != _loaderPointers.cend() && it.key()->_private != (*i)) { it = _loaderPointers.end(); } if (it == _loaderPointers.cend()) { - if (QNetworkReply *reply = i.key()->reply()) { + if (QNetworkReply *reply = (*i)->reply()) { _replies.remove(reply); reply->abort(); reply->deleteLater(); } - delete i.key(); + delete (*i); i = _loaders.erase(i); } else { ++i; } } } - for (Loaders::const_iterator i = newLoaders.cbegin(), e = newLoaders.cend(); i != e; ++i) { - if (_loaders.contains(i.key())) { - sendRequest(i.key()); + for_const (webFileLoaderPrivate *loader, newLoaders) { + if (_loaders.contains(loader)) { + sendRequest(loader); } } } @@ -1047,8 +1047,8 @@ void WebLoadManager::clear() { } _loaderPointers.clear(); - for (Loaders::iterator i = _loaders.begin(), e = _loaders.end(); i != e; ++i) { - delete i.key(); + for_const (webFileLoaderPrivate *loader, _loaders) { + delete loader; } _loaders.clear(); diff --git a/Telegram/SourceFiles/types.h b/Telegram/SourceFiles/types.h index 4d3263332..0c0b3f34b 100644 --- a/Telegram/SourceFiles/types.h +++ b/Telegram/SourceFiles/types.h @@ -36,13 +36,126 @@ T *getPointerAndReset(T *&ptr) { struct NullType { }; +// ordered set template based on QMap template -class OrderedSet : public QMap { +class OrderedSet { + typedef OrderedSet Self; + typedef QMap Impl; + typedef typename Impl::iterator IteratorImpl; + typedef typename Impl::const_iterator ConstIteratorImpl; + Impl impl_; + public: - void insert(const T &v) { - QMap::insert(v, NullType()); - } + inline bool operator==(const Self &other) const { return impl_ == other.impl_; } + inline bool operator!=(const Self &other) const { return impl_ != other.impl_; } + inline int size() const { return impl_.size(); } + inline bool isEmpty() const { return impl_.isEmpty(); } + inline void detach() { return impl_.detach(); } + inline bool isDetached() const { return impl_.isDetached(); } + inline void clear() { return impl_.clear(); } + inline QList values() const { return impl_.keys(); } + inline const T &first() const { return impl_.firstKey(); } + inline const T &last() const { return impl_.lastKey(); } + + class const_iterator; + class iterator { + public: + typedef typename IteratorImpl::iterator_category iterator_category; + typedef typename IteratorImpl::difference_type difference_type; + typedef T value_type; + typedef T *pointer; + typedef T &reference; + + explicit iterator(const IteratorImpl &impl) : impl_(impl) { + } + inline const T &operator*() const { return impl_.key(); } + inline const T *operator->() const { return &impl_.key(); } + inline bool operator==(const iterator &other) const { return impl_ == other.impl_; } + inline bool operator!=(const iterator &other) const { return impl_ != other.impl_; } + inline iterator &operator++() { ++impl_; return *this; } + inline iterator operator++(int) { return iterator(impl_++); } + inline iterator &operator--() { --impl_; return *this; } + inline iterator operator--(int) { return iterator(impl_--); } + inline iterator operator+(int j) const { return iterator(impl_ + j); } + inline iterator operator-(int j) const { return iterator(impl_ - j); } + inline iterator &operator+=(int j) { impl_ += j; return *this; } + inline iterator &operator-=(int j) { impl_ -= j; return *this; } + + friend class const_iterator; + inline bool operator==(const const_iterator &other) const { return impl_ == other.impl_; } + inline bool operator!=(const const_iterator &other) const { return impl_ != other.impl_; } + + private: + IteratorImpl impl_; + friend class Self; + + }; + friend class iterator; + + class const_iterator { + public: + typedef typename IteratorImpl::iterator_category iterator_category; + typedef typename IteratorImpl::difference_type difference_type; + typedef T value_type; + typedef T *pointer; + typedef T &reference; + + explicit const_iterator(const ConstIteratorImpl &impl) : impl_(impl) { + } + inline const T &operator*() const { return impl_.key(); } + inline const T *operator->() const { return &impl_.key(); } + inline bool operator==(const const_iterator &other) const { return impl_ == other.impl_; } + inline bool operator!=(const const_iterator &other) const { return impl_ != other.impl_; } + inline const_iterator &operator++() { ++impl_; return *this; } + inline const_iterator operator++(int) { return const_iterator(impl_++); } + inline const_iterator &operator--() { --impl_; return *this; } + inline const_iterator operator--(int) { return const_iterator(impl_--); } + inline const_iterator operator+(int j) const { return const_iterator(impl_ + j); } + inline const_iterator operator-(int j) const { return const_iterator(impl_ - j); } + inline const_iterator &operator+=(int j) { impl_ += j; return *this; } + inline const_iterator &operator-=(int j) { impl_ -= j; return *this; } + + friend class iterator; + inline bool operator==(const iterator &other) const { return impl_ == other.impl_; } + inline bool operator!=(const iterator &o) const { return impl_ != other.impl_; } + + private: + ConstIteratorImpl impl_; + friend class Self; + + }; + friend class const_iterator; + + // STL style + inline iterator begin() { return iterator(impl_.begin()); } + inline const_iterator begin() const { return const_iterator(impl_.cbegin()); } + inline const_iterator constBegin() const { return const_iterator(impl_.cbegin()); } + inline const_iterator cbegin() const { return const_iterator(impl_.cbegin()); } + inline iterator end() { detach(); return iterator(impl_.end()); } + inline const_iterator end() const { return const_iterator(impl_.cend()); } + inline const_iterator constEnd() const { return const_iterator(impl_.cend()); } + inline const_iterator cend() const { return const_iterator(impl_.cend()); } + inline iterator erase(iterator it) { return iterator(impl_.erase(it.impl_)); } + + inline iterator insert(const T &value) { return iterator(impl_.insert(value, NullType())); } + inline iterator insert(const_iterator pos, const T &value) { return iterator(impl_.insert(pos.impl_, value, NullType())); } + inline int remove(const T &value) { return impl_.remove(value); } + inline bool contains(const T &value) const { return impl_.contains(value); } + + // more Qt + typedef iterator Iterator; + typedef const_iterator ConstIterator; + inline int count() const { return impl_.count(); } + inline iterator find(const T &value) { return iterator(impl_.find(value)); } + inline const_iterator find(const T &value) const { return const_iterator(impl_.constFind(value)); } + inline const_iterator constFind(const T &value) const { return const_iterator(impl_.constFind(value)); } + inline Self &unite(const Self &other) { impl_.unite(other.impl_); return *this; } + + // STL compatibility + typedef typename Impl::difference_type difference_type; + typedef typename Impl::size_type size_type; + inline bool empty() const { return impl_.empty(); } };