NEML2 1.4.0
Loading...
Searching...
No Matches
OptionSet.cxx
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
25#include "neml2/base/OptionSet.h"
26
27namespace neml2
28{
29bool
30OptionSet::contains(const std::string & name) const
31{
33 if (it != _values.end())
34 return true;
35 return false;
36}
37
39OptionSet::get(const std::string & name) const
40{
41 neml_assert(this->contains(name),
42 "ERROR: no option named \"",
43 name,
44 "\" found.\n\nKnown options:\n",
45 *this);
46
47 return *_values.at(name);
48}
49
51OptionSet::set(const std::string & name)
52{
53 neml_assert(this->contains(name),
54 "ERROR: no option named \"",
55 name,
56 "\" found.\n\nKnown options:\n",
57 *this);
58
59 return *_values[name];
60}
61
62void
64{
65 _values.clear();
66}
67
70{
71 this->OptionSet::clear();
72 *this += source;
73 this->_metadata = source._metadata;
74 return *this;
75}
76
77void
79{
80 for (const auto & [key, value] : source._values)
81 _values[key] = value->clone();
82}
83
84OptionSet::OptionSet(const OptionSet & p) { *this = p; }
85
86// LCOV_EXCL_START
87void
88OptionSet::print(std::ostream & os) const
89{
91
92 os << " section: " << section() << '\n';
93 if (doc().empty())
94 os << " doc:\n";
95 else
96 {
97 os << " doc: |-\n";
98 os << " " << doc() << '\n';
99 }
100
101 while (it != _values.end())
102 {
103 os << " " << it->first << ":\n";
104 os << " type: " << it->second->type() << '\n';
105 if (it->second->doc().empty())
106 os << " doc:\n";
107 else
108 {
109 os << " doc: |-\n";
110 os << " " << it->second->doc() << '\n';
111 }
112 os << " suppressed: " << it->second->suppressed() << '\n';
113 os << " value: ";
114 it->second->print(os);
115 if (++it != _values.end())
116 os << '\n';
117 }
118}
119
120std::ostream &
121operator<<(std::ostream & os, const OptionSet & p)
122{
123 p.print(os);
124 return os;
125}
126// LCOV_EXCL_STOP
127
130{
131 return _values.begin();
132}
133
136{
137 return _values.begin();
138}
139
142{
143 return _values.end();
144}
145
148{
149 return _values.end();
150}
151} // namespace neml2
The wrapper (decorator) for cross-referencing unresolved values at parse time.
Definition CrossRef.h:52
Definition OptionSet.h:126
A custom map-like data structure. The keys are strings, and the values can be nonhomogeneously typed.
Definition OptionSet.h:59
map_type _values
Data structure to map names with values.
Definition OptionSet.h:374
const std::string & name() const
A readonly reference to the option set's name.
Definition OptionSet.h:79
OptionSet()=default
const std::string & doc() const
A readonly reference to the option set's docstring.
Definition OptionSet.h:91
map_type::const_iterator const_iterator
Constant option map iterator.
Definition OptionSet.h:284
const std::string & section() const
A readonly reference to the option set's section.
Definition OptionSet.h:95
iterator begin()
Iterator pointing to the beginning of the set of options.
Definition OptionSet.cxx:129
iterator end()
Iterator pointing to the end of the set of options.
Definition OptionSet.cxx:141
virtual void operator+=(const OptionSet &source)
Definition OptionSet.cxx:78
const T & get(const std::string &) const
Definition OptionSet.h:422
struct neml2::OptionSet::Metadata _metadata
virtual void clear()
Clear internal data structures & frees any allocated memory.
Definition OptionSet.cxx:63
T & set(const std::string &)
Definition OptionSet.h:436
void print(std::ostream &os=std::cout) const
Print the contents.
Definition OptionSet.cxx:88
virtual OptionSet & operator=(const OptionSet &source)
Assignment operator. Deep copy.
Definition OptionSet.cxx:69
bool contains(const std::string &) const
Definition OptionSet.h:411
map_type::iterator iterator
Option map iterator.
Definition OptionSet.h:281
Definition CrossRef.cxx:32
std::ostream & operator<<(std::ostream &os, const OptionCollection &p)
Definition OptionCollection.cxx:37
void neml_assert(bool assertion, Args &&... args)
Definition error.h:73