www.mooseframework.org
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
MooseEnumBase Class Referenceabstract

The base class for both the MooseEnum and MultiMooseEnum classes. More...

#include <MooseEnumBase.h>

Inheritance diagram for MooseEnumBase:
[legend]

Public Member Functions

 MooseEnumBase (std::string names, 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...
 
 MooseEnumBase (const MooseEnumBase &other_enum)
 Copy Constructor for use when creating vectors of MooseEnumBases. More...
 
virtual ~MooseEnumBase ()=default
 This class must have a virtual destructor since it has derived classes. More...
 
virtual void deprecate (const std::string &name, const std::string &raw_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...
 
std::vector< int > getIDs () const
 Method for returning a vector of ids for this instance. More...
 
virtual bool isValid () const =0
 IsValid. More...
 
bool isOutOfRangeAllowed () const
 isOutOfRangeAllowed More...
 
const std::set< MooseEnumItem > & items ()
 Return the complete set of available flags. More...
 
int getNextValidID () const
 Compute the next valid ID. 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 Member Functions

 MooseEnumBase ()
 Private constuctor for use by libmesh::Parameters. More...
 
virtual void checkDeprecated () const =0
 Method that must be implemented to check derived class values against the _deprecated_names. More...
 
void checkDeprecated (const MooseEnumItem &item) const
 Check and warn deprecated values. More...
 
void addEnumerationNames (const std::string &names)
 Methods to add possible enumeration value to the enum. More...
 
void addEnumerationName (const std::string &raw_name)
 
void addEnumerationName (const std::string &name, const int &value)
 
void addEnumerationItem (const MooseEnumItem &item)
 

Protected Attributes

std::set< MooseEnumItem_items
 Storage for the assigned items. More...
 
std::map< MooseEnumItem, MooseEnumItem_deprecated_items
 The map of deprecated names and optional replacements. More...
 
bool _allow_out_of_range
 Flag to enable enumeration items not previously defined. More...
 

Detailed Description

The base class for both the MooseEnum and MultiMooseEnum classes.

Definition at line 25 of file MooseEnumBase.h.

Constructor & Destructor Documentation

◆ MooseEnumBase() [1/3]

MooseEnumBase::MooseEnumBase ( std::string  names,
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
allow_out_of_range- determines whether this enumeration will accept values outside of its range of defined values.

Definition at line 23 of file MooseEnumBase.C.

24  : _allow_out_of_range(allow_out_of_range)
25 {
26  if (names.find(',') != std::string::npos)
27  mooseError("Spaces are required to separate options, comma support has been removed.");
28  else
29  addEnumerationNames(names);
30 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:208
void addEnumerationNames(const std::string &names)
Methods to add possible enumeration value to the enum.
Definition: MooseEnumBase.C:64
bool _allow_out_of_range
Flag to enable enumeration items not previously defined.

◆ MooseEnumBase() [2/3]

MooseEnumBase::MooseEnumBase ( const MooseEnumBase other_enum)

Copy Constructor for use when creating vectors of MooseEnumBases.

Parameters
other_enum- The other enumeration to copy state from

Definition at line 32 of file MooseEnumBase.C.

33  : _items(other_enum._items),
36 {
37 }
std::map< MooseEnumItem, MooseEnumItem > _deprecated_items
The map of deprecated names and optional replacements.
std::set< MooseEnumItem > _items
Storage for the assigned items.
bool _allow_out_of_range
Flag to enable enumeration items not previously defined.

◆ ~MooseEnumBase()

virtual MooseEnumBase::~MooseEnumBase ( )
virtualdefault

This class must have a virtual destructor since it has derived classes.

◆ MooseEnumBase() [3/3]

MooseEnumBase::MooseEnumBase ( )
protected

Private constuctor for use by libmesh::Parameters.

Definition at line 42 of file MooseEnumBase.C.

42 : _allow_out_of_range(false) {}
bool _allow_out_of_range
Flag to enable enumeration items not previously defined.

Member Function Documentation

◆ addEnumerationItem()

void MooseEnumBase::addEnumerationItem ( const MooseEnumItem item)
protected

Definition at line 117 of file MooseEnumBase.C.

Referenced by ExecFlagEnum::addAvailableFlags(), addEnumerationName(), and MultiMooseEnum::assign().

118 {
119  if (_items.count(item) > 0) // do nothing for identical insertions
120  return;
121 
122  if (find(item.id()) != _items.end())
123  mooseError("The supplied id ",
124  item.id(),
125  " already exists in the enumeration, cannot not add '",
126  item,
127  "'.");
128  if (find(item.name()) != _items.end())
129  mooseError("The name '", item.name(), "' already exists in the enumeration.");
130 
131  _items.insert(item);
132 }
const std::string & name() const
Definition: MooseEnumItem.h:38
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:208
std::set< MooseEnumItem > _items
Storage for the assigned items.
const int & id() const
Return the numeric, name, or raw name.
Definition: MooseEnumItem.h:37
std::set< MooseEnumItem >::const_iterator find(const MooseEnumItem &other) const
Locate an item.

◆ addEnumerationName() [1/2]

void MooseEnumBase::addEnumerationName ( const std::string &  raw_name)
protected

Definition at line 73 of file MooseEnumBase.C.

Referenced by addEnumerationNames().

74 {
75  // Make sure the option is not malformed
76  if (raw_name.find_first_of('=') == 0 || raw_name.find_last_of('=') == raw_name.length() - 1)
77  mooseError("You cannot place whitespace around the '=' character in MooseEnumBase");
78 
79  // Split on equals sign
80  std::vector<std::string> name_value;
81  MooseUtils::tokenize(MooseUtils::trim(raw_name), name_value, 1, "=");
82 
83  // There should be one or two items in the name_value
84  if (name_value.size() < 1 || name_value.size() > 2)
85  mooseError("Invalid option supplied in MooseEnumBase: ", raw_name);
86 
87  // Remove un-wanted space around string
88  name_value[0] = MooseUtils::trim(name_value[0]);
89 
90  // See if there is a value supplied for this option
91  // strtol allows for proper conversions of both int and hex strings
92  int value;
93  if (name_value.size() == 2)
94  value = std::strtol(name_value[1].c_str(), NULL, 0);
95  else
96  value = getNextValidID();
97 
98  addEnumerationName(name_value[0], value);
99 }
int getNextValidID() const
Compute the next valid ID.
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:377
void addEnumerationName(const std::string &raw_name)
Definition: MooseEnumBase.C:73
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:208
std::string trim(const std::string &str, const std::string &white_space=" \\\)
Standard scripting language trim function.
Definition: MooseUtils.C:112

◆ addEnumerationName() [2/2]

void MooseEnumBase::addEnumerationName ( const std::string &  name,
const int &  value 
)
protected

Definition at line 111 of file MooseEnumBase.C.

112 {
113  addEnumerationItem(MooseEnumItem(name, value));
114 }
void addEnumerationItem(const MooseEnumItem &item)
Class for containing MooseEnum item information.
Definition: MooseEnumItem.h:22

◆ addEnumerationNames()

void MooseEnumBase::addEnumerationNames ( const std::string &  names)
protected

Methods to add possible enumeration value to the enum.

The MooseEnum/MultiMooseEnum are not designed to be modified, with respect to the list of possible values. However, this is not the case for the ExecFlagEnum which is a special type of MultiMooseEnum for managing the "execute_on" flags. These methods are used by ExecFlagEnum to allow users to modify the available execute flags for their object.

Definition at line 64 of file MooseEnumBase.C.

Referenced by MooseEnumBase().

65 {
66  std::vector<std::string> elements;
67  MooseUtils::tokenize(names, elements, 1, " ");
68  for (const std::string & raw_name : elements)
69  addEnumerationName(raw_name);
70 }
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:377
void addEnumerationName(const std::string &raw_name)
Definition: MooseEnumBase.C:73

◆ checkDeprecated() [1/2]

virtual void MooseEnumBase::checkDeprecated ( ) const
protectedpure virtual

Method that must be implemented to check derived class values against the _deprecated_names.

Implemented in MultiMooseEnum, and MooseEnum.

Referenced by MooseEnum::checkDeprecated(), MultiMooseEnum::checkDeprecated(), and deprecate().

◆ checkDeprecated() [2/2]

void MooseEnumBase::checkDeprecated ( const MooseEnumItem item) const
protected

Check and warn deprecated values.

Definition at line 135 of file MooseEnumBase.C.

136 {
137  std::map<MooseEnumItem, MooseEnumItem>::const_iterator it = _deprecated_items.find(item);
138  if (it != _deprecated_items.end())
139  {
140  if (it->second.name().empty())
141  mooseWarning(item.name() + " is deprecated");
142  else
143  mooseWarning(item.name() + " is deprecated, consider using " + it->second.name());
144  }
145 }
const std::string & name() const
Definition: MooseEnumItem.h:38
std::map< MooseEnumItem, MooseEnumItem > _deprecated_items
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:220

◆ deprecate()

void MooseEnumBase::deprecate ( const std::string &  name,
const std::string &  raw_name = "" 
)
virtual

Deprecates various options in the MOOSE enum.

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

Definition at line 45 of file MooseEnumBase.C.

46 {
47  std::set<MooseEnumItem>::const_iterator deprecated = find(name);
48  if (deprecated == _items.end())
49  mooseError("Cannot deprecate the enum item ", name, ", is not an available value.");
50 
51  std::set<MooseEnumItem>::const_iterator replaced = find(raw_name);
52  if (replaced == _items.end())
53  mooseError("Cannot deprecate the enum item ",
54  name,
55  ", since the replaced item ",
56  raw_name,
57  " it is not an available value.");
58 
59  _deprecated_items.emplace(std::make_pair(*deprecated, *replaced));
61 }
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:208
std::map< MooseEnumItem, MooseEnumItem > _deprecated_items
The map of deprecated names and optional replacements.
virtual void checkDeprecated() const =0
Method that must be implemented to check derived class values against the _deprecated_names.
std::set< MooseEnumItem > _items
Storage for the assigned items.
std::set< MooseEnumItem >::const_iterator find(const MooseEnumItem &other) const
Locate an item.

◆ find() [1/3]

std::set< MooseEnumItem >::const_iterator MooseEnumBase::find ( const MooseEnumItem other) const

Locate an item.

Definition at line 190 of file MooseEnumBase.C.

Referenced by addEnumerationItem(), MooseApp::addExecFlag(), ExecFlagEnum::appendCurrent(), MultiMooseEnum::assign(), deprecate(), MooseEnum::operator=(), MooseEnum::operator==(), and ExecFlagEnum::removeAvailableFlags().

191 {
192  return std::find_if(_items.begin(), _items.end(), [&other](MooseEnumItem const & item) {
193  return item.id() == other.id();
194  });
195 }
std::set< MooseEnumItem > _items
Storage for the assigned items.
const int & id() const
Return the numeric, name, or raw name.
Definition: MooseEnumItem.h:37
Class for containing MooseEnum item information.
Definition: MooseEnumItem.h:22

◆ find() [2/3]

std::set< MooseEnumItem >::const_iterator MooseEnumBase::find ( const std::string &  name) const

Definition at line 174 of file MooseEnumBase.C.

175 {
176  std::string upper = MooseUtils::toUpper(name);
177  return std::find_if(_items.begin(), _items.end(), [&upper](MooseEnumItem const & item) {
178  return item.name() == upper;
179  });
180 }
std::string toUpper(const std::string &name)
Convert supplied string to upper case.
Definition: MooseUtils.C:708
std::set< MooseEnumItem > _items
Storage for the assigned items.
Class for containing MooseEnum item information.
Definition: MooseEnumItem.h:22

◆ find() [3/3]

std::set< MooseEnumItem >::const_iterator MooseEnumBase::find ( int  id) const

Definition at line 183 of file MooseEnumBase.C.

184 {
185  return std::find_if(
186  _items.begin(), _items.end(), [&id](MooseEnumItem const & item) { return item.id() == id; });
187 }
std::set< MooseEnumItem > _items
Storage for the assigned items.
Class for containing MooseEnum item information.
Definition: MooseEnumItem.h:22

◆ getIDs()

std::vector< int > MooseEnumBase::getIDs ( ) const

Method for returning a vector of ids for this instance.

Returns
a vector of ints containing the possible ids for this enumeration

Definition at line 164 of file MooseEnumBase.C.

Referenced by MooseEnum::operator=().

165 {
166  std::vector<int> out;
167  out.reserve(_items.size());
168  for (const auto & item : _items)
169  out.push_back(item.id());
170  return out;
171 }
std::set< MooseEnumItem > _items
Storage for the assigned items.

◆ getNames()

std::vector< std::string > MooseEnumBase::getNames ( ) const

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

Returns
a vector of names

Definition at line 148 of file MooseEnumBase.C.

Referenced by ExecFlagEnum::getDocString().

149 {
150  std::vector<std::string> out;
151  out.reserve(_items.size());
152  for (const auto & item : _items)
153  out.push_back(item.name());
154  return out;
155 }
std::set< MooseEnumItem > _items
Storage for the assigned items.

◆ getNextValidID()

int MooseEnumBase::getNextValidID ( ) const

Compute the next valid ID.

Definition at line 102 of file MooseEnumBase.C.

Referenced by addEnumerationName(), MooseApp::addExecFlag(), MultiMooseEnum::assign(), and MooseEnum::operator=().

103 {
104  int value = -1; // Use -1 so if no values exist the first will be zero
105  for (const auto & item : _items)
106  value = std::max(value, item.id());
107  return ++value;
108 }
std::set< MooseEnumItem > _items
Storage for the assigned items.

◆ getRawNames()

std::string MooseEnumBase::getRawNames ( ) const

Method for returning the raw name strings for this instance.

Returns
a space separated list of names

Definition at line 158 of file MooseEnumBase.C.

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

159 {
160  return Moose::stringify(_items, " ");
161 }
std::string stringify(const T &t)
conversion to string
Definition: Conversion.h:58
std::set< MooseEnumItem > _items
Storage for the assigned items.

◆ isOutOfRangeAllowed()

bool MooseEnumBase::isOutOfRangeAllowed ( ) const
inline

isOutOfRangeAllowed

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

Definition at line 83 of file MooseEnumBase.h.

83 { return _allow_out_of_range; }
bool _allow_out_of_range
Flag to enable enumeration items not previously defined.

◆ isValid()

virtual bool MooseEnumBase::isValid ( ) const
pure virtual

IsValid.

Returns
- a Boolean indicating whether this Enumeration has been set

Implemented in MultiMooseEnum, and MooseEnum.

◆ items()

const std::set<MooseEnumItem>& MooseEnumBase::items ( )
inline

Return the complete set of available flags.

Definition at line 88 of file MooseEnumBase.h.

88 { return _items; }
std::set< MooseEnumItem > _items
Storage for the assigned items.

Member Data Documentation

◆ _allow_out_of_range

bool MooseEnumBase::_allow_out_of_range
protected

Flag to enable enumeration items not previously defined.

Definition at line 139 of file MooseEnumBase.h.

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

◆ _deprecated_items

std::map<MooseEnumItem, MooseEnumItem> MooseEnumBase::_deprecated_items
protected

The map of deprecated names and optional replacements.

Definition at line 136 of file MooseEnumBase.h.

Referenced by checkDeprecated(), and deprecate().

◆ _items

std::set<MooseEnumItem> MooseEnumBase::_items
protected

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