Line data Source code
1 : //* This file is part of the MOOSE framework
2 : //* https://mooseframework.inl.gov
3 : //*
4 : //* All rights reserved, see COPYRIGHT for full restrictions
5 : //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 : //*
7 : //* Licensed under LGPL 2.1, please see LICENSE for details
8 : //* https://www.gnu.org/licenses/lgpl-2.1.html
9 :
10 : #include "MooseEnumBase.h"
11 : #include "MultiMooseEnum.h"
12 : #include "MooseUtils.h"
13 : #include "MooseError.h"
14 : #include "Conversion.h"
15 :
16 : #include <initializer_list>
17 : #include <sstream>
18 : #include <algorithm>
19 : #include <iterator>
20 : #include <limits>
21 : #include <string>
22 : #include <iostream>
23 :
24 17289479 : MultiMooseEnum::MultiMooseEnum(std::string valid_names,
25 : std::string initialization_values,
26 17289479 : bool allow_out_of_range)
27 17289482 : : MooseEnumBase(valid_names, allow_out_of_range)
28 : {
29 17289476 : *this = initialization_values;
30 17289476 : }
31 :
32 17273258 : MultiMooseEnum::MultiMooseEnum(std::string valid_names,
33 : const char * initialization_values,
34 17273258 : bool allow_out_of_range)
35 17273267 : : MultiMooseEnum(valid_names, std::string(initialization_values), allow_out_of_range)
36 : {
37 17273255 : }
38 :
39 1319136 : MultiMooseEnum::MultiMooseEnum(std::string valid_names, bool allow_out_of_range)
40 : // Set default variable value to nothing ("")
41 1319136 : : MultiMooseEnum(valid_names, "", allow_out_of_range)
42 : {
43 1319133 : }
44 :
45 20508609 : MultiMooseEnum::MultiMooseEnum(const MultiMooseEnum & other_enum)
46 20508609 : : MooseEnumBase(other_enum), _current_values(other_enum._current_values)
47 : {
48 20508609 : }
49 :
50 : /**
51 : * Private constuctor for use by libmesh::Parameters
52 : */
53 74887895 : MultiMooseEnum::MultiMooseEnum() {}
54 :
55 0 : MultiMooseEnum::MultiMooseEnum(const MooseEnumBase & other_enum) : MooseEnumBase(other_enum) {}
56 :
57 : bool
58 43130 : MultiMooseEnum::operator==(const MultiMooseEnum & value) const
59 : {
60 : // Not the same if the lengths are different
61 43130 : if (value.size() != size())
62 146 : return false;
63 :
64 : // Return false if this enum does not contain an item from the other, since they are the same
65 : // size at this point if this is true then they are equal.
66 42984 : return isValueSet(value);
67 : }
68 :
69 : bool
70 43130 : MultiMooseEnum::operator!=(const MultiMooseEnum & value) const
71 : {
72 43130 : return !(*this == value);
73 : }
74 :
75 : bool
76 76183459 : MultiMooseEnum::isValueSet(const std::string & value) const
77 : {
78 76183459 : return std::find_if(_current_values.begin(),
79 : _current_values.end(),
80 128054154 : [&value](const MooseEnumItem & item)
81 280421072 : { return item == value; }) != _current_values.end();
82 : }
83 :
84 : bool
85 37703 : MultiMooseEnum::isValueSet(int value) const
86 : {
87 37703 : return std::find_if(_current_values.begin(),
88 : _current_values.end(),
89 37703 : [&value](const MooseEnumItem & item)
90 113109 : { return item == value; }) != _current_values.end();
91 : }
92 :
93 : bool
94 0 : MultiMooseEnum::isValueSet(unsigned short value) const
95 : {
96 0 : return std::find_if(_current_values.begin(),
97 : _current_values.end(),
98 0 : [&value](const MooseEnumItem & item)
99 0 : { return item == value; }) != _current_values.end();
100 : }
101 :
102 : bool
103 42984 : MultiMooseEnum::isValueSet(const MultiMooseEnum & value) const
104 : {
105 106898 : for (const auto & item : value._current_values)
106 64452 : if (!isValueSet(item))
107 538 : return false;
108 42446 : return true;
109 : }
110 :
111 : bool
112 309582366 : MultiMooseEnum::isValueSet(const MooseEnumItem & value) const
113 : {
114 309582366 : return std::find_if(_current_values.begin(),
115 : _current_values.end(),
116 79757285 : [&value](const MooseEnumItem & item)
117 698922017 : { return item == value; }) != _current_values.end();
118 : }
119 :
120 : MultiMooseEnum &
121 19699976 : MultiMooseEnum::operator=(const std::string & names)
122 : {
123 19699976 : std::vector<std::string> names_vector;
124 19699976 : MooseUtils::tokenize(names, names_vector, 1, " ");
125 39399950 : return assignValues(names_vector.begin(), names_vector.end(), false);
126 19699976 : }
127 :
128 : MultiMooseEnum &
129 114 : MultiMooseEnum::operator=(const std::vector<std::string> & names)
130 : {
131 114 : return assignValues(names.begin(), names.end(), false);
132 : }
133 :
134 : MultiMooseEnum &
135 1 : MultiMooseEnum::operator=(const std::set<std::string> & names)
136 : {
137 1 : return assignValues(names.begin(), names.end(), false);
138 : }
139 :
140 : void
141 917 : MultiMooseEnum::eraseSetValue(const std::string & names)
142 : {
143 917 : std::vector<std::string> names_vector;
144 917 : MooseUtils::tokenize(names, names_vector, 1, " ");
145 917 : removeSetValues(names_vector.begin(), names_vector.end());
146 917 : }
147 :
148 : void
149 0 : MultiMooseEnum::erase(const std::string & names)
150 : {
151 0 : mooseDeprecated("MultiMooseEnum::erase is deprecated, use MultiMooseEnum::eraseSetValue");
152 0 : MultiMooseEnum::eraseSetValue(names);
153 0 : }
154 :
155 : void
156 0 : MultiMooseEnum::eraseSetValue(const std::vector<std::string> & names)
157 : {
158 0 : removeSetValues(names.begin(), names.end());
159 0 : }
160 :
161 : void
162 0 : MultiMooseEnum::erase(const std::vector<std::string> & names)
163 : {
164 0 : mooseDeprecated("MultiMooseEnum::erase is deprecated, use MultiMooseEnum::eraseSetValue");
165 0 : MultiMooseEnum::eraseSetValue(names);
166 0 : }
167 :
168 : void
169 0 : MultiMooseEnum::eraseSetValue(const std::set<std::string> & names)
170 : {
171 0 : removeSetValues(names.begin(), names.end());
172 0 : }
173 :
174 : void
175 0 : MultiMooseEnum::erase(const std::set<std::string> & names)
176 : {
177 0 : mooseDeprecated("MultiMooseEnum::erase is deprecated, use MultiMooseEnum::eraseSetValue");
178 0 : MultiMooseEnum::eraseSetValue(names);
179 0 : }
180 :
181 : void
182 321813 : MultiMooseEnum::setAdditionalValue(const std::string & names)
183 : {
184 321813 : std::vector<std::string> names_vector;
185 321813 : MooseUtils::tokenize(names, names_vector, 1, " ");
186 321813 : assignValues(names_vector.begin(), names_vector.end(), true);
187 321813 : }
188 :
189 : void
190 0 : MultiMooseEnum::push_back(const std::string & names)
191 : {
192 0 : mooseDeprecated(
193 : "MultiMooseEnum::push_back is deprecated, use MultiMooseEnum::setAdditionalValue");
194 0 : MultiMooseEnum::setAdditionalValue(names);
195 0 : }
196 :
197 : void
198 1 : MultiMooseEnum::setAdditionalValue(const std::vector<std::string> & names)
199 : {
200 1 : assignValues(names.begin(), names.end(), true);
201 1 : }
202 :
203 : void
204 0 : MultiMooseEnum::push_back(const std::vector<std::string> & names)
205 : {
206 0 : mooseDeprecated(
207 : "MultiMooseEnum::push_back is deprecated, use MultiMooseEnum::setAdditionalValue");
208 0 : MultiMooseEnum::setAdditionalValue(names);
209 0 : }
210 :
211 : void
212 0 : MultiMooseEnum::setAdditionalValue(const std::set<std::string> & names)
213 : {
214 0 : assignValues(names.begin(), names.end(), true);
215 0 : }
216 :
217 : void
218 0 : MultiMooseEnum::push_back(const std::set<std::string> & names)
219 : {
220 0 : mooseDeprecated(
221 : "MultiMooseEnum::push_back is deprecated, use MultiMooseEnum::setAdditionalValue");
222 0 : MultiMooseEnum::setAdditionalValue(names);
223 0 : }
224 :
225 : void
226 1 : MultiMooseEnum::setAdditionalValue(const MultiMooseEnum & other_enum)
227 : {
228 1 : assignValues(other_enum.begin(), other_enum.end(), true);
229 1 : }
230 :
231 : void
232 0 : MultiMooseEnum::push_back(const MultiMooseEnum & other_enum)
233 : {
234 0 : mooseDeprecated(
235 : "MultiMooseEnum::push_back is deprecated, use MultiMooseEnum::setAdditionalValue");
236 0 : MultiMooseEnum::setAdditionalValue(other_enum);
237 0 : }
238 :
239 : const std::string &
240 4992886 : MultiMooseEnum::operator[](unsigned int i) const
241 : {
242 : mooseAssert(i < _current_values.size(),
243 : "Access out of bounds in MultiMooseEnum (i: " << i << " size: "
244 : << _current_values.size() << ")");
245 :
246 4992886 : return _current_values[i].rawName();
247 : }
248 :
249 : unsigned int
250 2805 : MultiMooseEnum::get(unsigned int i) const
251 : {
252 : mooseAssert(i < _current_values.size(),
253 : "Access out of bounds in MultiMooseEnum (i: " << i << " size: "
254 : << _current_values.size() << ")");
255 :
256 2805 : return _current_values[i].id();
257 : }
258 :
259 : template <typename InputIterator>
260 : MultiMooseEnum &
261 20021906 : MultiMooseEnum::assignValues(InputIterator first, InputIterator last, bool append)
262 : {
263 20021906 : if (!append)
264 19700091 : clearSetValues();
265 :
266 39306559 : for (InputIterator it = first; it != last; ++it)
267 : {
268 19284659 : const auto iter = find(*it);
269 19284659 : if (iter == _items.end())
270 : {
271 1492433 : if (!_allow_out_of_range) // Are out of range values allowed?
272 13 : mooseError("Invalid option \"",
273 6 : *it,
274 : "\" in MultiMooseEnum. Valid options (not case-sensitive) are \"",
275 : getRawNames(),
276 : "\".");
277 : else
278 : {
279 1492427 : MooseEnumItem created(*it, getNextValidID());
280 1492427 : addEnumerationItem(created);
281 1492427 : _current_values.push_back(created);
282 1492427 : }
283 : }
284 : else
285 17792226 : _current_values.push_back(*iter);
286 : }
287 20021900 : checkDeprecated();
288 20021899 : return *this;
289 : }
290 :
291 : template <typename InputIterator>
292 : void
293 917 : MultiMooseEnum::removeSetValues(InputIterator first, InputIterator last)
294 : {
295 : // Create a new list of enumerations by striping out the supplied values
296 1834 : for (InputIterator it = first; it != last; ++it)
297 : {
298 : std::vector<MooseEnumItem>::iterator iter =
299 917 : std::find_if(_current_values.begin(),
300 : _current_values.end(),
301 2761 : [it](const MooseEnumItem & item) { return item == *it; });
302 917 : if (iter != _current_values.end())
303 917 : _current_values.erase(iter);
304 : }
305 917 : }
306 :
307 : void
308 54315029 : MultiMooseEnum::clearSetValues()
309 : {
310 54315029 : _current_values.clear();
311 54315029 : }
312 :
313 : void
314 0 : MultiMooseEnum::clear()
315 : {
316 0 : mooseDeprecated("MultiMooseEnum::clear is deprecated, use MultiMooseEnum::clearSetValues");
317 0 : clearSetValues();
318 0 : }
319 :
320 : unsigned int
321 3639019 : MultiMooseEnum::size() const
322 : {
323 3639019 : return _current_values.size();
324 : }
325 :
326 : void
327 52510765 : MultiMooseEnum::checkDeprecated() const
328 : {
329 111565943 : for (const auto & item : _current_values)
330 59055179 : MooseEnumBase::checkDeprecated(item);
331 52510764 : }
332 :
333 : std::ostream &
334 942554 : operator<<(std::ostream & out, const MultiMooseEnum & obj)
335 : {
336 942554 : out << Moose::stringify(obj._current_values, " ");
337 942554 : return out;
338 : }
339 :
340 : void
341 0 : MultiMooseEnum::addValidName(const std::string & name)
342 : {
343 0 : addEnumerationName(name);
344 0 : checkDeprecated();
345 0 : }
346 :
347 : MooseEnumBase &
348 0 : MultiMooseEnum::operator+=(const std::string & name)
349 : {
350 0 : mooseDeprecated("MultiMooseEnum::operator+= is deprecated, use MultiMooseEnum::addValidName");
351 0 : return MooseEnumBase::operator+=(name);
352 : }
353 :
354 : void
355 0 : MultiMooseEnum::addValidName(const std::initializer_list<std::string> & names)
356 : {
357 0 : for (const auto & name : names)
358 0 : addValidName(name);
359 0 : }
360 :
361 : MooseEnumBase &
362 0 : MultiMooseEnum::operator+=(const std::initializer_list<std::string> & names)
363 : {
364 0 : mooseDeprecated("MultiMooseEnum::operator+= is deprecated, use MultiMooseEnum::addValidName");
365 0 : return MooseEnumBase::operator+=(names);
366 : }
367 :
368 : void
369 1703976 : MultiMooseEnum::addValidName(const MultiMooseEnum & names)
370 : {
371 14057802 : for (const auto & item : names._items)
372 12353826 : addEnumerationItem(MooseEnumItem(item.name(), getNextValidID()));
373 1703976 : }
|