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


mvsim
Author(s):
autogenerated on Fri May 7 2021 03:05:51