Line data Source code
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 : #include <iostream>
26 :
27 : #include "neml2/base/OptionSet.h"
28 : #include "neml2/base/LabeledAxisAccessor.h"
29 : #include "neml2/misc/assertions.h"
30 :
31 : namespace neml2
32 : {
33 : bool
34 10 : options_compatible(const OptionSet & opts, const OptionSet & additional_opts)
35 : {
36 50 : for (const auto & [key, value] : additional_opts)
37 : {
38 40 : if (!opts.contains(key))
39 0 : return false;
40 40 : if (opts.get(key) != *value)
41 0 : return false;
42 : }
43 10 : return true;
44 : }
45 :
46 : bool
47 78913 : OptionSet::contains(const std::string & name) const
48 : {
49 78913 : return _values.find(name) != _values.end();
50 : }
51 :
52 : bool
53 30 : OptionSet::user_specified(const std::string & name) const
54 : {
55 30 : neml_assert(this->contains(name),
56 : "ERROR: no option named '",
57 : name,
58 : "' found.\n\nKnown options:\n",
59 : *this);
60 30 : return _values.at(name)->user_specified();
61 : }
62 :
63 : const OptionBase &
64 351 : OptionSet::get(const std::string & name) const
65 : {
66 351 : neml_assert(this->contains(name),
67 : "ERROR: no option named \"",
68 : name,
69 : "\" found.\n\nKnown options:\n",
70 : *this);
71 :
72 351 : return *_values.at(name);
73 : }
74 :
75 : OptionBase &
76 14242 : OptionSet::set(const std::string & name)
77 : {
78 14242 : neml_assert(this->contains(name),
79 : "ERROR: no option named \"",
80 : name,
81 : "\" found.\n\nKnown options:\n",
82 : *this);
83 :
84 14242 : return *_values[name];
85 : }
86 :
87 : LabeledAxisAccessor &
88 502 : OptionSet::set_input(const std::string & name)
89 : {
90 502 : return set<LabeledAxisAccessor, FType::INPUT>(name);
91 : }
92 :
93 : LabeledAxisAccessor &
94 395 : OptionSet::set_output(const std::string & name)
95 : {
96 395 : return set<LabeledAxisAccessor, FType::OUTPUT>(name);
97 : }
98 :
99 : void
100 15444 : OptionSet::clear()
101 : {
102 15444 : _values.clear();
103 15444 : }
104 :
105 11049 : OptionSet::OptionSet(const OptionSet & p) { *this = p; }
106 :
107 0 : OptionSet::OptionSet(OptionSet && p) noexcept { *this = std::move(p); }
108 :
109 : OptionSet &
110 14508 : OptionSet::operator=(const OptionSet & source)
111 : {
112 14508 : this->OptionSet::clear();
113 14508 : *this += source;
114 14508 : this->_metadata = source._metadata;
115 14508 : return *this;
116 : }
117 :
118 : OptionSet &
119 936 : OptionSet::operator=(OptionSet && source) noexcept
120 : {
121 936 : this->OptionSet::clear();
122 936 : *this += source;
123 936 : this->_metadata = std::move(source._metadata);
124 936 : return *this;
125 : }
126 :
127 : void
128 17062 : OptionSet::operator+=(const OptionSet & source)
129 : {
130 240829 : for (const auto & [key, value] : source._values)
131 223767 : _values[key] = value->clone();
132 17062 : }
133 :
134 : void
135 498 : OptionSet::operator+=(OptionSet && source)
136 : {
137 1958 : for (auto && [key, value] : std::move(source)._values)
138 1460 : _values[key] = value->clone();
139 498 : }
140 :
141 : // LCOV_EXCL_START
142 : std::string
143 : OptionSet::to_str() const
144 : {
145 : std::ostringstream os;
146 :
147 : OptionSet::const_iterator it = _values.begin();
148 :
149 : os << " section: " << section() << '\n';
150 : if (doc().empty())
151 : os << " doc:\n";
152 : else
153 : {
154 : os << " doc: |-\n";
155 : os << " " << doc() << '\n';
156 : }
157 :
158 : while (it != _values.end())
159 : {
160 : os << " " << it->first << ":\n";
161 : os << " type: " << it->second->type() << '\n';
162 : os << " ftype: " << it->second->ftype() << '\n';
163 : if (it->second->doc().empty())
164 : os << " doc:\n";
165 : else
166 : {
167 : os << " doc: |-\n";
168 : os << " " << it->second->doc() << '\n';
169 : }
170 : os << " suppressed: " << it->second->suppressed() << '\n';
171 : os << " value: ";
172 : it->second->print(os);
173 : if (++it != _values.end())
174 : os << '\n';
175 : }
176 :
177 : return os.str();
178 : }
179 :
180 : std::ostream &
181 : operator<<(std::ostream & os, const OptionSet & p)
182 : {
183 : os << p.to_str();
184 : return os;
185 : }
186 : // LCOV_EXCL_STOP
187 :
188 : OptionSet::iterator
189 7778 : OptionSet::begin()
190 : {
191 7778 : return _values.begin();
192 : }
193 :
194 : OptionSet::const_iterator
195 10 : OptionSet::begin() const
196 : {
197 10 : return _values.begin();
198 : }
199 :
200 : OptionSet::iterator
201 7778 : OptionSet::end()
202 : {
203 7778 : return _values.end();
204 : }
205 :
206 : OptionSet::const_iterator
207 10 : OptionSet::end() const
208 : {
209 10 : return _values.end();
210 : }
211 : } // namespace neml2
|