2017-09-03 12:43:58 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2017-09-03 12:43:58 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2017-09-03 12:43:58 +00:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <QPointer>
|
2017-11-30 17:33:27 +00:00
|
|
|
#include "base/weak_ptr.h"
|
2017-09-03 12:43:58 +00:00
|
|
|
|
2017-11-30 17:33:27 +00:00
|
|
|
// Guard lambda call by QObject* or has_weak_ptr* 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(
|
2017-11-30 17:33:27 +00:00
|
|
|
const base::has_weak_ptr *object,
|
2017-10-27 17:00:56 +00:00
|
|
|
OtherLambda &&other)
|
2017-11-30 17:33:27 +00:00
|
|
|
: _guard(base::make_weak(object))
|
2017-10-27 17:00:56 +00:00
|
|
|
, _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-11-30 17:33:27 +00:00
|
|
|
base::weak_ptr<const base::has_weak_ptr> _guard;
|
2017-10-27 17:00:56 +00:00
|
|
|
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(
|
2017-11-30 17:33:27 +00:00
|
|
|
const base::has_weak_ptr *object,
|
2017-10-27 17:00:56 +00:00
|
|
|
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
|