30#include "neml2/misc/errors.h"
31#include "neml2/base/Settings.h"
32#include "neml2/base/NEML2Object.h"
33#include "neml2/base/OptionCollection.h"
51void load_input(
const std::filesystem::path & path,
const std::string & additional_input =
"");
65void reload_input(
const std::filesystem::path & path,
const std::string & additional_input =
"");
90 static bool has_object(
const std::string & section,
const std::string &
name);
109 static std::shared_ptr<T>
get_object_ptr(
const std::string & section,
110 const std::string &
name,
112 bool force_create =
true);
131 static T &
get_object(
const std::string & section,
132 const std::string &
name,
134 bool force_create =
true);
144 static void print(std::ostream & os = std::cout);
160 std::map<std::string, std::map<std::string, std::vector<std::shared_ptr<NEML2Object>>>> _objects;
164inline std::shared_ptr<T>
166 const std::string &
name,
173 throw FactoryException(
"It appears that you are trying to get an object without loading any "
174 "options. Please load options first.");
178 if (factory._objects.count(section) && factory._objects.at(section).count(
name))
179 for (
const auto & neml2_obj : factory._objects[section][
name])
186 auto obj = std::dynamic_pointer_cast<T>(neml2_obj);
189 "Found object named " +
name +
" under section " + section +
190 ". But dynamic cast failed. Did you specify the correct object type?");
199 auto new_options =
options.second;
200 new_options += additional_options;
201 factory.create_object(section, new_options);
205 if (!factory._objects.count(section) || !factory._objects.at(section).count(
name))
208 auto obj = std::dynamic_pointer_cast<T>(factory._objects[section][
name].back());
219 const std::string &
name,
static OptionCollection & options()
Get the global options singleton.
Definition Factory.cxx:66
void create_object(const std::string §ion, const OptionSet &options)
Manufacture a single NEML2Object.
Definition Factory.cxx:87
static T & get_object(const std::string §ion, const std::string &name, const OptionSet &additional_options=OptionSet(), bool force_create=true)
Retrive an object reference under the given section with the given object name.
Definition Factory.h:218
static std::shared_ptr< T > get_object_ptr(const std::string §ion, const std::string &name, const OptionSet &additional_options=OptionSet(), bool force_create=true)
Retrive an object pointer under the given section with the given object name.
Definition Factory.h:165
static void load_options(const OptionCollection &all_options)
Provide all objects' options to the factory. The factory is ready to manufacture objects after this c...
Definition Factory.cxx:80
static bool has_object(const std::string §ion, const std::string &name)
Check if an object with the given name exists under the given section.
Definition Factory.cxx:73
static void clear()
Delete all factories and destruct all the objects.
Definition Factory.cxx:122
static Factory & get()
Get the Factory singleton.
Definition Factory.cxx:59
static void print(std::ostream &os=std::cout)
List all the manufactured objects.
Definition Factory.cxx:109
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:52
Definition DiagnosticsInterface.cxx:30
bool options_compatible(const OptionSet &opts, const OptionSet &additional_opts)
Definition OptionSet.cxx:34
std::string name(ElasticConstant p)
Definition ElasticityConverter.cxx:30
void reload_input(const std::filesystem::path &path, const std::string &additional_input)
Similar to neml2::load_input, but additionally clear the Factory before loading the options,...
Definition Factory.cxx:52
void load_input(const std::filesystem::path &path, const std::string &additional_input)
A convenient function to parse all options from an input file.
Definition Factory.cxx:35