2017-09-03 12:43:58 +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 <QPointer>
|
2017-10-27 17:00:56 +00:00
|
|
|
#include "base/weak_unique_ptr.h"
|
2017-09-03 12:43:58 +00:00
|
|
|
|
2017-10-27 17:00:56 +00:00
|
|
|
// Guard lambda call by QObject* or enable_weak_from_this* pointers.
|
2017-09-03 12:43:58 +00:00
|
|
|
|
2017-10-27 17:00:56 +00:00
|
|
|
namespace base {
|
2017-09-03 12:43:58 +00:00
|
|
|
namespace lambda_internal {
|
|
|
|
|
2017-10-27 17:00:56 +00:00
|
|
|
template <typename Lambda>
|
|
|
|
class guard_with_QObject {
|
2017-09-03 12:43:58 +00:00
|
|
|
public:
|
2017-10-27 17:00:56 +00:00
|
|
|
template <typename OtherLambda>
|
|
|
|
guard_with_QObject(const QObject *object, OtherLambda &&other)
|
|
|
|
: _guard(object)
|
|
|
|
, _callable(std::forward<OtherLambda>(other)) {
|
2017-09-03 12:43:58 +00:00
|
|
|
}
|
|
|
|
|
2017-10-27 15:37:27 +00:00
|
|
|
template <
|
2017-10-27 17:00:56 +00:00
|
|
|
typename ...OtherArgs,
|
|
|
|
typename Return = decltype(std::declval<Lambda>()(std::declval<OtherArgs>()...))>
|
|
|
|
Return operator()(OtherArgs &&...args) {
|
|
|
|
return _guard
|
|
|
|
? _callable(std::forward<OtherArgs>(args)...)
|
|
|
|
: Return();
|
2017-09-03 12:43:58 +00:00
|
|
|
}
|
|
|
|
|
2017-10-27 15:37:27 +00:00
|
|
|
template <
|
2017-10-27 17:00:56 +00:00
|
|
|
typename ...OtherArgs,
|
|
|
|
typename Return = decltype(std::declval<Lambda>()(std::declval<OtherArgs>()...))>
|
|
|
|
Return operator()(OtherArgs &&...args) const {
|
|
|
|
return _guard
|
|
|
|
? _callable(std::forward<OtherArgs>(args)...)
|
|
|
|
: Return();
|
2017-09-03 12:43:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-10-27 17:00:56 +00:00
|
|
|
QPointer<const QObject> _guard;
|
|
|
|
Lambda _callable;
|
2017-10-27 15:37:27 +00:00
|
|
|
|
|
|
|
};
|
2017-09-03 12:43:58 +00:00
|
|
|
|
2017-10-27 17:00:56 +00:00
|
|
|
template <typename Lambda>
|
|
|
|
class guard_with_weak {
|
2017-09-03 12:43:58 +00:00
|
|
|
public:
|
2017-10-27 17:00:56 +00:00
|
|
|
template <typename OtherLambda>
|
|
|
|
guard_with_weak(
|
|
|
|
const base::enable_weak_from_this *object,
|
|
|
|
OtherLambda &&other)
|
|
|
|
: _guard(base::make_weak_unique(object))
|
|
|
|
, _callable(std::forward<OtherLambda>(other)) {
|
2017-09-03 12:43:58 +00:00
|
|
|
}
|
|
|
|
|
2017-10-27 15:37:27 +00:00
|
|
|
template <
|
2017-10-27 17:00:56 +00:00
|
|
|
typename ...OtherArgs,
|
|
|
|
typename Return = decltype(std::declval<Lambda>()(std::declval<OtherArgs>()...))>
|
|
|
|
Return operator()(OtherArgs &&...args) {
|
|
|
|
return _guard
|
|
|
|
? _callable(std::forward<OtherArgs>(args)...)
|
2017-11-10 15:45:10 +00:00
|
|
|
: Return();
|
2017-09-03 12:43:58 +00:00
|
|
|
}
|
|
|
|
|
2017-10-27 15:37:27 +00:00
|
|
|
template <
|
2017-10-27 17:00:56 +00:00
|
|
|
typename ...OtherArgs,
|
|
|
|
typename Return = decltype(std::declval<Lambda>()(std::declval<OtherArgs>()...))>
|
|
|
|
Return operator()(OtherArgs &&...args) const {
|
|
|
|
return _guard
|
|
|
|
? _callable(std::forward<OtherArgs>(args)...)
|
2017-11-10 15:45:10 +00:00
|
|
|
: Return();
|
2017-09-03 12:43:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-10-27 17:00:56 +00:00
|
|
|
base::weak_unique_ptr<const base::enable_weak_from_this> _guard;
|
|
|
|
Lambda _callable;
|
2017-09-03 12:43:58 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2017-10-27 17:00:56 +00:00
|
|
|
template <typename Lambda>
|
|
|
|
struct lambda_call_type<guard_with_QObject<Lambda>> {
|
2017-10-27 15:37:27 +00:00
|
|
|
using type = lambda_call_type_t<Lambda>;
|
|
|
|
};
|
|
|
|
|
2017-10-27 17:00:56 +00:00
|
|
|
template <typename Lambda>
|
|
|
|
struct lambda_call_type<guard_with_weak<Lambda>> {
|
|
|
|
using type = lambda_call_type_t<Lambda>;
|
2017-09-03 12:43:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace lambda_internal
|
|
|
|
|
2017-10-27 17:00:56 +00:00
|
|
|
template <typename Lambda>
|
|
|
|
inline auto lambda_guarded(const QObject *object, Lambda &&lambda) {
|
|
|
|
using Guarded = lambda_internal::guard_with_QObject<
|
|
|
|
std::decay_t<Lambda>>;
|
|
|
|
return Guarded(object, std::forward<Lambda>(lambda));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Lambda>
|
|
|
|
inline auto lambda_guarded(
|
|
|
|
const base::enable_weak_from_this *object,
|
|
|
|
Lambda &&lambda) {
|
|
|
|
using Guarded = lambda_internal::guard_with_weak<
|
|
|
|
std::decay_t<Lambda>>;
|
|
|
|
return Guarded(object, std::forward<Lambda>(lambda));
|
2017-09-03 12:43:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace base
|