https://mooseframework.inl.gov
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 
24 MultiMooseEnum::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 
32 MultiMooseEnum::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 
39 MultiMooseEnum::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 
55 MultiMooseEnum::MultiMooseEnum(const MooseEnumBase & other_enum) : MooseEnumBase(other_enum) {}
56 
57 bool
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 
69 bool
71 {
72  return !(*this == value);
73 }
74 
75 bool
76 MultiMooseEnum::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 
84 bool
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 
93 bool
94 MultiMooseEnum::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 
102 bool
104 {
105  for (const auto & item : value._current_values)
106  if (!isValueSet(item))
107  return false;
108  return true;
109 }
110 
111 bool
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 
121 MultiMooseEnum::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 
129 MultiMooseEnum::operator=(const std::vector<std::string> & names)
130 {
131  return assignValues(names.begin(), names.end(), false);
132 }
133 
135 MultiMooseEnum::operator=(const std::set<std::string> & names)
136 {
137  return assignValues(names.begin(), names.end(), false);
138 }
139 
140 void
141 MultiMooseEnum::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 
148 void
149 MultiMooseEnum::erase(const std::string & names)
150 {
151  mooseDeprecated("MultiMooseEnum::erase is deprecated, use MultiMooseEnum::eraseSetValue");
153 }
154 
155 void
156 MultiMooseEnum::eraseSetValue(const std::vector<std::string> & names)
157 {
158  removeSetValues(names.begin(), names.end());
159 }
160 
161 void
162 MultiMooseEnum::erase(const std::vector<std::string> & names)
163 {
164  mooseDeprecated("MultiMooseEnum::erase is deprecated, use MultiMooseEnum::eraseSetValue");
166 }
167 
168 void
169 MultiMooseEnum::eraseSetValue(const std::set<std::string> & names)
170 {
171  removeSetValues(names.begin(), names.end());
172 }
173 
174 void
175 MultiMooseEnum::erase(const std::set<std::string> & names)
176 {
177  mooseDeprecated("MultiMooseEnum::erase is deprecated, use MultiMooseEnum::eraseSetValue");
179 }
180 
181 void
182 MultiMooseEnum::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 
189 void
190 MultiMooseEnum::push_back(const std::string & names)
191 {
193  "MultiMooseEnum::push_back is deprecated, use MultiMooseEnum::setAdditionalValue");
195 }
196 
197 void
198 MultiMooseEnum::setAdditionalValue(const std::vector<std::string> & names)
199 {
200  assignValues(names.begin(), names.end(), true);
201 }
202 
203 void
204 MultiMooseEnum::push_back(const std::vector<std::string> & names)
205 {
207  "MultiMooseEnum::push_back is deprecated, use MultiMooseEnum::setAdditionalValue");
209 }
210 
211 void
212 MultiMooseEnum::setAdditionalValue(const std::set<std::string> & names)
213 {
214  assignValues(names.begin(), names.end(), true);
215 }
216 
217 void
218 MultiMooseEnum::push_back(const std::set<std::string> & names)
219 {
221  "MultiMooseEnum::push_back is deprecated, use MultiMooseEnum::setAdditionalValue");
223 }
224 
225 void
227 {
228  assignValues(other_enum.begin(), other_enum.end(), true);
229 }
230 
231 void
233 {
235  "MultiMooseEnum::push_back is deprecated, use MultiMooseEnum::setAdditionalValue");
237 }
238 
239 const std::string &
240 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  return _current_values[i].rawName();
247 }
248 
249 unsigned int
250 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  return _current_values[i].id();
257 }
258 
259 template <typename InputIterator>
261 MultiMooseEnum::assignValues(InputIterator first, InputIterator last, bool append)
262 {
263  if (!append)
264  clearSetValues();
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  }
287  checkDeprecated();
288  return *this;
289 }
290 
291 template <typename InputIterator>
292 void
293 MultiMooseEnum::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 
307 void
309 {
310  _current_values.clear();
311 }
312 
313 void
315 {
316  mooseDeprecated("MultiMooseEnum::clear is deprecated, use MultiMooseEnum::clearSetValues");
317  clearSetValues();
318 }
319 
320 unsigned int
322 {
323  return _current_values.size();
324 }
325 
326 void
328 {
329  for (const auto & item : _current_values)
331 }
332 
333 std::ostream &
334 operator<<(std::ostream & out, const MultiMooseEnum & obj)
335 {
336  out << Moose::stringify(obj._current_values, " ");
337  return out;
338 }
339 
340 void
341 MultiMooseEnum::addValidName(const std::string & name)
342 {
344  checkDeprecated();
345 }
346 
348 MultiMooseEnum::operator+=(const std::string & name)
349 {
350  mooseDeprecated("MultiMooseEnum::operator+= is deprecated, use MultiMooseEnum::addValidName");
352 }
353 
354 void
355 MultiMooseEnum::addValidName(const std::initializer_list<std::string> & names)
356 {
357  for (const auto & name : names)
359 }
360 
362 MultiMooseEnum::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 
368 void
370 {
371  for (const auto & item : names._items)
373 }
std::ostream & operator<<(std::ostream &out, const MultiMooseEnum &obj)
const std::string & operator[](unsigned int i) const
Indexing operator Operator to retrieve an item from the MultiMooseEnum.
std::string name(const ElemQuality q)
int getNextValidID() const
Compute the next valid ID.
virtual void checkDeprecated() const override
Check whether any of the current values are deprecated when called.
MultiMooseEnum & assignValues(InputIterator first, InputIterator last, bool append)
Helper method for all inserts and assignment operators.
bool operator!=(const MultiMooseEnum &value) const
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 ...
MooseEnumIterator begin() const
Returns a begin/end iterator to all of the set values in the enum.
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
void setAdditionalValue(const std::string &names)
Insert operators Operator to insert (push_back) values into the enum.
MultiMooseEnum()
Protected constructor for use by libmesh::Parameters.
void addValidName(const std::string &name)
Extends the range of possible values the variable can be set to.
void removeSetValues(InputIterator first, InputIterator last)
Helper method for un-assigning enumeration values.
unsigned int size() const
Return the number of active items in the MultiMooseEnum.
MooseEnumIterator end() const
std::string getRawNames() const
Method for returning the raw name strings for this instance.
The base class for both the MooseEnum and MultiMooseEnum classes.
Definition: MooseEnumBase.h:24
MooseEnumBase & operator+=(const std::string &name)
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
const MooseEnumItem & addEnumerationName(const std::string &raw_name)
Definition: MooseEnumBase.C:73
virtual void checkDeprecated() const =0
Method that must be implemented to check derived class values against the _deprecated_names.
const MooseEnumItem & addEnumerationItem(const MooseEnumItem &item)
void mooseDeprecated(Args &&... args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition: MooseError.h:353
bool isValueSet(const std::string &value) const
Methods for seeing if a value is set in the MultiMooseEnum.
void erase(const std::string &names)
Un-assign, or unset a value.
std::string stringify(const T &t)
conversion to string
Definition: Conversion.h:64
std::set< MooseEnumItem > _items
Storage for the assigned items.
MultiMooseEnum & operator=(const MultiMooseEnum &other_enum)=default
Copy Assignment operator must be explicitly defined when a copy ctor exists and this method is used...
unsigned int get(unsigned int i) const
Indexing operator Operator to retrieve the id of an item from the MultiMooseEnum. ...
Class for containing MooseEnum item information.
Definition: MooseEnumItem.h:18
bool operator==(const MultiMooseEnum &value) const
Comparison operators for comparing with character constants, MultiMooseEnums or integer values...
void push_back(const std::string &names)
Insert operators Operator to insert (push_back) values into the enum.
OStreamProxy out
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type...
std::vector< MooseEnumItem > _current_values
The current value(s) of the MultiMooseEnum.
std::set< MooseEnumItem >::const_iterator find(const MooseEnumItem &other) const
Locate an item.
void clearSetValues()
Clear the MultiMooseEnum.
void eraseSetValue(const std::string &names)
Un-assign, or unset a value.
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.