www.mooseframework.org
MooseEnum.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 "MooseEnum.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 
22 MooseEnum::MooseEnum(std::string names, std::string default_name, bool allow_out_of_range)
23  : MooseEnumBase(names, allow_out_of_range), _current("", MooseEnumItem::INVALID_ID)
24 {
25  *this = default_name;
26 }
27 
28 MooseEnum::MooseEnum(const MooseEnum & other_enum)
29  : MooseEnumBase(other_enum), _current(other_enum._current)
30 {
31 }
32 
36 MooseEnum::MooseEnum() : _current("", MooseEnumItem::INVALID_ID) {}
37 
38 MooseEnum &
39 MooseEnum::operator=(const std::string & name)
40 {
41  if (name == "")
42  {
44  return *this;
45  }
46 
47  std::set<MooseEnumItem>::const_iterator iter = find(name);
48  if (iter == _items.end())
49  {
50  if (!_allow_out_of_range) // Are out of range values allowed?
51  mooseError("Invalid option \"",
52  name,
53  "\" in MooseEnum. Valid options (not case-sensitive) are \"",
54  getRawNames(),
55  "\".");
56  else
57  {
59  _items.insert(_current);
60  }
61  }
62  else
63  _current = *iter;
64 
66 
67  return *this;
68 }
69 
70 MooseEnum &
72 {
73  if (value == MooseEnumItem::INVALID_ID)
74  {
76  return *this;
77  }
78 
79  std::set<MooseEnumItem>::const_iterator iter = find(value);
80  if (iter == _items.end())
81  mooseError("Invalid id \"",
82  value,
83  "\" in MooseEnum. Valid ids are \"",
85  "\".");
86  else
87  _current = *iter;
88 
90 
91  return *this;
92 }
93 
94 bool
95 MooseEnum::operator==(const char * name) const
96 {
97  std::string upper(MooseUtils::toUpper(name));
98 
99  mooseAssert(_allow_out_of_range || find(upper) != _items.end(),
100  "Invalid string comparison \"" + upper +
101  "\" in MooseEnum. Valid options (not case-sensitive) are \"" + getRawNames() +
102  "\".");
103 
104  return _current == upper;
105 }
106 
107 bool
108 MooseEnum::operator!=(const char * name) const
109 {
110  return !(*this == name);
111 }
112 
113 bool
114 MooseEnum::operator==(int value) const
115 {
116  return value == _current;
117 }
118 
119 bool
120 MooseEnum::operator!=(int value) const
121 {
122  return value != _current;
123 }
124 
125 bool
126 MooseEnum::operator==(unsigned short value) const
127 {
128  return value == _current;
129 }
130 
131 bool
132 MooseEnum::operator!=(unsigned short value) const
133 {
134  return value != _current;
135 }
136 
137 bool
139 {
140  switch (mode)
141  {
143  return (_current.id() == other._current.id()) && (_current.name() == other._current.name());
145  return _current.name() == other._current.name();
147  return _current.id() == other._current.id();
148  }
149  return false;
150 }
151 
152 bool
153 MooseEnum::operator==(const MooseEnum & value) const
154 {
155  mooseDeprecated("This method will be removed because the meaning is not well defined, please use "
156  "the 'compareCurrent' method instead.");
157  return value._current.name() == _current.name();
158 }
159 
160 bool
161 MooseEnum::operator!=(const MooseEnum & value) const
162 {
163  mooseDeprecated("This method will be removed because the meaning is not well defined, please use "
164  "the 'compareCurrent' method instead.");
165  return value._current.name() != _current.name();
166 }
167 
168 void
170 {
172 }
int getNextValidID() const
Compute the next valid ID.
bool operator!=(const char *value) const
Definition: MooseEnum.C:108
const std::string & name() const
Definition: MooseEnumItem.h:37
std::string toUpper(const std::string &name)
Convert supplied string to upper case.
Definition: MooseUtils.C:651
CompareMode
Enum item for controlling comparison in the compareCurrent method.
Definition: MooseEnum.h:37
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:207
static const int INVALID_ID
Definition: MooseEnumItem.h:24
virtual void checkDeprecated() const override
Check whether the current value is deprecated when called.
Definition: MooseEnum.C:169
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
MooseEnumItem _current
The current id.
Definition: MooseEnum.h:134
MooseEnum()
Private constructor for use by libmesh::Parameters.
Definition: MooseEnum.C:36
virtual void checkDeprecated() const =0
Method that must be implemented to check derived class values against the _deprecated_names.
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:31
void mooseDeprecated(Args &&... args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition: MooseError.h:236
std::vector< int > getIDs() const
Method for returning a vector of ids for this instance.
std::string stringify(const T &t)
conversion to string
Definition: Conversion.h:60
std::set< MooseEnumItem > _items
Storage for the assigned items.
const int & id() const
Return the numeric, name, or raw name.
Definition: MooseEnumItem.h:36
Class for containing MooseEnum item information.
Definition: MooseEnumItem.h:21
bool operator==(const char *value) const
Comparison operators for comparing with character constants, MooseEnums or integer values...
Definition: MooseEnum.C:95
bool compareCurrent(const MooseEnum &other, CompareMode mode=CompareMode::COMPARE_NAME) const
Method for comparing currently set values between MooseEnum.
Definition: MooseEnum.C:138
std::set< MooseEnumItem >::const_iterator find(const MooseEnumItem &other) const
Locate an item.
MooseEnum & operator=(const std::string &name)
Assignment operators.
Definition: MooseEnum.C:39
bool _allow_out_of_range
Flag to enable enumeration items not previously defined.