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.
registry.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: Pooyan Dadvand
11 // Ruben Zorrilla
12 //
13 
14 #pragma once
15 
16 // System includes
17 #include <string>
18 #include <iostream>
19 
20 // External includes
21 
22 // Project includes
23 #include "includes/registry_item.h"
26 
27 namespace Kratos
28 {
29 
32 
35 
41 class KRATOS_API(KRATOS_CORE) Registry final
42 {
43 public:
46 
49 
53 
56 
59 
63 
64 
68 
69  template<typename TItemType, class... TArgumentsList >
71  std::string const& rItemFullName,
72  TArgumentsList&&... Arguments)
73  {
75 
76  const std::lock_guard<LockObject> scope_lock(ParallelUtilities::GetGlobalLock());
77 
78  auto item_path = StringUtilities::SplitStringByDelimiter(rItemFullName, '.');
79  KRATOS_ERROR_IF(item_path.empty()) << "The item full name is empty" << std::endl;
80 
81  RegistryItem* p_current_item = &GetRootRegistryItem();
82 
83  for(std::size_t i = 0 ; i < item_path.size() - 1 ; i++){
84  auto& r_item_name = item_path[i];
85  if(p_current_item->HasItem(r_item_name)){
86  p_current_item = &p_current_item->GetItem(r_item_name);
87  }
88  else{
89  p_current_item = &p_current_item->AddItem<RegistryItem>(r_item_name);
90  }
91  }
92 
93  // I am doing the last one out of the loop to create it with the given type and argument
94  auto& r_item_name = item_path.back();
95  if(p_current_item->HasItem(r_item_name)){
96  KRATOS_ERROR << "The item \"" << rItemFullName << "\" is already registered." << std::endl;
97  }
98  else{
99  p_current_item = &p_current_item->AddItem<TItemType>(r_item_name, std::forward<TArgumentsList>(Arguments)...);
100  }
101 
102  return *p_current_item;
103 
104  KRATOS_CATCH("")
105  }
106 
107  template<typename... Types>
108  static std::string RegistryTemplateToString(Types&&... args) {
109  std::string f_name = (... += ("," + std::to_string(args)));
110  f_name.erase(0,1);
111  return f_name;
112  }
113 
117 
118  static auto begin()
119  {
120  return mspRootRegistryItem->begin();
121  }
122 
123  static auto cbegin()
124  {
125  return mspRootRegistryItem->cbegin();
126  }
127 
128  static auto end()
129  {
130  return mspRootRegistryItem->end();
131  }
132 
133  static auto const cend()
134  {
135  return mspRootRegistryItem->cend();
136  }
137 
138  static RegistryItem& GetItem(std::string const& rItemFullName);
139 
140  template<typename TDataType>
141  static TDataType const& GetValue(std::string const& rItemFullName)
142  {
143  return GetItem(rItemFullName).GetValue<TDataType>();
144  }
145 
146  template<typename TDataType, typename TCastType>
147  static typename std::enable_if<std::is_base_of<TDataType, TCastType>::value, TCastType>::type const GetValueAs(std::string const& rItemFullName)
148  {
149  return GetItem(rItemFullName).GetValueAs<TDataType, TCastType>();
150  }
151 
152  static void RemoveItem(std::string const& ItemName);
153 
157 
158  static std::size_t size();
159 
160  static bool HasItem(std::string const& rItemFullName);
161 
162  static bool HasValue(std::string const& rItemFullName);
163 
164  static bool HasItems(std::string const& rItemFullName);
165 
169 
171  std::string Info() const;
172 
174  void PrintInfo(std::ostream& rOStream) const;
175 
177  void PrintData(std::ostream& rOStream) const;
178 
179  std::string ToJson(std::string const& Indentation) const;
180 
184 
185 
187 private:
190 
191  static RegistryItem* mspRootRegistryItem;
192 
196 
197 
201 
202 
206 
207 
211 
212  static RegistryItem& GetRootRegistryItem();
213 
214  static std::vector<std::string> SplitFullName(std::string const& FullName);
215 
219 
220 
224 
226  Registry& operator=(Registry const& rOther);
227 
229  Registry(Registry const& rOther);
230 
232 }; // Class Registry
233 
235 
238 
239 
243 
244 
246 inline std::istream& operator >> (
247  std::istream& rIStream,
248  Registry& rThis);
249 
251 inline std::ostream& operator << (
252  std::ostream& rOStream,
253  const Registry& rThis)
254 {
255  rThis.PrintInfo(rOStream);
256  rOStream << std::endl;
257  rThis.PrintData(rOStream);
258 
259  return rOStream;
260 }
261 
264 
265 } // namespace Kratos.
PeriodicInterfaceProcess & operator=(const PeriodicInterfaceProcess &)=delete
std::string Info() const override
Turn back information as a string.
Definition: periodic_interface_process.hpp:93
static LockObject & GetGlobalLock()
Returns the global lock Global lock that can be used for critical sections.
Definition: parallel_utilities.cpp:125
Kratos base registry This class is intended to act as global registry Each time the AddItem method is...
Definition: registry.h:42
Registry()
Default constructor.
Definition: registry.h:55
static auto end()
Definition: registry.h:128
static std::string RegistryTemplateToString(Types &&... args)
Definition: registry.h:108
static auto cbegin()
Definition: registry.h:123
~Registry()
Destructor.
Definition: registry.h:58
KRATOS_CLASS_POINTER_DEFINITION(Registry)
Pointer definition of Registry.
static TDataType const & GetValue(std::string const &rItemFullName)
Definition: registry.h:141
void PrintInfo(std::ostream &rOStream) const
Print information about this object.
Definition: registry.cpp:124
static auto begin()
Definition: registry.h:118
static std::enable_if< std::is_base_of< TDataType, TCastType >::value, TCastType >::type const GetValueAs(std::string const &rItemFullName)
Definition: registry.h:147
static auto const cend()
Definition: registry.h:133
void PrintData(std::ostream &rOStream) const
Print object's data.
Definition: registry.cpp:129
static RegistryItem & AddItem(std::string const &rItemFullName, TArgumentsList &&... Arguments)
Definition: registry.h:70
The registry item to be stored by Registry class. It is the base class for some more specific ones.
Definition: registry_item.h:44
RegistryItem const & GetItem(std::string const &rItemName) const
Definition: registry_item.cpp:154
bool HasItem(std::string const &rItemName) const
Definition: registry_item.cpp:134
RegistryItem & AddItem(std::string const &ItemName, TArgumentsList &&... Arguments)
Definition: registry_item.h:195
#define KRATOS_CATCH(MoreInfo)
Definition: define.h:110
#define KRATOS_TRY
Definition: define.h:109
#define KRATOS_ERROR
Definition: exception.h:161
#define KRATOS_ERROR_IF(conditional)
Definition: exception.h:162
std::vector< std::string > SplitStringByDelimiter(const std::string &rString, const char Delimiter)
This method splits a string by a delimiter.
Definition: string_utilities.cpp:144
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
std::istream & operator>>(std::istream &rIStream, LinearMasterSlaveConstraint &rThis)
input stream function
std::ostream & operator<<(std::ostream &rOStream, const LinearMasterSlaveConstraint &rThis)
output stream function
Definition: linear_master_slave_constraint.h:432
type
Definition: generate_gid_list_file.py:35
args
Definition: generate_gid_list_file.py:37
Registry
Definition: __init__.py:106
integer i
Definition: TensorModule.f:17