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 17044038 : MultiMooseEnum::MultiMooseEnum(std::string valid_names,
25 : std::string initialization_values,
26 17044038 : bool allow_out_of_range)
27 17044041 : : MooseEnumBase(valid_names, allow_out_of_range)
28 : {
29 17044035 : *this = initialization_values;
30 17044035 : }
31 :
32 17027904 : MultiMooseEnum::MultiMooseEnum(std::string valid_names,
33 : const char * initialization_values,
34 17027904 : bool allow_out_of_range)
35 17027913 : : MultiMooseEnum(valid_names, std::string(initialization_values), allow_out_of_range)
36 : {
37 17027901 : }
38 :
39 1283777 : MultiMooseEnum::MultiMooseEnum(std::string valid_names, bool allow_out_of_range)
40 : // Set default variable value to nothing ("")
41 1283777 : : MultiMooseEnum(valid_names, "", allow_out_of_range)
42 : {
43 1283774 : }
44 :
45 19939839 : MultiMooseEnum::MultiMooseEnum(const MultiMooseEnum & other_enum)
46 19939839 : : MooseEnumBase(other_enum), _current_values(other_enum._current_values)
47 : {
48 19939839 : }
49 :
50 : /**
51 : * Private constuctor for use by libmesh::Parameters
52 : */
53 73266008 : MultiMooseEnum::MultiMooseEnum() {}
54 :
55 0 : MultiMooseEnum::MultiMooseEnum(const MooseEnumBase & other_enum) : MooseEnumBase(other_enum) {}
56 :
57 : bool
58 39941 : MultiMooseEnum::operator==(const MultiMooseEnum & value) const
59 : {
60 : // Not the same if the lengths are different
61 39941 : if (value.size() != size())
62 117 : 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 39824 : return isValueSet(value);
67 : }
68 :
69 : bool
70 39941 : MultiMooseEnum::operator!=(const MultiMooseEnum & value) const
71 : {
72 39941 : return !(*this == value);
73 : }
74 :
75 : bool
76 69853994 : MultiMooseEnum::isValueSet(const std::string & value) const
77 : {
78 69853994 : return std::find_if(_current_values.begin(),
79 : _current_values.end(),
80 117623755 : [&value](const MooseEnumItem & item)
81 257331743 : { return item == value; }) != _current_values.end();
82 : }
83 :
84 : bool
85 34872 : MultiMooseEnum::isValueSet(int value) const
86 : {
87 34872 : return std::find_if(_current_values.begin(),
88 : _current_values.end(),
89 34872 : [&value](const MooseEnumItem & item)
90 104616 : { 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 39824 : MultiMooseEnum::isValueSet(const MultiMooseEnum & value) const
104 : {
105 99275 : for (const auto & item : value._current_values)
106 59949 : if (!isValueSet(item))
107 498 : return false;
108 39326 : return true;
109 : }
110 :
111 : bool
112 277382535 : MultiMooseEnum::isValueSet(const MooseEnumItem & value) const
113 : {
114 277382535 : return std::find_if(_current_values.begin(),
115 : _current_values.end(),
116 72578144 : [&value](const MooseEnumItem & item)
117 627343214 : { return item == value; }) != _current_values.end();
118 : }
119 :
120 : MultiMooseEnum &
121 19394565 : MultiMooseEnum::operator=(const std::string & names)
122 : {
123 19394565 : std::vector<std::string> names_vector;
124 19394565 : MooseUtils::tokenize(names, names_vector, 1, " ");
125 38789128 : return assignValues(names_vector.begin(), names_vector.end(), false);
126 19394565 : }
127 :
128 : MultiMooseEnum &
129 123 : MultiMooseEnum::operator=(const std::vector<std::string> & names)
130 : {
131 123 : 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 866 : MultiMooseEnum::eraseSetValue(const std::string & names)
142 : {
143 866 : std::vector<std::string> names_vector;
144 866 : MooseUtils::tokenize(names, names_vector, 1, " ");
145 866 : removeSetValues(names_vector.begin(), names_vector.end());
146 866 : }
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 298366 : MultiMooseEnum::setAdditionalValue(const std::string & names)
183 : {
184 298366 : std::vector<std::string> names_vector;
185 298366 : MooseUtils::tokenize(names, names_vector, 1, " ");
186 298366 : assignValues(names_vector.begin(), names_vector.end(), true);
187 298366 : }
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 4428563 : 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 4428563 : return _current_values[i].rawName();
247 : }
248 :
249 : unsigned int
250 2277 : 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 2277 : return _current_values[i].id();
257 : }
258 :
259 : template <typename InputIterator>
260 : MultiMooseEnum &
261 19693057 : MultiMooseEnum::assignValues(InputIterator first, InputIterator last, bool append)
262 : {
263 19693057 : if (!append)
264 19394689 : clearSetValues();
265 :
266 38607195 : for (InputIterator it = first; it != last; ++it)
267 : {
268 18914144 : const auto iter = find(*it);
269 18914144 : if (iter == _items.end())
270 : {
271 1464154 : 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 1464148 : MooseEnumItem created(*it, getNextValidID());
280 1464148 : addEnumerationItem(created);
281 1464148 : _current_values.push_back(created);
282 1464148 : }
283 : }
284 : else
285 17449990 : _current_values.push_back(*iter);
286 : }
287 19693051 : checkDeprecated();
288 19693050 : return *this;
289 : }
290 :
291 : template <typename InputIterator>
292 : void
293 866 : MultiMooseEnum::removeSetValues(InputIterator first, InputIterator last)
294 : {
295 : // Create a new list of enumerations by striping out the supplied values
296 1732 : for (InputIterator it = first; it != last; ++it)
297 : {
298 : std::vector<MooseEnumItem>::iterator iter =
299 866 : std::find_if(_current_values.begin(),
300 : _current_values.end(),
301 2608 : [it](const MooseEnumItem & item) { return item == *it; });
302 866 : if (iter != _current_values.end())
303 866 : _current_values.erase(iter);
304 : }
305 866 : }
306 :
307 : void
308 53388667 : MultiMooseEnum::clearSetValues()
309 : {
310 53388667 : _current_values.clear();
311 53388667 : }
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 3265105 : MultiMooseEnum::size() const
322 : {
323 3265105 : return _current_values.size();
324 : }
325 :
326 : void
327 51715335 : MultiMooseEnum::checkDeprecated() const
328 : {
329 109668908 : for (const auto & item : _current_values)
330 57953574 : MooseEnumBase::checkDeprecated(item);
331 51715334 : }
332 :
333 : std::ostream &
334 896046 : operator<<(std::ostream & out, const MultiMooseEnum & obj)
335 : {
336 896046 : out << Moose::stringify(obj._current_values, " ");
337 896046 : 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 1660660 : MultiMooseEnum::addValidName(const MultiMooseEnum & names)
370 : {
371 13700445 : for (const auto & item : names._items)
372 12039785 : addEnumerationItem(MooseEnumItem(item.name(), getNextValidID()));
373 1660660 : }
|