NEML2 1.4.0
Loading...
Searching...
No Matches
Registry.h
1// Copyright 2024, 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/OptionSet.h"
27
28namespace neml2
29{
32#define register_NEML2_object(classname) \
33 static const char dummyvar_for_registering_obj_##classname = Registry::add<classname>(#classname)
34
37#define register_NEML2_object_alias(classname, registryname) \
38 static const char dummyvar_for_registering_obj_##classname = \
39 Registry::add<classname>(registryname)
40
41class NEML2Object;
42
43using BuildPtr = std::shared_ptr<NEML2Object> (*)(const OptionSet & options);
44
53{
54public:
56 static Registry & get();
57
59 template <typename T>
60 static char add(std::string name)
61 {
62 add_inner(name, utils::demangle(typeid(T).name()), T::expected_options(), &build<T>);
63 return 0;
64 }
65
67 static std::map<std::string, OptionSet> expected_options();
68
70 static OptionSet expected_options(const std::string & name);
71
73 static std::string syntax_type(const std::string & type);
74
76 static BuildPtr builder(const std::string & name);
77
78private:
79 Registry() {}
80
81 static void add_inner(const std::string &, const std::string &, const OptionSet &, BuildPtr);
82
83 template <typename T>
84 static std::shared_ptr<NEML2Object> build(const OptionSet & options)
85 {
86 return std::make_shared<T>(options);
87 }
88
89 std::map<std::string, OptionSet> _expected_options;
90
91 std::map<std::string, BuildPtr> _objects;
92
93 std::map<std::string, std::string> _syntax_type;
94};
95} // namespace neml2
The wrapper (decorator) for cross-referencing unresolved values at parse time.
Definition CrossRef.h:56
A custom map-like data structure. The keys are strings, and the values can be nonhomogeneously typed.
Definition OptionSet.h:100
Definition Registry.h:53
static std::map< std::string, OptionSet > expected_options()
Return the expected options of all registered classs.
Definition Registry.cxx:38
static Registry & get()
Get the global Registry singleton.
Definition Registry.cxx:31
static BuildPtr builder(const std::string &name)
Return the build method pointer of a specific registered class.
Definition Registry.cxx:63
static char add(std::string name)
Add information on a NEML2Object to the registry.
Definition Registry.h:60
static std::string syntax_type(const std::string &type)
Return the syntax type (what appears in the input file) given a registered object's type.
Definition Registry.cxx:56
std::string demangle(const char *name)
Demangle a piece of cxx abi type information.
Definition utils.cxx:33
Definition CrossRef.cxx:30
std::shared_ptr< NEML2Object >(*)(const OptionSet &options) BuildPtr
Definition Registry.h:43