www.mooseframework.org
MultiMooseEnum.C
Go to the documentation of this file.
1 /****************************************************************/
2 /* DO NOT MODIFY THIS HEADER */
3 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
4 /* */
5 /* (c) 2010 Battelle Energy Alliance, LLC */
6 /* ALL RIGHTS RESERVED */
7 /* */
8 /* Prepared by Battelle Energy Alliance, LLC */
9 /* Under Contract No. DE-AC07-05ID14517 */
10 /* With the U. S. Department of Energy */
11 /* */
12 /* See COPYRIGHT for full restrictions */
13 /****************************************************************/
14 
15 #include "MultiMooseEnum.h"
16 #include "MooseUtils.h"
17 #include "MooseError.h"
18 #include "InfixIterator.h"
19 #include "Conversion.h"
20 
21 #include <sstream>
22 #include <algorithm>
23 #include <iterator>
24 #include <limits>
25 #include <string>
26 #include <iostream>
27 
29  std::string default_names,
30  bool allow_out_of_range)
31  : MooseEnumBase(names, allow_out_of_range)
32 {
33  *this = default_names;
34 }
35 
37  : MooseEnumBase(other_enum), _current(other_enum._current)
38 {
39 }
40 
45 
46 MultiMooseEnum::MultiMooseEnum(const MooseEnumBase & other_enum) : MooseEnumBase(other_enum) {}
47 
48 bool
50 {
51  // Not the same if the lengths are different
52  if (value.size() != size())
53  return false;
54 
55  // Return false if this enum does not contain an item from the other
56  return (_current == value._current) && (_items == value._items);
57 }
58 
59 bool
61 {
62  return !(*this == value);
63 }
64 
65 bool
66 MultiMooseEnum::contains(const std::string & value) const
67 {
68  std::string upper(MooseUtils::toUpper(value));
69  return std::find_if(_current.begin(), _current.end(), [&upper](const MooseEnumItem & item) {
70  return item.name() == upper;
71  }) != _current.end();
72 }
73 
74 bool
75 MultiMooseEnum::contains(int value) const
76 {
77  return std::find_if(_current.begin(), _current.end(), [&value](const MooseEnumItem & item) {
78  return item.id() == value;
79  }) != _current.end();
80 }
81 
82 bool
83 MultiMooseEnum::contains(unsigned short value) const
84 {
85  return std::find_if(_current.begin(), _current.end(), [&value](const MooseEnumItem & item) {
86  return item.id() == value;
87  }) != _current.end();
88 }
89 
90 bool
92 {
93  for (const auto & item : value._current)
94  if (!contains(item))
95  return false;
96  return true;
97 }
98 
99 bool
101 {
102  return std::find_if(_current.begin(), _current.end(), [&value](const MooseEnumItem & item) {
103  return item.id() == value.id();
104  }) != _current.end();
105 }
106 
108 MultiMooseEnum::operator=(const std::string & names)
109 {
110  std::vector<std::string> names_vector;
111  MooseUtils::tokenize(names, names_vector, 1, " ");
112  return assign(names_vector.begin(), names_vector.end(), false);
113 }
114 
116 MultiMooseEnum::operator=(const std::vector<std::string> & names)
117 {
118  return assign(names.begin(), names.end(), false);
119 }
120 
122 MultiMooseEnum::operator=(const std::set<std::string> & names)
123 {
124  return assign(names.begin(), names.end(), false);
125 }
126 
127 void
128 MultiMooseEnum::erase(const std::string & names)
129 {
130  std::vector<std::string> names_vector;
131  MooseUtils::tokenize(names, names_vector, 1, " ");
132  remove(names_vector.begin(), names_vector.end());
133 }
134 
135 void
136 MultiMooseEnum::erase(const std::vector<std::string> & names)
137 {
138  remove(names.begin(), names.end());
139 }
140 
141 void
142 MultiMooseEnum::erase(const std::set<std::string> & names)
143 {
144  remove(names.begin(), names.end());
145 }
146 
147 void
148 MultiMooseEnum::push_back(const std::string & names)
149 {
150  std::vector<std::string> names_vector;
151  MooseUtils::tokenize(names, names_vector, 1, " ");
152  assign(names_vector.begin(), names_vector.end(), true);
153 }
154 
155 void
156 MultiMooseEnum::push_back(const std::vector<std::string> & names)
157 {
158  assign(names.begin(), names.end(), true);
159 }
160 
161 void
162 MultiMooseEnum::push_back(const std::set<std::string> & names)
163 {
164  assign(names.begin(), names.end(), true);
165 }
166 
167 const std::string & MultiMooseEnum::operator[](unsigned int i) const
168 {
169  mooseAssert(i < _current.size(),
170  "Access out of bounds in MultiMooseEnum (i: " << i << " size: " << _current.size()
171  << ")");
172 
173  return _current[i].rawName();
174 }
175 
176 unsigned int
177 MultiMooseEnum::get(unsigned int i) const
178 {
179  mooseAssert(i < _current.size(),
180  "Access out of bounds in MultiMooseEnum (i: " << i << " size: " << _current.size()
181  << ")");
182 
183  return _current[i].id();
184 }
185 
186 template <typename InputIterator>
188 MultiMooseEnum::assign(InputIterator first, InputIterator last, bool append)
189 {
190  if (!append)
191  clear();
192 
193  for (InputIterator it = first; it != last; ++it)
194  {
195  std::string upper(MooseUtils::toUpper(*it));
196  checkDeprecatedBase(upper);
197  const auto iter = find(upper);
198 
199  if (iter == _items.end())
200  {
201  if (_out_of_range_index == 0) // Are out of range values allowed?
202  mooseError("Invalid option \"",
203  upper,
204  "\" in MultiMooseEnum. Valid options (not case-sensitive) are \"",
205  getRawNames(),
206  "\".");
207  else
208  {
209  MooseEnumItem created(upper, _out_of_range_index++);
210  _current.push_back(created);
211  _items.insert(created);
212  }
213  }
214  else
215  _current.push_back(*iter);
216  }
217  return *this;
218 }
219 
220 template <typename InputIterator>
221 void
222 MultiMooseEnum::remove(InputIterator first, InputIterator last)
223 {
224  // Create a new list of enumerations by striping out the supplied values
225  for (InputIterator it = first; it != last; ++it)
226  {
227  // Values stored as upper case
228  std::string upper(MooseUtils::toUpper(*it));
229  std::vector<MooseEnumItem>::iterator iter =
230  std::find_if(_current.begin(), _current.end(), [&upper](const MooseEnumItem & item) {
231  return item.name() == upper;
232  });
233  if (iter != _current.end())
234  _current.erase(iter);
235  }
236 }
237 
238 void
240 {
241  _current.clear();
242 }
243 
244 unsigned int
246 {
247  return _current.size();
248 }
249 
250 void
252 {
253  for (const auto & item : _current)
254  checkDeprecatedBase(item.name());
255 }
256 
257 std::ostream &
258 operator<<(std::ostream & out, const MultiMooseEnum & obj)
259 {
260  out << Moose::stringify(obj._current, " ");
261  return out;
262 }
friend std::ostream & operator<<(std::ostream &out, const MultiMooseEnum &obj)
Operator for printing to iostreams.
void checkDeprecatedBase(const std::string &name_upper) const
Check and warn deprecated values.
Definition: MooseEnumBase.C:99
MultiMooseEnum & assign(InputIterator first, InputIterator last, bool append)
Helper method for all inserts and assignment operators.
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 ...
Definition: MooseUtils.h:350
std::vector< MooseEnumItem > _current
The current id.
std::string toUpper(const std::string &name)
Convert supplied string to upper case.
Definition: MooseUtils.C:555
const int & id() const
Return the numeric, name, or raw name.
Definition: MooseEnumItem.h:38
MultiMooseEnum()
Private constructor for use by libmesh::Parameters.
void mooseError(Args &&...args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:182
int _out_of_range_index
The index of values assigned that are NOT values in this enum.
The base class for both the MooseEnum and MultiMooseEnum classes.
Definition: MooseEnumBase.h:30
virtual void checkDeprecated() const override
Check whether any of the current values are deprecated when called.
bool contains(const std::string &value) const
Contains methods for seeing if a value is in the MultiMooseEnum.
bool operator==(const MultiMooseEnum &value) const
Comparison operators for comparing with character constants, MultiMooseEnums or integer values...
unsigned int get(unsigned int i) const
Indexing operator Operator to retrieve an item from the MultiMooseEnum.
void erase(const std::string &names)
Un-assign a value.
std::string stringify(const T &t)
conversion to string
Definition: Conversion.h:66
void clear()
Clear the MultiMooseEnum.
std::set< MooseEnumItem > _items
Storage for the assigned items.
std::string getRawNames() const
Method for returning the raw name strings for this instance.
void remove(InputIterator first, InputIterator last)
Helper method for un-assigning enumeration values.
Class for containing MooseEnum item information.
Definition: MooseEnumItem.h:24
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 items in the MultiMooseEnum.
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
MultiMooseEnum & operator=(const std::string &names)
Assignment operators.
std::set< MooseEnumItem >::const_iterator find(const MooseEnumItem &other) const
Locate an item.
const std::string & operator[](unsigned int i) const
Indexing operator Operator to retrieve an item from the MultiMooseEnum.
bool operator!=(const MultiMooseEnum &value) const