Skip to content

Commit

Permalink
Use std::function for base::lambda implementation.
Browse files Browse the repository at this point in the history
(cherry picked from commit 101d4f6)
  • Loading branch information
john-preston authored and leha-bot committed Jul 27, 2018
1 parent 9f1896c commit f488d5f
Show file tree
Hide file tree
Showing 5 changed files with 279 additions and 60 deletions.
2 changes: 1 addition & 1 deletion Telegram/SourceFiles/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ bool loggedOut() {

void logOut() {
if (auto mtproto = Messenger::Instance().mtp()) {
mtproto->logout(rpcDone(&loggedOut), rpcFail(&loggedOut));
mtproto->logout(rpcDone([] { return loggedOut(); }), rpcFail([] { return loggedOut(); }));
} else {
// We log out because we've forgotten passcode.
// So we just start mtproto from scratch.
Expand Down
28 changes: 25 additions & 3 deletions Telegram/SourceFiles/base/lambda.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,29 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
#include <cstddef> // std::max_align_t
#include <memory>

#ifndef CUSTOM_LAMBDA_WRAP

#include "base/unique_function.h"
#include <functional>

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
#define LambdaAssertDefined
#define Assert(v) ((v) ? ((void)0) : std::abort())
Expand Down Expand Up @@ -63,9 +86,6 @@ template <typename Lambda> struct type_helper {

template <typename Lambda> 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 {

constexpr auto kFullStorageSize = 32U;
Expand Down Expand Up @@ -416,3 +436,5 @@ template <typename Return, typename... Args> class lambda<Return(Args...)> final
#ifdef LambdaUnexpectedDefined
#undef Unexpected
#endif // LambdaUnexpectedDefined

#endif // CUSTOM_LAMBDA_WRAP
33 changes: 18 additions & 15 deletions Telegram/SourceFiles/base/lambda_guard.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ namespace lambda_internal {

template <int N, typename Lambda> class guard_data {
public:
using return_type = typename lambda_type<Lambda>::return_type;

template <typename... PointersAndLambda>
inline guard_data(PointersAndLambda &&... qobjectsAndLambda)
: _lambda(init(_pointers, std::forward<PointersAndLambda>(qobjectsAndLambda)...)) {}
Expand All @@ -44,7 +42,8 @@ template <int N, typename Lambda> class guard_data {
}
}

template <typename... Args> inline return_type operator()(Args &&... args) {
template <typename... Args, typename return_type = decltype(std::declval<Lambda>()(std::declval<Args>()...))>
inline auto operator()(Args &&... args) {
for (int i = 0; i != N; ++i) {
if (!_pointers[i]) {
return return_type();
Expand All @@ -53,7 +52,8 @@ template <int N, typename Lambda> class guard_data {
return _lambda(std::forward<Args>(args)...);
}

template <typename... Args> inline return_type operator()(Args &&... args) const {
template <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) {
if (!_pointers[i]) {
return return_type();
Expand All @@ -73,13 +73,15 @@ template <int N, typename Lambda> class guard_data {
}

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> class guard {
public:
using return_type = typename lambda_type<Lambda>::return_type;

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),
Expand All @@ -103,11 +105,13 @@ template <int N, typename Lambda> class guard {
return *this;
}

template <typename... Args> inline return_type operator()(Args &&... args) {
template <typename... Args, typename = decltype(std::declval<Lambda>()(std::declval<Args>()...))>
inline decltype(auto) operator()(Args &&... args) {
return (*_data)(std::forward<Args>(args)...);
}

template <typename... Args> inline return_type operator()(Args &&... args) const {
template <typename... Args, typename = decltype(std::declval<Lambda>()(std::declval<Args>()...))>
inline decltype(auto) operator()(Args &&... args) const {
return (*_data)(std::forward<Args>(args)...);
}

Expand All @@ -119,14 +123,18 @@ template <int N, typename Lambda> class guard {
mutable std::unique_ptr<guard_data<N, Lambda>> _data;
};

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> struct guard_type;

template <int N, int K, typename Pointer, typename... PointersAndLambda>
struct guard_type<N, K, Pointer, PointersAndLambda...> {
using type = typename guard_type<N, K - 1, PointersAndLambda...>::type;
};

template <int N, typename Lambda> struct guard_type<N, 0, Lambda> { using type = guard<N, Lambda>; };
template <int N, typename Lambda> struct guard_type<N, 0, Lambda> { using type = guard<N, std::decay_t<Lambda>>; };

template <typename... PointersAndLambda> struct guard_type_helper {
static constexpr int N = sizeof...(PointersAndLambda);
Expand All @@ -135,11 +143,6 @@ template <typename... PointersAndLambda> struct guard_type_helper {

template <typename... PointersAndLambda> 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

template <typename... PointersAndLambda>
Expand Down
148 changes: 148 additions & 0 deletions Telegram/SourceFiles/base/unique_function.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/*
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 <functional>

#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
Loading

0 comments on commit f488d5f

Please sign in to comment.