20 #ifndef BOOSTX_FACTORY_HPP_
21 #define BOOSTX_FACTORY_HPP_
23 #include <unordered_map>
25 #include <type_traits>
29 template<
typename T,
typename REGISTRY,
typename...ARGS>
37 template<
typename INTERFACE,
template<
class>
class TYPEINFO,
typename...ARGS>
103 if (!
exists(_key))
return nullptr;
119 static_assert(std::is_base_of<interface_type,T>::value,
120 "Given type T must be a base class of the interface class");
123 typedef std::is_same<decltype(registrar_type<T>::type_id()),
key_type> is_same_type;
124 static_assert(is_same_type::value,
125 "Type of type id to be registered and registrys type id must be equal!");
140 template<
typename T,
typename REGISTRY,
typename...ARGS>
183 return new T(_args...);
202 template<
typename T,
typename R,
typename...ARGS>
203 registrar<T,R,ARGS...>* registrar<T,R,ARGS...>::instance_
static void unreg()
Unregisters class by getting key from registrar.
Definition: factory.hpp:94
static void unreg(const key_type &_key)
Unregisters class with custom key.
Definition: factory.hpp:86
static bool registered()
The class is registered when the instance is not a null pointer.
Definition: factory.hpp:175
typeinfo_type< interface_type >::typeid_type key_type
Get key type from typeinfo struct.
Definition: factory.hpp:51
registry_type::template typeinfo_type< T > typeinfo_type
Get type info type from registry.
Definition: factory.hpp:153
registrar()
Constructor of registrar registeres the object if not already registered.
Definition: factory.hpp:162
std::unordered_map< key_type, constructor_type > class_map_type
Internal type of map for storing ids.
Definition: factory.hpp:57
static registrar * instance()
Holds the instance which cares about auto registration.
Definition: factory.hpp:193
registry_type::key_type key_type
Gets key type from registry.
Definition: factory.hpp:147
static interface_type * create(const key_type &_key, const ARGS &..._args)
Instantiates an object of the class by id and constructor arguments.
Definition: factory.hpp:101
friend registry_type
Friend of registry so registry can access registrar's private functions.
Definition: factory.hpp:156
Registrar mixin class which holds flag of registered class, to be derived from T. ...
Definition: factory.hpp:30
std::function< interface_type *(ARGS...)> constructor_type
Constructor function type.
Definition: factory.hpp:54
TYPEINFO< T > typeinfo_type
Template alias for typeinfo type.
Definition: factory.hpp:48
static registrar * instance_
This static instance is responsible for auto registration.
Definition: factory.hpp:159
factory< interface_type, TYPEINFO, ARGS...> type
Typedef for this factory type.
Definition: factory.hpp:44
INTERFACE interface_type
Typedef for our abstract interface.
Definition: factory.hpp:41
static class_map_type & private_classes()
Definition: factory.hpp:128
static decltype(typeinfo_type()()) type_id()
Return type id by calling the call operator of type info template.
Definition: factory.hpp:187
static interface_type * create(const ARGS &..._args)
Create an instance of the interface and return raw pointer.
Definition: factory.hpp:181
static void key_type_check()
Checks if key types are the same.
Definition: factory.hpp:116
static class_map_type const & classes()
Gives readonly access to registered classes.
Definition: factory.hpp:108
registry_type::interface_type interface_type
Gets interface type from registry.
Definition: factory.hpp:150
static void reg()
Registers class by getting key from registrar's type id.
Definition: factory.hpp:79
registrar & operator=(const registrar &)
Overload assignment operator so no copying is possible.
Definition: factory.hpp:169
REGISTRY registry_type
Typedef for registry.
Definition: factory.hpp:144
static void reg(const key_type &_key)
Registers class with custom key.
Definition: factory.hpp:71
The central factory class.
Definition: factory.hpp:38
static bool exists(const key_type &_key)
Checks if class with element id is registered.
Definition: factory.hpp:64