www.mooseframework.org
Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | Static Protected Attributes | Private Member Functions | Private Attributes | Friends | List of all members
MooseEnum Class Reference

This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It should be initialized with a space-delimited list of strings which become the enum values. More...

#include <MooseEnum.h>

Inheritance diagram for MooseEnum:
[legend]

Public Types

enum  CompareMode { CompareMode::COMPARE_NAME, CompareMode::COMPARE_ID, CompareMode::COMPARE_BOTH }
 Enum item for controlling comparison in the compareCurrent method. More...
 

Public Member Functions

 MooseEnum (std::string names, std::string default_name="", bool allow_out_of_range=false)
 Constructor that takes a list of enumeration values, and a separate string to set a default for this instance. More...
 
 MooseEnum (const MooseEnum &other_enum)
 Copy Constructor for use when creating vectors of MooseEnums. More...
 
virtual ~MooseEnum ()=default
 
 operator int () const
 Cast operators to make this object behave as value_types and std::string these methods can be used so that this class behaves more like a normal value_type enumeration. More...
 
 operator std::string () const
 
bool operator== (const char *value) const
 Comparison operators for comparing with character constants, MooseEnums or integer values. More...
 
bool operator!= (const char *value) const
 
bool operator== (int value) const
 
bool operator!= (int value) const
 
bool operator== (unsigned short value) const
 
bool operator!= (unsigned short value) const
 
bool operator== (const MooseEnum &value) const
 
bool operator!= (const MooseEnum &value) const
 
bool compareCurrent (const MooseEnum &other, CompareMode mode=CompareMode::COMPARE_NAME) const
 Method for comparing currently set values between MooseEnum. More...
 
MooseEnumoperator= (const std::string &name)
 Assignment operators TODO: Perhaps we should implement an int assignment operator. More...
 
virtual bool isValid () const override
 IsValid. More...
 
template<typename T >
getEnum () const
 get the current value cast to the enum type T More...
 
void deprecate (const std::string &name, const std::string &new_name="")
 Deprecates various options in the MOOSE enum. More...
 
std::vector< std::string > getNames () const
 Method for returning a vector of all valid enumeration names for this instance. More...
 
std::string getRawNames () const
 Method for returning the raw name strings for this instance. More...
 
bool isOutOfRangeAllowed () const
 isOutOfRangeAllowed More...
 

Protected Member Functions

virtual void checkDeprecated () const override
 Check whether the current value is deprecated when called. More...
 
void fillNames (std::string names, std::string option_delim=" ")
 Populates the _names vector. More...
 
void checkDeprecatedBase (const std::string &name_upper) const
 Check and warn deprecated values. More...
 
std::set< MooseEnumItem >::const_iterator find (const MooseEnumItem &other) const
 Locate an item. More...
 
std::set< MooseEnumItem >::const_iterator find (const std::string &name) const
 
std::set< MooseEnumItem >::const_iterator find (int id) const
 

Protected Attributes

std::set< MooseEnumItem_items
 Storage for the assigned items. More...
 
std::map< std::string, std::string > _deprecated_names
 The map of deprecated names and optional replacements. More...
 
int _out_of_range_index
 The index of values assigned that are NOT values in this enum. More...
 

Static Protected Attributes

static const int INVALID_ID = std::numeric_limits<int>::min()
 Constants. More...
 

Private Member Functions

 MooseEnum ()
 Private constructor for use by libmesh::Parameters. More...
 

Private Attributes

MooseEnumItem _current
 The current id. More...
 

Friends

class libMesh::Parameters
 
std::ostream & operator<< (std::ostream &out, const MooseEnum &obj)
 Operator for printing to iostreams. More...
 

Detailed Description

This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It should be initialized with a space-delimited list of strings which become the enum values.

You may also optionally supply numeric ints for one or more values similar to a C++ enum. This is done with the "=" sign (no spaces). It can be used any place where an integer (switch statements), const char* or std::string is expected. In addition the InputParameters system has full support for this Enum type

Definition at line 37 of file MooseEnum.h.

Member Enumeration Documentation

Enum item for controlling comparison in the compareCurrent method.

Enumerator
COMPARE_NAME 
COMPARE_ID 
COMPARE_BOTH 

Definition at line 43 of file MooseEnum.h.

44  {
45  COMPARE_NAME,
46  COMPARE_ID,
47  COMPARE_BOTH
48  };

Constructor & Destructor Documentation

MooseEnum::MooseEnum ( std::string  names,
std::string  default_name = "",
bool  allow_out_of_range = false 
)

Constructor that takes a list of enumeration values, and a separate string to set a default for this instance.

Parameters
names- a list of names for this enumeration
default_name- the default value for this enumeration instance
allow_out_of_range- determines whether this enumeration will accept values outside of it's range of defined values.

Definition at line 26 of file MooseEnum.C.

27  : MooseEnumBase(names, allow_out_of_range), _current("", INVALID_ID)
28 {
29  *this = default_name;
30 }
MooseEnumBase()
Private constuctor for use by libmesh::Parameters.
Definition: MooseEnumBase.C:54
MooseEnumItem _current
The current id.
Definition: MooseEnum.h:138
static const int INVALID_ID
Constants.
MooseEnum::MooseEnum ( const MooseEnum other_enum)

Copy Constructor for use when creating vectors of MooseEnums.

Parameters
other_enum- The other enumeration to copy state from

Definition at line 32 of file MooseEnum.C.

33  : MooseEnumBase(other_enum), _current(other_enum._current)
34 {
35 }
MooseEnumBase()
Private constuctor for use by libmesh::Parameters.
Definition: MooseEnumBase.C:54
MooseEnumItem _current
The current id.
Definition: MooseEnum.h:138
virtual MooseEnum::~MooseEnum ( )
virtualdefault
MooseEnum::MooseEnum ( )
private

Private constructor for use by libmesh::Parameters.

Private constuctor for use by libmesh::Parameters.

Definition at line 40 of file MooseEnum.C.

40 : _current("", INVALID_ID) {}
MooseEnumItem _current
The current id.
Definition: MooseEnum.h:138
static const int INVALID_ID
Constants.

Member Function Documentation

void MooseEnum::checkDeprecated ( ) const
overrideprotectedvirtual

Check whether the current value is deprecated when called.

Implements MooseEnumBase.

Definition at line 148 of file MooseEnum.C.

149 {
151 }
void checkDeprecatedBase(const std::string &name_upper) const
Check and warn deprecated values.
Definition: MooseEnumBase.C:99
MooseEnumItem _current
The current id.
Definition: MooseEnum.h:138
const std::string & name() const
Definition: MooseEnumItem.h:39
void MooseEnumBase::checkDeprecatedBase ( const std::string &  name_upper) const
protectedinherited

Check and warn deprecated values.

Definition at line 99 of file MooseEnumBase.C.

Referenced by MultiMooseEnum::assign(), checkDeprecated(), MultiMooseEnum::checkDeprecated(), MooseEnumBase::isOutOfRangeAllowed(), and operator=().

100 {
101  std::map<std::string, std::string>::const_iterator it = _deprecated_names.find(name_upper);
102 
103  if (it != _deprecated_names.end())
104  {
105  if (it->second != "")
106  mooseWarning(name_upper + " is deprecated, consider using " + it->second);
107  else
108  mooseWarning(name_upper + " is deprecated");
109  }
110 }
std::map< std::string, std::string > _deprecated_names
The map of deprecated names and optional replacements.
void mooseWarning(Args &&...args)
Emit a warning message with the given stringified, concatenated args.
Definition: MooseError.h:194
bool MooseEnum::compareCurrent ( const MooseEnum other,
CompareMode  mode = CompareMode::COMPARE_NAME 
) const

Method for comparing currently set values between MooseEnum.

Definition at line 117 of file MooseEnum.C.

118 {
119  switch (mode)
120  {
122  return (_current.id() == other._current.id()) && (_current.name() == other._current.name());
124  return _current.name() == other._current.name();
126  return _current.id() == other._current.id();
127  }
128  return false;
129 }
const int & id() const
Return the numeric, name, or raw name.
Definition: MooseEnumItem.h:38
MooseEnumItem _current
The current id.
Definition: MooseEnum.h:138
const std::string & name() const
Definition: MooseEnumItem.h:39
void MooseEnumBase::deprecate ( const std::string &  name,
const std::string &  new_name = "" 
)
inherited

Deprecates various options in the MOOSE enum.

For each deprecated option, you may supply an option new option that will be used in a message telling the user which new option replaces the old one.

Definition at line 57 of file MooseEnumBase.C.

58 {
59  std::string upper(MooseUtils::toUpper(name));
60  std::string upper_new(MooseUtils::toUpper(new_name));
61  _deprecated_names[upper] = upper_new;
63 }
std::string toUpper(const std::string &name)
Convert supplied string to upper case.
Definition: MooseUtils.C:555
std::map< std::string, std::string > _deprecated_names
The map of deprecated names and optional replacements.
virtual void checkDeprecated() const =0
void MooseEnumBase::fillNames ( std::string  names,
std::string  option_delim = " " 
)
protectedinherited

Populates the _names vector.

Parameters
names- a space separated list of names used to populate the internal names vector

Definition at line 66 of file MooseEnumBase.C.

Referenced by MooseEnumBase::isOutOfRangeAllowed(), and MooseEnumBase::MooseEnumBase().

67 {
68  std::vector<std::string> elements;
69  // split on spaces
70  MooseUtils::tokenize(names, elements, 1, option_delim);
71 
72  int value = 0;
73  for (unsigned int i = 0; i < elements.size(); ++i)
74  {
75  std::vector<std::string> name_value;
76 
77  // Make sure the option is not malformed
78  if (elements[i].find_first_of('=') == 0 ||
79  elements[i].find_last_of('=') == elements[i].length() - 1)
80  mooseError("You cannot place whitespace around the '=' character in MooseEnumBase");
81 
82  // split on equals sign
83  MooseUtils::tokenize(MooseUtils::trim(elements[i]), name_value, 1, "=");
84 
85  if (name_value.size() < 1 || name_value.size() > 2)
86  mooseError("Invalid option supplied in MooseEnumBase: ", elements[i]);
87 
88  // See if there is a value supplied for this option
89  // strtol allows for proper conversions of both int and hex strings
90  if (name_value.size() == 2)
91  value = strtol(name_value[1].c_str(), NULL, 0);
92 
93  // create item entry
94  _items.emplace(name_value[0], value++);
95  }
96 }
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
void mooseError(Args &&...args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:182
std::set< MooseEnumItem > _items
Storage for the assigned items.
std::string trim(const std::string &str, const std::string &white_space=" \t\n\v\f\r")
Standard scripting language trim function.
Definition: MooseUtils.C:95
std::set< MooseEnumItem >::const_iterator MooseEnumBase::find ( const MooseEnumItem other) const
protectedinherited

Locate an item.

Definition at line 144 of file MooseEnumBase.C.

Referenced by MultiMooseEnum::assign(), MooseEnumBase::isOutOfRangeAllowed(), operator=(), and operator==().

145 {
146  return std::find_if(
147  _items.begin(), _items.end(), [&other](MooseEnumItem const & item) { return item == other; });
148 }
std::set< MooseEnumItem > _items
Storage for the assigned items.
Class for containing MooseEnum item information.
Definition: MooseEnumItem.h:24
std::set< MooseEnumItem >::const_iterator MooseEnumBase::find ( const std::string &  name) const
protectedinherited

Definition at line 129 of file MooseEnumBase.C.

130 {
131  return std::find_if(_items.begin(), _items.end(), [&name](MooseEnumItem const & item) {
132  return item.name() == name;
133  });
134 }
std::set< MooseEnumItem > _items
Storage for the assigned items.
Class for containing MooseEnum item information.
Definition: MooseEnumItem.h:24
std::set< MooseEnumItem >::const_iterator MooseEnumBase::find ( int  id) const
protectedinherited

Definition at line 137 of file MooseEnumBase.C.

138 {
139  return std::find_if(
140  _items.begin(), _items.end(), [&id](MooseEnumItem const & item) { return item.id() == id; });
141 }
std::set< MooseEnumItem > _items
Storage for the assigned items.
Class for containing MooseEnum item information.
Definition: MooseEnumItem.h:24
template<typename T >
T MooseEnum::getEnum ( ) const

get the current value cast to the enum type T

Definition at line 143 of file MooseEnum.h.

144 {
145 #ifdef LIBMESH_HAVE_CXX11_TYPE_TRAITS
146  static_assert(std::is_enum<T>::value == true,
147  "The type requested from MooseEnum::getEnum must be an enum type!\n\n");
148 #endif
149  return static_cast<T>(_current.id());
150 }
const int & id() const
Return the numeric, name, or raw name.
Definition: MooseEnumItem.h:38
MooseEnumItem _current
The current id.
Definition: MooseEnum.h:138
std::vector< std::string > MooseEnumBase::getNames ( ) const
inherited

Method for returning a vector of all valid enumeration names for this instance.

Returns
a vector of names

Definition at line 113 of file MooseEnumBase.C.

114 {
115  std::vector<std::string> out;
116  out.reserve(_items.size());
117  for (auto it = _items.begin(); it != _items.end(); ++it)
118  out.push_back(it->name());
119  return out;
120 }
std::set< MooseEnumItem > _items
Storage for the assigned items.
std::string MooseEnumBase::getRawNames ( ) const
inherited

Method for returning the raw name strings for this instance.

Returns
a space separated list of names

Definition at line 123 of file MooseEnumBase.C.

Referenced by MultiMooseEnum::assign(), AdvancedOutput::enableOutputTypes(), operator=(), operator==(), validParams< LeastSquaresFit >(), validParams< MultiAppTransfer >(), validParams< PiecewiseConstant >(), and validParams< Residual >().

124 {
125  return Moose::stringify(_items, " ");
126 }
std::string stringify(const T &t)
conversion to string
Definition: Conversion.h:66
std::set< MooseEnumItem > _items
Storage for the assigned items.
bool MooseEnumBase::isOutOfRangeAllowed ( ) const
inlineinherited

isOutOfRangeAllowed

Returns
- a Boolean indicating whether enum names out of range are allowed

Definition at line 83 of file MooseEnumBase.h.

83 { return _out_of_range_index; }
int _out_of_range_index
The index of values assigned that are NOT values in this enum.
virtual bool MooseEnum::isValid ( ) const
inlineoverridevirtual

IsValid.

Returns
- a Boolean indicating whether this Enumeration has been set

Implements MooseEnumBase.

Definition at line 110 of file MooseEnum.h.

Referenced by MultiAppPostprocessorTransfer::MultiAppPostprocessorTransfer().

110 { return _current.id() > INVALID_ID; }
const int & id() const
Return the numeric, name, or raw name.
Definition: MooseEnumItem.h:38
MooseEnumItem _current
The current id.
Definition: MooseEnum.h:138
static const int INVALID_ID
Constants.
MooseEnum::operator int ( ) const
inline

Cast operators to make this object behave as value_types and std::string these methods can be used so that this class behaves more like a normal value_type enumeration.

Definition at line 72 of file MooseEnum.h.

72 { return _current.id(); }
const int & id() const
Return the numeric, name, or raw name.
Definition: MooseEnumItem.h:38
MooseEnumItem _current
The current id.
Definition: MooseEnum.h:138
MooseEnum::operator std::string ( ) const
inline

Definition at line 73 of file MooseEnum.h.

73 { return _current.rawName(); }
const std::string & rawName() const
Definition: MooseEnumItem.h:40
MooseEnumItem _current
The current id.
Definition: MooseEnum.h:138
bool MooseEnum::operator!= ( const char *  value) const

Definition at line 87 of file MooseEnum.C.

88 {
89  return !(*this == name);
90 }
bool MooseEnum::operator!= ( int  value) const

Definition at line 99 of file MooseEnum.C.

100 {
101  return value != _current;
102 }
MooseEnumItem _current
The current id.
Definition: MooseEnum.h:138
bool MooseEnum::operator!= ( unsigned short  value) const

Definition at line 111 of file MooseEnum.C.

112 {
113  return value != _current;
114 }
MooseEnumItem _current
The current id.
Definition: MooseEnum.h:138
bool MooseEnum::operator!= ( const MooseEnum value) const

Definition at line 140 of file MooseEnum.C.

141 {
142  mooseDeprecated("This method will be removed becuase the meaning is not well defined, please use "
143  "the 'compareCurrent' method instead.");
144  return value._current.name() != _current.name();
145 }
void mooseDeprecated(Args &&...args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition: MooseError.h:202
MooseEnumItem _current
The current id.
Definition: MooseEnum.h:138
const std::string & name() const
Definition: MooseEnumItem.h:39
MooseEnum & MooseEnum::operator= ( const std::string &  name)

Assignment operators TODO: Perhaps we should implement an int assignment operator.

Parameters
name- a string representing one of the enumeration values.
Returns
A reference to this object for chaining

Definition at line 43 of file MooseEnum.C.

44 {
45  if (name == "")
46  {
48  return *this;
49  }
50 
51  std::string upper(MooseUtils::toUpper(name));
52  checkDeprecatedBase(upper);
53 
54  std::set<MooseEnumItem>::const_iterator iter = find(upper);
55  if (iter == _items.end())
56  {
57  if (_out_of_range_index == 0) // Are out of range values allowed?
58  mooseError(std::string("Invalid option \"") + upper +
59  "\" in MooseEnum. Valid options (not case-sensitive) are \"" + getRawNames() +
60  "\".");
61  else
62  {
64  _items.insert(_current);
65  }
66  }
67  else
68  _current = *iter;
69 
70  return *this;
71 }
void checkDeprecatedBase(const std::string &name_upper) const
Check and warn deprecated values.
Definition: MooseEnumBase.C:99
std::string toUpper(const std::string &name)
Convert supplied string to upper case.
Definition: MooseUtils.C:555
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.
MooseEnumItem _current
The current id.
Definition: MooseEnum.h:138
static const int INVALID_ID
Constants.
std::set< MooseEnumItem > _items
Storage for the assigned items.
std::string getRawNames() const
Method for returning the raw name strings for this instance.
Class for containing MooseEnum item information.
Definition: MooseEnumItem.h:24
std::set< MooseEnumItem >::const_iterator find(const MooseEnumItem &other) const
Locate an item.
bool MooseEnum::operator== ( const char *  value) const

Comparison operators for comparing with character constants, MooseEnums or integer values.

Parameters
value- RHS value to compare against
Returns
bool - the truth value for the comparison

Definition at line 74 of file MooseEnum.C.

75 {
76  std::string upper(MooseUtils::toUpper(name));
77 
78  mooseAssert(_out_of_range_index != 0 || find(upper) != _items.end(),
79  std::string("Invalid string comparison \"") + upper +
80  "\" in MooseEnum. Valid options (not case-sensitive) are \"" + getRawNames() +
81  "\".");
82 
83  return _current == upper;
84 }
std::string toUpper(const std::string &name)
Convert supplied string to upper case.
Definition: MooseUtils.C:555
int _out_of_range_index
The index of values assigned that are NOT values in this enum.
MooseEnumItem _current
The current id.
Definition: MooseEnum.h:138
std::set< MooseEnumItem > _items
Storage for the assigned items.
std::string getRawNames() const
Method for returning the raw name strings for this instance.
std::set< MooseEnumItem >::const_iterator find(const MooseEnumItem &other) const
Locate an item.
bool MooseEnum::operator== ( int  value) const

Definition at line 93 of file MooseEnum.C.

94 {
95  return value == _current;
96 }
MooseEnumItem _current
The current id.
Definition: MooseEnum.h:138
bool MooseEnum::operator== ( unsigned short  value) const

Definition at line 105 of file MooseEnum.C.

106 {
107  return value == _current;
108 }
MooseEnumItem _current
The current id.
Definition: MooseEnum.h:138
bool MooseEnum::operator== ( const MooseEnum value) const

Definition at line 132 of file MooseEnum.C.

133 {
134  mooseDeprecated("This method will be removed becuase the meaning is not well defined, please use "
135  "the 'compareCurrent' method instead.");
136  return value._current.name() == _current.name();
137 }
void mooseDeprecated(Args &&...args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition: MooseError.h:202
MooseEnumItem _current
The current id.
Definition: MooseEnum.h:138
const std::string & name() const
Definition: MooseEnumItem.h:39

Friends And Related Function Documentation

friend class libMesh::Parameters
friend

Definition at line 114 of file MooseEnum.h.

std::ostream& operator<< ( std::ostream &  out,
const MooseEnum obj 
)
friend

Operator for printing to iostreams.

Definition at line 117 of file MooseEnum.h.

118  {
119  out << obj._current.rawName();
120  return out;
121  }
const std::string & rawName() const
Definition: MooseEnumItem.h:40
MooseEnumItem _current
The current id.
Definition: MooseEnum.h:138

Member Data Documentation

MooseEnumItem MooseEnum::_current
private

The current id.

Definition at line 138 of file MooseEnum.h.

Referenced by checkDeprecated(), compareCurrent(), operator!=(), operator=(), and operator==().

std::map<std::string, std::string> MooseEnumBase::_deprecated_names
protectedinherited

The map of deprecated names and optional replacements.

Definition at line 116 of file MooseEnumBase.h.

Referenced by MooseEnumBase::checkDeprecatedBase(), and MooseEnumBase::deprecate().

std::set<MooseEnumItem> MooseEnumBase::_items
protectedinherited
int MooseEnumBase::_out_of_range_index
protectedinherited

The index of values assigned that are NOT values in this enum.

If this index is 0 (false) then out of range values are not allowed.

Definition at line 122 of file MooseEnumBase.h.

Referenced by MultiMooseEnum::assign(), MooseEnumBase::isOutOfRangeAllowed(), operator=(), and operator==().

const int MooseEnumBase::INVALID_ID = std::numeric_limits<int>::min()
staticprotectedinherited

Constants.

Definition at line 125 of file MooseEnumBase.h.

Referenced by operator=().


The documentation for this class was generated from the following files: