NEML2 2.0.0
Loading...
Searching...
No Matches
OptionSet.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 <map>
28#include <string>
29#include <memory>
30
31#include "neml2/base/Option.h"
32#include "neml2/misc/errors.h"
33
34namespace neml2
35{
36// Forward decl
37class OptionSet;
38class LabeledAxisAccessor;
39
40bool options_compatible(const OptionSet & opts, const OptionSet & additional_opts);
41
42// Streaming operators
43std::ostream & operator<<(std::ostream & os, const OptionSet & p);
44
51{
52public:
53 OptionSet() = default;
54
55 OptionSet(const OptionSet &);
56 OptionSet(OptionSet &&) noexcept;
57 OptionSet & operator=(const OptionSet &);
58 OptionSet & operator=(OptionSet &&) noexcept;
59 virtual ~OptionSet() = default;
60
62
69 void operator+=(const OptionSet & source);
70 void operator+=(OptionSet && source);
72
74 const std::string & name() const { return _metadata.name; }
76 std::string & name() { return _metadata.name; }
78 const std::string & type() const { return _metadata.type; }
80 std::string & type() { return _metadata.type; }
82 const std::string & path() const { return _metadata.path; }
84 std::string & path() { return _metadata.path; }
86 const std::string & doc() const { return _metadata.doc; }
88 std::string & doc() { return _metadata.doc; }
90 const std::string & section() const { return _metadata.section; }
92 std::string & section() { return _metadata.section; }
93
95 bool contains(const std::string &) const;
96
98 bool user_specified(const std::string & name) const;
99
101 std::size_t size() const { return _values.size(); }
102
104 void clear();
105
107 std::string to_str() const;
108
113 template <typename T>
114 T get(const std::string &) const;
115
117 const OptionBase & get(const std::string &) const;
118
120
125 template <typename T, FType f = FType::NONE>
126 T & set(const std::string &);
127 OptionBase & set(const std::string &);
129
131 LabeledAxisAccessor & set_input(const std::string &);
133 LabeledAxisAccessor & set_output(const std::string &);
135 template <typename T>
136 T & set_parameter(const std::string &);
138 template <typename T>
139 T & set_buffer(const std::string &);
140
142 using map_type = std::map<std::string, std::unique_ptr<OptionBase>, std::less<>>;
144 using iterator = map_type::iterator;
146 using const_iterator = map_type::const_iterator;
147
149 iterator begin();
151 const_iterator begin() const;
153 iterator end();
155 const_iterator end() const;
156
157protected:
161 struct Metadata
162 {
176 std::string name = "";
191 std::string type = "";
210 std::string path = "";
221 std::string doc = "";
229 std::string section = "";
231
234};
235
237// Implementation
239
240template <typename T>
241T
242OptionSet::get(const std::string & name) const
243{
244 if (!this->contains(name))
245 throw NEMLException("ERROR: no option named \"" + name + "\" found.\n\nKnown options:\n" +
246 to_str());
247
248 auto ptr = dynamic_cast<Option<T> *>(_values.at(name).get());
249 return ptr->get();
250}
251
252template <typename T, FType F>
253T &
254OptionSet::set(const std::string & name)
255{
256 if (!this->contains(name))
257 _values[name] = std::make_unique<Option<T>>(name);
258 auto ptr = dynamic_cast<Option<T> *>(_values[name].get());
259 ptr->ftype() = F;
260 return ptr->set();
261}
262
263template <typename T>
264T &
265OptionSet::set_parameter(const std::string & name)
266{
268}
269
270template <typename T>
271T &
272OptionSet::set_buffer(const std::string & name)
273{
275}
276} // namespace neml2
The accessor containing all the information needed to access an item in a LabeledAxis.
Definition LabeledAxisAccessor.h:56
Definition errors.h:34
Definition OptionBase.h:44
A custom map-like data structure. The keys are strings, and the values can be nonhomogeneously typed.
Definition OptionSet.h:51
map_type::iterator iterator
Option map iterator.
Definition OptionSet.h:144
map_type _values
Data structure to map names with values.
Definition OptionSet.h:233
T & set_buffer(const std::string &)
Convenient method to request a buffer.
Definition OptionSet.h:272
const std::string & name() const
A readonly reference to the option set's name.
Definition OptionSet.h:74
OptionSet()=default
const std::string & type() const
A readonly reference to the option set's type.
Definition OptionSet.h:78
LabeledAxisAccessor & set_output(const std::string &)
Definition OptionSet.cxx:94
std::string to_str() const
Print the contents.
Definition OptionSet.cxx:143
T get(const std::string &) const
Definition OptionSet.h:242
map_type::const_iterator const_iterator
Constant option map iterator.
Definition OptionSet.h:146
std::map< std::string, std::unique_ptr< OptionBase >, std::less<> > map_type
The type of the map that we store internally.
Definition OptionSet.h:142
std::string & name()
A writable reference to the option set's name.
Definition OptionSet.h:76
LabeledAxisAccessor & set_input(const std::string &)
Definition OptionSet.cxx:88
std::string & type()
A writable reference to the option set's type.
Definition OptionSet.h:80
const std::string & doc() const
A readonly reference to the option set's docstring.
Definition OptionSet.h:86
const std::string & section() const
A readonly reference to the option set's section.
Definition OptionSet.h:90
T & set_parameter(const std::string &)
Convenient method to request a parameter.
Definition OptionSet.h:265
iterator begin()
Iterator pointing to the beginning of the set of options.
Definition OptionSet.cxx:189
iterator end()
Iterator pointing to the end of the set of options.
Definition OptionSet.cxx:201
const std::string & path() const
A readonly reference to the option set's path.
Definition OptionSet.h:82
std::string & doc()
A writable reference to the option set's docstring.
Definition OptionSet.h:88
bool user_specified(const std::string &name) const
Definition OptionSet.cxx:53
struct neml2::OptionSet::Metadata _metadata
void clear()
Clear internal data structures & frees any allocated memory.
Definition OptionSet.cxx:100
std::string & path()
A writable reference to the option set's path.
Definition OptionSet.h:84
std::string & section()
A writable reference to the option set's section.
Definition OptionSet.h:92
std::size_t size() const
Definition OptionSet.h:101
bool contains(const std::string &) const
Definition OptionSet.cxx:47
T & set(const std::string &)
Definition OptionSet.h:254
Definition Option.h:70
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
std::ostream & operator<<(std::ostream &os, const EnumSelection &es)
Definition EnumSelection.cxx:31
Definition OptionSet.h:162
std::string path
Path to the option set.
Definition OptionSet.h:210
std::string type
Type of the option set.
Definition OptionSet.h:191
std::string name
Name of the option set.
Definition OptionSet.h:176
std::string section
Which NEML2 input file section this object belongs to.
Definition OptionSet.h:229
std::string doc
Option set's doc string.
Definition OptionSet.h:221