diff --git a/Telegram/SourceFiles/rpl/lifetime.h b/Telegram/SourceFiles/rpl/lifetime.h index 8c21184a5..a8a2b77f7 100644 --- a/Telegram/SourceFiles/rpl/lifetime.h +++ b/Telegram/SourceFiles/rpl/lifetime.h @@ -8,7 +8,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #pragma once #include "base/unique_function.h" -#include +#include namespace rpl { namespace details { @@ -25,6 +25,7 @@ public: lifetime() = default; lifetime(lifetime &&other); lifetime &operator=(lifetime &&other); + ~lifetime() { destroy(); } template ()())> lifetime(Destroy &&destroy); @@ -38,18 +39,16 @@ public: template Type *make_state(Args&& ...args) { - auto result = new Type(std::forward(args)...); - add([result] { + const auto result = new Type(std::forward(args)...); + add([=] { static_assert(sizeof(Type) > 0, "Can't delete unknown type."); delete result; }); return result; } - ~lifetime() { destroy(); } - private: - std::deque> _callbacks; + std::vector> _callbacks; }; @@ -70,20 +69,21 @@ inline lifetime::lifetime(Destroy &&destroy) { template inline void lifetime::add(Destroy &&destroy) { - _callbacks.push_front(destroy); + _callbacks.emplace_back(std::forward(destroy)); } inline void lifetime::add(lifetime &&other) { auto callbacks = details::take(other._callbacks); _callbacks.insert( - _callbacks.begin(), + _callbacks.end(), std::make_move_iterator(callbacks.begin()), std::make_move_iterator(callbacks.end())); } inline void lifetime::destroy() { - for (auto &callback : details::take(_callbacks)) { - callback(); + auto callbacks = details::take(_callbacks); + for (auto i = callbacks.rbegin(), e = callbacks.rend(); i != e; ++i) { + (*i)(); } }