Scanframe Modular Application 0.1.0
Loading...
Searching...
No Matches
FunctionRegistry.h
Go to the documentation of this file.
1#pragma once
2#include <functional>
3#include <memory>
4#include <misc/gen/string.h>
5#include <string>
6#include <typeindex>
7#include <unordered_map>
8#include <vector>
9
10namespace sf
11{
12
42{
43 public:
48 {
49 public:
50 virtual ~Base() = default;
51
56 virtual std::type_index returnType() const = 0;
57
62 virtual std::vector<std::type_index> argTypes() const = 0;
63 };
64
68 template<typename R, typename... Args>
69 static bool isValid(const std::string& name)
70 {
71 return Registration<R, Args...>(name).isValid();
72 }
73
77 template<typename R, typename... Args>
78 static R call(const std::string& name, Args... args)
79 {
80 auto func = Registration<R, Args...>(name);
81 if (func.isValid())
82 return func.call(args...);
83 return {};
84 }
85
91 template<typename R, typename... Args>
93 {
94 public:
98 class Holder final : public Base
99 {
100 public:
105 explicit Holder(std::function<R(Args...)> f);
106
107 std::type_index returnType() const override;
108
109 std::vector<std::type_index> argTypes() const override;
110
116 R invoke(Args... args) const;
117
118 private:
119 std::function<R(Args...)> func;
120 };
121
126 explicit Registration(const std::string& name);
127
132 bool isValid() const noexcept;
133
137 operator bool() const noexcept;
138
145 R call(Args... args) const;
146
150 R operator()(Args... args) const;
151
152 private:
157 bool link();
158
159 unsigned _counter;
160 std::string _key;
161 std::shared_ptr<Base> _holder;
162 };
163
173 template<typename R, typename... Args, typename F>
174 static void add(const std::string& name, F&& f, const std::string& desc = {});
175
183 template<typename R, typename... Args>
184 static bool contains(const std::string& name);
185
194 template<class R, class... Args>
195 static bool remove(const std::string& name);
196
201 static void list(std::ostream& os);
202
203 private:
211 template<typename R, typename... Args>
212 static std::string makeKey(const std::string& name);
213
217 struct Entry
218 {
219 std::shared_ptr<Base> holder;
220 std::string description;
221 };
222
227 static std::unordered_map<std::string, Entry>& registry();
228
233 static unsigned& counter();
234};
235
236}// namespace sf
237
238// Include all inlined functions and template implementations.
239#include <misc/gen/FunctionRegistry.hpp>
Base class for type-erased function storage.
Definition FunctionRegistry.h:48
virtual ~Base()=default
virtual std::type_index returnType() const =0
Get the return type of the stored function.
virtual std::vector< std::type_index > argTypes() const =0
Get the argument types of the stored function.
Nested holder storing a function with a specific signature.
Definition FunctionRegistry.h:99
std::vector< std::type_index > argTypes() const override
Get the argument types of the stored function.
R invoke(Args... args) const
Invoke the stored function.
std::type_index returnType() const override
Get the return type of the stored function.
Holder(std::function< R(Args...)> f)
Constructor accepting a std::function.
Registration handle templated on function signature.
Definition FunctionRegistry.h:93
bool isValid() const noexcept
Check if the registration is valid.
Registration(const std::string &name)
Construct registration by function name.
R call(Args... args) const
Calls the registered function when it exists.
Registry managing function registration and lookup.
Definition FunctionRegistry.h:42
static void list(std::ostream &os)
List all registered functions to the provided stream.
static bool remove(const std::string &name)
Removes a registered function by name and signature. Existing Registration instances are still able t...
static bool isValid(const std::string &name)
Check validity of the function using the registration.
Definition FunctionRegistry.h:69
static bool contains(const std::string &name)
Check if a function is registered.
static R call(const std::string &name, Args... args)
Check validity of registration the function and makes the call.
Definition FunctionRegistry.h:78
#define _MISC_CLASS
Definition misc/global.h:40
Definition Application.h:10