/* 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" #include "base/weak_unique_ptr.h" #include // Guard lambda call by QObject* or enable_weak_from_this* pointers. namespace base { namespace lambda_internal { template class guard_with_QObject { public: template guard_with_QObject(const QObject *object, OtherLambda &&other) : _guard(object) , _callable(std::forward(other)) {} template ()(std::declval()...))> Return operator()(OtherArgs &&... args) { return _guard ? _callable(std::forward(args)...) : Return(); } template ()(std::declval()...))> Return operator()(OtherArgs &&... args) const { return _guard ? _callable(std::forward(args)...) : Return(); } private: QPointer _guard; Lambda _callable; }; template class guard_with_weak { public: template guard_with_weak(const base::enable_weak_from_this *object, OtherLambda &&other) : _guard(base::make_weak_unique(object)) , _callable(std::forward(other)) {} template ()(std::declval()...))> Return operator()(OtherArgs &&... args) { return _guard ? _callable(std::forward(args)...) : Return{}; } template ()(std::declval()...))> Return operator()(OtherArgs &&... args) const { return _guard ? _callable(std::forward(args)...) : Return{}; } private: base::weak_unique_ptr _guard; Lambda _callable; }; template struct lambda_call_type> { using type = lambda_call_type_t; }; template struct lambda_call_type> { using type = lambda_call_type_t; }; } // namespace lambda_internal template inline auto lambda_guarded(const QObject *object, Lambda &&lambda) { using Guarded = lambda_internal::guard_with_QObject>; return Guarded(object, std::forward(lambda)); } template inline auto lambda_guarded(const base::enable_weak_from_this *object, Lambda &&lambda) { using Guarded = lambda_internal::guard_with_weak>; return Guarded(object, std::forward(lambda)); } } // namespace base