From d326c7e3fa5e028e77ffe111f427569fdb17764a Mon Sep 17 00:00:00 2001 From: John Preston Date: Thu, 18 Jan 2018 23:02:50 +0300 Subject: [PATCH] Remove HistoryItemInstantiated. --- .../admin_log/history_admin_log_item.cpp | 14 +- Telegram/SourceFiles/history/history.cpp | 16 +- Telegram/SourceFiles/history/history_item.cpp | 17 +- Telegram/SourceFiles/history/history_item.h | 20 -- .../SourceFiles/history/history_message.h | 235 +++++------------- .../SourceFiles/history/history_service.h | 47 ++-- 6 files changed, 103 insertions(+), 246 deletions(-) diff --git a/Telegram/SourceFiles/history/admin_log/history_admin_log_item.cpp b/Telegram/SourceFiles/history/admin_log/history_admin_log_item.cpp index 332d510bbe..16e826d833 100644 --- a/Telegram/SourceFiles/history/admin_log/history_admin_log_item.cpp +++ b/Telegram/SourceFiles/history/admin_log/history_admin_log_item.cpp @@ -334,7 +334,7 @@ void GenerateItems( auto addSimpleServiceMessage = [&](const QString &text, PhotoData *photo = nullptr) { auto message = HistoryService::PreparedText { text }; message.links.push_back(fromLink); - addPart(HistoryService::create(history, idManager.next(), ::date(date), message, 0, peerToUser(from->id), photo)); + addPart(new HistoryService(history, idManager.next(), ::date(date), message, 0, peerToUser(from->id), photo)); }; auto createChangeTitle = [&](const MTPDchannelAdminLogEventActionChangeTitle &action) { @@ -355,7 +355,7 @@ void GenerateItems( auto bodyReplyTo = 0; auto bodyViaBotId = 0; auto newDescription = PrepareText(newValue, QString()); - auto body = HistoryMessage::create(history, idManager.next(), bodyFlags, bodyReplyTo, bodyViaBotId, ::date(date), peerToUser(from->id), QString(), newDescription); + auto body = new HistoryMessage(history, idManager.next(), bodyFlags, bodyReplyTo, bodyViaBotId, ::date(date), peerToUser(from->id), QString(), newDescription); if (!oldValue.isEmpty()) { auto oldDescription = PrepareText(oldValue, QString()); body->addLogEntryOriginal(id, lang(lng_admin_log_previous_description), oldDescription); @@ -376,7 +376,7 @@ void GenerateItems( auto bodyReplyTo = 0; auto bodyViaBotId = 0; auto newLink = newValue.isEmpty() ? TextWithEntities() : PrepareText(Messenger::Instance().createInternalLinkFull(newValue), QString()); - auto body = HistoryMessage::create(history, idManager.next(), bodyFlags, bodyReplyTo, bodyViaBotId, ::date(date), peerToUser(from->id), QString(), newLink); + auto body = new HistoryMessage(history, idManager.next(), bodyFlags, bodyReplyTo, bodyViaBotId, ::date(date), peerToUser(from->id), QString(), newLink); if (!oldValue.isEmpty()) { auto oldLink = PrepareText(Messenger::Instance().createInternalLinkFull(oldValue), QString()); body->addLogEntryOriginal(id, lang(lng_admin_log_previous_link), oldLink); @@ -486,7 +486,7 @@ void GenerateItems( auto bodyReplyTo = 0; auto bodyViaBotId = 0; auto bodyText = GenerateParticipantChangeText(channel, action.vparticipant); - addPart(HistoryMessage::create(history, idManager.next(), bodyFlags, bodyReplyTo, bodyViaBotId, ::date(date), peerToUser(from->id), QString(), bodyText)); + addPart(new HistoryMessage(history, idManager.next(), bodyFlags, bodyReplyTo, bodyViaBotId, ::date(date), peerToUser(from->id), QString(), bodyText)); }; auto createParticipantToggleBan = [&](const MTPDchannelAdminLogEventActionParticipantToggleBan &action) { @@ -494,7 +494,7 @@ void GenerateItems( auto bodyReplyTo = 0; auto bodyViaBotId = 0; auto bodyText = GenerateParticipantChangeText(channel, action.vnew_participant, &action.vprev_participant); - addPart(HistoryMessage::create(history, idManager.next(), bodyFlags, bodyReplyTo, bodyViaBotId, ::date(date), peerToUser(from->id), QString(), bodyText)); + addPart(new HistoryMessage(history, idManager.next(), bodyFlags, bodyReplyTo, bodyViaBotId, ::date(date), peerToUser(from->id), QString(), bodyText)); }; auto createParticipantToggleAdmin = [&](const MTPDchannelAdminLogEventActionParticipantToggleAdmin &action) { @@ -502,7 +502,7 @@ void GenerateItems( auto bodyReplyTo = 0; auto bodyViaBotId = 0; auto bodyText = GenerateParticipantChangeText(channel, action.vnew_participant, &action.vprev_participant); - addPart(HistoryMessage::create(history, idManager.next(), bodyFlags, bodyReplyTo, bodyViaBotId, ::date(date), peerToUser(from->id), QString(), bodyText)); + addPart(new HistoryMessage(history, idManager.next(), bodyFlags, bodyReplyTo, bodyViaBotId, ::date(date), peerToUser(from->id), QString(), bodyText)); }; auto createChangeStickerSet = [&](const MTPDchannelAdminLogEventActionChangeStickerSet &action) { @@ -523,7 +523,7 @@ void GenerateItems( auto message = HistoryService::PreparedText { text }; message.links.push_back(fromLink); message.links.push_back(setLink); - addPart(HistoryService::create(history, idManager.next(), ::date(date), message, 0, peerToUser(from->id))); + addPart(new HistoryService(history, idManager.next(), ::date(date), message, 0, peerToUser(from->id))); } }; diff --git a/Telegram/SourceFiles/history/history.cpp b/Telegram/SourceFiles/history/history.cpp index ac099511c3..83ce21bfb8 100644 --- a/Telegram/SourceFiles/history/history.cpp +++ b/Telegram/SourceFiles/history/history.cpp @@ -437,7 +437,7 @@ HistoryJoined *ChannelHistory::insertJoinedMessage(bool unread) { auto inviteDate = peer->asChannel()->inviteDate; if (unread) _maxReadMessageDate = inviteDate; if (isEmpty()) { - _joinedMessage = HistoryJoined::create(this, inviteDate, inviter, flags); + _joinedMessage = new HistoryJoined(this, inviteDate, inviter, flags); addNewItem(_joinedMessage, unread); return _joinedMessage; } @@ -456,7 +456,7 @@ HistoryJoined *ChannelHistory::insertJoinedMessage(bool unread) { } if (item->date <= inviteDate) { ++itemIndex; - _joinedMessage = HistoryJoined::create( + _joinedMessage = new HistoryJoined( this, inviteDate, inviter, @@ -476,7 +476,7 @@ HistoryJoined *ChannelHistory::insertJoinedMessage(bool unread) { startBuildingFrontBlock(); - _joinedMessage = HistoryJoined::create(this, inviteDate, inviter, flags); + _joinedMessage = new HistoryJoined(this, inviteDate, inviter, flags); addItemToBlock(_joinedMessage); finishBuildingFrontBlock(); @@ -766,7 +766,7 @@ not_null History::createItemForwarded( UserId from, const QString &postAuthor, HistoryMessage *original) { - return HistoryMessage::create( + return new HistoryMessage( this, id, flags, @@ -787,7 +787,7 @@ not_null History::createItemDocument( DocumentData *document, const TextWithEntities &caption, const MTPReplyMarkup &markup) { - return HistoryMessage::create( + return new HistoryMessage( this, id, flags, @@ -812,7 +812,7 @@ not_null History::createItemPhoto( PhotoData *photo, const TextWithEntities &caption, const MTPReplyMarkup &markup) { - return HistoryMessage::create( + return new HistoryMessage( this, id, flags, @@ -836,7 +836,7 @@ not_null History::createItemGame( const QString &postAuthor, GameData *game, const MTPReplyMarkup &markup) { - return HistoryMessage::create( + return new HistoryMessage( this, id, flags, @@ -857,7 +857,7 @@ not_null History::addNewService( bool unread) { auto message = HistoryService::PreparedText { text }; return addNewItem( - HistoryService::create(this, msgId, date, message, flags), + new HistoryService(this, msgId, date, message, flags), unread); } diff --git a/Telegram/SourceFiles/history/history_item.cpp b/Telegram/SourceFiles/history/history_item.cpp index 27438a2c1e..4f5f5ce533 100644 --- a/Telegram/SourceFiles/history/history_item.cpp +++ b/Telegram/SourceFiles/history/history_item.cpp @@ -56,7 +56,7 @@ not_null CreateUnsupportedMessage( text.entities.push_front( EntityInText(EntityInTextItalic, 0, text.text.size())); flags &= ~MTPDmessage::Flag::f_post_author; - return HistoryMessage::create( + return new HistoryMessage( history, msgId, flags, @@ -81,9 +81,6 @@ HistoryItem::HistoryItem( , _history(history) , _from(from ? App::user(from) : history->peer) , _flags(flags) { -} - -void HistoryItem::finishCreate() { App::historyRegItem(this); } @@ -783,7 +780,7 @@ not_null HistoryItem::Create( const auto text = HistoryService::PreparedText { lang(lng_message_empty) }; - return HistoryService::create(history, data.vid.v, ::date(), text); + return new HistoryService(history, data.vid.v, ::date(), text); } break; case mtpc_message: { @@ -881,7 +878,7 @@ not_null HistoryItem::Create( const auto text = HistoryService::PreparedText { lang(lng_message_empty) }; - return HistoryService::create( + return new HistoryService( history, data.vid.v, ::date(data.vdate), @@ -889,17 +886,17 @@ not_null HistoryItem::Create( data.vflags.v, data.has_from_id() ? data.vfrom_id.v : UserId(0)); } else if (badMedia == MediaCheckResult::HasTimeToLive) { - return HistoryService::create(history, data); + return new HistoryService(history, data); } - return HistoryMessage::create(history, data); + return new HistoryMessage(history, data); } break; case mtpc_messageService: { auto &data = message.c_messageService(); if (data.vaction.type() == mtpc_messageActionPhoneCall) { - return HistoryMessage::create(history, data); + return new HistoryMessage(history, data); } - return HistoryService::create(history, data); + return new HistoryService(history, data); } break; } diff --git a/Telegram/SourceFiles/history/history_item.h b/Telegram/SourceFiles/history/history_item.h index 4ac0df8a07..e10d247e18 100644 --- a/Telegram/SourceFiles/history/history_item.h +++ b/Telegram/SourceFiles/history/history_item.h @@ -287,10 +287,6 @@ protected: QDateTime date, UserId from); - // To completely create history item we need to call - // a virtual method, it can not be done from constructor. - virtual void finishCreate(); - virtual void markMediaAsReadHook() { } @@ -327,22 +323,6 @@ private: }; -// make all the constructors in HistoryItem children protected -// and wrapped with a static create() call with the same args -// so that history item can not be created directly, without -// calling a virtual finishCreate() method -template -class HistoryItemInstantiated { -public: - template - static not_null _create(Args &&... args) { - auto result = new T(std::forward(args)...); - result->finishCreate(); - return result; - } - -}; - ClickHandlerPtr goToMessageClickHandler( not_null peer, MsgId msgId); diff --git a/Telegram/SourceFiles/history/history_message.h b/Telegram/SourceFiles/history/history_message.h index 2bdf89b933..de8fa124ed 100644 --- a/Telegram/SourceFiles/history/history_message.h +++ b/Telegram/SourceFiles/history/history_message.h @@ -25,182 +25,8 @@ void FastShareMessage(not_null item); QString FormatViewsCount(int views); class HistoryMessage - : public HistoryItem - , private HistoryItemInstantiated { + : public HistoryItem { public: - static not_null create( - not_null history, - const MTPDmessage &msg) { - return _create(history, msg); - } - static not_null create( - not_null history, - const MTPDmessageService &msg) { - return _create(history, msg); - } - static not_null create( - not_null history, - MsgId msgId, - MTPDmessage::Flags flags, - QDateTime date, - UserId from, - const QString &postAuthor, - not_null fwd) { - return _create(history, msgId, flags, date, from, postAuthor, fwd); - } - static not_null create( - not_null history, - MsgId msgId, - MTPDmessage::Flags flags, - MsgId replyTo, - UserId viaBotId, - QDateTime date, - UserId from, - const QString &postAuthor, - const TextWithEntities &textWithEntities) { - return _create( - history, - msgId, - flags, - replyTo, - viaBotId, - date, - from, - postAuthor, - textWithEntities); - } - static not_null create( - not_null history, - MsgId msgId, - MTPDmessage::Flags flags, - MsgId replyTo, - UserId viaBotId, - QDateTime date, - UserId from, - const QString &postAuthor, - not_null document, - const TextWithEntities &caption, - const MTPReplyMarkup &markup) { - return _create( - history, - msgId, - flags, - replyTo, - viaBotId, - date, - from, - postAuthor, - document, - caption, - markup); - } - static not_null create( - not_null history, - MsgId msgId, - MTPDmessage::Flags flags, - MsgId replyTo, - UserId viaBotId, - QDateTime date, - UserId from, - const QString &postAuthor, - not_null photo, - const TextWithEntities &caption, - const MTPReplyMarkup &markup) { - return _create( - history, - msgId, - flags, - replyTo, - viaBotId, - date, - from, - postAuthor, - photo, - caption, - markup); - } - static not_null create( - not_null history, - MsgId msgId, - MTPDmessage::Flags flags, - MsgId replyTo, - UserId viaBotId, - QDateTime date, - UserId from, - const QString &postAuthor, - not_null game, - const MTPReplyMarkup &markup) { - return _create( - history, - msgId, - flags, - replyTo, - viaBotId, - date, - from, - postAuthor, - game, - markup); - } - - void refreshMedia(const MTPMessageMedia *media); - void refreshSentMedia(const MTPMessageMedia *media); - void setMedia(const MTPMessageMedia &media); - static std::unique_ptr CreateMedia( - not_null item, - const MTPMessageMedia &media); - - int32 plainMaxWidth() const; - - bool allowsForward() const override; - bool allowsEdit(const QDateTime &now) const override; - bool uploading() const; - - void applyGroupAdminChanges( - const base::flat_map &changes) override; - - void setViewsCount(int32 count) override; - void setRealId(MsgId newId) override; - - void dependencyItemRemoved(HistoryItem *dependency) override; - - QString notificationHeader() const override; - - void applyEdition(const MTPDmessage &message) override; - void applyEdition(const MTPDmessageService &message) override; - void updateSentMedia(const MTPMessageMedia *media) override; - void updateReplyMarkup(const MTPReplyMarkup *markup) override { - setReplyMarkup(markup); - } - - void addToUnreadMentions(UnreadMentionType type) override; - void eraseFromUnreadMentions() override; - Storage::SharedMediaTypesMask sharedMediaTypes() const override; - - void setText(const TextWithEntities &textWithEntities) override; - TextWithEntities originalText() const override; - bool textHasLinks() const override; - - int viewsCount() const override; - not_null displayFrom() const; - bool updateDependencyItem() override; - MsgId dependencyMsgId() const override { - return replyToId(); - } - - HistoryMessage *toHistoryMessage() override { // dynamic_cast optimize - return this; - } - const HistoryMessage *toHistoryMessage() const override { // dynamic_cast optimize - return this; - } - - std::unique_ptr createView( - not_null delegate) override; - - ~HistoryMessage(); - -private: HistoryMessage( not_null history, const MTPDmessage &msg); @@ -260,8 +86,65 @@ private: const QString &postAuthor, not_null game, const MTPReplyMarkup &markup); // local game - friend class HistoryItemInstantiated; + void refreshMedia(const MTPMessageMedia *media); + void refreshSentMedia(const MTPMessageMedia *media); + void setMedia(const MTPMessageMedia &media); + static std::unique_ptr CreateMedia( + not_null item, + const MTPMessageMedia &media); + + int32 plainMaxWidth() const; + + bool allowsForward() const override; + bool allowsEdit(const QDateTime &now) const override; + bool uploading() const; + + void applyGroupAdminChanges( + const base::flat_map &changes) override; + + void setViewsCount(int32 count) override; + void setRealId(MsgId newId) override; + + void dependencyItemRemoved(HistoryItem *dependency) override; + + QString notificationHeader() const override; + + void applyEdition(const MTPDmessage &message) override; + void applyEdition(const MTPDmessageService &message) override; + void updateSentMedia(const MTPMessageMedia *media) override; + void updateReplyMarkup(const MTPReplyMarkup *markup) override { + setReplyMarkup(markup); + } + + void addToUnreadMentions(UnreadMentionType type) override; + void eraseFromUnreadMentions() override; + Storage::SharedMediaTypesMask sharedMediaTypes() const override; + + void setText(const TextWithEntities &textWithEntities) override; + TextWithEntities originalText() const override; + bool textHasLinks() const override; + + int viewsCount() const override; + not_null displayFrom() const; + bool updateDependencyItem() override; + MsgId dependencyMsgId() const override { + return replyToId(); + } + + HistoryMessage *toHistoryMessage() override { // dynamic_cast optimize + return this; + } + const HistoryMessage *toHistoryMessage() const override { // dynamic_cast optimize + return this; + } + + std::unique_ptr createView( + not_null delegate) override; + + ~HistoryMessage(); + +private: void setEmptyText(); bool hasAdminBadge() const { return _flags & MTPDmessage_ClientFlag::f_has_admin_badge; diff --git a/Telegram/SourceFiles/history/history_service.h b/Telegram/SourceFiles/history/history_service.h index a49723c74c..820192ea49 100644 --- a/Telegram/SourceFiles/history/history_service.h +++ b/Telegram/SourceFiles/history/history_service.h @@ -51,22 +51,25 @@ namespace HistoryView { class ServiceMessagePainter; } // namespace HistoryView -class HistoryService : public HistoryItem, private HistoryItemInstantiated { +class HistoryService : public HistoryItem { public: struct PreparedText { QString text; QList links; }; - static not_null create(not_null history, const MTPDmessage &message) { - return _create(history, message); - } - static not_null create(not_null history, const MTPDmessageService &message) { - return _create(history, message); - } - static not_null create(not_null history, MsgId msgId, QDateTime date, const PreparedText &message, MTPDmessage::Flags flags = 0, UserId from = 0, PhotoData *photo = nullptr) { - return _create(history, msgId, date, message, flags, from, photo); - } + HistoryService(not_null history, const MTPDmessage &message); + HistoryService( + not_null history, + const MTPDmessageService &message); + HistoryService( + not_null history, + MsgId msgId, + QDateTime date, + const PreparedText &message, + MTPDmessage::Flags flags = 0, + UserId from = 0, + PhotoData *photo = nullptr); bool updateDependencyItem() override; MsgId dependencyMsgId() const override { @@ -104,11 +107,6 @@ public: protected: friend class HistoryView::ServiceMessagePainter; - HistoryService(not_null history, const MTPDmessage &message); - HistoryService(not_null history, const MTPDmessageService &message); - HistoryService(not_null history, MsgId msgId, QDateTime date, const PreparedText &message, MTPDmessage::Flags flags = 0, UserId from = 0, PhotoData *photo = 0); - friend class HistoryItemInstantiated; - void markMediaAsReadHook() override; void setServiceText(const PreparedText &prepared); @@ -153,18 +151,17 @@ private: }; -class HistoryJoined : public HistoryService, private HistoryItemInstantiated { +class HistoryJoined : public HistoryService { public: - static not_null create(not_null history, const QDateTime &inviteDate, not_null inviter, MTPDmessage::Flags flags) { - return _create(history, inviteDate, inviter, flags); - } - -protected: - HistoryJoined(not_null history, const QDateTime &inviteDate, not_null inviter, MTPDmessage::Flags flags); - using HistoryItemInstantiated::_create; - friend class HistoryItemInstantiated; + HistoryJoined( + not_null history, + const QDateTime &inviteDate, + not_null inviter, + MTPDmessage::Flags flags); private: - static PreparedText GenerateText(not_null history, not_null inviter); + static PreparedText GenerateText( + not_null history, + not_null inviter); };