/* 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 namespace base { template class lambda_once; template class lambda; // Get lambda type from a lambda template parameter. namespace lambda_internal { template struct type_resolver; template struct type_resolver { using type = lambda; static constexpr auto is_mutable = false; }; template struct type_resolver { using type = lambda; static constexpr auto is_mutable = true; }; template struct type_helper { using type = typename type_resolver::type; static constexpr auto is_mutable = type_resolver::is_mutable; }; } // namespace lambda_internal template using lambda_type = typename lambda_internal::type_helper>::type; template constexpr bool lambda_is_mutable = lambda_internal::type_helper>::is_mutable; namespace lambda_internal { constexpr auto kFullStorageSize = 32U; static_assert(kFullStorageSize % sizeof(void*) == 0, "Invalid pointer size!"); constexpr auto kStorageSize = kFullStorageSize - sizeof(void*); using alignment = std::max_align_t; template constexpr bool is_large = (sizeof(std::decay_t) > kStorageSize); [[noreturn]] inline void bad_construct_copy(void *lambda, const void *source) { Unexpected("base::lambda bad_construct_copy() called!"); } template [[noreturn]] Return bad_const_call(const void *lambda, Args...) { Unexpected("base::lambda bad_const_call() called!"); } template struct vtable_base { using construct_copy_other_type = void(*)(void *, const void *); // dst, src using construct_move_other_type = void(*)(void *, void *); // dst, src using const_call_type = Return(*)(const void *, Args...); using call_type = Return(*)(void *, Args...); using destruct_type = void(*)(const void *); vtable_base() = delete; vtable_base(const vtable_base &other) = delete; vtable_base &operator=(const vtable_base &other) = delete; vtable_base( construct_copy_other_type construct_copy_other, construct_move_other_type construct_move_other, const_call_type const_call, call_type call, destruct_type destruct) : construct_copy_other(construct_copy_other) , construct_move_other(construct_move_other) , const_call(const_call) , call(call) , destruct(destruct) { } const construct_copy_other_type construct_copy_other; const construct_move_other_type construct_move_other; const const_call_type const_call; const call_type call; const destruct_type destruct; }; template struct vtable_once_impl; template struct vtable_once_impl : public vtable_base { using JustLambda = std::decay_t; using LambdaPtr = std::unique_ptr; using Parent = vtable_base; static void construct_move_other_method(void *storage, void *source) { auto source_lambda_ptr = static_cast(source); new (storage) LambdaPtr(std::move(*source_lambda_ptr)); } static Return call_method(void *storage, Args... args) { return (**static_cast(storage))(std::forward(args)...); } static void destruct_method(const void *storage) { static_cast(storage)->~LambdaPtr(); } vtable_once_impl() : Parent( &bad_construct_copy, &vtable_once_impl::construct_move_other_method, &bad_const_call, &vtable_once_impl::call_method, &vtable_once_impl::destruct_method) { } // Used directly. static void construct_move_lambda_method(void *storage, void *source) { auto source_lambda = static_cast(source); new (storage) LambdaPtr(std::make_unique(static_cast(*source_lambda))); } protected: vtable_once_impl( typename Parent::construct_copy_other_type construct_copy_other, typename Parent::const_call_type const_call ) : Parent( construct_copy_other, &vtable_once_impl::construct_move_other_method, const_call, &vtable_once_impl::call_method, &vtable_once_impl::destruct_method) { } }; template struct vtable_once_impl : public vtable_base { using JustLambda = std::decay_t; using Parent = vtable_base; static void construct_move_other_method(void *storage, void *source) { auto source_lambda = static_cast(source); new (storage) JustLambda(static_cast(*source_lambda)); } static Return call_method(void *storage, Args... args) { return (*static_cast(storage))(std::forward(args)...); } static void destruct_method(const void *storage) { static_cast(storage)->~JustLambda(); } vtable_once_impl() : Parent( &bad_construct_copy, &vtable_once_impl::construct_move_other_method, &bad_const_call, &vtable_once_impl::call_method, &vtable_once_impl::destruct_method) { } // Used directly. static void construct_move_lambda_method(void *storage, void *source) { auto source_lambda = static_cast(source); new (storage) JustLambda(static_cast(*source_lambda)); } protected: vtable_once_impl( typename Parent::construct_copy_other_type construct_copy_other, typename Parent::const_call_type const_call ) : Parent( construct_copy_other, &vtable_once_impl::construct_move_other_method, const_call, &vtable_once_impl::call_method, &vtable_once_impl::destruct_method) { } }; template struct vtable_once : public vtable_once_impl, Return, Args...> { static const vtable_once instance; }; template const vtable_once vtable_once::instance = {}; template struct vtable_impl; template struct vtable_impl : public vtable_once_impl { using JustLambda = std::decay_t; using LambdaPtr = std::unique_ptr; using Parent = vtable_once_impl; static void construct_copy_other_method(void *storage, const void *source) { auto source_lambda = static_cast(source); new (storage) LambdaPtr(std::make_unique(*source_lambda->get())); } static Return const_call_method(const void *storage, Args... args) { auto lambda_ptr = static_cast(storage)->get(); return (*static_cast(lambda_ptr))(std::forward(args)...); } vtable_impl() : Parent( &vtable_impl::construct_copy_other_method, &vtable_impl::const_call_method ) { } }; template struct vtable_impl : public vtable_once_impl { using JustLambda = std::decay_t; using Parent = vtable_once_impl; static void construct_copy_other_method(void *storage, const void *source) { auto source_lambda = static_cast(source); new (storage) JustLambda(static_cast(*source_lambda)); } static Return const_call_method(const void *storage, Args... args) { static_assert(!lambda_is_mutable, "For mutable lambda use base::lambda_once wrapper"); return (*static_cast(storage))(std::forward(args)...); } vtable_impl() : Parent( &vtable_impl::construct_copy_other_method, &vtable_impl::const_call_method ) { } }; template struct vtable : public vtable_impl, Return, Args...> { static const vtable instance; }; template const vtable vtable::instance = {}; } // namespace lambda_internal template class lambda_once { using VTable = lambda_internal::vtable_base; public: using return_type = Return; lambda_once() { data_.vtable = nullptr; } lambda_once(const lambda_once &other) = delete; lambda_once &operator=(const lambda_once &other) = delete; // Move construct / assign from the same type. lambda_once(lambda_once &&other) { if ((data_.vtable = other.data_.vtable)) { data_.vtable->construct_move_other(data_.storage, other.data_.storage); } } lambda_once &operator=(lambda_once &&other) { if (this != &other) { if (data_.vtable) { data_.vtable->destruct(data_.storage); } if ((data_.vtable = other.data_.vtable)) { data_.vtable->construct_move_other(data_.storage, other.data_.storage); data_.vtable->destruct(other.data_.storage); other.data_.vtable = nullptr; } } return *this; } // Move construct / assign from a derived type. lambda_once(lambda &&other) { if ((data_.vtable = other.data_.vtable)) { data_.vtable->construct_move_other(data_.storage, other.data_.storage); data_.vtable->destruct(other.data_.storage); other.data_.vtable = nullptr; } } lambda_once &operator=(lambda &&other) { if (this != &other) { if (data_.vtable) { data_.vtable->destruct(data_.storage); } if ((data_.vtable = other.data_.vtable)) { data_.vtable->construct_move_other(data_.storage, other.data_.storage); data_.vtable->destruct(other.data_.storage); other.data_.vtable = nullptr; } } return *this; } // Copy construct / assign from a derived type. lambda_once(const lambda &other) { if ((data_.vtable = other.data_.vtable)) { data_.vtable->construct_copy_other(data_.storage, other.data_.storage); } } lambda_once &operator=(const lambda &other) { if (this != &other) { if (data_.vtable) { data_.vtable->destruct(data_.storage); } if ((data_.vtable = other.data_.vtable)) { data_.vtable->construct_copy_other(data_.storage, other.data_.storage); } } return *this; } // Copy / move construct / assign from an arbitrary type. template lambda_once(Lambda other) { data_.vtable = &lambda_internal::vtable_once::instance; lambda_internal::vtable_once::construct_move_lambda_method(data_.storage, &other); } template lambda_once &operator=(Lambda other) { if (data_.vtable) { data_.vtable->destruct(data_.storage); } data_.vtable = &lambda_internal::vtable_once::instance; lambda_internal::vtable_once::construct_move_lambda_method(data_.storage, &other); return *this; } void swap(lambda_once &other) { if (this != &other) { std::swap(*this, other); } } inline Return operator()(Args... args) { t_assert(data_.vtable != nullptr); return data_.vtable->call(data_.storage, std::forward(args)...); } explicit operator bool() const { return (data_.vtable != nullptr); } ~lambda_once() { if (data_.vtable) { data_.vtable->destruct(data_.storage); } } protected: struct Private { }; lambda_once(const VTable *vtable, const Private &) { data_.vtable = vtable; } struct Data { char storage[lambda_internal::kStorageSize]; const VTable *vtable; }; union { lambda_internal::alignment alignment_; char raw_[lambda_internal::kFullStorageSize]; Data data_; }; }; template class lambda final : public lambda_once { using Parent = lambda_once; public: lambda() = default; // Move construct / assign from the same type. lambda(lambda &&other) : Parent(std::move(other)) { } lambda &operator=(lambda &&other) { Parent::operator=(std::move(other)); return *this; } // Copy construct / assign from the same type. lambda(const lambda &other) : Parent(other) { } lambda &operator=(const lambda &other) { Parent::operator=(other); return *this; } // Copy / move construct / assign from an arbitrary type. template lambda(Lambda other) : Parent(&lambda_internal::vtable::instance, typename Parent::Private()) { lambda_internal::vtable::construct_move_lambda_method(this->data_.storage, &other); } template lambda &operator=(Lambda other) { if (this->data_.vtable) { this->data_.vtable->destruct(this->data_.storage); } this->data_.vtable = &lambda_internal::vtable::instance; lambda_internal::vtable::construct_move_lambda_method(this->data_.storage, &other); return *this; } inline Return operator()(Args... args) const { t_assert(this->data_.vtable != nullptr); return this->data_.vtable->const_call(this->data_.storage, std::forward(args)...); } void swap(lambda &other) { if (this != &other) { std::swap(*this, other); } } }; // Guard lambda call by one or many QObject* weak pointers. namespace lambda_internal { template class guard_data { public: using return_type = typename lambda_type::return_type; template inline guard_data(PointersAndLambda&&... qobjectsAndLambda) : _lambda(init(_pointers, std::forward(qobjectsAndLambda)...)) { } inline guard_data(const guard_data &other) : _lambda(other._lambda) { for (auto i = 0; i != N; ++i) { _pointers[i] = other._pointers[i]; } } template inline return_type operator()(Args&&... args) { for (int i = 0; i != N; ++i) { if (!_pointers[i]) { return return_type(); } } return _lambda(std::forward(args)...); } template inline return_type operator()(Args&&... args) const { for (int i = 0; i != N; ++i) { if (!_pointers[i]) { return return_type(); } } return _lambda(std::forward(args)...); } private: template Lambda init(QPointer *pointers, QObject *qobject, PointersAndLambda&&... qobjectsAndLambda) { *pointers = qobject; return init(++pointers, std::forward(qobjectsAndLambda)...); } Lambda init(QPointer *pointers, Lambda &&lambda) { return std::move(lambda); } QPointer _pointers[N]; Lambda _lambda; }; template class guard { public: using return_type = typename lambda_type::return_type; template inline guard(Pointer &&qobject, Other &&other, PointersAndLambda&&... qobjectsAndLambda) : _data(std::make_unique>(std::forward(qobject), std::forward(other), std::forward(qobjectsAndLambda)...)) { static_assert(1 + 1 + sizeof...(PointersAndLambda) == N + 1, "Wrong argument count!"); } inline guard(const guard &other) : _data(std::make_unique>(static_cast &>(*other._data))) { } inline guard(guard &&other) : _data(std::move(other._data)) { } inline guard &operator=(const guard &&other) { _data = std::move(other._data); return *this; } inline guard &operator=(guard &&other) { _data = std::move(other._data); return *this; } template inline return_type operator()(Args&&... args) { return (*_data)(std::forward(args)...); } template inline return_type operator()(Args&&... args) const { return (*_data)(std::forward(args)...); } bool isNull() const { return !_data; } private: mutable std::unique_ptr> _data; }; template struct guard_type; template struct guard_type { using type = typename guard_type::type; }; template struct guard_type { using type = guard; }; template struct guard_type_helper { static constexpr int N = sizeof...(PointersAndLambda); using type = typename guard_type::type; }; template using guard_t = typename guard_type_helper::type; template struct type_helper> { using type = typename type_helper::type; static constexpr auto is_mutable = type_helper::is_mutable; }; } // namespace lambda_internal template inline lambda_internal::guard_t lambda_guarded(PointersAndLambda&&... qobjectsAndLambda) { static_assert(sizeof...(PointersAndLambda) > 0, "Lambda should be passed here."); return lambda_internal::guard_t(std::forward(qobjectsAndLambda)...); } // Pass lambda instead of a Qt void() slot. class lambda_slot_wrap : public QObject { Q_OBJECT public: lambda_slot_wrap(QObject *parent, lambda lambda) : QObject(parent), _lambda(std::move(lambda)) { } public slots : void action() { _lambda(); } private: lambda _lambda; }; inline lambda_slot_wrap *lambda_slot(QObject *parent, lambda lambda) { return new lambda_slot_wrap(parent, std::move(lambda)); } class lambda_slot_once_wrap : public QObject { Q_OBJECT public: lambda_slot_once_wrap(QObject *parent, lambda_once lambda) : QObject(parent), _lambda(std::move(lambda)) { } public slots : void action() { _lambda(); delete this; } private: lambda_once _lambda; }; inline lambda_slot_once_wrap *lambda_slot_once(QObject *parent, lambda_once lambda) { return new lambda_slot_once_wrap(parent, std::move(lambda)); } } // namespace base