www.mooseframework.org
MultiMooseEnum.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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 "MultiMooseEnum.h"
11 #include "MooseUtils.h"
12 #include "MooseError.h"
13 #include "Conversion.h"
14 
15 #include <sstream>
16 #include <algorithm>
17 #include <iterator>
18 #include <limits>
19 #include <string>
20 #include <iostream>
21 
23  std::string default_names,
24  bool allow_out_of_range)
25  : MooseEnumBase(names, allow_out_of_range)
26 {
27  *this = default_names;
28 }
29 
31  : MooseEnumBase(other_enum), _current(other_enum._current)
32 {
33 }
34 
39 
40 MultiMooseEnum::MultiMooseEnum(const MooseEnumBase & other_enum) : MooseEnumBase(other_enum) {}
41 
42 bool
44 {
45  // Not the same if the lengths are different
46  if (value.size() != size())
47  return false;
48 
49  // Return false if this enum does not contain an item from the other, since they are the same
50  // size at this point if this is true then they are equal.
51  return contains(value);
52 }
53 
54 bool
56 {
57  return !(*this == value);
58 }
59 
60 bool
61 MultiMooseEnum::contains(const std::string & value) const
62 {
63  return std::find_if(_current.begin(), _current.end(), [&value](const MooseEnumItem & item) {
64  return item == value;
65  }) != _current.end();
66 }
67 
68 bool
69 MultiMooseEnum::contains(int value) const
70 {
71  return std::find_if(_current.begin(), _current.end(), [&value](const MooseEnumItem & item) {
72  return item == value;
73  }) != _current.end();
74 }
75 
76 bool
77 MultiMooseEnum::contains(unsigned short value) const
78 {
79  return std::find_if(_current.begin(), _current.end(), [&value](const MooseEnumItem & item) {
80  return item == value;
81  }) != _current.end();
82 }
83 
84 bool
86 {
87  for (const auto & item : value._current)
88  if (!contains(item))
89  return false;
90  return true;
91 }
92 
93 bool
95 {
96  return std::find_if(_current.begin(), _current.end(), [&value](const MooseEnumItem & item) {
97  return item == value;
98  }) != _current.end();
99 }
100 
102 MultiMooseEnum::operator=(const std::string & names)
103 {
104  std::vector<std::string> names_vector;
105  MooseUtils::tokenize(names, names_vector, 1, " ");
106  return assign(names_vector.begin(), names_vector.end(), false);
107 }
108 
110 MultiMooseEnum::operator=(const std::vector<std::string> & names)
111 {
112  return assign(names.begin(), names.end(), false);
113 }
114 
116 MultiMooseEnum::operator=(const std::set<std::string> & names)
117 {
118  return assign(names.begin(), names.end(), false);
119 }
120 
121 void
122 MultiMooseEnum::erase(const std::string & names)
123 {
124  std::vector<std::string> names_vector;
125  MooseUtils::tokenize(names, names_vector, 1, " ");
126  remove(names_vector.begin(), names_vector.end());
127 }
128 
129 void
130 MultiMooseEnum::erase(const std::vector<std::string> & names)
131 {
132  remove(names.begin(), names.end());
133 }
134 
135 void
136 MultiMooseEnum::erase(const std::set<std::string> & names)
137 {
138  remove(names.begin(), names.end());
139 }
140 
141 void
142 MultiMooseEnum::push_back(const std::string & names)
143 {
144  std::vector<std::string> names_vector;
145  MooseUtils::tokenize(names, names_vector, 1, " ");
146  assign(names_vector.begin(), names_vector.end(), true);
147 }
148 
149 void
150 MultiMooseEnum::push_back(const std::vector<std::string> & names)
151 {
152  assign(names.begin(), names.end(), true);
153 }
154 
155 void
156 MultiMooseEnum::push_back(const std::set<std::string> & names)
157 {
158  assign(names.begin(), names.end(), true);
159 }
160 
161 const std::string & MultiMooseEnum::operator[](unsigned int i) const
162 {
163  mooseAssert(i < _current.size(),
164  "Access out of bounds in MultiMooseEnum (i: " << i << " size: " << _current.size()
165  << ")");
166 
167  return _current[i].rawName();
168 }
169 
170 unsigned int
171 MultiMooseEnum::get(unsigned int i) const
172 {
173  mooseAssert(i < _current.size(),
174  "Access out of bounds in MultiMooseEnum (i: " << i << " size: " << _current.size()
175  << ")");
176 
177  return _current[i].id();
178 }
179 
180 template <typename InputIterator>
182 MultiMooseEnum::assign(InputIterator first, InputIterator last, bool append)
183 {
184  if (!append)
185  clear();
186 
187  for (InputIterator it = first; it != last; ++it)
188  {
189  const auto iter = find(*it);
190  if (iter == _items.end())
191  {
192  if (!_allow_out_of_range) // Are out of range values allowed?
193  mooseError("Invalid option \"",
194  *it,
195  "\" in MultiMooseEnum. Valid options (not case-sensitive) are \"",
196  getRawNames(),
197  "\".");
198  else
199  {
200  MooseEnumItem created(*it, getNextValidID());
201  addEnumerationItem(created);
202  _current.push_back(created);
203  }
204  }
205  else
206  _current.push_back(*iter);
207  }
208  checkDeprecated();
209  return *this;
210 }
211 
212 template <typename InputIterator>
213 void
214 MultiMooseEnum::remove(InputIterator first, InputIterator last)
215 {
216  // Create a new list of enumerations by striping out the supplied values
217  for (InputIterator it = first; it != last; ++it)
218  {
219  std::vector<MooseEnumItem>::iterator iter = std::find_if(
220  _current.begin(), _current.end(), [it](const MooseEnumItem & item) { return item == *it; });
221  if (iter != _current.end())
222  _current.erase(iter);
223  }
224 }
225 
226 void
228 {
229  _current.clear();
230 }
231 
232 unsigned int
234 {
235  return _current.size();
236 }
237 
238 void
240 {
241  for (const auto & item : _current)
243 }
244 
245 std::ostream &
246 operator<<(std::ostream & out, const MultiMooseEnum & obj)
247 {
248  out << Moose::stringify(obj._current, " ");
249  return out;
250 }
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.
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 & assign(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 ...
Definition: MooseUtils.h:532
std::vector< MooseEnumItem > _current
The current id.
void addEnumerationItem(const MooseEnumItem &item)
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:207
MultiMooseEnum()
Protected constructor for use by libmesh::Parameters.
unsigned int size() const
Return the number of active items in the MultiMooseEnum.
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
bool contains(const std::string &value) const
Contains methods for seeing if a value is in the MultiMooseEnum.
virtual void checkDeprecated() const =0
Method that must be implemented to check derived class values against the _deprecated_names.
void erase(const std::string &names)
Un-assign a value.
std::string stringify(const T &t)
conversion to string
Definition: Conversion.h:60
void clear()
Clear the MultiMooseEnum.
std::set< MooseEnumItem > _items
Storage for the assigned items.
void remove(InputIterator first, InputIterator last)
Helper method for un-assigning enumeration values.
unsigned int get(unsigned int i) const
Indexing operator Operator to retrieve an item from the MultiMooseEnum.
Class for containing MooseEnum item information.
Definition: MooseEnumItem.h:21
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.
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.
bool _allow_out_of_range
Flag to enable enumeration items not previously defined.