NEML2 1.4.0
Loading...
Searching...
No Matches
Factory.h
1// Copyright 2023, UChicago Argonne, LLC
2// All Rights Reserved
3// Software Name: NEML2 -- the New Engineering material Model Library, version 2
4// By: Argonne National Laboratory
5// OPEN SOURCE LICENSE (MIT)
6//
7// Permission is hereby granted, free of charge, to any person obtaining a copy
8// of this software and associated documentation files (the "Software"), to deal
9// in the Software without restriction, including without limitation the rights
10// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11// copies of the Software, and to permit persons to whom the Software is
12// furnished to do so, subject to the following conditions:
13//
14// The above copyright notice and this permission notice shall be included in
15// all copies or substantial portions of the Software.
16//
17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23// THE SOFTWARE.
24#pragma once
25
26#include "neml2/base/Registry.h"
27#include "neml2/base/NEML2Object.h"
28#include "neml2/misc/error.h"
29#include "neml2/base/OptionCollection.h"
30
31namespace neml2
32{
39{
40public:
42 static std::vector<std::string> pipeline;
43
45 static Factory & get();
46
63 template <class T>
64 static std::shared_ptr<T> get_object_ptr(const std::string & section,
65 const std::string & name,
67 bool force_create = false);
68
85 template <class T>
86 static T & get_object(const std::string & section,
87 const std::string & name,
89 bool force_create = false);
90
97 static void load(const OptionCollection & all_options);
98
103 static void clear();
104
110 static void print(std::ostream & os = std::cout);
111
112protected:
119 void create_object(const std::string & section, const OptionSet & options);
120
121private:
123 OptionCollection _all_options;
124
125 // Manufactured objects. The key of the outer map is the section name, and the key of the inner
126 // map is the object name.
127 std::map<std::string, std::map<std::string, std::vector<std::shared_ptr<NEML2Object>>>> _objects;
128};
129
130template <class T>
131inline std::shared_ptr<T>
132Factory::get_object_ptr(const std::string & section,
133 const std::string & name,
135 bool force_create)
136{
137 auto & factory = Factory::get();
138
139 // Easy if it already exists
140 if (!force_create)
141 if (factory._objects.count(section) && factory._objects.at(section).count(name))
142 {
143 const auto & neml2_obj = factory._objects[section][name].back();
144
145 // Error on option clash (these errors should only be developer-facing)
146 for (const auto & [key, value] : additional_options)
147 {
148 neml_assert_dbg(neml2_obj->input_options().contains(key),
149 "While retrieving object named '",
150 name,
151 "' under section ",
152 section,
153 ", additional option '",
154 key,
155 "'");
156 neml_assert_dbg(neml2_obj->input_options().get(key) == *value,
157 "While retrieving object named '",
158 name,
159 "' under section ",
160 section,
161 ", encountered option clash for '",
162 key,
163 "'");
164 }
165
166 // Check for object type
167 auto obj = std::dynamic_pointer_cast<T>(neml2_obj);
168 neml_assert(obj != nullptr,
169 "Found object named ",
170 name,
171 " under section ",
172 section,
173 ". But dynamic cast failed. Did you specify the correct object type?");
174
175 return obj;
176 }
177
178 // Otherwise try to create it
179 for (auto & options : factory._all_options[section])
180 if (options.first == name)
181 {
182 auto new_options = options.second;
184 factory.create_object(section, new_options);
185 break;
186 }
187
188 neml_assert(factory._objects.count(section) && factory._objects.at(section).count(name),
189 "Failed to get object named ",
190 name,
191 " under section ",
192 section);
193
194 return Factory::get_object_ptr<T>(section, name, OptionSet(), false);
195}
196
197template <class T>
198inline T &
199Factory::get_object(const std::string & section,
200 const std::string & name,
202 bool force_create)
203{
204 return *Factory::get_object_ptr<T>(section, name, additional_options, force_create);
205}
206} // namespace neml2
The wrapper (decorator) for cross-referencing unresolved values at parse time.
Definition CrossRef.h:52
Definition Factory.h:39
static void load(const OptionCollection &all_options)
Provide all objects' options to the factory. The factory is ready to manufacture objects after this c...
Definition Factory.cxx:39
void create_object(const std::string &section, const OptionSet &options)
Manufacture a single NEML2Object.
Definition Factory.cxx:45
static std::vector< std::string > pipeline
The sequence which we use to manufacture objects.
Definition Factory.h:42
static std::shared_ptr< T > get_object_ptr(const std::string &section, const std::string &name, const OptionSet &additional_options=OptionSet(), bool force_create=false)
Retrive an object pointer under the given section with the given object name.
Definition Factory.h:132
static void clear()
Destruct all the objects.
Definition Factory.cxx:73
static T & get_object(const std::string &section, const std::string &name, const OptionSet &additional_options=OptionSet(), bool force_create=false)
Retrive an object reference under the given section with the given object name.
Definition Factory.h:199
static Factory & get()
Get the global Factory singleton.
Definition Factory.cxx:32
static void print(std::ostream &os=std::cout)
List all the manufactured objects.
Definition Factory.cxx:58
A data structure that holds options of multiple objects.
Definition OptionCollection.h:38
A custom map-like data structure. The keys are strings, and the values can be nonhomogeneously typed.
Definition OptionSet.h:59
Definition CrossRef.cxx:32
void neml_assert_dbg(bool assertion, Args &&... args)
Definition error.h:85
void neml_assert(bool assertion, Args &&... args)
Definition error.h:73