2019-04-10 05:53:51 +02:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2019 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#define HAVE_IRCD_UTIL_CLOSURE_H
|
|
|
|
|
2019-06-23 10:01:26 +02:00
|
|
|
namespace ircd {
|
|
|
|
inline namespace util
|
2019-04-10 05:53:51 +02:00
|
|
|
{
|
2020-11-28 01:57:31 +01:00
|
|
|
template<template<class, class...>
|
|
|
|
class F,
|
|
|
|
class R,
|
|
|
|
class... A>
|
|
|
|
struct closure;
|
|
|
|
|
|
|
|
template<template<class, class...>
|
|
|
|
class F,
|
|
|
|
class... A>
|
|
|
|
struct closure<F, bool, A...>;
|
|
|
|
|
|
|
|
/// Template for creating callback-based iterations allowing closures of
|
|
|
|
/// either `void (...)` or `bool (...)` to be passed to the same overload.
|
|
|
|
///
|
|
|
|
/// This reduces/deduplicates interfaces which offer an overload for each
|
|
|
|
/// and thus two library symbols, usually just calling each other...
|
|
|
|
///
|
|
|
|
template<template<class, class...>
|
|
|
|
class F,
|
|
|
|
class... A>
|
|
|
|
using closure_bool = closure<F, bool, A...>;
|
2023-02-17 05:53:30 +01:00
|
|
|
|
|
|
|
template<class... A>
|
|
|
|
using function_bool = closure_bool<std::function, A...>;
|
2019-06-23 10:01:26 +02:00
|
|
|
}}
|
2019-04-10 05:53:51 +02:00
|
|
|
|
2020-11-28 01:57:31 +01:00
|
|
|
template<template<class, class...>
|
|
|
|
class F,
|
|
|
|
class R,
|
|
|
|
class... A>
|
2019-04-10 05:53:51 +02:00
|
|
|
struct ircd::util::closure
|
2020-11-28 01:57:31 +01:00
|
|
|
:F<R (A...)>
|
|
|
|
{
|
|
|
|
using proto_type = R (A...);
|
|
|
|
using func_type = F<proto_type>;
|
|
|
|
|
|
|
|
using func_type::func_type;
|
|
|
|
using func_type::operator=;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<template<class, class...>
|
|
|
|
class F,
|
|
|
|
class... A>
|
|
|
|
struct ircd::util::closure<F, bool, A...>
|
|
|
|
:F<bool (A...)>
|
2019-04-10 05:53:51 +02:00
|
|
|
{
|
2022-02-04 04:46:54 +01:00
|
|
|
using proto_bool_type = bool (A...);
|
|
|
|
using proto_void_type = void (A...);
|
|
|
|
|
|
|
|
using func_bool_type = F<proto_bool_type>;
|
|
|
|
using func_void_type = F<proto_void_type>;
|
2019-04-10 05:53:51 +02:00
|
|
|
|
2020-11-28 01:57:31 +01:00
|
|
|
template<class lambda>
|
2022-02-04 04:46:54 +01:00
|
|
|
closure(lambda &&o,
|
2022-06-25 22:38:30 +02:00
|
|
|
typename std::enable_if<!std::is_constructible<func_bool_type, lambda>::value, int>::type = 0)
|
|
|
|
noexcept(std::is_nothrow_invocable<lambda, void, A...>());
|
2022-02-04 04:46:54 +01:00
|
|
|
|
|
|
|
template<class lambda>
|
|
|
|
closure(lambda &&o,
|
2022-06-25 22:38:30 +02:00
|
|
|
typename std::enable_if<std::is_constructible<func_bool_type, lambda>::value, int>::type = 0)
|
|
|
|
noexcept(std::is_nothrow_invocable<lambda, bool, A...>());
|
2023-02-23 23:39:42 +01:00
|
|
|
|
|
|
|
closure() = default;
|
2019-04-10 05:53:51 +02:00
|
|
|
};
|
2020-11-28 01:57:31 +01:00
|
|
|
|
|
|
|
template<template<class, class...>
|
|
|
|
class F,
|
|
|
|
class... A>
|
|
|
|
template<class lambda>
|
|
|
|
[[gnu::always_inline]]
|
|
|
|
inline
|
2022-02-04 04:46:54 +01:00
|
|
|
ircd::util::closure<F, bool, A...>::closure(lambda &&o,
|
|
|
|
typename std::enable_if<!std::is_constructible<func_bool_type, lambda>::value, int>::type)
|
2022-06-25 22:38:30 +02:00
|
|
|
noexcept(std::is_nothrow_invocable<lambda, void, A...>())
|
2020-11-28 01:57:31 +01:00
|
|
|
:F<bool (A...)>
|
|
|
|
{
|
|
|
|
[o(std::move(o))](A&&... a)
|
2022-06-25 22:38:30 +02:00
|
|
|
noexcept(std::is_nothrow_invocable<lambda, void, A...>())
|
2020-11-28 01:57:31 +01:00
|
|
|
{
|
2022-02-04 04:46:54 +01:00
|
|
|
static_assert
|
|
|
|
(
|
|
|
|
std::is_same<void, decltype(o(std::forward<A>(a)...))>()
|
|
|
|
);
|
|
|
|
|
2020-11-28 01:57:31 +01:00
|
|
|
o(std::forward<A>(a)...);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{}
|
2022-02-04 04:46:54 +01:00
|
|
|
|
|
|
|
template<template<class, class...>
|
|
|
|
class F,
|
|
|
|
class... A>
|
|
|
|
template<class lambda>
|
|
|
|
[[gnu::always_inline]]
|
|
|
|
inline
|
|
|
|
ircd::util::closure<F, bool, A...>::closure(lambda &&o,
|
|
|
|
typename std::enable_if<std::is_constructible<func_bool_type, lambda>::value, int>::type)
|
2022-06-25 22:38:30 +02:00
|
|
|
noexcept(std::is_nothrow_invocable<lambda, bool, A...>())
|
2022-02-04 04:46:54 +01:00
|
|
|
:F<bool (A...)>
|
|
|
|
{
|
|
|
|
std::forward<lambda>(o)
|
|
|
|
}
|
|
|
|
{}
|