21 #ifndef LIB_LIGHTMETRICA_COMPONENT_H
22 #define LIB_LIGHTMETRICA_COMPONENT_H
29 #include <type_traits>
83 typedef std::function<Component* ()> CreateComponentFunc;
102 static bool CheckRegistered(
const std::string& interfaceType,
const std::string& implType);
114 static bool Register(
const std::string& interfaceType,
const std::string& implType,
const CreateComponentFunc& func);
122 static Component* Create(
const std::string& interfaceType,
const std::string& implType);
129 static void LoadPlugins(
const std::string& pluginDir);
135 static void UnloadPlugins();
148 template <
typename InterfaceType>
151 std::string interfaceType = InterfaceType::InterfaceTypeName();
152 return CheckRegistered(interfaceType, implType);
162 template <
typename InterfaceType>
163 static InterfaceType*
Create(
const std::string& implType)
165 std::string interfaceType = InterfaceType::InterfaceTypeName();
167 auto* p1 = Create(interfaceType, implType);
170 LM_LOG_ERROR(
"Invalid instance type '" + implType +
"' (interface type : '" + interfaceType +
"')");
174 auto* p2 =
dynamic_cast<InterfaceType*
>(p1);
177 LM_LOG_ERROR(
"An instance of type '" + implType +
"' is not inherited from '" + interfaceType +
"'");
191 template <
typename InterfaceType>
194 return Create<InterfaceType>(
"default");
209 template <
typename ImplType,
typename InterfaceType>
235 LM_LOG_ERROR(
"Failed to register component '" + std::string(ImplType::ImplTypeName()) +
"'");
266 #define LM_COMPONENT_INTERFACE_DEF(Name) \
267 static const char* InterfaceTypeName() { return Name; } \
268 virtual std::string ComponentInterfaceTypeName() const { return InterfaceTypeName(); }
270 #define LM_COMPONENT_IMPL_DEF(Name) \
271 static const char* ImplTypeName() { return Name; } \
272 virtual std::string ComponentImplTypeName() const { return ImplTypeName(); }
300 #define LM_COMPONENT_CREATE_HAS_MEMBER_FUNCTION(FuncName, Signature) \
301 template <typename T, typename = std::true_type> \
302 struct has_member_function_##FuncName : std::false_type {}; \
304 template <typename T> \
305 struct has_member_function_##FuncName< \
307 std::integral_constant< \
309 check_func_signature<Signature, &T::FuncName>::value \
311 > : std::true_type {}
313 #define LM_COMPONENT_CHECK_HAS_MEMBER_FUNCTION(Type, FuncName) \
315 has_member_function_##FuncName<Type>::value, \
316 "Component class of type '" #Type "' must have the member function '" #FuncName "'")
318 #define LM_COMPONENT_CHECK_IS_DERIVED_CLASS(ImplType, InterfaceType) \
320 std::is_base_of<InterfaceType, ImplType>::value, \
321 "Component class of type '" #ImplType "' must be inherited from the class of type '" #InterfaceType "'")
327 template <
typename T, T>
330 LM_COMPONENT_CREATE_HAS_MEMBER_FUNCTION(ImplTypeName,
const char* (*)());
331 LM_COMPONENT_CREATE_HAS_MEMBER_FUNCTION(InterfaceTypeName,
const char* (*)());
348 #define LM_COMPONENT_REGISTER_IMPL(ImplType, InterfaceType) \
351 template <typename T1, typename T2> \
352 class ComponentFactoryEntryInstance; \
355 class ComponentFactoryEntryInstance<ImplType, InterfaceType> \
357 static const ::lightmetrica::ComponentFactoryEntry<ImplType, InterfaceType>& reg; \
360 const ::lightmetrica::ComponentFactoryEntry<ImplType, InterfaceType>& \
361 ComponentFactoryEntryInstance<ImplType, InterfaceType>::reg = \
362 ::lightmetrica::ComponentFactoryEntry<ImplType, InterfaceType>::Instance(); \
364 LM_COMPONENT_CHECK_IS_DERIVED_CLASS(ImplType, InterfaceType); \
365 LM_COMPONENT_CHECK_IS_DERIVED_CLASS(InterfaceType, Component); \
366 LM_COMPONENT_CHECK_HAS_MEMBER_FUNCTION(ImplType, ImplTypeName); \
367 LM_COMPONENT_CHECK_HAS_MEMBER_FUNCTION(InterfaceType, InterfaceTypeName); \
371 #endif // LIB_LIGHTMETRICA_COMPONENT_H
static bool Register(const std::string &interfaceType, const std::string &implType, const CreateComponentFunc &func)
Definition: component.cpp:270
Definition: component.h:45
static InterfaceType * Create(const std::string &implType)
Definition: component.h:163
static bool CheckRegistered(const std::string &implType)
Definition: component.h:149
static ComponentFactoryEntry< ImplType, InterfaceType > & Instance()
Definition: component.h:218
Definition: component.h:210
static InterfaceType * Create()
Definition: component.h:192
Definition: component.h:79
virtual std::string ComponentInterfaceTypeName() const =0
Definition: component.h:328
virtual std::string ComponentImplTypeName() const =0