Async 1.8.0
AsyncFactory.h
Go to the documentation of this file.
1
27#ifndef ASYNC_FACTORY_INCLUDED
28#define ASYNC_FACTORY_INCLUDED
29
30
31/****************************************************************************
32 *
33 * System Includes
34 *
35 ****************************************************************************/
36
37#include <map>
38#include <string>
39#include <sstream>
40#include <cassert>
41
42
43/****************************************************************************
44 *
45 * Project Includes
46 *
47 ****************************************************************************/
48
49
50
51/****************************************************************************
52 *
53 * Local Includes
54 *
55 ****************************************************************************/
56
57
58
59/****************************************************************************
60 *
61 * Forward declarations
62 *
63 ****************************************************************************/
64
65
66
67/****************************************************************************
68 *
69 * Namespace
70 *
71 ****************************************************************************/
72
73namespace Async
74{
75
76
77/****************************************************************************
78 *
79 * Forward declarations of classes inside of the declared namespace
80 *
81 ****************************************************************************/
82
83
84
85/****************************************************************************
86 *
87 * Defines & typedefs
88 *
89 ****************************************************************************/
90
91
92
93/****************************************************************************
94 *
95 * Exported Global Variables
96 *
97 ****************************************************************************/
98
99
100
101/****************************************************************************
102 *
103 * Class definitions
104 *
105 ****************************************************************************/
106
147template <class T, typename... Args>
149{
150 public:
157 static T *createNamedObject(const std::string& name, Args... args)
158 {
159 typename std::map<std::string, Factory<T, Args...>*>::iterator it;
160 it = factories().find(name);
161 if (it == factories().end())
162 {
163 return 0;
164 }
165
166 return (*it).second->createObject(args...);
167 } /* Factory::createNamedObject */
168
176 static std::string validFactories(void)
177 {
178 std::stringstream ss;
179 for (typename FactoryMap::const_iterator it = factories().begin();
180 it != factories().end(); ++it)
181 {
182 ss << "\"" << (*it).first << "\" ";
183 }
184 return ss.str();
185 }
186
191 Factory(const std::string &name) : m_name(name)
192 {
193 typename FactoryMap::iterator it = factories().find(m_name);
194 assert(it == factories().end());
195 factories()[name] = this;
196 } /* Factory::Factory */
197
201 Factory(const Factory<T, Args...>&) = delete;
202
207
211 virtual ~Factory(void)
212 {
213 typename FactoryMap::iterator it = factories().find(m_name);
214 assert(it != factories().end());
215 factories().erase(it);
216 } /* Factory::~Factory */
217
218 protected:
224 virtual T *createObject(Args... args) = 0;
225
226 private:
227 typedef std::map<std::string, Factory<T, Args...>*> FactoryMap;
228 std::string m_name;
229
230 static FactoryMap& factories(void)
231 {
232 static FactoryMap factory_map;
233 return factory_map;
234 }
235}; /* class Factory */
236
237
289template <class Base, class T, typename... Args>
290class SpecificFactory : public Factory<Base, Args...>
291{
292 public:
297 SpecificFactory(const std::string &name) : Factory<Base, Args...>(name) {}
298
299 protected:
304 T *createObject(Args... args)
305 {
306 return new T(args...);
307 }
308};
309
310
311} /* namespace Async */
312
313#endif /* ASYNC_FACTORY_INCLUDED */
314
315/*
316 * This file has not been truncated
317 */
Factory(const Factory< T, Args... > &)=delete
Don't allow copy construction.
static std::string validFactories(void)
Get a list of valid class names.
virtual ~Factory(void)
Destructor.
virtual T * createObject(Args... args)=0
Create and return a new instance of an object.
Factory(const std::string &name)
Constructor.
Factory & operator=(const Factory< T, Args... > &)=delete
Don't allow assignment.
static T * createNamedObject(const std::string &name, Args... args)
Create an instance of the named class.
SpecificFactory(const std::string &name)
Constructor.
T * createObject(Args... args)
Create and return a new instance of an object.
Namespace for the asynchronous programming classes.