NEML2 1.4.0
Loading...
Searching...
No Matches
Registry.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/OptionSet.h"
27
28namespace neml2
29{
32#define register_NEML2_object(classname) \
33 static char dummyvar_for_registering_obj_##classname = Registry::add<classname>(#classname)
34
37#define register_NEML2_object_alias(classname, registryname) \
38 static char dummyvar_for_registering_obj_##classname = Registry::add<classname>(registryname)
39
40class NEML2Object;
41
42using BuildPtr = std::shared_ptr<NEML2Object> (*)(const OptionSet & options);
43
52{
53public:
55 static Registry & get();
56
58 template <typename T>
59 static char add(std::string name)
60 {
61 add_inner(name, utils::demangle(typeid(T).name()), T::expected_options(), &build<T>);
62 return 0;
63 }
64
66 static std::map<std::string, OptionSet> expected_options();
67
69 static OptionSet expected_options(const std::string & name);
70
72 static std::string syntax_type(const std::string & type);
73
75 static BuildPtr builder(const std::string & name);
76
77private:
78 Registry() {}
79
80 static void add_inner(const std::string &, const std::string &, const OptionSet &, BuildPtr);
81
82 template <typename T>
83 static std::shared_ptr<NEML2Object> build(const OptionSet & options)
84 {
85 return std::make_shared<T>(options);
86 }
87
88 std::map<std::string, OptionSet> _expected_options;
89
90 std::map<std::string, BuildPtr> _objects;
91
92 std::map<std::string, std::string> _syntax_type;
93};
94} // namespace neml2
The wrapper (decorator) for cross-referencing unresolved values at parse time.
Definition CrossRef.h:52
A custom map-like data structure. The keys are strings, and the values can be nonhomogeneously typed.
Definition OptionSet.h:59
Definition Registry.h:52
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:59
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)
Definition parser_utils.cxx:46
Definition CrossRef.cxx:32
std::shared_ptr< NEML2Object >(*)(const OptionSet &options) BuildPtr
Definition Registry.h:42