From a56ece4efe80cbd89c5688a1af65127074ae1aa8 Mon Sep 17 00:00:00 2001 From: John Preston Date: Thu, 7 Apr 2016 20:16:02 +0400 Subject: [PATCH] NeverFreedPointer implementation improved and fixed for MSVC. --- Telegram/SourceFiles/basic_types.h | 14 +++++++------- Telegram/SourceFiles/gui/text.cpp | 2 +- Telegram/SourceFiles/gui/text.h | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Telegram/SourceFiles/basic_types.h b/Telegram/SourceFiles/basic_types.h index 0b9e39603b..62c9c40564 100644 --- a/Telegram/SourceFiles/basic_types.h +++ b/Telegram/SourceFiles/basic_types.h @@ -824,6 +824,13 @@ public: } } + template + void makeIfNull(Args&&... args) { + if (isNull()) { + reset(new T(std::forward(args)...)); + } + }; + T *data() const { return _p; } @@ -857,13 +864,6 @@ private: }; -template -using NeverFreedPointerCreator = T*(*)(); -template -inline NeverFreedPointerCreator MakeNeverFreedCreator(Args&&... args) { - return [&args...]() -> T* { return new T(std_::forward(args)...); }; -} - // This pointer is used for static non-POD variables that are allocated // on first use by constructor and are never automatically freed. template diff --git a/Telegram/SourceFiles/gui/text.cpp b/Telegram/SourceFiles/gui/text.cpp index c1b14ec0f7..c6ab96669e 100644 --- a/Telegram/SourceFiles/gui/text.cpp +++ b/Telegram/SourceFiles/gui/text.cpp @@ -86,7 +86,7 @@ bool ClickHandler::setActive(const ClickHandlerPtr &p, ClickHandlerHost *host) { } } if (p) { - _active.createIfNull(MakeNeverFreedCreator()); + _active.makeIfNull(); *_active = p; if ((_activeHost = host)) { bool emitClickHandlerActiveChanged = (!_pressed || !*_pressed || *_pressed == *_active); diff --git a/Telegram/SourceFiles/gui/text.h b/Telegram/SourceFiles/gui/text.h index 08301e1b68..997f3e1a78 100644 --- a/Telegram/SourceFiles/gui/text.h +++ b/Telegram/SourceFiles/gui/text.h @@ -366,7 +366,7 @@ public: if (!_active || !*_active) { return; } - _pressed.createIfNull(MakeNeverFreedCreator()); + _pressed.makeIfNull(); *_pressed = *_active; if ((_pressedHost = _activeHost)) { _pressedHost->clickHandlerPressedChanged(*_pressed, true);