NEML2 2.0.0
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
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
25#pragma once
26
27#include <memory>
28#include <map>
29
30#include "neml2/misc/string_utils.h"
31
32namespace neml2
33{
36#define register_NEML2_object(classname) \
37 static const char dummyvar_for_registering_obj_##classname = Registry::add<classname>(#classname)
38
41#define register_NEML2_object_alias(classname, registryname) \
42 static const char dummyvar_for_registering_obj_##classname = \
43 Registry::add<classname>(registryname)
44
45class OptionSet;
46class NEML2Object;
47
48using BuildPtr = std::shared_ptr<NEML2Object> (*)(const OptionSet & options);
49
57class Registry
58{
59public:
61 static Registry & get();
62
64 template <typename T>
65 static char add(const std::string & name)
66 {
67 add_inner(name, utils::demangle(typeid(T).name()), T::expected_options(), &build<T>);
68 return 0;
69 }
70
72 static std::map<std::string, OptionSet> expected_options();
73
75 static OptionSet expected_options(const std::string & name);
76
78 static std::string syntax_type(const std::string & type);
79
81 static BuildPtr builder(const std::string & name);
82
83private:
84 Registry() = default;
85
86 static void add_inner(const std::string &, const std::string &, const OptionSet &, BuildPtr);
87
88 template <typename T>
89 static std::shared_ptr<NEML2Object> build(const OptionSet & options)
90 {
91 return std::make_shared<T>(options);
92 }
93
94 std::map<std::string, OptionSet> _expected_options;
95
96 std::map<std::string, BuildPtr> _objects;
97
98 std::map<std::string, std::string> _syntax_type;
99};
100} // namespace neml2
The base class of all "manufacturable" objects in the NEML2 library.
Definition NEML2Object.h:42
A custom map-like data structure. The keys are strings, and the values can be nonhomogeneously typed.
Definition OptionSet.h:52
Definition Registry.h:58
static std::map< std::string, OptionSet > expected_options()
Return the expected options of all registered classs.
Definition Registry.cxx:40
static Registry & get()
Get the global Registry singleton.
Definition Registry.cxx:33
static BuildPtr builder(const std::string &name)
Return the build method pointer of a specific registered class.
Definition Registry.cxx:65
static char add(const std::string &name)
Add information on a NEML2Object to the registry.
Definition Registry.h:65
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:58
std::string demangle(const char *name)
Demangle a piece of cxx abi type information.
Definition string_utils.cxx:32
Definition DiagnosticsInterface.cxx:30
std::string name(ElasticConstant p)
Definition ElasticityConverter.cxx:30
std::shared_ptr< NEML2Object >(*)(const OptionSet &options) BuildPtr
Definition Registry.h:48