|
DBC Framework
DBC parsing and CAN signal decoding framework
|
Registry-based factory for creating objects by runtime identifier. More...
#include <typefactory.h>
Inheritance diagram for TypeFactory< ClassId, BaseClass, Args >:Public Types | |
| using | BasePtr = std::shared_ptr< BaseClass > |
| using | CreatorFn = BasePtr(*)(Args...) |
Public Member Functions | |
| template<class Derived > | |
| void | registerType (const ClassId &id) |
| Registers a Derived type under the given id. | |
| CreatorFn | creator (const ClassId &id) const |
| Returns creator function for the given id. | |
| BasePtr | create (const ClassId &id, Args... args) const |
| Creates an instance for the given id using provided arguments. | |
| bool | contains (const ClassId &id) const |
| Checks whether an id is registered. | |
Protected Attributes | |
| std::unordered_map< ClassId, CreatorFn > | classes |
Registry-based factory for creating objects by runtime identifier.
Allows registering derived types and instantiating them using a ClassId key.
Notes:
| ClassId | -Unique class identifier type (e.g. std::string, int, enum). |
| BaseClass | -Base type for all created objects. |
| Arg | - Constructor arguments forwarded to registered derived types (by value). |
Usage example:
Definition at line 59 of file typefactory.h.
| using TypeFactory< ClassId, BaseClass, Args >::BasePtr = std::shared_ptr<BaseClass> |
Definition at line 61 of file typefactory.h.
| using TypeFactory< ClassId, BaseClass, Args >::CreatorFn = BasePtr (*)(Args...) |
Definition at line 62 of file typefactory.h.
|
inline |
Registers a Derived type under the given id.
Requirements (checked at compile-time):
Re-registering the same id overwrites the previous entry ("last wins").
Definition at line 76 of file typefactory.h.
|
inline |
Returns creator function for the given id.
| std::out_of_range | when id is not registered. |
Definition at line 89 of file typefactory.h.
Referenced by TypeFactory< ClassId, BaseClass, Args >::create().
|
inline |
Creates an instance for the given id using provided arguments.
| std::out_of_range | when id is not registered. |
Definition at line 97 of file typefactory.h.
References TypeFactory< ClassId, BaseClass, Args >::creator().
|
inline |
Checks whether an id is registered.
Definition at line 104 of file typefactory.h.
|
protected |
Definition at line 110 of file typefactory.h.