https://mooseframework.inl.gov
MultiMooseEnum.h
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 #pragma once
11 
12 // MOOSE includes
13 #include "Moose.h"
14 #include "MooseEnumBase.h"
15 #include "MooseError.h"
16 
17 // C++ includes
18 #include <vector>
19 
20 // Forward declarations
21 class ExecFlagEnum;
22 namespace libMesh
23 {
24 class Parameters;
25 }
26 
27 typedef std::vector<MooseEnumItem>::const_iterator MooseEnumIterator;
28 
42 {
43 public:
45 
54  MultiMooseEnum(std::string valid_names,
55  std::string initialization_values,
56  bool allow_out_of_range = false);
57  // We need to explicitly define this version so MultiMooseEnum("one two", "one")
58  // doesn't implicitly convert "one" to a bool and use the 2-parameter constructor
59  MultiMooseEnum(std::string valid_names,
60  const char * initialization_values,
61  bool allow_out_of_range = false);
63 
72  MultiMooseEnum(std::string valid_names, bool allow_out_of_range = false);
73 
78  MultiMooseEnum(const MultiMooseEnum & other_enum);
79 
84  MultiMooseEnum & operator=(const MultiMooseEnum & other_enum) = default;
85 
87 
93  bool operator==(const MultiMooseEnum & value) const;
94  bool operator!=(const MultiMooseEnum & value) const;
96 
98 
102  bool contains(const std::string & value) const { return isValueSet(value); }
103  bool contains(int value) const { return isValueSet(value); }
104  bool contains(unsigned short value) const { return isValueSet(value); }
105  bool contains(const MultiMooseEnum & value) const { return isValueSet(value); }
106  bool contains(const MooseEnumItem & value) const { return isValueSet(value); }
108 
109  // The following are aliases for contains with more descriptive name
111 
115  bool isValueSet(const std::string & value) const;
116  bool isValueSet(int value) const;
117  bool isValueSet(unsigned short value) const;
118  bool isValueSet(const MultiMooseEnum & value) const;
119  bool isValueSet(const MooseEnumItem & value) const;
121 
123 
128  MultiMooseEnum & operator=(const std::string & names);
129  MultiMooseEnum & operator=(const std::vector<std::string> & names);
130  MultiMooseEnum & operator=(const std::set<std::string> & names);
132 
134 
138  void erase(const std::string & names);
139  void erase(const std::vector<std::string> & names);
140  void erase(const std::set<std::string> & names);
142 
143  // The following replaces erase with a more descriptive name
145 
149  void eraseSetValue(const std::string & names);
150  void eraseSetValue(const std::vector<std::string> & names);
151  void eraseSetValue(const std::set<std::string> & names);
153 
155 
161  void push_back(const std::string & names);
162  void push_back(const std::vector<std::string> & names);
163  void push_back(const std::set<std::string> & names);
164  void push_back(const MultiMooseEnum & other_enum);
166 
167  // The following replaces push_back with a more descriptive name
169 
175  void setAdditionalValue(const std::string & names);
176  void setAdditionalValue(const std::vector<std::string> & names);
177  void setAdditionalValue(const std::set<std::string> & names);
178  void setAdditionalValue(const MultiMooseEnum & other_enum);
180 
188  const std::string & operator[](unsigned int i) const;
189 
196  unsigned int get(unsigned int i) const;
197 
199  template <typename T>
200  std::vector<T> getEnum() const;
201 
202  // The following replaces getEnum with a more descriptive name
204  template <typename T>
205  std::vector<T> getSetValueIDs() const;
206 
208 
212  MooseEnumIterator begin() const { return _current_values.begin(); }
213  MooseEnumIterator end() const { return _current_values.end(); }
215 
217 
220  void clearSetValues();
221  void clear();
223 
227  unsigned int size() const;
228 
233  virtual bool isValid() const override { return !_current_values.empty(); }
234 
235  // InputParameters and Output is allowed to create an empty enum but is responsible for
236  // filling it in after the fact
237  friend class libMesh::Parameters;
238 
240  friend std::ostream & operator<<(std::ostream & out, const MultiMooseEnum & obj);
241 
242  // The following functions would add possible values
243  // (to _items) that an enumerated variable can take. However "+=" is not a
244  // descriptive enough funtion name for this and should not be used in case
245  // users get confused that the operator actually changes the set values of the
246  // variable (_current_values). We have re-implemented this logic under a method
247  // with a more descirptive name to force users to be informed. These are deprecated.
248  MooseEnumBase & operator+=(const std::string & name);
249  MooseEnumBase & operator+=(const std::initializer_list<std::string> & names);
250 
251  // The following replaces operator+= with a more descriptive name
253  void addValidName(const std::string & name);
254  void addValidName(const std::initializer_list<std::string> & names);
255  void addValidName(const MultiMooseEnum & names);
256 
257 protected:
259  virtual void checkDeprecated() const override;
260 
266  template <typename InputIterator>
267  MultiMooseEnum & assignValues(InputIterator first, InputIterator last, bool append);
268 
274  template <typename InputIterator>
275  void removeSetValues(InputIterator first, InputIterator last);
276 
278  std::vector<MooseEnumItem> _current_values;
279 
283  MultiMooseEnum();
284 
289  MultiMooseEnum(const MooseEnumBase & other_enum);
290 };
291 
292 template <typename T>
293 std::vector<T>
295 {
296 #ifdef LIBMESH_HAVE_CXX11_TYPE_TRAITS
297  static_assert(std::is_enum<T>::value == true,
298  "The type requested from MooseEnum::getEnum must be an enum type!\n\n");
299 #endif
300  std::vector<T> enum_vec;
301  for (const auto & current_value : _current_values)
302  enum_vec.push_back(static_cast<T>(current_value.id()));
303  return enum_vec;
304 }
305 
306 template <typename T>
307 std::vector<T>
309 {
310  mooseDeprecated("MultiMooseEnum::getEnum is deprecated, use MultiMooseEnum::getSetValueIDs");
311  return MultiMooseEnum::getSetValueIDs<T>();
312 }
const std::string & operator[](unsigned int i) const
Indexing operator Operator to retrieve an item from the MultiMooseEnum.
virtual void checkDeprecated() const override
Check whether any of the current values are deprecated when called.
friend std::ostream & operator<<(std::ostream &out, const MultiMooseEnum &obj)
Operator for printing to iostreams.
MultiMooseEnum & assignValues(InputIterator first, InputIterator last, bool append)
Helper method for all inserts and assignment operators.
A MultiMooseEnum object to hold "execute_on" flags.
Definition: ExecFlagEnum.h:21
bool operator!=(const MultiMooseEnum &value) const
MooseEnumIterator begin() const
Returns a begin/end iterator to all of the set values in the enum.
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.
virtual bool isValid() const override
IsValid.
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
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...
The base class for both the MooseEnum and MultiMooseEnum classes.
Definition: MooseEnumBase.h:24
MooseEnumBase & operator+=(const std::string &name)
bool contains(const std::string &value) const
Methods for seeing if a value is set in the MultiMooseEnum.
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
bool contains(unsigned short value) const
bool contains(int value) const
std::vector< MooseEnumItem >::const_iterator MooseEnumIterator
void mooseDeprecated(Args &&... args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition: MooseError.h:384
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::vector< T > getSetValueIDs() const
get the current values cast to a vector of enum type T
std::vector< T > getEnum() const
get the current values cast to a vector of enum type T. Deprecated, use getSetValueIDs instead...
bool contains(const MooseEnumItem &value) const
MultiMooseEnum & operator=(const MultiMooseEnum &other_enum)=default
Copy Assignment operator must be explicitly defined when a copy ctor exists and this method is used...
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.
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.
bool contains(const MultiMooseEnum &value) const
OStreamProxy out(std::cout)
void clearSetValues()
Clear the MultiMooseEnum.
void eraseSetValue(const std::string &names)
Un-assign, or unset a value.