Scanframe Modular Application 0.1.0
Loading...
Searching...
No Matches
TClassRegistration.h
Go to the documentation of this file.
1#pragma once
4#include <misc/gen/TClosure.h>
5#include <misc/gen/TVector.h>
6#include <misc/gen/dbgutils.h>
7#include <misc/global.h>
8
12#define SF_DL_NAME_FUNC_NAME "_dl_name_"
13#define SF_DL_NAME_FUNC _dl_name_
14
15#if IS_WIN
16 // Select copy function which is allowed by MS Windows.
17 #define SF_DL_STRING_COPY(dest, src, sz) strncpy_s(dest, sz, src, sz)
18#else
19 #define SF_DL_STRING_COPY(dest, src, sz) strncpy(dest, src, sz)
20#endif
21
22// clang-format off
26#define SF_DL_INFORMATION(name, description) \
27 namespace \
28 { \
29 const char* _dl_ = SF_DL_MARKER_BEGIN name SF_DL_NAME_SEPARATOR description SF_DL_MARKER_END; \
30 \
31 extern "C" TARGET_EXPORT const char* SF_DL_NAME_FUNC(const char* nm) \
32 { \
33 static char storage[256]; \
34 if (nm) \
35 SF_DL_STRING_COPY(storage, nm, sizeof(storage)); \
36 return storage; \
37 } \
38 }
39
43extern "C" const char* SF_DL_NAME_FUNC(const char* name);
44
48#define SF_DL_NAME_FUNC_TYPE const char* (*) (const char*)
49
50namespace sf
51{
52
56inline std::string getLibraryName()
57{
58 return SF_DL_NAME_FUNC(nullptr);
59}
60
61}// namespace sf
62
71#define SF_DECL_IFACE(InterfaceType, ParamType, FuncName) \
72private: \
73 std::string _Register_Name_; \
74 \
75public: \
76 static sf::TClassRegistration<InterfaceType, ParamType> FuncName(); \
77 friend class sf::TClassRegistration<InterfaceType, ParamType>;
78
82#define SF_IMPL_IFACE(InterfaceType, ParamType, FuncName) \
83 sf::TClassRegistration<InterfaceType, ParamType> InterfaceType::FuncName() \
84 { \
85 static sf::TClassRegistration<InterfaceType, ParamType>::entries_t entries; \
86 return sf::TClassRegistration<InterfaceType, ParamType>(entries); \
87 }
88
96#define SF_REG_CLASS(InterfaceType, ParamType, FuncName, DerivedType, RegName, Description) \
97 namespace \
98 { \
99 __attribute__((constructor)) void _## DerivedType## _() \
100 { \
101 size_t dist = InterfaceType::FuncName().registerClass( \
102 RegName, Description, sf::TClassRegistration<InterfaceType, ParamType>::callback_t([](const ParamType& params) -> InterfaceType* { \
103 auto inst = new DerivedType(params); \
104 sf::TClassRegistration<InterfaceType, ParamType>::setRegisterName(inst, RegName); \
105 return inst; \
106 }) \
107 ); \
108 } \
109 }
110// clang-format on
111
112namespace sf
113{
114
122template<typename T, typename P>
124{
125 public:
126 // Type and forward definitions.
129 class entry_t;
133 typedef std::vector<entry_t> entries_t;
134
139
143 static auto constexpr npos = std::numeric_limits<size_t>::max();
144
148 explicit TClassRegistration(entries_t& entries);
149
154
159
164 {
165 // Prevent self assignment.
166 if (this != &inst)
167 {
168 _entries = inst._entries;
169 }
170 return *this;
171 }
172
179 size_t registerClass(const char* name, const char* description, const callback_t& callback);
180
187 [[nodiscard]] size_t indexOf(const std::string& name) const;
188
195 T* create(const std::string& name, const P& params = P{}) const;
196
203 T* create(size_t index, const P& params = P{}) const;
204
209 [[nodiscard]] size_t size() const;
210
215 static void setRegisterName(T* inst, const char* name)
216 {
217 inst->_Register_Name_ = name;
218 }
219
223 const std::string& getRegisterName(T* inst) const;
224
228 [[nodiscard]] const char* getName(size_t index) const;
229
233 [[nodiscard]] strings_t getNames() const;
234
238 [[nodiscard]] const char* getDescription(size_t index) const;
239
244 {
245 public:
249 entry_t(const char* name, const char* description, const callback_t& callback);
250
254 entry_t(const entry_t& e);
255
256 private:
260 const char* _name;
264 const char* _description;
268 callback_t _callback;
269
270 friend class TClassRegistration;
271 };
272
273 private:
279 const entry_t* find(const std::string& name) const;
280
284 entries_t* _entries;
285
289 typename entries_t::const_iterator lookup(const std::string& name) const;
290};
291
292}// namespace sf
293
294// Include all inlined functions and template implementations.
295#include <misc/gen/TClassRegistration.hpp>
#define SF_DL_NAME_FUNC
Definition TClassRegistration.h:13
Type of registered entry in the list.
Definition TClassRegistration.h:244
entry_t(const char *name, const char *description, const callback_t &callback)
Constructor.
entry_t(const entry_t &e)
Copy constructor.
Template class used to register derived classes from a (virtual) interface class. Registering name an...
Definition TClassRegistration.h:124
TClosure< T *, const P & > callback_t
Definition TClassRegistration.h:128
size_t registerClass(const char *name, const char *description, const callback_t &callback)
Register a derived class.
TClassRegistration self_t
Definition TClassRegistration.h:127
const char * getName(size_t index) const
Gets the name of the class from the passed index.
TClassRegistration(entries_t &entries)
Constructor for the base class.
T * create(const std::string &name, const P &params=P{}) const
Find and create registered class by name.
static auto constexpr npos
Index value for when not found.
Definition TClassRegistration.h:143
strings_t getNames() const
Gets the all the registered names.
const char * getDescription(size_t index) const
Returns the description of the class from the passed index.
size_t size() const
Returns the amount of registered entries.
TClassRegistration & operator=(const TClassRegistration &inst)
Assignment operator.
Definition TClassRegistration.h:163
TClassRegistration(const TClassRegistration &inst)
Copy constructor.
T * create(size_t index, const P &params=P{}) const
Function create registered class by index.
const std::string & getRegisterName(T *inst) const
Gets the register name of the passed.
TVector< std::string > strings_t
Type used to report the list of implementations in.
Definition TClassRegistration.h:138
std::vector< entry_t > entries_t
Vector type to register implementations in.
Definition TClassRegistration.h:133
size_t indexOf(const std::string &name) const
Find a registered class structure index. When not found it returns.
TClassRegistration(TClassRegistration &&inst) noexcept
Move constructor.
static void setRegisterName(T *inst, const char *name)
Sets the register name on the passed instance. This automatically is called from the callback_t when ...
Definition TClassRegistration.h:215
Encapsulates the std::function() template.
Definition TClosure.h:14
Counted vector having additional methods and operators for ease of usage.
Definition TVector.h:25
Definition Application.h:10
std::string getLibraryName()
function to get the library filename this function is called from.
Definition TClassRegistration.h:56