NEML2 2.0.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
25#pragma once
26
27#include <memory>
28#include <map>
29#include <filesystem>
30
31#include "neml2/misc/string_utils.h"
32#include "neml2/base/OptionSet.h"
33
34namespace neml2
35{
38#define register_NEML2_object(classname) \
39 static const char dummyvar_for_registering_obj_##classname = Registry::add<classname>(#classname)
40
43#define register_NEML2_object_alias(classname, registryname) \
44 static const char dummyvar_for_registering_obj_##classname = \
45 Registry::add<classname>(registryname)
46
47class NEML2Object;
48
49using BuildPtr = std::shared_ptr<NEML2Object> (*)(const OptionSet & options);
50
61
70{
71public:
73 static Registry & get();
74
76 template <typename T>
77 static char add(const std::string & name)
78 {
79 add_inner(name, utils::demangle(typeid(T).name()), T::expected_options(), &build<T>);
80 return 0;
81 }
82
84 static void load(const std::filesystem::path &);
85
87 static const std::map<std::string, NEML2ObjectInfo> & info();
88
90 static const NEML2ObjectInfo & info(const std::string &);
91
92private:
93 Registry() = default;
94
95 static void add_inner(const std::string &, const std::string &, const OptionSet &, BuildPtr);
96
97 template <typename T>
98 static std::shared_ptr<NEML2Object> build(const OptionSet & options)
99 {
100 return std::make_shared<T>(options);
101 }
102
109 std::map<std::string, NEML2ObjectInfo> _info;
110};
111} // namespace neml2
A custom map-like data structure. The keys are strings, and the values can be nonhomogeneously typed.
Definition OptionSet.h:51
Definition Registry.h:70
static const std::map< std::string, NEML2ObjectInfo > & info()
Get information of all registered objects.
Definition Registry.cxx:70
static Registry & get()
Get the global Registry singleton.
Definition Registry.cxx:36
static void load(const std::filesystem::path &)
Load registry from a dynamic library.
Definition Registry.cxx:43
static char add(const std::string &name)
Add information on a NEML2Object to the registry.
Definition Registry.h:77
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:49
Information on a NEML2Object that is registered in the Registry.
Definition Registry.h:53
std::string type_name
The object type demangled from the C++ typeid.
Definition Registry.h:55
OptionSet expected_options
Expected options for this object.
Definition Registry.h:57
BuildPtr build
Build method pointer.
Definition Registry.h:59