PandA-2024.02
Factory.hpp
Go to the documentation of this file.
1 /*
2  *
3  * _/_/_/ _/_/ _/ _/ _/_/_/ _/_/
4  * _/ _/ _/ _/ _/_/ _/ _/ _/ _/ _/
5  * _/_/_/ _/_/_/_/ _/ _/_/ _/ _/ _/_/_/_/
6  * _/ _/ _/ _/ _/ _/ _/ _/ _/
7  * _/ _/ _/ _/ _/ _/_/_/ _/ _/
8  *
9  * ***********************************************
10  * PandA Project
11  * URL: http://panda.dei.polimi.it
12  * Politecnico di Milano - DEIB
13  * System Architectures Group
14  * ***********************************************
15  * Copyright (C) 2004-2024 Politecnico di Milano
16  *
17  * This file is part of the PandA framework.
18  *
19  * The PandA framework is free software; you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation; either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with this program. If not, see <http://www.gnu.org/licenses/>.
31  *
32  */
48 #ifndef FACTORY_HPP
49 #define FACTORY_HPP
50 
51 #include "custom_map.hpp"
52 #include "refcount.hpp"
53 #include "string_manipulation.hpp"
54 
55 template <class Base, class... Args>
56 class Factory
57 {
58  public:
59  template <class... T>
60  static refcount<Base> Create(const std::string& s, T&&... args)
61  {
62  const auto& registry = Registry();
63  const auto it = registry.find(s);
64  if(it != registry.end())
65  {
66  return it->second(std::forward<T>(args)...);
67  }
68  return nullptr;
69  }
70 
71  template <class T>
72  struct Registrar : Base
73  {
74  friend T;
75 
76  static bool RegisterType()
77  {
78  const auto name = GET_CLASS(T);
79  Factory::Registry()[name] = [](Args... args) -> refcount<Base> {
80  return refcount<T>(new T(std::forward<Args>(args)...));
81  };
82  return true;
83  }
84  static bool __registered;
85 
86  private:
87  Registrar(Args... args) : Base(Key{}, std::forward<Args>(args)...)
88  {
89  (void)__registered;
90  }
91  };
92 
93  friend Base;
94 
95  private:
96  class Key
97  {
98  Key() = default;
99 
100  template <class T>
101  friend struct Registrar;
102  };
103  using FuncType = refcount<Base> (*)(Args...);
104  Factory() = default;
105 
106  static auto& Registry()
107  {
109  return registry;
110  }
111 };
112 
113 template <class Base, class... Args>
114 template <class T>
115 bool Factory<Base, Args...>::Registrar<T>::__registered = Factory<Base, Args...>::Registrar<T>::RegisterType();
116 
117 #endif
static refcount< Base > Create(const std::string &s, T &&... args)
Definition: Factory.hpp:60
#define GET_CLASS(obj)
Macro returning the actual type of an object.
friend Base
Definition: Factory.hpp:93
static bool RegisterType()
Definition: Factory.hpp:76
redefinition of map to manage ordered/unordered structures
static bool __registered
Definition: Factory.hpp:84
Auxiliary methods for manipulating string.
Factory()=default
absl::flat_hash_map< T, U, Hash, Eq, Alloc > CustomUnorderedMap
Definition: custom_map.hpp:148
Template definition of refcount.
Registrar(Args... args)
Definition: Factory.hpp:87
static auto & Registry()
Definition: Factory.hpp:106
Template borrowed from the ANTLR library by Terence Parr (http://www.jGuru.com - Software rights: htt...
Definition: refcount.hpp:94

Generated on Mon Feb 12 2024 13:02:56 for PandA-2024.02 by doxygen 1.8.13