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_item.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 // Carlos Roig
13 //
14 
15 #pragma once
16 
17 // System includes
18 #include <string>
19 #include <iostream>
20 #include <unordered_map>
21 #include <any>
22 
23 // External includes
24 
25 
26 // Project includes
27 #include "includes/define.h"
28 
29 namespace Kratos
30 {
35 
37 
43 class KRATOS_API(KRATOS_CORE) RegistryItem
44 {
45 public:
48 
51 
53  using SubRegistryItemType = std::unordered_map<std::string, Kratos::shared_ptr<RegistryItem>>;
54 
57 
60  {
61  public:
64 
65  using BaseIterator = SubRegistryItemType::const_iterator;
66  using iterator_category = std::forward_iterator_tag;
67  using difference_type = BaseIterator::difference_type;
68  using value_type = SubRegistryItemType::key_type;
69  using const_pointer = const value_type*;
70  using const_reference = const value_type&;
71 
75 
77  {}
78 
80  : mIterator(Iterator)
81  {}
82 
84  : mIterator(rIterator.mIterator)
85  {}
86 
90 
92  {
93  this->mIterator = rIterator.mIterator;
94  return *this;
95  }
96 
98  {
99  return mIterator->first;
100  }
101 
103  {
104  return &(mIterator->first);
105  }
106 
108  {
109  ++mIterator;
110  return *this;
111  }
112 
114  {
116  ++(*this);
117  return tmp;
118  }
119 
120  bool operator==(const KeyReturnConstIterator& rIterator) const
121  {
122  return this->mIterator == rIterator.mIterator;
123  }
124 
125  bool operator!=(const KeyReturnConstIterator& rIterator) const
126  {
127  return this->mIterator != rIterator.mIterator;
128  }
129 
131  private:
134 
135  BaseIterator mIterator;
136 
138  };
139 
143 
145  RegistryItem() = delete;
146 
148  RegistryItem(const std::string& rName)
149  : mName(rName),
151  mGetValueStringMethod(&RegistryItem::GetRegistryItemType) {}
152 
154  template <typename TItemType, typename... TArgs>
156  const std::string &rName,
157  const std::function<std::shared_ptr<TItemType>(TArgs...)> &rValue)
158  : mName(rName),
159  mpValue(rValue()),
160  mGetValueStringMethod(&RegistryItem::GetItemString<TItemType>) {}
161 
163  template<class TItemType>
165  const std::string& rName,
166  const TItemType& rValue)
167  : mName(rName),
168  mpValue(Kratos::make_shared<TItemType>(rValue)),
169  mGetValueStringMethod(&RegistryItem::GetItemString<TItemType>) {}
170 
172  template<class TItemType>
174  const std::string& rName,
175  const shared_ptr<TItemType>& pValue)
176  : mName(rName),
177  mpValue(pValue),
178  mGetValueStringMethod(&RegistryItem::GetItemString<TItemType>) {}
179 
180  // Copy constructor deleted
181  RegistryItem(RegistryItem const& rOther) = delete;
182 
184  ~RegistryItem() = default;
185 
187  RegistryItem& operator=(RegistryItem& rOther) = delete;
188 
192 
193  // Adding sub value item
194  template<typename TItemType, class... TArgumentsList>
196  std::string const& ItemName,
197  TArgumentsList&&... Arguments)
198  {
199  KRATOS_ERROR_IF(this->HasItem(ItemName))
200  << "The RegistryItem '" << this->Name() << "' already has an item with name "
201  << ItemName << "." << std::endl;
202 
203  using ValueType = typename std::conditional<std::is_same<TItemType, RegistryItem>::value, SubRegistryItemFunctor, SubValueItemFunctor<TItemType>>::type;
204 
205  auto insert_result = GetSubRegistryItemMap().emplace(
206  std::make_pair(
207  ItemName,
208  ValueType::Create(ItemName, std::forward<TArgumentsList>(Arguments)...)
209  )
210  );
211 
212  KRATOS_ERROR_IF_NOT(insert_result.second)
213  << "Error in inserting '" << ItemName
214  << "' in registry item with name '" << this->Name() << "'." << std::endl;
215 
216  return *insert_result.first->second;
217  }
218 
222 
223  SubRegistryItemType::iterator begin();
224 
225  SubRegistryItemType::const_iterator cbegin() const;
226 
227  SubRegistryItemType::iterator end();
228 
229  SubRegistryItemType::const_iterator cend() const;
230 
231  KeyReturnConstIterator KeyConstBegin() const;
232 
233  KeyReturnConstIterator KeyConstEnd() const;
234 
235  const std::string& Name() const { return mName; }
236 
237  RegistryItem const& GetItem(std::string const& rItemName) const;
238 
239  RegistryItem& GetItem(std::string const& rItemName);
240 
241  template<typename TDataType>
242  TDataType const& GetValue() const
243  {
244  KRATOS_TRY
245 
246  return *(std::any_cast<std::shared_ptr<TDataType>>(mpValue));
247 
248  KRATOS_CATCH("");
249  }
250 
251  template<typename TDataType, typename TCastType>
252  TCastType const& GetValueAs() const
253  {
254  KRATOS_TRY
255 
256  return *std::dynamic_pointer_cast<TCastType>(std::any_cast<std::shared_ptr<TDataType>>(mpValue));
257 
258  KRATOS_CATCH("");
259  }
260 
261  void RemoveItem(std::string const& rItemName);
262 
266 
267  std::size_t size();
268 
269  bool HasValue() const;
270 
271  bool HasItems() const;
272 
273  bool HasItem(std::string const& rItemName) const;
274 
278 
280  std::string Info() const;
281 
283  void PrintInfo(std::ostream& rOStream) const;
284 
286  void PrintData(std::ostream& rOStream) const;
287 
288  std::string ToJson(std::string const& rTabSpacing = "", const std::size_t Level = 0) const;
289 
291 private:
294 
295  std::string mName;
296  std::any mpValue;
297  std::string (RegistryItem::*mGetValueStringMethod)() const;
298 
302 
303  std::string GetRegistryItemType() const
304  {
305  return mpValue.type().name();
306  }
307 
308  template<class TItemType>
309  std::string GetItemString() const
310  {
311  std::stringstream buffer;
312  buffer << this->GetValue<TItemType>();
313  return buffer.str();
314  }
315 
319 
320  class SubRegistryItemFunctor
321  {
322  public:
323  template<class... TArgumentsList>
324  static inline RegistryItem::Pointer Create(
325  std::string const& ItemName,
326  TArgumentsList&&... Arguments)
327  {
328  return Kratos::make_shared<RegistryItem>(ItemName);
329  }
330  };
331 
332  template<typename TItemType>
333  class SubValueItemFunctor
334  {
335  public:
336  template<class... TArgumentsList, class TFunctionType = std::function<std::shared_ptr<TItemType>(TArgumentsList...)>>
337  static inline RegistryItem::Pointer Create(
338  std::string const& ItemName,
339  TFunctionType && Function)
340  {
341  return Kratos::make_shared<RegistryItem>(ItemName, std::forward<TFunctionType>(Function));
342  }
343 
344  template<class... TArgumentsList>
345  static inline RegistryItem::Pointer Create(
346  std::string const& ItemName,
347  TArgumentsList&&... Arguments)
348  {
349  return Kratos::make_shared<RegistryItem>(ItemName, Kratos::make_shared<TItemType>(std::forward<TArgumentsList>(Arguments)...));
350  }
351 
352  };
353 
357 
358  std::string GetValueString() const;
359 
360  SubRegistryItemType& GetSubRegistryItemMap();
361 
362  SubRegistryItemType& GetSubRegistryItemMap() const;
363 
365 
366 }; // Class RegistryItem
367 
371 
373 inline std::istream& operator >> (
374  std::istream& rIStream,
375  RegistryItem& rThis);
376 
378 inline std::ostream& operator << (
379  std::ostream& rOStream,
380  const RegistryItem& rThis)
381 {
382  rThis.PrintInfo(rOStream);
383  rOStream << std::endl;
384  rThis.PrintData(rOStream);
385 
386  return rOStream;
387 }
389 
391 
392 } // namespace Kratos.
std::string Info() const override
Turn back information as a string.
Definition: periodic_interface_process.hpp:93
Custom iterator with key as return type to be used in the Python export.
Definition: registry_item.h:60
KeyReturnConstIterator operator++(int)
Definition: registry_item.h:113
SubRegistryItemType::key_type value_type
Definition: registry_item.h:68
KeyReturnConstIterator & operator++()
Definition: registry_item.h:107
std::forward_iterator_tag iterator_category
Definition: registry_item.h:66
KeyReturnConstIterator()
Definition: registry_item.h:76
KeyReturnConstIterator & operator=(const KeyReturnConstIterator &rIterator)
Definition: registry_item.h:91
KeyReturnConstIterator(const BaseIterator Iterator)
Definition: registry_item.h:79
bool operator==(const KeyReturnConstIterator &rIterator) const
Definition: registry_item.h:120
const value_type * const_pointer
Definition: registry_item.h:69
BaseIterator::difference_type difference_type
Definition: registry_item.h:67
const value_type & const_reference
Definition: registry_item.h:70
KeyReturnConstIterator(const KeyReturnConstIterator &rIterator)
Definition: registry_item.h:83
const_pointer operator->() const
Definition: registry_item.h:102
bool operator!=(const KeyReturnConstIterator &rIterator) const
Definition: registry_item.h:125
SubRegistryItemType::const_iterator BaseIterator
Definition: registry_item.h:65
const_reference operator*() const
Definition: registry_item.h:97
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 & operator=(RegistryItem &rOther)=delete
Assignment operator deleted.
const std::string & Name() const
Definition: registry_item.h:235
TCastType const & GetValueAs() const
Definition: registry_item.h:252
KRATOS_CLASS_POINTER_DEFINITION(RegistryItem)
Pointer definition of RegistryItem.
TDataType const & GetValue() const
Definition: registry_item.h:242
std::unordered_map< std::string, Kratos::shared_ptr< RegistryItem > > SubRegistryItemType
Subregistry item type definition.
Definition: registry_item.h:53
RegistryItem(const std::string &rName, const TItemType &rValue)
Constructor with the name and value.
Definition: registry_item.h:164
RegistryItem(const std::string &rName)
Constructor with the name.
Definition: registry_item.h:148
RegistryItem(const std::string &rName, const shared_ptr< TItemType > &pValue)
Constructor with the name and shared ptr.
Definition: registry_item.h:173
RegistryItem()=delete
Default constructor deleted.
Kratos::shared_ptr< SubRegistryItemType > SubRegistryItemPointerType
Pointer definition of SubRegistryItemType.
Definition: registry_item.h:56
void PrintData(std::ostream &rOStream) const
Print object's data.
Definition: registry_item.cpp:75
RegistryItem & AddItem(std::string const &ItemName, TArgumentsList &&... Arguments)
Definition: registry_item.h:195
RegistryItem(RegistryItem const &rOther)=delete
RegistryItem(const std::string &rName, const std::function< std::shared_ptr< TItemType >(TArgs...)> &rValue)
Constructor with the name and lambda.
Definition: registry_item.h:155
void PrintInfo(std::ostream &rOStream) const
Print information about this object.
Definition: registry_item.cpp:70
~RegistryItem()=default
Destructor.
#define KRATOS_CATCH(MoreInfo)
Definition: define.h:110
#define KRATOS_TRY
Definition: define.h:109
#define KRATOS_ERROR_IF_NOT(conditional)
Definition: exception.h:163
#define KRATOS_ERROR_IF(conditional)
Definition: exception.h:162
end
Definition: DEM_benchmarks.py:180
Modeler::Pointer Create(const std::string &ModelerName, Model &rModel, const Parameters ModelParameters)
Checks if the modeler is registered.
Definition: modeler_factory.cpp:30
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
std::shared_ptr< T > shared_ptr
Definition: smart_pointers.h:27
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
shared_ptr< C > make_shared(Args &&...args)
Definition: smart_pointers.h:40
type
Definition: generate_gid_list_file.py:35
tuple tmp
Definition: generate_total_lagrangian_mixed_volumetric_strain_element.py:98