KratosMultiphysics
KRATOS Multiphysics (Kratos) is a framework for building parallel, multi-disciplinary simulation software, aiming at modularity, extensibility, and high performance. Kratos is written in C++, and counts with an extensive Python interface.
factory.h
Go to the documentation of this file.
1 // | / |
2 // ' / __| _` | __| _ \ __|
3 // . \ | ( | | ( |\__ `
4 // _|\_\_| \__,_|\__|\___/ ____/
5 // Multi-Physics
6 //
7 // License: BSD License
8 // Kratos default license: kratos/license.txt
9 //
10 // Main authors: Vicente Mataix Ferrandiz
11 //
12 
13 #if !defined(KRATOS_FACTORY_H_INCLUDED )
14 #define KRATOS_FACTORY_H_INCLUDED
15 
16 // System includes
17 
18 // External includes
19 
20 // Project includes
21 #include "includes/define.h"
24 
25 namespace Kratos
26 {
29 
33 
42 {
43 public:
46 
49 
53 
56  explicit FactoryBase(){}
57 
60  virtual ~FactoryBase(){}
61 
65 
71  virtual bool Has(const std::string& rClassName) const
72  {
73  KRATOS_ERROR << "Methods must be implemented in the derived class" << std::endl;
74  return false;
75  }
76 
80 
82  virtual std::string Info() const
83  {
84  return "FactoryBase";
85  }
86 
88  virtual void PrintInfo(std::ostream& rOStream) const
89  {
90  rOStream << this->Info() << std::endl;
91  }
92 
94  virtual void PrintData(std::ostream& rOStream) const
95  {
96  }
97 };
98 
107 template<typename TClass>
108 class Factory
109  : public FactoryBase
110 {
111 public:
114 
116  typedef TClass ClassType;
117 
120 
124 
127  explicit Factory(){}
128 
131  virtual ~Factory() = default;
132 
136 
140 
146  bool Has(const std::string& rClassName) const override
147  {
148  return KratosComponents<ClassType>::Has( rClassName );
149  }
150 
157  template<typename... TArgumentsType >
158  typename ClassType::Pointer Create(TArgumentsType&&... Arguments) const
159  {
160  std::tuple<TArgumentsType...> args(Arguments...);
161  constexpr std::size_t args_size = std::tuple_size<std::tuple<TArgumentsType...>>::value;
162  Kratos::Parameters Settings = std::get<args_size - 1>(args);
163  const std::string& r_name = Settings["name"].GetString();
164  KRATOS_ERROR_IF_NOT(Has( r_name )) << "Trying to construct a class with type name= " << r_name << "\n" <<
165  "Which does not exist. The list of available options (for currently loaded applications) are: \n" <<
166  KratosComponents<ClassType>() << std::endl;
167  const ClassType& aux = KratosComponents<ClassType>::Get( r_name );
168  return aux.Create(std::forward<TArgumentsType>(Arguments)...);
169  }
170 
174 
178 
182 
184  std::string Info() const override
185  {
186  return "Factory";
187  }
188 
190  void PrintInfo(std::ostream& rOStream) const override
191  {
192  rOStream << this->Info() << std::endl;
193  const auto factory_components = KratosComponents<ClassType>::GetComponents();
194  for (const auto& r_comp : factory_components) {
195  rOStream << "\t" << r_comp.first << std::endl;
196  }
197  }
198 
200  virtual void PrintData(std::ostream& rOStream) const override
201  {
202  }
203 
205 };
206 
207 namespace Internals
208 {
209  template <typename TBaseCategoryType>
211  public:
213  };
214 
215  template <typename TClassType, typename TBaseCategoryType>
216  class RegisteredPrototype : public RegisteredPrototypeBase<TBaseCategoryType> {
217  public:
218  explicit RegisteredPrototype(const std::string& rName, const TClassType& rPrototype)
219  {
220  KratosComponents<TBaseCategoryType>::Add(rName, rPrototype);
221  }
222  };
223 
224 }
225 
229 
233 
235 template<class TClass>
236 inline std::ostream& operator << (std::ostream& rOStream,
237  const Factory<TClass>& rThis)
238 {
239  rThis.PrintInfo(rOStream);
240  rOStream << std::endl;
241  rThis.PrintData(rOStream);
242 
243  return rOStream;
244 }
245 
247 
248 } // namespace Kratos.
249 
250 #endif // KRATOS_FACTORY_H_INCLUDED defined
Here we define some common methods.
Definition: factory.h:42
KRATOS_CLASS_POINTER_DEFINITION(FactoryBase)
Pointer definition of FactoryBase.
virtual bool Has(const std::string &rClassName) const
This method checks if the linear class is registered.
Definition: factory.h:71
virtual ~FactoryBase()
Definition: factory.h:60
virtual void PrintData(std::ostream &rOStream) const
Print object's data.
Definition: factory.h:94
virtual std::string Info() const
Turn back information as a string.
Definition: factory.h:82
FactoryBase()
Definition: factory.h:56
virtual void PrintInfo(std::ostream &rOStream) const
Print information about this object.
Definition: factory.h:88
Here we add the functions needed for the registration of class.
Definition: factory.h:110
bool Has(const std::string &rClassName) const override
This method checks if the linear class is registered.
Definition: factory.h:146
std::string Info() const override
Turn back information as a string.
Definition: factory.h:184
ClassType::Pointer Create(TArgumentsType &&... Arguments) const
This method creates a new class.
Definition: factory.h:158
virtual ~Factory()=default
Factory()
Definition: factory.h:127
void PrintInfo(std::ostream &rOStream) const override
Print information about this object.
Definition: factory.h:190
virtual void PrintData(std::ostream &rOStream) const override
Print object's data.
Definition: factory.h:200
TClass ClassType
The definition of the class.
Definition: factory.h:116
KRATOS_CLASS_POINTER_DEFINITION(Factory)
Pointer definition of Factory.
RegisteredPrototype(const std::string &rName, const TClassType &rPrototype)
Definition: factory.h:218
KratosComponents class encapsulates a lookup table for a family of classes in a generic way.
Definition: kratos_components.h:49
static void Add(const std::string &rName, const TComponentType &rComponent)
Adds a component to the collection.
Definition: kratos_components.h:88
static ComponentsContainerType & GetComponents()
Retrieves the ComponentsContainer.
Definition: kratos_components.h:138
static bool Has(const std::string &rName)
Check if the given name exists in the set of components.
Definition: kratos_components.h:161
static const TComponentType & Get(const std::string &rName)
Retrieves a component with the specified name.
Definition: kratos_components.h:114
This class provides to Kratos a data structure for I/O based on the standard of JSON.
Definition: kratos_parameters.h:59
std::string GetString() const
This method returns the string contained in the current Parameter.
Definition: kratos_parameters.cpp:684
#define KRATOS_ERROR
Definition: exception.h:161
#define KRATOS_ERROR_IF_NOT(conditional)
Definition: exception.h:163
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
std::ostream & operator<<(std::ostream &rOStream, const LinearMasterSlaveConstraint &rThis)
output stream function
Definition: linear_master_slave_constraint.h:432
args
Definition: generate_gid_list_file.py:37