ClassFactory.h
Go to the documentation of this file.
1 /*+-------------------------------------------------------------------------+
2  | MultiVehicle simulator (libmvsim) |
3  | |
4  | Copyright (C) 2014 Jose Luis Blanco Claraco (University of Almeria) |
5  | Copyright (C) 2017 Borys Tymchenko (Odessa Polytechnic University) |
6  | Distributed under GNU General Public License version 3 |
7  | See <http://www.gnu.org/licenses/> |
8  +-------------------------------------------------------------------------+ */
9 #pragma once
10 
11 #include <mvsim/basic_types.h>
12 #include <map>
13 #include <stdexcept>
14 #include <string>
15 
16 namespace mvsim
17 {
20 template <class CLASS, typename ARG1 = void, typename ARG2 = int>
22 {
23  public:
24  struct TClassData
25  {
26  CLASS* (*ptr_factory1)(ARG1);
27  CLASS* (*ptr_factory2)(ARG1, ARG2);
28  TClassData() : ptr_factory1(NULL), ptr_factory2(NULL) {}
29  };
30 
31  void do_register(const std::string& class_name, const TClassData& data)
32  {
33  m_classes[class_name] = data;
34  }
35 
36  CLASS* create(const std::string& class_name, ARG1 a1) const
37  {
38  typename std::map<std::string, TClassData>::const_iterator it =
39  m_classes.find(class_name);
40  if (it == m_classes.end())
41  throw std::runtime_error(
42  (std::string("ClassFactory: Unknown class ") + class_name)
43  .c_str());
44  if (!it->second.ptr_factory1)
45  throw std::runtime_error(
46  (std::string("ClassFactory: factory(1) pointer is NULL for ") +
47  class_name)
48  .c_str());
49  return (*it->second.ptr_factory1)(a1);
50  }
51  CLASS* create(const std::string& class_name, ARG1 a1, ARG2 a2) const
52  {
53  typename std::map<std::string, TClassData>::const_iterator it =
54  m_classes.find(class_name);
55  if (it == m_classes.end())
56  throw std::runtime_error(
57  (std::string("ClassFactory: Unknown class ") + class_name)
58  .c_str());
59  if (!it->second.ptr_factory2)
60  throw std::runtime_error(
61  (std::string("ClassFactory: factory(2) pointer is NULL for ") +
62  class_name)
63  .c_str());
64  return (*it->second.ptr_factory2)(a1, a2);
65  }
66 
67  private:
68  std::map<std::string, TClassData> m_classes;
69 }; // end class
70 
71 #define DECLARES_REGISTER_CLASS1(CLASS_NAME, BASE_CLASS, ARG1) \
72  public: \
73  static BASE_CLASS* Create(ARG1 a1) { return new CLASS_NAME(a1); }
74 #define DECLARES_REGISTER_CLASS2(CLASS_NAME, BASE_CLASS, ARG1, ARG2) \
75  public: \
76  static BASE_CLASS* Create(ARG1 a1, ARG2 a2) \
77  { \
78  return new CLASS_NAME(a1, a2); \
79  }
80 
81 #define REGISTER_CLASS1(FACTORY_TYPE, FACTORY_OBJ, TEXTUAL_NAME, CLASS_NAME) \
82  { \
83  FACTORY_TYPE::TClassData data; \
84  data.ptr_factory1 = &CLASS_NAME::Create; \
85  FACTORY_OBJ.do_register(TEXTUAL_NAME, data); \
86  }
87 
88 #define REGISTER_CLASS2(FACTORY_TYPE, FACTORY_OBJ, TEXTUAL_NAME, CLASS_NAME) \
89  { \
90  FACTORY_TYPE::TClassData data; \
91  data.ptr_factory2 = &CLASS_NAME::Create; \
92  FACTORY_OBJ.do_register(TEXTUAL_NAME, data); \
93  }
94 }
void do_register(const std::string &class_name, const TClassData &data)
Definition: ClassFactory.h:31
CLASS * create(const std::string &class_name, ARG1 a1) const
Definition: ClassFactory.h:36
CLASS *(* ptr_factory2)(ARG1, ARG2)
Definition: ClassFactory.h:27
std::map< std::string, TClassData > m_classes
Definition: ClassFactory.h:68
CLASS * create(const std::string &class_name, ARG1 a1, ARG2 a2) const
Definition: ClassFactory.h:51


mvsim
Author(s):
autogenerated on Thu Jun 6 2019 19:36:40