DBC Framework
DBC parsing and CAN signal decoding framework
Loading...
Searching...
No Matches
TypeFactory< ClassId, BaseClass, Args > Class Template Reference

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
 

Detailed Description

template<class ClassId, class BaseClass, class... Args>
class TypeFactory< ClassId, BaseClass, Args >

Registry-based factory for creating objects by runtime identifier.

Allows registering derived types and instantiating them using a ClassId key.

Notes:

  • Not thread-safe. Register types during initialization phase only.
  • Re-registering the same id overwrites the previous entry ("last wins").
  • ClassId must be hashable (std::hash<ClassId> specialization must exist).
Template Parameters
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:

#include <string>
struct Base {
virtual ~Base() = default;
virtual int get() = 0;
};
struct Derived1 : Base {
explicit Derived1(int start) : m_start(start) {}
int get() override { return m_start + 1; }
int m_start = 0;
};
struct Derived2 : Base {
explicit Derived2(int start) : m_start(start) {}
int get() override { return m_start + 2; }
int m_start = 0;
};
factory.registerType<Derived1>("one");
factory.registerType<Derived2>("two");
auto a = factory.create("one", 10);
auto b = factory.creator("two")(10); // advanced API: get creator function
(void)a->get();
(void)b->get();
Registry-based factory for creating objects by runtime identifier.
Definition typefactory.h:59
BasePtr create(const ClassId &id, Args... args) const
Creates an instance for the given id using provided arguments.
Definition typefactory.h:97
void registerType(const ClassId &id)
Registers a Derived type under the given id.
Definition typefactory.h:76
CreatorFn creator(const ClassId &id) const
Returns creator function for the given id.
Definition typefactory.h:89

Definition at line 59 of file typefactory.h.

Member Typedef Documentation

◆ BasePtr

template<class ClassId , class BaseClass , class... Args>
using TypeFactory< ClassId, BaseClass, Args >::BasePtr = std::shared_ptr<BaseClass>

Definition at line 61 of file typefactory.h.

◆ CreatorFn

template<class ClassId , class BaseClass , class... Args>
using TypeFactory< ClassId, BaseClass, Args >::CreatorFn = BasePtr (*)(Args...)

Definition at line 62 of file typefactory.h.

Member Function Documentation

◆ registerType()

template<class ClassId , class BaseClass , class... Args>
template<class Derived >
void TypeFactory< ClassId, BaseClass, Args >::registerType ( const ClassId &  id)
inline

Registers a Derived type under the given id.

Requirements (checked at compile-time):

  • Derived must inherit from BaseClass
  • Derived must be constructible from Args...

Re-registering the same id overwrites the previous entry ("last wins").

Definition at line 76 of file typefactory.h.

◆ creator()

template<class ClassId , class BaseClass , class... Args>
CreatorFn TypeFactory< ClassId, BaseClass, Args >::creator ( const ClassId &  id) const
inline

Returns creator function for the given id.

Exceptions
std::out_of_rangewhen id is not registered.

Definition at line 89 of file typefactory.h.

Referenced by TypeFactory< ClassId, BaseClass, Args >::create().

◆ create()

template<class ClassId , class BaseClass , class... Args>
BasePtr TypeFactory< ClassId, BaseClass, Args >::create ( const ClassId &  id,
Args...  args 
) const
inline

Creates an instance for the given id using provided arguments.

Exceptions
std::out_of_rangewhen id is not registered.

Definition at line 97 of file typefactory.h.

References TypeFactory< ClassId, BaseClass, Args >::creator().

◆ contains()

template<class ClassId , class BaseClass , class... Args>
bool TypeFactory< ClassId, BaseClass, Args >::contains ( const ClassId &  id) const
inline

Checks whether an id is registered.

Definition at line 104 of file typefactory.h.

Member Data Documentation

◆ classes

template<class ClassId , class BaseClass , class... Args>
std::unordered_map<ClassId, CreatorFn> TypeFactory< ClassId, BaseClass, Args >::classes
protected

Definition at line 110 of file typefactory.h.


The documentation for this class was generated from the following file: