https://mooseframework.inl.gov
Loading...
Searching...
No Matches
MultiMooseEnum.C
Go to the documentation of this file.
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
24MultiMooseEnum::MultiMooseEnum(std::string valid_names,
25 std::string initialization_values,
26 bool allow_out_of_range)
27 : MooseEnumBase(valid_names, allow_out_of_range)
28{
29 *this = initialization_values;
30}
31
32MultiMooseEnum::MultiMooseEnum(std::string valid_names,
33 const char * initialization_values,
34 bool allow_out_of_range)
35 : MultiMooseEnum(valid_names, std::string(initialization_values), allow_out_of_range)
36{
37}
38
39MultiMooseEnum::MultiMooseEnum(std::string valid_names, bool allow_out_of_range)
40 // Set default variable value to nothing ("")
41 : MultiMooseEnum(valid_names, "", allow_out_of_range)
42{
43}
44
46 : MooseEnumBase(other_enum), _current_values(other_enum._current_values)
47{
48}
49
54
56
57bool
59{
60 // Not the same if the lengths are different
61 if (value.size() != size())
62 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 return isValueSet(value);
67}
68
69bool
71{
72 return !(*this == value);
73}
74
75bool
76MultiMooseEnum::isValueSet(const std::string & value) const
77{
78 return std::find_if(_current_values.begin(),
79 _current_values.end(),
80 [&value](const MooseEnumItem & item)
81 { return item == value; }) != _current_values.end();
82}
83
84bool
86{
87 return std::find_if(_current_values.begin(),
88 _current_values.end(),
89 [&value](const MooseEnumItem & item)
90 { return item == value; }) != _current_values.end();
91}
92
93bool
94MultiMooseEnum::isValueSet(unsigned short value) const
95{
96 return std::find_if(_current_values.begin(),
97 _current_values.end(),
98 [&value](const MooseEnumItem & item)
99 { return item == value; }) != _current_values.end();
100}
101
102bool
104{
105 for (const auto & item : value._current_values)
106 if (!isValueSet(item))
107 return false;
108 return true;
109}
110
111bool
113{
114 return std::find_if(_current_values.begin(),
115 _current_values.end(),
116 [&value](const MooseEnumItem & item)
117 { return item == value; }) != _current_values.end();
118}
119
121MultiMooseEnum::operator=(const std::string & names)
122{
123 std::vector<std::string> names_vector;
124 MooseUtils::tokenize(names, names_vector, 1, " ");
125 return assignValues(names_vector.begin(), names_vector.end(), false);
126}
127
129MultiMooseEnum::operator=(const std::vector<std::string> & names)
131 return assignValues(names.begin(), names.end(), false);
132}
133
135MultiMooseEnum::operator=(const std::set<std::string> & names)
136{
137 return assignValues(names.begin(), names.end(), false);
138}
139
140void
141MultiMooseEnum::eraseSetValue(const std::string & names)
142{
143 std::vector<std::string> names_vector;
144 MooseUtils::tokenize(names, names_vector, 1, " ");
145 removeSetValues(names_vector.begin(), names_vector.end());
146}
147
148void
149MultiMooseEnum::erase(const std::string & names)
150{
151 mooseDeprecated("MultiMooseEnum::erase is deprecated, use MultiMooseEnum::eraseSetValue");
153}
154
155void
156MultiMooseEnum::eraseSetValue(const std::vector<std::string> & names)
157{
158 removeSetValues(names.begin(), names.end());
159}
160
161void
162MultiMooseEnum::erase(const std::vector<std::string> & names)
163{
164 mooseDeprecated("MultiMooseEnum::erase is deprecated, use MultiMooseEnum::eraseSetValue");
166}
167
168void
169MultiMooseEnum::eraseSetValue(const std::set<std::string> & names)
170{
171 removeSetValues(names.begin(), names.end());
172}
173
174void
175MultiMooseEnum::erase(const std::set<std::string> & names)
176{
177 mooseDeprecated("MultiMooseEnum::erase is deprecated, use MultiMooseEnum::eraseSetValue");
179}
180
181void
182MultiMooseEnum::setAdditionalValue(const std::string & names)
183{
184 std::vector<std::string> names_vector;
185 MooseUtils::tokenize(names, names_vector, 1, " ");
186 assignValues(names_vector.begin(), names_vector.end(), true);
187}
188
189void
190MultiMooseEnum::push_back(const std::string & names)
191{
193 "MultiMooseEnum::push_back is deprecated, use MultiMooseEnum::setAdditionalValue");
195}
196
197void
198MultiMooseEnum::setAdditionalValue(const std::vector<std::string> & names)
199{
200 assignValues(names.begin(), names.end(), true);
201}
202
203void
204MultiMooseEnum::push_back(const std::vector<std::string> & names)
205{
207 "MultiMooseEnum::push_back is deprecated, use MultiMooseEnum::setAdditionalValue");
209}
210
211void
212MultiMooseEnum::setAdditionalValue(const std::set<std::string> & names)
213{
214 assignValues(names.begin(), names.end(), true);
215}
216
217void
218MultiMooseEnum::push_back(const std::set<std::string> & names)
219{
221 "MultiMooseEnum::push_back is deprecated, use MultiMooseEnum::setAdditionalValue");
223}
224
225void
227{
228 assignValues(other_enum.begin(), other_enum.end(), true);
229}
230
231void
233{
235 "MultiMooseEnum::push_back is deprecated, use MultiMooseEnum::setAdditionalValue");
237}
238
239const std::string &
240MultiMooseEnum::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 return _current_values[i].rawName();
247}
248
249unsigned int
250MultiMooseEnum::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 return _current_values[i].id();
257}
258
259template <typename InputIterator>
261MultiMooseEnum::assignValues(InputIterator first, InputIterator last, bool append)
262{
263 if (!append)
265
266 for (InputIterator it = first; it != last; ++it)
267 {
268 const auto iter = find(*it);
269 if (iter == _items.end())
270 {
271 if (!_allow_out_of_range) // Are out of range values allowed?
272 mooseError("Invalid option \"",
273 *it,
274 "\" in MultiMooseEnum. Valid options (not case-sensitive) are \"",
275 getRawNames(),
276 "\".");
277 else
278 {
279 MooseEnumItem created(*it, getNextValidID());
280 addEnumerationItem(created);
281 _current_values.push_back(created);
282 }
283 }
284 else
285 _current_values.push_back(*iter);
286 }
288 return *this;
289}
290
291template <typename InputIterator>
292void
293MultiMooseEnum::removeSetValues(InputIterator first, InputIterator last)
294{
295 // Create a new list of enumerations by striping out the supplied values
296 for (InputIterator it = first; it != last; ++it)
297 {
298 std::vector<MooseEnumItem>::iterator iter =
299 std::find_if(_current_values.begin(),
300 _current_values.end(),
301 [it](const MooseEnumItem & item) { return item == *it; });
302 if (iter != _current_values.end())
303 _current_values.erase(iter);
304 }
305}
306
307void
312
313void
315{
316 mooseDeprecated("MultiMooseEnum::clear is deprecated, use MultiMooseEnum::clearSetValues");
318}
319
320unsigned int
322{
323 return _current_values.size();
324}
325
326void
328{
329 for (const auto & item : _current_values)
331}
332
333std::ostream &
334operator<<(std::ostream & out, const MultiMooseEnum & obj)
335{
336 out << Moose::stringify(obj._current_values, " ");
337 return out;
338}
339
340void
341MultiMooseEnum::addValidName(const std::string & name)
342{
343 addEnumerationName(name);
345}
346
348MultiMooseEnum::operator+=(const std::string & name)
349{
350 mooseDeprecated("MultiMooseEnum::operator+= is deprecated, use MultiMooseEnum::addValidName");
351 return MooseEnumBase::operator+=(name);
352}
353
354void
355MultiMooseEnum::addValidName(const std::initializer_list<std::string> & names)
356{
357 for (const auto & name : names)
358 addValidName(name);
359}
360
362MultiMooseEnum::operator+=(const std::initializer_list<std::string> & names)
363{
364 mooseDeprecated("MultiMooseEnum::operator+= is deprecated, use MultiMooseEnum::addValidName");
365 return MooseEnumBase::operator+=(names);
366}
367
368void
370{
371 for (const auto & item : names._items)
373}
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
void mooseDeprecated(Args &&... args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition MooseError.h:363
std::ostream & operator<<(std::ostream &out, const MultiMooseEnum &obj)
The base class for both the MooseEnum and MultiMooseEnum classes.
bool _allow_out_of_range
Flag to enable enumeration items not previously defined.
MooseEnumBase & operator+=(const std::string &name)
Adds an enumeration item from name.
std::set< MooseEnumItem > _items
Storage for the assigned items.
int getNextValidID() const
Compute the next valid ID.
const MooseEnumItem & addEnumerationName(const std::string &raw_name)
std::set< MooseEnumItem >::const_iterator find(const MooseEnumItem &other) const
Locate an item.
const MooseEnumItem & addEnumerationItem(const MooseEnumItem &item)
virtual void checkDeprecated() const =0
Method that must be implemented to check derived class values against the _deprecated_names.
std::string getRawNames() const
Method for returning the raw name strings for this instance.
Class for containing MooseEnum item information.
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type.
MultiMooseEnum()
Protected constructor for use by libmesh::Parameters.
const std::string & operator[](unsigned int i) const
Indexing operator Operator to retrieve an item from the MultiMooseEnum.
bool operator!=(const MultiMooseEnum &value) const
MooseEnumBase & operator+=(const std::string &name)
std::vector< MooseEnumItem > _current_values
The current value(s) of the MultiMooseEnum.
void eraseSetValue(const std::string &names)
Un-assign, or unset a value.
void erase(const std::string &names)
Un-assign, or unset a value.
void setAdditionalValue(const std::string &names)
Insert operators Operator to insert (push_back) values into the enum.
virtual void checkDeprecated() const override
Check whether any of the current values are deprecated when called.
bool operator==(const MultiMooseEnum &value) const
Comparison operators for comparing with character constants, MultiMooseEnums or integer values.
bool isValueSet(const std::string &value) const
Methods for seeing if a value is set in the MultiMooseEnum.
unsigned int get(unsigned int i) const
Indexing operator Operator to retrieve the id of an item from the MultiMooseEnum.
MooseEnumIterator end() const
void addValidName(const std::string &name)
Extends the range of possible values the variable can be set to.
MultiMooseEnum & assignValues(InputIterator first, InputIterator last, bool append)
Helper method for all inserts and assignment operators.
void push_back(const std::string &names)
Insert operators Operator to insert (push_back) values into the enum.
unsigned int size() const
Return the number of active items in the MultiMooseEnum.
void clearSetValues()
Clear the MultiMooseEnum.
MooseEnumIterator begin() const
Returns a begin/end iterator to all of the set values in the enum.
MultiMooseEnum & operator=(const MultiMooseEnum &other_enum)=default
Copy Assignment operator must be explicitly defined when a copy ctor exists and this method is used.
void removeSetValues(InputIterator first, InputIterator last)
Helper method for un-assigning enumeration values.
void tokenize(const std::string &str, std::vector< T > &elements, unsigned int min_len=1, const std::string &delims="/")
This function will split the passed in string on a set of delimiters appending the substrings to the ...
std::string stringify(const T &t)
conversion to string
Definition Conversion.h:64