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