/* 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-2016 John Preston, https://desktop.telegram.org */ #pragma once #include "core/stl_subset.h" namespace base { namespace internal { template struct lambda_wrap_helper_base { using construct_copy_other_type = void(*)(void *, const void *); // dst, src using construct_move_other_type = void(*)(void *, void *); // dst, src using call_type = Return(*)(const void *, Args...); using destruct_type = void(*)(const void *); lambda_wrap_helper_base() = delete; lambda_wrap_helper_base(const lambda_wrap_helper_base &other) = delete; lambda_wrap_helper_base &operator=(const lambda_wrap_helper_base &other) = delete; lambda_wrap_helper_base( construct_copy_other_type construct_copy_other, construct_move_other_type construct_move_other, call_type call, destruct_type destruct) : construct_copy_other(construct_copy_other) , construct_move_other(construct_move_other) , call(call) , destruct(destruct) { } const construct_copy_other_type construct_copy_other; const construct_move_other_type construct_move_other; const call_type call; const destruct_type destruct; static constexpr size_t kFullStorageSize = 24U + sizeof(void*); static constexpr size_t kStorageSize = kFullStorageSize - sizeof(void*); using alignment = uint64; template using IsLarge = std_::integral_constant) <= kStorageSize)>; protected: static void bad_construct_copy(void *lambda, const void *source) { t_assert(!"base::lambda bad_construct_copy() called!"); } }; template struct lambda_wrap_empty : public lambda_wrap_helper_base { static void construct_copy_other_method(void *lambda, const void *source) { } static void construct_move_other_method(void *lambda, void *source) { } static Return call_method(const void *lambda, Args... args) { t_assert(!"base::lambda empty call_method() called!"); return Return(); } static void destruct_method(const void *lambda) { } lambda_wrap_empty() : lambda_wrap_helper_base( &lambda_wrap_empty::construct_copy_other_method, &lambda_wrap_empty::construct_move_other_method, &lambda_wrap_empty::call_method, &lambda_wrap_empty::destruct_method) { } static const lambda_wrap_empty instance; }; template const lambda_wrap_empty lambda_wrap_empty::instance = {}; template struct lambda_wrap_helper_move_impl; // // Disable large lambda support. // If you really need it, just store data in some std_::unique_ptr. // //template //struct lambda_wrap_helper_move_impl : public lambda_wrap_helper_base { // using JustLambda = std_::decay_simple_t; // using LambdaPtr = std_::unique_ptr; // using Parent = lambda_wrap_helper_base; // static void construct_move_other_method(void *lambda, void *source) { // auto source_lambda = static_cast(source); // new (lambda) LambdaPtr(std_::move(*source_lambda)); // } // static void construct_move_lambda_method(void *lambda, void *source) { // auto source_lambda = static_cast(source); // new (lambda) LambdaPtr(std_::make_unique(static_cast(*source_lambda))); // } // static Return call_method(const void *lambda, Args... args) { // return (**static_cast(lambda))(std_::forward(args)...); // } // static void destruct_method(const void *lambda) { // static_cast(lambda)->~LambdaPtr(); // } // lambda_wrap_helper_move_impl() : Parent( // &Parent::bad_construct_copy, // &lambda_wrap_helper_move_impl::construct_move_other_method, // &lambda_wrap_helper_move_impl::call_method, // &lambda_wrap_helper_move_impl::destruct_method) { // } // //protected: // lambda_wrap_helper_move_impl( // typename Parent::construct_copy_other_type construct_copy_other // ) : Parent( // construct_copy_other, // &lambda_wrap_helper_move_impl::construct_move_other_method, // &lambda_wrap_helper_move_impl::call_method, // &lambda_wrap_helper_move_impl::destruct_method) { // } // //}; template struct lambda_wrap_helper_move_impl : public lambda_wrap_helper_base { using JustLambda = std_::decay_simple_t; using Parent = lambda_wrap_helper_base; static void construct_move_other_method(void *lambda, void *source) { auto source_lambda = static_cast(source); new (lambda) JustLambda(static_cast(*source_lambda)); } static void construct_move_lambda_method(void *lambda, void *source) { static_assert(alignof(JustLambda) <= alignof(typename Parent::alignment), "Bad lambda alignment."); auto space = sizeof(JustLambda); auto aligned = std_::align(alignof(JustLambda), space, lambda, space); t_assert(aligned == lambda); auto source_lambda = static_cast(source); new (lambda) JustLambda(static_cast(*source_lambda)); } static Return call_method(const void *lambda, Args... args) { return (*static_cast(lambda))(std_::forward(args)...); } static void destruct_method(const void *lambda) { static_cast(lambda)->~JustLambda(); } lambda_wrap_helper_move_impl() : Parent( &Parent::bad_construct_copy, &lambda_wrap_helper_move_impl::construct_move_other_method, &lambda_wrap_helper_move_impl::call_method, &lambda_wrap_helper_move_impl::destruct_method) { } protected: lambda_wrap_helper_move_impl( typename Parent::construct_copy_other_type construct_copy_other ) : Parent( construct_copy_other, &lambda_wrap_helper_move_impl::construct_move_other_method, &lambda_wrap_helper_move_impl::call_method, &lambda_wrap_helper_move_impl::destruct_method) { } }; template struct lambda_wrap_helper_move : public lambda_wrap_helper_move_impl::template IsLarge , Return, Args...> { static const lambda_wrap_helper_move instance; }; template const lambda_wrap_helper_move lambda_wrap_helper_move::instance = {}; template struct lambda_wrap_helper_copy_impl; // // Disable large lambda support. // If you really need it, just store data in some QSharedPointer. // //template //struct lambda_wrap_helper_copy_impl : public lambda_wrap_helper_move_impl { // using JustLambda = std_::decay_simple_t; // using LambdaPtr = std_::unique_ptr; // using Parent = lambda_wrap_helper_move_impl; // static void construct_copy_other_method(void *lambda, const void *source) { // auto source_lambda = static_cast(source); // new (lambda) LambdaPtr(std_::make_unique(*source_lambda->get())); // } // static void construct_copy_lambda_method(void *lambda, const void *source) { // auto source_lambda = static_cast(source); // new (lambda) LambdaPtr(std_::make_unique(static_cast(*source_lambda))); // } // lambda_wrap_helper_copy_impl() : Parent(&lambda_wrap_helper_copy_impl::construct_copy_other_method) { // } // //}; template struct lambda_wrap_helper_copy_impl : public lambda_wrap_helper_move_impl { using JustLambda = std_::decay_simple_t; using Parent = lambda_wrap_helper_move_impl; static void construct_copy_other_method(void *lambda, const void *source) { auto source_lambda = static_cast(source); new (lambda) JustLambda(static_cast(*source_lambda)); } static void construct_copy_lambda_method(void *lambda, const void *source) { static_assert(alignof(JustLambda) <= alignof(typename Parent::alignment), "Bad lambda alignment."); auto space = sizeof(JustLambda); auto aligned = std_::align(alignof(JustLambda), space, lambda, space); t_assert(aligned == lambda); auto source_lambda = static_cast(source); new (lambda) JustLambda(static_cast(*source_lambda)); } lambda_wrap_helper_copy_impl() : Parent(&lambda_wrap_helper_copy_impl::construct_copy_other_method) { } }; template struct lambda_wrap_helper_copy : public lambda_wrap_helper_copy_impl::template IsLarge , Return, Args...> { static const lambda_wrap_helper_copy instance; }; template const lambda_wrap_helper_copy lambda_wrap_helper_copy::instance = {}; } // namespace internal template class lambda; template class lambda_copy; template class lambda { using BaseHelper = internal::lambda_wrap_helper_base; using EmptyHelper = internal::lambda_wrap_empty; template using IsUnique = std_::is_same>; template using IsWrap = std_::is_same, std_::decay_simple_t>; template using IsOther = std_::enable_if_t::value && !IsWrap::value>; template using IsRvalue = std_::enable_if_t::value>; public: using return_type = Return; lambda() : helper_(&EmptyHelper::instance) { } lambda(const lambda &other) = delete; lambda &operator=(const lambda &other) = delete; lambda(lambda &&other) : helper_(other.helper_) { helper_->construct_move_other(storage_, other.storage_); } lambda &operator=(lambda &&other) { auto temp = std_::move(other); helper_->destruct(storage_); helper_ = temp.helper_; helper_->construct_move_other(storage_, temp.storage_); return *this; } void swap(lambda &other) { if (this != &other) std_::swap_moveable(*this, other); } template , typename = IsRvalue> lambda(Lambda &&other) : helper_(&internal::lambda_wrap_helper_move::instance) { internal::lambda_wrap_helper_move::construct_move_lambda_method(storage_, &other); } template , typename = IsRvalue> lambda &operator=(Lambda &&other) { auto temp = std_::move(other); helper_->destruct(storage_); helper_ = &internal::lambda_wrap_helper_move::instance; internal::lambda_wrap_helper_move::construct_move_lambda_method(storage_, &temp); return *this; } inline Return operator()(Args... args) const { return helper_->call(storage_, std_::forward(args)...); } explicit operator bool() const { return (helper_ != &EmptyHelper::instance); } ~lambda() { helper_->destruct(storage_); } protected: struct Private { }; lambda(const BaseHelper *helper, const Private &) : helper_(helper) { } using alignment = typename BaseHelper::alignment; static_assert(BaseHelper::kStorageSize % sizeof(alignment) == 0, "Bad storage size."); alignas(typename BaseHelper::alignment) alignment storage_[BaseHelper::kStorageSize / sizeof(alignment)]; const BaseHelper *helper_; }; template class lambda_copy : public lambda { using BaseHelper = internal::lambda_wrap_helper_base; using Parent = lambda; template using IsOther = std_::enable_if_t>::value>; template using IsRvalue = std_::enable_if_t::value>; template using IsNotRvalue = std_::enable_if_t::value>; public: lambda_copy() = default; lambda_copy(const lambda_copy &other) : Parent(other.helper_, typename Parent::Private()) { this->helper_->construct_copy_other(this->storage_, other.storage_); } lambda_copy &operator=(const lambda_copy &other) { auto temp = other; temp.swap(*this); return *this; } lambda_copy(lambda_copy &&other) = default; lambda_copy &operator=(lambda_copy &&other) = default; void swap(lambda_copy &other) { if (this != &other) std_::swap_moveable(*this, other); } lambda_copy clone() const { return *this; } template > lambda_copy(const Lambda &other) : Parent(&internal::lambda_wrap_helper_copy::instance, typename Parent::Private()) { internal::lambda_wrap_helper_copy::construct_copy_lambda_method(this->storage_, &other); } template , typename = IsRvalue> lambda_copy(Lambda &&other) : Parent(&internal::lambda_wrap_helper_copy::instance, typename Parent::Private()) { internal::lambda_wrap_helper_copy::construct_move_lambda_method(this->storage_, &other); } template > lambda_copy &operator=(const Lambda &other) { auto temp = other; this->helper_->destruct(this->storage_); this->helper_ = &internal::lambda_wrap_helper_copy::instance; internal::lambda_wrap_helper_copy::construct_copy_lambda_method(this->storage_, &other); return *this; } template , typename = IsRvalue> lambda_copy &operator=(Lambda &&other) { auto temp = std_::move(other); this->helper_->destruct(this->storage_); this->helper_ = &internal::lambda_wrap_helper_copy::instance; internal::lambda_wrap_helper_copy::construct_move_lambda_method(this->storage_, &other); return *this; } }; // Get lambda type from a lambda template parameter. namespace internal { template struct lambda_type_helper; template struct lambda_type_helper { using type = lambda; }; } // namespace internal template using lambda_type = typename internal::lambda_type_helper::type; // Guard lambda call by one or many QObject* weak pointers. namespace internal { template class lambda_guard_creator; template class lambda_guard_data { public: using return_type = typename lambda_type::return_type; template lambda_guard_data(PointersAndLambda&&... qobjectsAndLambda) : _lambda(init(_pointers, std_::forward(qobjectsAndLambda)...)) { } 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 lambda_guard { public: using return_type = typename lambda_type::return_type; template lambda_guard(PointersAndLambda&&... qobjectsAndLambda) : _data(std_::make_unique>(std_::forward(qobjectsAndLambda)...)) { static_assert(sizeof...(PointersAndLambda) == N + 1, "Wrong argument count!"); } template inline return_type operator()(Args... args) const { return (*_data)(std_::forward(args)...); } private: std_::unique_ptr> _data; }; template struct lambda_guard_type; template struct lambda_guard_type { using type = typename lambda_guard_type::type; }; template struct lambda_guard_type { using type = lambda_guard; }; template struct lambda_guard_type_helper { static constexpr int N = sizeof...(PointersAndLambda); using type = typename lambda_guard_type::type; }; template using lambda_guard_t = typename lambda_guard_type_helper::type; } // namespace internal template inline internal::lambda_guard_t lambda_guarded(PointersAndLambda&&... qobjectsAndLambda) { static_assert(sizeof...(PointersAndLambda) > 0, "Lambda should be passed here."); return internal::lambda_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 &&lambda) : QObject(parent), _lambda(std_::move(lambda)) { } public slots : void action() { _lambda(); delete this; } private: lambda _lambda; }; inline lambda_slot_once_wrap *lambda_slot_once(QObject *parent, lambda &&lambda) { return new lambda_slot_once_wrap(parent, std_::move(lambda)); } } // namespace base