factory.h
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * Software License Agreement
4  *
5  * Copyright (c) 2020,
6  * TU Dortmund - Institute of Control Theory and Systems Engineering.
7  * All rights reserved.
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <https://www.gnu.org/licenses/>.
21  *
22  * Authors: Christoph Rösmann
23  *********************************************************************/
24 
25 #ifndef SRC_CORE_INCLUDE_CORBO_CORE_FACTORY_H_
26 #define SRC_CORE_INCLUDE_CORBO_CORE_FACTORY_H_
27 
28 #include <corbo-core/console.h>
29 #include <corbo-core/macros.h>
30 #include <functional>
31 #include <memory>
32 #include <string>
33 #include <type_traits>
34 #include <unordered_map>
35 
36 namespace corbo {
37 
67 template <typename Base>
68 class Factory
69 {
70  public:
72  static Factory& instance()
73  {
74  static Factory factory;
75  return factory;
76  }
77 
83  bool hasObject(const std::string& name) { return _object_map.count(name) > 0; }
84 
92  template <typename Derived = Base>
93  std::shared_ptr<Derived> create(const std::string& name, bool print_error = true) const
94  {
95  static_assert(std::is_base_of<Base, Derived>::value, "Factory::create(): specified dervied class type is an actual subclass of base.");
96 
97  auto obj = _object_map.find(name);
98  if (obj == _object_map.end())
99  {
100  PRINT_ERROR_COND(print_error, "Factory<" << typeid(Base).name() << ">::create():: unkown object '" << name << "'.");
101  return std::shared_ptr<Derived>();
102  }
103 
104  std::shared_ptr<Derived> derived_ptr = std::dynamic_pointer_cast<Derived>(obj->second->getInstance());
105  PRINT_ERROR_COND(!derived_ptr && print_error, "Factory::create():: cannot cast base object " << typeid(Base).name() << " to derived object "
106  << typeid(Derived).name() << ".");
107  return derived_ptr;
108  }
109 
116  bool registerObject(const std::string& name, std::shared_ptr<Base> object_type)
117  {
118  _object_map[name] = object_type;
119  return true;
120  }
121 
123  void printObjects() const
124  {
125  for (auto it = _object_map.begin(); it != _object_map.end(); ++it) PRINT_INFO("* " << it->first);
126  }
127 
128  const std::unordered_map<std::string, std::shared_ptr<Base>>& getObjectMap() const { return _object_map; }
129 
130  private:
132  std::unordered_map<std::string, std::shared_ptr<Base>> _object_map;
133 };
134 
135 #define FACTORY_REGISTER_OBJECT_ID(type, base, id) \
136  static const bool corbo_CAT(corbo_CAT(type, __regged), id) = \
137  Factory<base>::instance().registerObject(corbo_STRINGIZE(type), std::make_shared<type>());
138 
139 #define FACTORY_REGISTER_OBJECT(type, base) FACTORY_REGISTER_OBJECT_ID(type, base, 0)
140 
149 template <typename Object>
150 std::shared_ptr<Object> create_from_factory(const std::string& name)
151 {
152  return Object::getFactory().template create<Object>(name);
153 }
154 
155 } // namespace corbo
156 
157 #endif // SRC_CORE_INCLUDE_CORBO_CORE_FACTORY_H_
bool registerObject(const std::string &name, std::shared_ptr< Base > object_type)
Register a new object.
Definition: factory.h:116
Generic factory object.
Definition: factory.h:68
#define PRINT_ERROR_COND(cond, msg)
Print msg-stream only if cond == true.
Definition: console.h:186
const std::unordered_map< std::string, std::shared_ptr< Base > > & getObjectMap() const
Definition: factory.h:128
bool hasObject(const std::string &name)
Check if a specified object (name) has been registered.
Definition: factory.h:83
std::shared_ptr< Object > create_from_factory(const std::string &name)
Helper function to create new (derived) objects from factory.
Definition: factory.h:150
static Factory & instance()
< Retrieve static instance of the factory
Definition: factory.h:72
std::unordered_map< std::string, std::shared_ptr< Base > > _object_map
map of identifiers and their corresponding objects (default-constructed)
Definition: factory.h:132
std::shared_ptr< Derived > create(const std::string &name, bool print_error=true) const
Create a shared instance of the desired object.
Definition: factory.h:93
void printObjects() const
Print registered object names to console.
Definition: factory.h:123
#define PRINT_INFO(msg)
Print msg-stream.
Definition: console.h:117


control_box_rst
Author(s): Christoph Rösmann
autogenerated on Mon Feb 28 2022 22:06:51