mirror of https://github.com/procxx/kepka.git
Use std::function for base::lambda implementation.
base::lambda becomes just std::function and base::lambda_once becomes base::unique_function - a move-only wrapper around std::function. This is required because Visual C++ 2017 15.4.1 has a compiler bug with static member variables of class templates, they may collide. The std::function uses inheritance and virtual functions instead of custom vtables done by static members of class templates used in custom base::lambda implementation, so they work fine.
This commit is contained in:
parent
fde3ff1bbf
commit
101d4f6444
|
@ -208,7 +208,11 @@ namespace {
|
||||||
|
|
||||||
void logOut() {
|
void logOut() {
|
||||||
if (auto mtproto = Messenger::Instance().mtp()) {
|
if (auto mtproto = Messenger::Instance().mtp()) {
|
||||||
mtproto->logout(rpcDone(&loggedOut), rpcFail(&loggedOut));
|
mtproto->logout(rpcDone([] {
|
||||||
|
return loggedOut();
|
||||||
|
}), rpcFail([] {
|
||||||
|
return loggedOut();
|
||||||
|
}));
|
||||||
} else {
|
} else {
|
||||||
// We log out because we've forgotten passcode.
|
// We log out because we've forgotten passcode.
|
||||||
// So we just start mtproto from scratch.
|
// So we just start mtproto from scratch.
|
||||||
|
|
|
@ -22,6 +22,36 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
|
#ifndef CUSTOM_LAMBDA_WRAP
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
|
#include "base/unique_function.h"
|
||||||
|
|
||||||
|
namespace base {
|
||||||
|
|
||||||
|
template <typename Function>
|
||||||
|
using lambda = std::function<Function>;
|
||||||
|
|
||||||
|
template <typename Function>
|
||||||
|
using lambda_once = unique_function<Function>;
|
||||||
|
|
||||||
|
namespace lambda_internal {
|
||||||
|
|
||||||
|
template <typename Lambda>
|
||||||
|
struct lambda_call_type {
|
||||||
|
using type = decltype(&Lambda::operator());
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace lambda_internal
|
||||||
|
|
||||||
|
template <typename Lambda>
|
||||||
|
using lambda_call_type_t
|
||||||
|
= typename lambda_internal::lambda_call_type<Lambda>::type;
|
||||||
|
|
||||||
|
} // namespace base
|
||||||
|
|
||||||
|
#else // CUSTOM_LAMBDA_WRAP
|
||||||
|
|
||||||
#ifndef Assert
|
#ifndef Assert
|
||||||
#define LambdaAssertDefined
|
#define LambdaAssertDefined
|
||||||
#define Assert(v) ((v) ? ((void)0) : std::abort())
|
#define Assert(v) ((v) ? ((void)0) : std::abort())
|
||||||
|
@ -67,9 +97,6 @@ struct type_helper {
|
||||||
template <typename Lambda>
|
template <typename Lambda>
|
||||||
using lambda_type = typename lambda_internal::type_helper<std::decay_t<Lambda>>::type;
|
using lambda_type = typename lambda_internal::type_helper<std::decay_t<Lambda>>::type;
|
||||||
|
|
||||||
template <typename Lambda>
|
|
||||||
constexpr bool lambda_is_mutable = lambda_internal::type_helper<std::decay_t<Lambda>>::is_mutable;
|
|
||||||
|
|
||||||
namespace lambda_internal {
|
namespace lambda_internal {
|
||||||
|
|
||||||
constexpr auto kFullStorageSize = 32U;
|
constexpr auto kFullStorageSize = 32U;
|
||||||
|
@ -469,3 +496,5 @@ public:
|
||||||
#ifdef LambdaUnexpectedDefined
|
#ifdef LambdaUnexpectedDefined
|
||||||
#undef Unexpected
|
#undef Unexpected
|
||||||
#endif // LambdaUnexpectedDefined
|
#endif // LambdaUnexpectedDefined
|
||||||
|
|
||||||
|
#endif // CUSTOM_LAMBDA_WRAP
|
||||||
|
|
|
@ -31,8 +31,6 @@ namespace lambda_internal {
|
||||||
template <int N, typename Lambda>
|
template <int N, typename Lambda>
|
||||||
class guard_data {
|
class guard_data {
|
||||||
public:
|
public:
|
||||||
using return_type = typename lambda_type<Lambda>::return_type;
|
|
||||||
|
|
||||||
template <typename ...PointersAndLambda>
|
template <typename ...PointersAndLambda>
|
||||||
inline guard_data(PointersAndLambda&&... qobjectsAndLambda) : _lambda(init(_pointers, std::forward<PointersAndLambda>(qobjectsAndLambda)...)) {
|
inline guard_data(PointersAndLambda&&... qobjectsAndLambda) : _lambda(init(_pointers, std::forward<PointersAndLambda>(qobjectsAndLambda)...)) {
|
||||||
}
|
}
|
||||||
|
@ -43,8 +41,10 @@ public:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename ...Args>
|
template <
|
||||||
inline return_type operator()(Args&&... args) {
|
typename ...Args,
|
||||||
|
typename return_type = decltype(std::declval<Lambda>()(std::declval<Args>()...))>
|
||||||
|
inline auto operator()(Args&&... args) {
|
||||||
for (int i = 0; i != N; ++i) {
|
for (int i = 0; i != N; ++i) {
|
||||||
if (!_pointers[i]) {
|
if (!_pointers[i]) {
|
||||||
return return_type();
|
return return_type();
|
||||||
|
@ -53,8 +53,10 @@ public:
|
||||||
return _lambda(std::forward<Args>(args)...);
|
return _lambda(std::forward<Args>(args)...);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename ...Args>
|
template <
|
||||||
inline return_type operator()(Args&&... args) const {
|
typename ...Args,
|
||||||
|
typename return_type = decltype(std::declval<Lambda>()(std::declval<Args>()...))>
|
||||||
|
inline auto operator()(Args&&... args) const {
|
||||||
for (int i = 0; i != N; ++i) {
|
for (int i = 0; i != N; ++i) {
|
||||||
if (!_pointers[i]) {
|
if (!_pointers[i]) {
|
||||||
return return_type();
|
return return_type();
|
||||||
|
@ -74,15 +76,18 @@ private:
|
||||||
}
|
}
|
||||||
|
|
||||||
QPointer<QObject> _pointers[N];
|
QPointer<QObject> _pointers[N];
|
||||||
Lambda _lambda;
|
mutable Lambda _lambda;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <int N, typename Lambda>
|
||||||
|
struct lambda_call_type<guard_data<N, Lambda>> {
|
||||||
|
using type = lambda_call_type_t<Lambda>;
|
||||||
|
};
|
||||||
|
|
||||||
template <int N, typename Lambda>
|
template <int N, typename Lambda>
|
||||||
class guard {
|
class guard {
|
||||||
public:
|
public:
|
||||||
using return_type = typename lambda_type<Lambda>::return_type;
|
|
||||||
|
|
||||||
template <typename Pointer, typename Other, typename ...PointersAndLambda>
|
template <typename Pointer, typename Other, typename ...PointersAndLambda>
|
||||||
inline guard(Pointer &&qobject, Other &&other, PointersAndLambda&&... qobjectsAndLambda) : _data(std::make_unique<guard_data<N, Lambda>>(std::forward<Pointer>(qobject), std::forward<Other>(other), std::forward<PointersAndLambda>(qobjectsAndLambda)...)) {
|
inline guard(Pointer &&qobject, Other &&other, PointersAndLambda&&... qobjectsAndLambda) : _data(std::make_unique<guard_data<N, Lambda>>(std::forward<Pointer>(qobject), std::forward<Other>(other), std::forward<PointersAndLambda>(qobjectsAndLambda)...)) {
|
||||||
static_assert(1 + 1 + sizeof...(PointersAndLambda) == N + 1, "Wrong argument count!");
|
static_assert(1 + 1 + sizeof...(PointersAndLambda) == N + 1, "Wrong argument count!");
|
||||||
|
@ -104,13 +109,17 @@ public:
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename ...Args>
|
template <
|
||||||
inline return_type operator()(Args&&... args) {
|
typename ...Args,
|
||||||
|
typename = decltype(std::declval<Lambda>()(std::declval<Args>()...))>
|
||||||
|
inline decltype(auto) operator()(Args&&... args) {
|
||||||
return (*_data)(std::forward<Args>(args)...);
|
return (*_data)(std::forward<Args>(args)...);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename ...Args>
|
template <
|
||||||
inline return_type operator()(Args&&... args) const {
|
typename ...Args,
|
||||||
|
typename = decltype(std::declval<Lambda>()(std::declval<Args>()...))>
|
||||||
|
inline decltype(auto) operator()(Args&&... args) const {
|
||||||
return (*_data)(std::forward<Args>(args)...);
|
return (*_data)(std::forward<Args>(args)...);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -123,6 +132,11 @@ private:
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <int N, typename Lambda>
|
||||||
|
struct lambda_call_type<guard<N, Lambda>> {
|
||||||
|
using type = lambda_call_type_t<Lambda>;
|
||||||
|
};
|
||||||
|
|
||||||
template <int N, int K, typename ...PointersAndLambda>
|
template <int N, int K, typename ...PointersAndLambda>
|
||||||
struct guard_type;
|
struct guard_type;
|
||||||
|
|
||||||
|
@ -133,7 +147,7 @@ struct guard_type<N, K, Pointer, PointersAndLambda...> {
|
||||||
|
|
||||||
template <int N, typename Lambda>
|
template <int N, typename Lambda>
|
||||||
struct guard_type<N, 0, Lambda> {
|
struct guard_type<N, 0, Lambda> {
|
||||||
using type = guard<N, Lambda>;
|
using type = guard<N, std::decay_t<Lambda>>;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename ...PointersAndLambda>
|
template <typename ...PointersAndLambda>
|
||||||
|
@ -145,12 +159,6 @@ struct guard_type_helper {
|
||||||
template <typename ...PointersAndLambda>
|
template <typename ...PointersAndLambda>
|
||||||
using guard_t = typename guard_type_helper<PointersAndLambda...>::type;
|
using guard_t = typename guard_type_helper<PointersAndLambda...>::type;
|
||||||
|
|
||||||
template <int N, typename Lambda>
|
|
||||||
struct type_helper<guard<N, Lambda>> {
|
|
||||||
using type = typename type_helper<Lambda>::type;
|
|
||||||
static constexpr auto is_mutable = type_helper<Lambda>::is_mutable;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace lambda_internal
|
} // namespace lambda_internal
|
||||||
|
|
||||||
template <typename ...PointersAndLambda>
|
template <typename ...PointersAndLambda>
|
||||||
|
|
|
@ -0,0 +1,190 @@
|
||||||
|
/*
|
||||||
|
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
|
||||||
|
|
||||||
|
#ifndef Unexpected
|
||||||
|
#define Unexpected(message) std::abort()
|
||||||
|
#define UniqueFunctionUnexpected
|
||||||
|
#endif // Unexpected
|
||||||
|
|
||||||
|
namespace base {
|
||||||
|
namespace details {
|
||||||
|
|
||||||
|
template <typename Callable>
|
||||||
|
class moveable_callable_wrap {
|
||||||
|
public:
|
||||||
|
static_assert(
|
||||||
|
std::is_move_constructible_v<Callable>,
|
||||||
|
"Should be at least moveable.");
|
||||||
|
|
||||||
|
moveable_callable_wrap(Callable &&other)
|
||||||
|
: _value(std::move(other)) {
|
||||||
|
}
|
||||||
|
moveable_callable_wrap &operator=(Callable &&other) {
|
||||||
|
_value = std::move(other);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
moveable_callable_wrap(moveable_callable_wrap &&other)
|
||||||
|
: _value(std::move(other._value)) {
|
||||||
|
}
|
||||||
|
moveable_callable_wrap(
|
||||||
|
const moveable_callable_wrap &other)
|
||||||
|
: _value(fail_construct()) {
|
||||||
|
}
|
||||||
|
moveable_callable_wrap &operator=(
|
||||||
|
moveable_callable_wrap &&other) {
|
||||||
|
_value = std::move(other._value);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
moveable_callable_wrap &operator=(
|
||||||
|
const moveable_callable_wrap &other) {
|
||||||
|
return fail_assign();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename ...Args>
|
||||||
|
decltype(auto) operator()(Args &&...args) const {
|
||||||
|
return _value(std::forward<Args>(args)...);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
[[noreturn]] Callable fail_construct() {
|
||||||
|
Unexpected("Attempt to copy-construct a move-only type.");
|
||||||
|
}
|
||||||
|
[[noreturn]] moveable_callable_wrap &fail_assign() {
|
||||||
|
Unexpected("Attempt to copy-assign a move-only type.");
|
||||||
|
}
|
||||||
|
|
||||||
|
mutable Callable _value;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace details
|
||||||
|
|
||||||
|
template <typename Function>
|
||||||
|
class unique_function;
|
||||||
|
|
||||||
|
template <typename Return, typename ...Args>
|
||||||
|
class unique_function<Return(Args...)> final {
|
||||||
|
public:
|
||||||
|
unique_function(std::nullptr_t = nullptr) noexcept {
|
||||||
|
}
|
||||||
|
unique_function(const unique_function &other) = delete;
|
||||||
|
unique_function &operator=(const unique_function &other) = delete;
|
||||||
|
|
||||||
|
// Move construct / assign from the same type.
|
||||||
|
unique_function(unique_function &&other)
|
||||||
|
: _impl(std::move(other._impl)) {
|
||||||
|
}
|
||||||
|
unique_function &operator=(unique_function &&other) {
|
||||||
|
_impl = std::move(other._impl);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <
|
||||||
|
typename Callable,
|
||||||
|
typename = std::enable_if_t<
|
||||||
|
std::is_convertible_v<
|
||||||
|
decltype(std::declval<Callable>()(
|
||||||
|
std::declval<Args>()...)),
|
||||||
|
Return>>>
|
||||||
|
unique_function(Callable &&other)
|
||||||
|
: unique_function(
|
||||||
|
std::forward<Callable>(other),
|
||||||
|
std::is_copy_constructible<std::decay_t<Callable>>{}) {
|
||||||
|
}
|
||||||
|
|
||||||
|
template <
|
||||||
|
typename Callable,
|
||||||
|
typename = std::enable_if_t<
|
||||||
|
std::is_convertible_v<
|
||||||
|
decltype(std::declval<Callable>()(
|
||||||
|
std::declval<Args>()...)),
|
||||||
|
Return>>>
|
||||||
|
unique_function &operator=(Callable &&other) {
|
||||||
|
using Decayed = std::decay_t<Callable>;
|
||||||
|
if constexpr (std::is_copy_constructible_v<Decayed>) {
|
||||||
|
_impl = std::forward<Callable>(other);
|
||||||
|
} else if constexpr (std::is_move_constructible_v<Decayed>) {
|
||||||
|
_impl = details::moveable_callable_wrap<Decayed>(
|
||||||
|
std::forward<Callable>(other));
|
||||||
|
} else {
|
||||||
|
static_assert(false_t(other), "Should be moveable.");
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
void swap(unique_function &other) {
|
||||||
|
_impl.swap(other._impl);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename ...OtherArgs>
|
||||||
|
Return operator()(OtherArgs &&...args) {
|
||||||
|
return _impl(std::forward<OtherArgs>(args)...);
|
||||||
|
}
|
||||||
|
|
||||||
|
explicit operator bool() const {
|
||||||
|
return _impl.operator bool();
|
||||||
|
}
|
||||||
|
|
||||||
|
friend inline bool operator==(
|
||||||
|
const unique_function &value,
|
||||||
|
std::nullptr_t) noexcept {
|
||||||
|
return value._impl == nullptr;
|
||||||
|
}
|
||||||
|
friend inline bool operator==(
|
||||||
|
std::nullptr_t,
|
||||||
|
const unique_function &value) noexcept {
|
||||||
|
return value._impl == nullptr;
|
||||||
|
}
|
||||||
|
friend inline bool operator!=(
|
||||||
|
const unique_function &value,
|
||||||
|
std::nullptr_t) noexcept {
|
||||||
|
return value._impl != nullptr;
|
||||||
|
}
|
||||||
|
friend inline bool operator!=(
|
||||||
|
std::nullptr_t,
|
||||||
|
const unique_function &value) noexcept {
|
||||||
|
return value._impl != nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
template <typename Callable>
|
||||||
|
unique_function(Callable &&other, std::true_type)
|
||||||
|
: _impl(std::forward<Callable>(other)) {
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Callable>
|
||||||
|
unique_function(Callable &&other, std::false_type)
|
||||||
|
: _impl(details::moveable_callable_wrap<std::decay_t<Callable>>(
|
||||||
|
std::forward<Callable>(other))) {
|
||||||
|
}
|
||||||
|
|
||||||
|
std::function<Return(Args...)> _impl;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace base
|
||||||
|
|
||||||
|
#ifdef UniqueFunctionUnexpected
|
||||||
|
#undef UniqueFunctionUnexpected
|
||||||
|
#undef Unexpected
|
||||||
|
#endif // UniqueFunctionUnexpectedb
|
||||||
|
|
|
@ -20,6 +20,8 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
|
||||||
*/
|
*/
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <rpl/details/callable.h>
|
||||||
|
|
||||||
class RPCError {
|
class RPCError {
|
||||||
public:
|
public:
|
||||||
RPCError(const MTPrpcError &error) : _code(error.c_rpc_error().verror_code.v) {
|
RPCError(const MTPrpcError &error) : _code(error.c_rpc_error().verror_code.v) {
|
||||||
|
@ -921,7 +923,9 @@ class RPCDoneHandlerImplementationNo : public RPCDoneHandlerImplementation<R()>
|
||||||
public:
|
public:
|
||||||
using RPCDoneHandlerImplementation<R()>::Parent::Parent;
|
using RPCDoneHandlerImplementation<R()>::Parent::Parent;
|
||||||
void operator()(mtpRequestId requestId, const mtpPrime *from, const mtpPrime *end) override {
|
void operator()(mtpRequestId requestId, const mtpPrime *from, const mtpPrime *end) override {
|
||||||
return this->_handler ? this->_handler() : void(0);
|
if (this->_handler) {
|
||||||
|
this->_handler();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
@ -931,44 +935,101 @@ class RPCDoneHandlerImplementationNoReq : public RPCDoneHandlerImplementation<R(
|
||||||
public:
|
public:
|
||||||
using RPCDoneHandlerImplementation<R(mtpRequestId)>::Parent::Parent;
|
using RPCDoneHandlerImplementation<R(mtpRequestId)>::Parent::Parent;
|
||||||
void operator()(mtpRequestId requestId, const mtpPrime *from, const mtpPrime *end) override {
|
void operator()(mtpRequestId requestId, const mtpPrime *from, const mtpPrime *end) override {
|
||||||
return this->_handler ? this->_handler(requestId) : void(0);
|
if (this->_handler) {
|
||||||
|
this->_handler(requestId);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename R>
|
template <typename Lambda>
|
||||||
inline RPCDoneHandlerPtr rpcDone_lambda_wrap_helper(base::lambda_once<R(const mtpPrime*, const mtpPrime*)> lambda) {
|
constexpr bool rpcDone_canCallBare_v = rpl::details::is_callable_plain_v<
|
||||||
return RPCDoneHandlerPtr(new RPCDoneHandlerImplementationBare<R>(std::move(lambda)));
|
Lambda, const mtpPrime*, const mtpPrime*>;
|
||||||
}
|
|
||||||
|
|
||||||
template <typename R>
|
|
||||||
inline RPCDoneHandlerPtr rpcDone_lambda_wrap_helper(base::lambda_once<R(const mtpPrime*, const mtpPrime*, mtpRequestId)> lambda) {
|
|
||||||
return RPCDoneHandlerPtr(new RPCDoneHandlerImplementationBareReq<R>(std::move(lambda)));
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename R, typename T>
|
|
||||||
inline RPCDoneHandlerPtr rpcDone_lambda_wrap_helper(base::lambda_once<R(const T&)> lambda) {
|
|
||||||
return RPCDoneHandlerPtr(new RPCDoneHandlerImplementationPlain<R, T>(std::move(lambda)));
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename R, typename T>
|
|
||||||
inline RPCDoneHandlerPtr rpcDone_lambda_wrap_helper(base::lambda_once<R(const T&, mtpRequestId)> lambda) {
|
|
||||||
return RPCDoneHandlerPtr(new RPCDoneHandlerImplementationReq<R, T>(std::move(lambda)));
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename R>
|
|
||||||
inline RPCDoneHandlerPtr rpcDone_lambda_wrap_helper(base::lambda_once<R()> lambda) {
|
|
||||||
return RPCDoneHandlerPtr(new RPCDoneHandlerImplementationNo<R>(std::move(lambda)));
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename R>
|
|
||||||
inline RPCDoneHandlerPtr rpcDone_lambda_wrap_helper(base::lambda_once<R(mtpRequestId)> lambda) {
|
|
||||||
return RPCDoneHandlerPtr(new RPCDoneHandlerImplementationNoReq<R>(std::move(lambda)));
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename Lambda>
|
template <typename Lambda>
|
||||||
|
constexpr bool rpcDone_canCallBareReq_v = rpl::details::is_callable_plain_v<
|
||||||
|
Lambda, const mtpPrime*, const mtpPrime*, mtpRequestId>;
|
||||||
|
|
||||||
|
template <typename Lambda>
|
||||||
|
constexpr bool rpcDone_canCallNo_v = rpl::details::is_callable_plain_v<
|
||||||
|
Lambda>;
|
||||||
|
|
||||||
|
template <typename Lambda>
|
||||||
|
constexpr bool rpcDone_canCallNoReq_v = rpl::details::is_callable_plain_v<
|
||||||
|
Lambda, mtpRequestId>;
|
||||||
|
|
||||||
|
template <typename Function>
|
||||||
|
struct rpcDone_canCallPlain : std::false_type {
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename Lambda, typename Return, typename T>
|
||||||
|
struct rpcDone_canCallPlain<Return(Lambda::*)(const T&)> : std::true_type {
|
||||||
|
using Arg = T;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename Lambda, typename Return, typename T>
|
||||||
|
struct rpcDone_canCallPlain<Return(Lambda::*)(const T&)const>
|
||||||
|
: rpcDone_canCallPlain<Return(Lambda::*)(const T&)> {
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename Function>
|
||||||
|
constexpr bool rpcDone_canCallPlain_v = rpcDone_canCallPlain<Function>::value;
|
||||||
|
|
||||||
|
template <typename Function>
|
||||||
|
struct rpcDone_canCallReq : std::false_type {
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename Lambda, typename Return, typename T>
|
||||||
|
struct rpcDone_canCallReq<Return(Lambda::*)(const T&, mtpRequestId)> : std::true_type {
|
||||||
|
using Arg = T;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename Lambda, typename Return, typename T>
|
||||||
|
struct rpcDone_canCallReq<Return(Lambda::*)(const T&, mtpRequestId)const>
|
||||||
|
: rpcDone_canCallReq<Return(Lambda::*)(const T&, mtpRequestId)> {
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename Function>
|
||||||
|
constexpr bool rpcDone_canCallReq_v = rpcDone_canCallReq<Function>::value;
|
||||||
|
|
||||||
|
template <typename Function>
|
||||||
|
struct rpcDone_returnType;
|
||||||
|
|
||||||
|
template <typename Lambda, typename Return, typename ...Args>
|
||||||
|
struct rpcDone_returnType<Return(Lambda::*)(Args...)> {
|
||||||
|
using type = Return;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename Lambda, typename Return, typename ...Args>
|
||||||
|
struct rpcDone_returnType<Return(Lambda::*)(Args...)const> {
|
||||||
|
using type = Return;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename Function>
|
||||||
|
using rpcDone_returnType_t = typename rpcDone_returnType<Function>::type;
|
||||||
|
|
||||||
|
template <
|
||||||
|
typename Lambda,
|
||||||
|
typename Function = base::lambda_call_type_t<Lambda>>
|
||||||
RPCDoneHandlerPtr rpcDone(Lambda lambda) {
|
RPCDoneHandlerPtr rpcDone(Lambda lambda) {
|
||||||
return rpcDone_lambda_wrap_helper(base::lambda_type<Lambda>(std::move(lambda)));
|
using R = rpcDone_returnType_t<Function>;
|
||||||
|
if constexpr (rpcDone_canCallBare_v<Lambda>) {
|
||||||
|
return RPCDoneHandlerPtr(new RPCDoneHandlerImplementationBare<R>(std::move(lambda)));
|
||||||
|
} else if constexpr (rpcDone_canCallBareReq_v<Lambda>) {
|
||||||
|
return RPCDoneHandlerPtr(new RPCDoneHandlerImplementationBareReq<R>(std::move(lambda)));
|
||||||
|
} else if constexpr (rpcDone_canCallNo_v<Lambda>) {
|
||||||
|
return RPCDoneHandlerPtr(new RPCDoneHandlerImplementationNo<R>(std::move(lambda)));
|
||||||
|
} else if constexpr (rpcDone_canCallNoReq_v<Lambda>) {
|
||||||
|
return RPCDoneHandlerPtr(new RPCDoneHandlerImplementationNoReq<R>(std::move(lambda)));
|
||||||
|
} else if constexpr (rpcDone_canCallPlain_v<Function>) {
|
||||||
|
using T = typename rpcDone_canCallPlain<Function>::Arg;
|
||||||
|
return RPCDoneHandlerPtr(new RPCDoneHandlerImplementationPlain<R, T>(std::move(lambda)));
|
||||||
|
} else if constexpr (rpcDone_canCallReq_v<Function>) {
|
||||||
|
using T = typename rpcDone_canCallReq<Function>::Arg;
|
||||||
|
return RPCDoneHandlerPtr(new RPCDoneHandlerImplementationReq<R, T>(std::move(lambda)));
|
||||||
|
} else {
|
||||||
|
static_assert(false_t(lambda), "Unknown method.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename FunctionType>
|
template <typename FunctionType>
|
||||||
|
@ -1010,23 +1071,35 @@ public:
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
inline RPCFailHandlerPtr rpcFail_lambda_wrap_helper(base::lambda_once<bool(const RPCError&)> lambda) {
|
template <typename Lambda>
|
||||||
return RPCFailHandlerPtr(new RPCFailHandlerImplementationPlain(std::move(lambda)));
|
constexpr bool rpcFail_canCallNo_v = rpl::details::is_callable_plain_v<
|
||||||
}
|
Lambda>;
|
||||||
|
|
||||||
inline RPCFailHandlerPtr rpcFail_lambda_wrap_helper(base::lambda_once<bool(const RPCError&, mtpRequestId)> lambda) {
|
|
||||||
return RPCFailHandlerPtr(new RPCFailHandlerImplementationReq(std::move(lambda)));
|
|
||||||
}
|
|
||||||
|
|
||||||
inline RPCFailHandlerPtr rpcFail_lambda_wrap_helper(base::lambda_once<bool()> lambda) {
|
|
||||||
return RPCFailHandlerPtr(new RPCFailHandlerImplementationNo(std::move(lambda)));
|
|
||||||
}
|
|
||||||
|
|
||||||
inline RPCFailHandlerPtr rpcFail_lambda_wrap_helper(base::lambda_once<bool(mtpRequestId)> lambda) {
|
|
||||||
return RPCFailHandlerPtr(new RPCFailHandlerImplementationNoReq(std::move(lambda)));
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename Lambda>
|
template <typename Lambda>
|
||||||
|
constexpr bool rpcFail_canCallNoReq_v = rpl::details::is_callable_plain_v<
|
||||||
|
Lambda, mtpRequestId>;
|
||||||
|
|
||||||
|
template <typename Lambda>
|
||||||
|
constexpr bool rpcFail_canCallPlain_v = rpl::details::is_callable_plain_v<
|
||||||
|
Lambda, const RPCError&>;
|
||||||
|
|
||||||
|
template <typename Lambda>
|
||||||
|
constexpr bool rpcFail_canCallReq_v = rpl::details::is_callable_plain_v<
|
||||||
|
Lambda, const RPCError&, mtpRequestId>;
|
||||||
|
|
||||||
|
template <
|
||||||
|
typename Lambda,
|
||||||
|
typename Function = base::lambda_call_type_t<Lambda>>
|
||||||
RPCFailHandlerPtr rpcFail(Lambda lambda) {
|
RPCFailHandlerPtr rpcFail(Lambda lambda) {
|
||||||
return rpcFail_lambda_wrap_helper(base::lambda_type<Lambda>(std::move(lambda)));
|
if constexpr (rpcFail_canCallNo_v<Lambda>) {
|
||||||
|
return RPCFailHandlerPtr(new RPCFailHandlerImplementationNo(std::move(lambda)));
|
||||||
|
} else if constexpr (rpcFail_canCallNoReq_v<Lambda>) {
|
||||||
|
return RPCFailHandlerPtr(new RPCFailHandlerImplementationNoReq(std::move(lambda)));
|
||||||
|
} else if constexpr (rpcFail_canCallPlain_v<Lambda>) {
|
||||||
|
return RPCFailHandlerPtr(new RPCFailHandlerImplementationPlain(std::move(lambda)));
|
||||||
|
} else if constexpr (rpcFail_canCallReq_v<Lambda>) {
|
||||||
|
return RPCFailHandlerPtr(new RPCFailHandlerImplementationReq(std::move(lambda)));
|
||||||
|
} else {
|
||||||
|
static_assert(false_t(lambda), "Unknown method.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,6 +27,7 @@
|
||||||
<(src_loc)/base/timer.h
|
<(src_loc)/base/timer.h
|
||||||
<(src_loc)/base/type_traits.h
|
<(src_loc)/base/type_traits.h
|
||||||
<(src_loc)/base/unique_any.h
|
<(src_loc)/base/unique_any.h
|
||||||
|
<(src_loc)/base/unique_function.h
|
||||||
<(src_loc)/base/variant.h
|
<(src_loc)/base/variant.h
|
||||||
<(src_loc)/base/virtual_method.h
|
<(src_loc)/base/virtual_method.h
|
||||||
<(src_loc)/base/weak_unique_ptr.h
|
<(src_loc)/base/weak_unique_ptr.h
|
||||||
|
|
Loading…
Reference in New Issue