NEML2 2.0.0
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
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 <vector>
30#include <memory>
31
32#include "neml2/base/Option.h"
33#include "neml2/misc/errors.h"
34
35namespace neml2
36{
37// Forward decl
38class OptionSet;
40
41bool options_compatible(const OptionSet & opts, const OptionSet & additional_opts);
42
43// Streaming operators
44std::ostream & operator<<(std::ostream & os, const OptionSet & p);
45
52{
53public:
54 OptionSet() = default;
55
56 OptionSet(const OptionSet &);
57 OptionSet(OptionSet &&) noexcept;
58 OptionSet & operator=(const OptionSet &);
59 OptionSet & operator=(OptionSet &&) noexcept;
60 virtual ~OptionSet() = default;
61
63
70 void operator+=(const OptionSet & source);
71 void operator+=(OptionSet && source);
73
75 const std::string & name() const { return _metadata.name; }
77 std::string & name() { return _metadata.name; }
79 const std::string & type() const { return _metadata.type; }
81 std::string & type() { return _metadata.type; }
83 const std::string & path() const { return _metadata.path; }
85 std::string & path() { return _metadata.path; }
87 const std::string & doc() const { return _metadata.doc; }
89 std::string & doc() { return _metadata.doc; }
91 const std::string & section() const { return _metadata.section; }
93 std::string & section() { return _metadata.section; }
94
99 template <typename T>
100 bool contains(const std::string &) const;
101
107 bool contains(const std::string &) const;
108
110 std::size_t size() const { return _values.size(); }
111
113 void clear();
114
116 std::string to_str() const;
117
122 template <typename T>
123 const T & get(const std::string &) const;
124
125 const OptionBase & get(const std::string &) const;
126
128
133 template <typename T, FType f = FType::NONE>
134 T & set(const std::string &);
135 OptionBase & set(const std::string &);
137
139 LabeledAxisAccessor & set_input(const std::string &);
141 LabeledAxisAccessor & set_output(const std::string &);
143 template <typename T>
144 T & set_parameter(const std::string &);
146 template <typename T>
147 T & set_buffer(const std::string &);
148
150 using map_type = std::map<std::string, std::unique_ptr<OptionBase>, std::less<>>;
152 using iterator = map_type::iterator;
154 using const_iterator = map_type::const_iterator;
155
157 iterator begin();
159 const_iterator begin() const;
161 iterator end();
163 const_iterator end() const;
164
165protected:
169 struct Metadata
170 {
184 std::string name = "";
199 std::string type = "";
218 std::string path = "";
229 std::string doc = "";
237 std::string section = "";
239
242};
243
245// Implementation
247
248template <typename T>
249bool
250OptionSet::contains(const std::string & name) const
251{
253 if (it != _values.end())
254 if (dynamic_cast<const Option<T> *>(it->second.get()))
255 return true;
256 return false;
257}
258
259template <typename T>
260const T &
261OptionSet::get(const std::string & name) const
262{
263 if (!this->contains<T>(name))
264 throw NEMLException("ERROR: no option named \"" + name + "\" found.\n\nKnown options:\n" +
265 to_str());
266
267 auto ptr = dynamic_cast<Option<T> *>(_values.at(name).get());
268 return ptr->get();
269}
270
271template <typename T, FType F>
272T &
273OptionSet::set(const std::string & name)
274{
275 if (!this->contains<T>(name))
276 _values[name] = std::make_unique<Option<T>>(name);
277 auto ptr = dynamic_cast<Option<T> *>(_values[name].get());
278 ptr->ftype() = F;
279 return ptr->set();
280}
281
282template <typename T>
283T &
284OptionSet::set_parameter(const std::string & name)
285{
287}
288
289template <typename T>
290T &
291OptionSet::set_buffer(const std::string & name)
292{
294}
295} // 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:52
map_type::iterator iterator
Option map iterator.
Definition OptionSet.h:152
map_type _values
Data structure to map names with values.
Definition OptionSet.h:241
T & set_buffer(const std::string &)
Convenient method to request a buffer.
Definition OptionSet.h:291
const std::string & name() const
A readonly reference to the option set's name.
Definition OptionSet.h:75
OptionSet()=default
const std::string & type() const
A readonly reference to the option set's type.
Definition OptionSet.h:79
LabeledAxisAccessor & set_output(const std::string &)
Definition OptionSet.cxx:83
std::string to_str() const
Print the contents.
Definition OptionSet.cxx:132
map_type::const_iterator const_iterator
Constant option map iterator.
Definition OptionSet.h:154
std::map< std::string, std::unique_ptr< OptionBase >, std::less<> > map_type
The type of the map that we store internally.
Definition OptionSet.h:150
std::string & name()
A writable reference to the option set's name.
Definition OptionSet.h:77
LabeledAxisAccessor & set_input(const std::string &)
Definition OptionSet.cxx:77
std::string & type()
A writable reference to the option set's type.
Definition OptionSet.h:81
const std::string & doc() const
A readonly reference to the option set's docstring.
Definition OptionSet.h:87
const std::string & section() const
A readonly reference to the option set's section.
Definition OptionSet.h:91
T & set_parameter(const std::string &)
Convenient method to request a parameter.
Definition OptionSet.h:284
iterator begin()
Iterator pointing to the beginning of the set of options.
Definition OptionSet.cxx:178
iterator end()
Iterator pointing to the end of the set of options.
Definition OptionSet.cxx:190
const std::string & path() const
A readonly reference to the option set's path.
Definition OptionSet.h:83
std::string & doc()
A writable reference to the option set's docstring.
Definition OptionSet.h:89
const T & get(const std::string &) const
Definition OptionSet.h:261
struct neml2::OptionSet::Metadata _metadata
void clear()
Clear internal data structures & frees any allocated memory.
Definition OptionSet.cxx:89
std::string & path()
A writable reference to the option set's path.
Definition OptionSet.h:85
std::string & section()
A writable reference to the option set's section.
Definition OptionSet.h:93
std::size_t size() const
Definition OptionSet.h:110
T & set(const std::string &)
Definition OptionSet.h:273
bool contains(const std::string &) const
Definition OptionSet.h:250
Definition Option.h:70
Definition DiagnosticsInterface.cxx:30
bool options_compatible(const OptionSet &opts, const OptionSet &additional_opts)
Definition OptionSet.cxx:34
std::ostream & operator<<(std::ostream &os, const EnumSelection &es)
Definition EnumSelection.cxx:32
Definition OptionSet.h:170
std::string path
Path to the option set.
Definition OptionSet.h:218
std::string type
Type of the option set.
Definition OptionSet.h:199
std::string name
Name of the option set.
Definition OptionSet.h:184
std::string section
Which NEML2 input file section this object belongs to.
Definition OptionSet.h:237
std::string doc
Option set's doc string.
Definition OptionSet.h:229