Chi-Tech
ChiObjectFactory.cc
Go to the documentation of this file.
1#include "ChiObjectFactory.h"
2
3#include "chi_runtime.h"
4#include "chi_log.h"
5
6// ###################################################################
7/**Access to the singleton*/
9{
10 static ChiObjectFactory singleton;
11 return singleton;
12}
13
14// ###################################################################
15/**Returns a constant reference to the object registry.*/
16const std::map<std::string, ChiObjectFactory::ObjectRegistryEntry>&
18{
19 return object_registry_;
20}
21
22// ###################################################################
23/**Checks if the object registry has a specific text key.*/
24bool ChiObjectFactory::RegistryHasKey(const std::string& key) const
25{
26 return object_registry_.count(key) > 0;
27}
28
29// ###################################################################
30/**Makes an object with the given parameters and places on the global
31 * object stack. Returns a handle to the object. The object type is
32 * obtained from a string parameter name `chi_obj_type`.*/
33size_t
35{
36 if (Chi::log.GetVerbosity() >= 2)
37 Chi::log.Log() << "Making object with type from parameters";
38
39 const std::string fname = __PRETTY_FUNCTION__;
40
41 if (not params.Has("chi_obj_type"))
42 throw std::invalid_argument(
43 fname + ": Requires a parameter block with a field called "
44 "\"chi_obj_type\". The given parameter block does not seem to "
45 "have this parameter.");
46
47 const auto type = params.GetParamValue<std::string>("chi_obj_type");
48
49 return MakeRegisteredObjectOfType(type, params);
50}
51
52// ###################################################################
53/**Makes an object with the given parameters and places on the global
54 * object stack. Returns a handle to the object.*/
56 const std::string& type, const chi::ParameterBlock& params) const
57{
58 if (Chi::log.GetVerbosity() >= 2)
59 Chi::log.Log() << "Making object with specified type";
60
61 const std::string fname = __PRETTY_FUNCTION__;
62
63 if (object_registry_.count(type) == 0)
64 throw std::logic_error(fname + ": No registered type \"" + type +
65 "\" found.");
66
67 if (Chi::log.GetVerbosity() >= 2)
68 Chi::log.Log() << "Making object type " << type;
69
70 auto object_entry = object_registry_.at(type);
71
72 ChiLogicalErrorIf(not object_entry.constructor_func,
73 "Object is not constructable since it has no registered "
74 "constructor");
75
76 auto input_params = object_entry.get_in_params_func();
77
78 input_params.SetObjectType(type);
79 input_params.SetErrorOriginScope(type);
80
81 if (Chi::log.GetVerbosity() >= 2)
82 Chi::log.Log() << "Assigning parameters for object " << type;
83
84 input_params.AssignParameters(params);
85
86 if (Chi::log.GetVerbosity() >= 2)
87 Chi::log.Log() << "Constructing object " << type;
88
89 auto new_object = object_entry.constructor_func(input_params);
90
91 new_object->PushOntoStack(new_object);
92
93 if (Chi::log.GetVerbosity() >= 2)
94 Chi::log.Log() << "Done making object type " << type << " with handle "
95 << new_object->StackID();
96
97 return new_object->StackID();
98}
99
100// ##################################################################
101/**Returns the input parameters of a registered object.*/
104{
105 auto iter = object_registry_.find(type);
107 "Object type \"" + type +
108 "\" is not registered in ChiObjectFactory.");
109
110 auto& reg_entry = iter->second;
111
112 return reg_entry.get_in_params_func();
113}
114
115// ##################################################################
116/**Dumps the registry to stdout.*/
118{
119 Chi::log.Log() << "\n\n";
120 for (const auto& [key, entry] : object_registry_)
121 {
122 if (Chi::log.GetVerbosity() == 0)
123 {
124 Chi::log.Log() << key;
125 continue;
126 }
127
128 Chi::log.Log() << "OBJECT_BEGIN " << key;
129
130 if (entry.constructor_func == nullptr)
131 Chi::log.Log() << "NOT_CONSTRUCTIBLE";
132
133 const auto in_params = entry.get_in_params_func();
134 in_params.DumpParameters();
135
136 Chi::log.Log() << "OBJECT_END\n\n";
137 }
138 Chi::log.Log() << "\n\n";
139}
140
141// ##################################################################
142/**Checks that the registry key is available and throws a
143 * `std::logical_error` if it is not.*/
145 const std::string& key, const std::string& calling_function) const
146{
147 if (RegistryHasKey(key))
149 calling_function + ": Attempted to register Object \"" + key +
150 "\" but an object with the same name is already registered.");
151}
#define ChiLogicalErrorIf(condition, message)
#define ChiLogicalError(message)
#define ChiInvalidArgumentIf(condition, message)
static chi::ChiLog & log
Definition: chi_runtime.h:81
void DumpRegister() const
Dumps the object registry to stdout.
bool RegistryHasKey(const std::string &key) const
void AssertRegistryKeyAvailable(const std::string &key, const std::string &calling_function) const
size_t MakeRegisteredObject(const chi::ParameterBlock &params) const
std::map< std::string, ObjectRegistryEntry > object_registry_
chi::InputParameters GetRegisteredObjectParameters(const std::string &type) const
const std::map< std::string, ObjectRegistryEntry > & Registry() const
size_t MakeRegisteredObjectOfType(const std::string &type, const chi::ParameterBlock &params) const
static ChiObjectFactory & GetInstance() noexcept
LogStream Log(LOG_LVL level=LOG_0)
Definition: chi_log.cc:35
bool Has(const std::string &param_name) const
T GetParamValue(const std::string &param_name) const