2017-09-03 12:44:21 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop version of Telegram messaging app, see https://telegram.org
|
|
|
|
|
|
|
|
Telegram Desktop is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
In addition, as a special exception, the copyright holders give permission
|
|
|
|
to link the code of portions of this program with the OpenSSL library.
|
|
|
|
|
|
|
|
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
|
|
|
|
Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "base/lambda.h"
|
2017-09-04 11:24:35 +00:00
|
|
|
#include <deque>
|
2017-09-03 12:44:21 +00:00
|
|
|
|
|
|
|
namespace rpl {
|
2017-11-20 12:23:20 +00:00
|
|
|
namespace details {
|
|
|
|
|
|
|
|
template <typename Type>
|
|
|
|
inline Type take(Type &value) {
|
|
|
|
return std::exchange(value, Type{});
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace details
|
2017-09-03 12:44:21 +00:00
|
|
|
|
|
|
|
class lifetime {
|
|
|
|
public:
|
|
|
|
lifetime() = default;
|
|
|
|
lifetime(lifetime &&other);
|
|
|
|
lifetime &operator=(lifetime &&other);
|
|
|
|
|
|
|
|
template <typename Destroy, typename = decltype(std::declval<Destroy>()())>
|
2017-09-04 11:24:35 +00:00
|
|
|
lifetime(Destroy &&destroy);
|
2017-09-03 12:44:21 +00:00
|
|
|
|
2017-09-04 11:24:35 +00:00
|
|
|
explicit operator bool() const { return !_callbacks.empty(); }
|
2017-09-03 12:44:21 +00:00
|
|
|
|
2017-09-04 11:24:35 +00:00
|
|
|
template <typename Destroy, typename = decltype(std::declval<Destroy>()())>
|
|
|
|
void add(Destroy &&destroy);
|
|
|
|
void add(lifetime &&other);
|
|
|
|
void destroy();
|
|
|
|
|
|
|
|
template <typename Type, typename... Args>
|
|
|
|
Type *make_state(Args&& ...args) {
|
|
|
|
auto result = new Type(std::forward<Args>(args)...);
|
2017-11-21 11:59:18 +00:00
|
|
|
add([result] {
|
2017-09-04 11:24:35 +00:00
|
|
|
static_assert(sizeof(Type) > 0, "Can't delete unknown type.");
|
2017-11-21 11:59:18 +00:00
|
|
|
delete result;
|
2017-09-04 11:24:35 +00:00
|
|
|
});
|
|
|
|
return result;
|
2017-09-03 12:44:21 +00:00
|
|
|
}
|
|
|
|
|
2017-09-04 11:24:35 +00:00
|
|
|
~lifetime() { destroy(); }
|
2017-09-03 12:44:21 +00:00
|
|
|
|
|
|
|
private:
|
2017-09-04 11:24:35 +00:00
|
|
|
std::deque<base::lambda_once<void()>> _callbacks;
|
2017-09-03 12:44:21 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2017-09-04 11:24:35 +00:00
|
|
|
inline lifetime::lifetime(lifetime &&other)
|
2017-11-20 12:23:20 +00:00
|
|
|
: _callbacks(details::take(other._callbacks)) {
|
2017-09-03 12:44:21 +00:00
|
|
|
}
|
|
|
|
|
2017-09-04 11:24:35 +00:00
|
|
|
inline lifetime &lifetime::operator=(lifetime &&other) {
|
|
|
|
std::swap(_callbacks, other._callbacks);
|
2017-09-03 15:57:51 +00:00
|
|
|
other.destroy();
|
2017-09-03 12:44:21 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-09-04 11:24:35 +00:00
|
|
|
template <typename Destroy, typename>
|
|
|
|
inline lifetime::lifetime(Destroy &&destroy) {
|
|
|
|
_callbacks.emplace_back(std::forward<Destroy>(destroy));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Destroy, typename>
|
|
|
|
inline void lifetime::add(Destroy &&destroy) {
|
|
|
|
_callbacks.push_front(destroy);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void lifetime::add(lifetime &&other) {
|
2017-11-20 12:23:20 +00:00
|
|
|
auto callbacks = details::take(other._callbacks);
|
2017-09-04 11:24:35 +00:00
|
|
|
_callbacks.insert(
|
|
|
|
_callbacks.begin(),
|
|
|
|
std::make_move_iterator(callbacks.begin()),
|
|
|
|
std::make_move_iterator(callbacks.end()));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void lifetime::destroy() {
|
2017-11-20 12:23:20 +00:00
|
|
|
for (auto &callback : details::take(_callbacks)) {
|
2017-09-04 11:24:35 +00:00
|
|
|
callback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-03 12:44:21 +00:00
|
|
|
} // namespace rpl
|