https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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
21class ExecFlagEnum;
22namespace libMesh
23{
24class Parameters;
25}
26
27typedef std::vector<MooseEnumItem>::const_iterator MooseEnumIterator;
28
42{
43public:
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
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
257protected:
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
284
289 MultiMooseEnum(const MooseEnumBase & other_enum);
290};
291
292template <typename T>
293std::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
306template <typename T>
307std::vector<T>
309{
310 mooseDeprecated("MultiMooseEnum::getEnum is deprecated, use MultiMooseEnum::getSetValueIDs");
311 return MultiMooseEnum::getSetValueIDs<T>();
312}
void mooseDeprecated(Args &&... args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition MooseError.h:363
std::vector< MooseEnumItem >::const_iterator MooseEnumIterator
A MultiMooseEnum object to hold "execute_on" flags.
The base class for both the MooseEnum and MultiMooseEnum classes.
Class for containing MooseEnum item information.
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type.
bool contains(unsigned short value) const
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)
bool contains(int value) const
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.
friend std::ostream & operator<<(std::ostream &out, const MultiMooseEnum &obj)
Operator for printing to iostreams.
bool operator==(const MultiMooseEnum &value) const
Comparison operators for comparing with character constants, MultiMooseEnums or integer values.
std::vector< T > getSetValueIDs() const
get the current values cast to a vector of enum type T
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.
bool contains(const std::string &value) const
Methods for seeing if a value is set in the MultiMooseEnum.
MooseEnumIterator end() const
void addValidName(const std::string &name)
Extends the range of possible values the variable can be set to.
bool contains(const MooseEnumItem &value) const
virtual bool isValid() const override
IsValid.
std::vector< T > getEnum() const
get the current values cast to a vector of enum type T. Deprecated, use getSetValueIDs instead.
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.
bool contains(const MultiMooseEnum &value) const
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...