Audacity 3.2.0
Callable.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*!********************************************************************
3
4 Audacity: A Digital Audio Editor
5
6 @file Callable.h
7
8 @brief Functions and classes that generate callable objects
9
10 Paul Licameli
11
12 **********************************************************************/
13#ifndef __AUDACITY_CALLABLE__
14#define __AUDACITY_CALLABLE__
15
16#include <functional>
17#include <memory>
18#include <type_traits>
19#include <utility>
20
21namespace Callable {
22
24template<typename T> struct type_identity { using type = T; };
25
26namespace detail {
27template<typename> struct FunctionInvoker;
28template<typename R, typename... Args> struct FunctionInvoker<R(Args...)> {
29 using type = R(Args...);
30 type *const f;
31 R operator()(Args... args) const { return f(std::forward<Args>(args)...); }
32};
33
35template<typename M, typename C> struct MemberInvoker {
36 using Member = M C::*;
37 template<typename T> using Result = std::invoke_result_t<Member, T>;
38 template<typename, typename = void> struct CanAccept : std::false_type {};
39 template<typename T> struct CanAccept<T, std::void_t<Result<T>>>
40 : std::true_type {};
41
42 explicit constexpr MemberInvoker(Member member) : member{ member } {}
46 template<typename Obj> auto operator() (Obj&& obj) const
47 -> std::enable_if_t<CanAccept<Obj&&>::value, Result<Obj&&>>
48 {
49 return std::invoke(member, std::forward<Obj>(obj));
50 }
52};
53
55template<typename Invocable> struct InvocableBase {
56 using type = typename std::conditional_t<std::is_class_v<Invocable>,
57 Invocable,
59 >;
60};
61template<typename Invocable> using InvocableBase_t =
64template<typename M, typename C> struct InvocableBase<M C::*> {
66};
67
68template<typename... Invocables> struct OverloadSetBase
69 : detail::InvocableBase_t<Invocables>...
70{
71 constexpr OverloadSetBase() = default;
73 template<typename... Is>
74 constexpr OverloadSetBase(Is&&... invocables)
75 : detail::InvocableBase_t<Invocables>{ std::forward<Is>(invocables) }...
76 {}
77 constexpr OverloadSetBase(const OverloadSetBase&) = default;
78 constexpr OverloadSetBase(OverloadSetBase&&) = default;
80};
81}
82
85template<typename... Invocables> struct OverloadSet
86 : detail::OverloadSetBase<std::remove_reference_t<Invocables>...>
87{
90};
91template<typename... Is> OverloadSet(Is&&... invocables)
92 -> OverloadSet<Is&&...>;
93
95template<typename Type, typename... Arguments> struct UniquePtrFactory {
96 [[nodiscard]]
97 static auto Function(Arguments... arguments) -> std::unique_ptr<Type>
98 {
99 return std::make_unique<Type>(std::forward<Arguments&&>(arguments)...);
100 }
101};
102
104template<typename Type, typename... Arguments> struct SharedPtrFactory {
105 [[nodiscard]]
106 static auto Function(Arguments... arguments) -> std::shared_ptr<Type>
107 {
108 return std::make_shared<Type>(std::forward<Arguments&&>(arguments)...);
109 }
110};
111
113template<auto Value, typename... Arguments> struct Constantly {
114 static decltype(Value) Function (Arguments...) { return Value; }
115};
116
118
122template<typename T, typename... FixedArgs> constexpr auto UniqueMaker() {
123 return [](FixedArgs... fixedArgs, auto&&... args) {
124 return std::make_unique<T>(
125 std::forward<FixedArgs>(fixedArgs)...,
126 std::forward<decltype(args)>(args)...);
127 };
128 }
129
130}
131
132#endif
typename InvocableBase< Invocable >::type InvocableBase_t
Definition: Callable.h:62
OverloadSet(Is &&... invocables) -> OverloadSet< Is &&... >
constexpr auto UniqueMaker()
Generate variadic factory functions.
Definition: Callable.h:122
Call< Predicate, T > Is
Apply a metapredicate to a type.
Definition: TypeList.h:391
STL namespace.
Generates functions useable as non-type template parameters.
Definition: Callable.h:113
static decltype(Value) Function(Arguments...)
Definition: Callable.h:114
Generates functions useable as non-type template parameters.
Definition: Callable.h:104
static auto Function(Arguments... arguments) -> std::shared_ptr< Type >
Definition: Callable.h:106
Generates functions useable as non-type template parameters.
Definition: Callable.h:95
static auto Function(Arguments... arguments) -> std::unique_ptr< Type >
Definition: Callable.h:97
Capture any invocable as a class, using std::function only when needed.
Definition: Callable.h:55
typename std::conditional_t< std::is_class_v< Invocable >, Invocable, FunctionInvoker< std::remove_pointer_t< Invocable > > > type
Definition: Callable.h:59
Capture pointer to member.
Definition: Callable.h:35
std::invoke_result_t< Member, T > Result
Definition: Callable.h:37
constexpr MemberInvoker(Member member)
Definition: Callable.h:42
auto operator()(Obj &&obj) const -> std::enable_if_t< CanAccept< Obj && >::value, Result< Obj && > >
Definition: Callable.h:46
constexpr OverloadSetBase(Is &&... invocables)
Variadic constructor allowing arguments with different value categories.
Definition: Callable.h:74
constexpr OverloadSetBase(const OverloadSetBase &)=default
constexpr OverloadSetBase()=default
constexpr OverloadSetBase(OverloadSetBase &&)=default
standard in C++20; add a level of indirection to a type
Definition: Callable.h:24