VR-Engage  2.2
Loading...
Searching...
No Matches
factory.h
Go to the documentation of this file.
1/******************************************************************************
2** Copyright (c) 2025 MAK Technologies
3** All rights reserved.
4******************************************************************************/
5
6#pragma once
7
8//! \file factory.h
9//! \brief Provides a template-based factory system for object creation
10
11#include "vreUtil/logger.h"
12
13#include <map>
14#include <string>
15
16// Included for type checking to prevent warnings when the same creator is added multiple times
17
18namespace makVre
19{
20//! \brief Base template class for creators in the factory pattern
21//!
22//! This class serves as the base for all creator classes, defining the
23//! interface for object creation. It allows different types of creators
24//! to be stored in the same container.
25//!
26//! \tparam BASE_T The base type that created objects will inherit from
27template <typename BASE_T>
29{
30public:
31 //! \brief Default constructor
33
34 //! \brief Virtual destructor
35 virtual ~DtBaseCreator() {}
36
37 //! \brief Pure virtual method to create an object
38 //!
39 //! \return A pointer to a new instance of BASE_T or a derived class
40 virtual BASE_T* create() = 0;
41};
42
43//! \brief Concrete creator class for objects with a default constructor
44//!
45//! This template implements the DtBaseCreator interface for a specific
46//! derived class type. It creates objects of type CREATE_T which must
47//! inherit from BASE_T.
48//!
49//! \tparam BASE_T The base type that created objects will inherit from
50//! \tparam CREATE_T The concrete type to create, must inherit from BASE_T
51template <typename BASE_T, typename CREATE_T>
52class DtCreator : public DtBaseCreator<BASE_T>
53{
54public:
55 //! \brief Default constructor
57
58 //! \brief Virtual destructor
59 virtual ~DtCreator() override {}
60
61 //! \brief Creates a new instance of CREATE_T
62 //!
63 //! \return A pointer to the newly created object
64 virtual CREATE_T* create() override
65 {
66 CREATE_T* obj = new CREATE_T();
67 return obj;
68 }
69};
70
71//! \brief Factory class for creating objects of different derived types
72//!
73//! This template provides a factory for creating objects that inherit from
74//! a common base class. It maintains a map of creator objects indexed by name.
75//!
76//! \tparam BASE_T The base type that all created objects will inherit from
77template <typename BASE_T>
79{
80public:
81 //! \brief Type alias for the map of creators
82 using CreatorMap = std::map<std::string, DtBaseCreator<BASE_T>*>;
83
84 //! \brief Default constructor
86
87 //! \brief Virtual destructor
88 //!
89 //! Cleans up all registered creators
90 virtual ~DtFactory()
91 {
92 auto iter = myCreators.begin();
93
94 for (iter; iter != myCreators.end(); ++iter)
95 {
96 delete iter->second;
97 }
98
99 myCreators.clear();
100 }
101
102 //! \brief Registers a creator for a specific type
103 //!
104 //! Adds a creator to the factory, allowing objects of that type to be
105 //! created by name. If a creator with the same name already exists,
106 //! it will be replaced, but a warning will only be logged if the new
107 //! creator is of a different type.
108 //!
109 //! \tparam CREATE_T The concrete type to register, must inherit from BASE_T
110 //! \param name The name to register the creator under. If empty, the typeid name is used.
111 template <typename CREATE_T>
112 void addCreator(std::string name = "")
113 {
114 if (name.empty())
115 {
116 name = typeid(CREATE_T).name();
117 }
118
119 auto iter = myCreators.find(name);
120
121 // Warn only if the new creator is added with the same name and a different type.
122 // This prevents warnings when multiple roles add the same type.
123 if (iter != myCreators.end() && typeid(DtCreator<BASE_T, CREATE_T>).name() != typeid(*iter->second).name())
124 {
125 LOG_WARN("Util") << "Replacing existing creator " << name << std::endl;
126 delete iter->second;
127 }
128
129 LOG_INFO("Util") << "Adding creator" << name << std::endl;
131 }
132
133 //! \brief Creates an object by name
134 //!
135 //! \param name The registered name of the creator to use
136 //! \return A pointer to the newly created object, or NULL if no creator is found
137 BASE_T* create(std::string name)
138 {
139 auto iter = myCreators.find(name);
140 if (iter == myCreators.end())
141 {
142 LOG_WARN("Util") << "Cannot find creator for " << name << std::endl;
143 return NULL;
144 }
145
146 return iter->second->create();
147 }
148
149 //! \brief Gets the map of registered creators
150 //!
151 //! \return A reference to the map of creators
153
154protected:
155 //! \brief Map of creator objects indexed by name
157};
158
159} // namespace makVre
virtual BASE_T * create()=0
Pure virtual method to create an object.
DtBaseCreator()
Default constructor.
Definition factory.h:32
virtual ~DtBaseCreator()
Virtual destructor.
Definition factory.h:35
Concrete creator class for objects with a default constructor.
Definition factory.h:53
DtCreator()
Default constructor.
Definition factory.h:56
virtual CREATE_T * create() override
Creates a new instance of CREATE_T.
Definition factory.h:64
virtual ~DtCreator() override
Virtual destructor.
Definition factory.h:59
CreatorMap myCreators
Definition factory.h:156
BASE_T * create(std::string name)
Creates an object by name.
Definition factory.h:137
std::map< std::string, DtBaseCreator< DtValueTransform > * > CreatorMap
Definition factory.h:82
DtFactory()
Default constructor.
Definition factory.h:85
virtual ~DtFactory()
Virtual destructor.
Definition factory.h:90
CreatorMap & creatorMap()
Gets the map of registered creators.
Definition factory.h:152
void addCreator(std::string name="")
Registers a creator for a specific type.
Definition factory.h:112
Provides logging functionality with various severity levels and channels.
#define LOG_WARN(channel)
Macro to log a warning message to log files.
Definition logger.h:69
#define LOG_INFO(channel)
Macro to log an informational message to log files.
Definition logger.h:74
Include export definitions for this library.
Definition glsVreMessageUtil.h:49