ppap4lmp  0.7.2
gen_dict.cpp
Go to the documentation of this file.
1 
8 #include "gen_dict.h"
9 #include "../utils/runtime_error.h"
10 
11 namespace ut = utils;
12 
13 /* ------------------------------------------------------------------ */
14 
16  const Map<Str,ShPtr<Generator>> &generator_dict_)
17 {
18  generator_dict = generator_dict_;
19 
20  for (const auto &item : generator_dict)
21  {
22  merge_update_chain(item.second->get_update_chain());
23  }
24 }
25 
26 /* ------------------------------------------------------------------ */
27 
29  const Json &name)
30 {
31  if (!name.is_string())
32  {
33  ut::runtime_error("Rejection of non-string Json");
34  }
35 
36  return std::dynamic_pointer_cast<Element>(
37  generator_dict[name.get<Str>()]);
38 }
39 
40 /* ------------------------------------------------------------------ */
41 
43  const Json &name)
44 {
45  if (!name.is_string())
46  {
47  ut::runtime_error("Rejection of non-string Json");
48  }
49 
50  return generator_dict[name.get<Str>()];
51 }
52 
53 /* ------------------------------------------------------------------ */
54 
56  const Str &classname)
57 {
58  for (const auto &item : generator_dict)
59  {
60  item.second->accessed_by_instance_of(classname);
61  }
62 }
63 
64 /* ------------------------------------------------------------------ */
65 
67 {
68  Set<Str> keys;
69 
70  for (const auto &item : generator_dict)
71  {
72  keys.insert(item.first);
73  }
74 
75  return keys;
76 }
ShPtr< Element > ElPtr
An alias for a shared pointer of Element class.
Definition: element.h:378
This file has a definition of GenDict class.
virtual ElPtr get_element(const Json &name=nullptr) override
Get a value in generator_dict as a shared pointer of Element class.
Definition: gen_dict.cpp:28
std::string Str
Str is an alias for string.
Definition: std.h:21
nlohmann::json Json
Json is an alias for nlohmann::json.
Definition: json.h:22
Map< Str, ShPtr< Generator > > generator_dict
Definition: gen_dict.h:27
std::unordered_set< T > Set
Set is an alias for unordered set (same as set in Python).
Definition: std.h:49
void merge_update_chain(const Vec< UpdatePair > &new_chain)
Definition: generator.cpp:15
virtual void accessed_by_instance_of(const Str &classname) override
Set class name of an instance calling Element::check_required_keys.
Definition: gen_dict.cpp:55
Element inherits Generator class and contains data element (or entity) as a Json object.
Definition: element.h:42
void runtime_error(const Str &msg)
Raise (for Python) and throw (for C++) a runtime error.
virtual ShPtr< Generator > get_generator(const Json &name=nullptr) override
Get a value in generator_dict as a shared pointer of Generator class.
Definition: gen_dict.cpp:42
Namespace for utility functions.
Definition: join.h:14
GenDict(const Map< Str, ShPtr< Generator >> &generator_dict_)
Constructor of GenDict class.
Definition: gen_dict.cpp:15
std::shared_ptr< T > ShPtr
ShPtr is an alias for shared pointer.
Definition: std.h:16
Set< Str > get_keys()
Get a set of the keys of generator_dict.
Definition: gen_dict.cpp:66
std::unordered_map< T, U > Map
Map is an alias for unordered map (same as dict in Python).
Definition: std.h:38