https://mooseframework.inl.gov
MooseEnum.C
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 #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  assign(name);
42  return *this;
43 }
44 
45 MooseEnum &
47 {
48  assign(value);
49  return *this;
50 }
51 
52 MooseEnum &
54 {
55  assign(item);
56  return *this;
57 }
58 
59 void
60 MooseEnum::assign(const std::string & name, const std::optional<std::string> & context)
61 {
62  if (MooseUtils::trim(name) == "")
63  {
65  return;
66  }
67 
68  // Check if name can be parsed cleanly into an integer
69  int value = 0;
70  bool integer = (std::istringstream(MooseUtils::trim(name)) >> value).eof();
71 
72  if (auto iter = find(name); iter != _items.end())
73  _current = *iter;
74  else if (auto subs = find(value); subs != _items.end() && integer)
75  _current = *subs;
76  else
77  {
78  if (!_allow_out_of_range) // Are out of range values allowed?
79  mooseError(context ? (*context + ":\n\n") : std::string(""),
80  "Invalid option \"",
81  name,
82  "\" in MooseEnum. Valid options (not case-sensitive) are \"",
83  getRawNames(),
84  "\".");
85 
87  _items.insert(_current);
88  }
89 
91 }
92 
93 void
95 {
97  {
99  return;
100  }
101 
102  if (auto iter = find(value); iter != _items.end())
103  _current = *iter;
104  else
105  mooseError("Invalid id \"",
106  value,
107  "\" in MooseEnum. Valid ids are \"",
109  "\".");
110 
111  checkDeprecated();
112 }
113 
114 void
116 {
117  std::set<MooseEnumItem>::const_iterator iter = find(item);
118  if (iter == _items.end())
119  mooseError("Invalid item \"",
120  item,
121  "\" in MooseEnum. Valid ids are \"",
123  "\".");
124  else
125  _current = *iter;
126 
127  checkDeprecated();
128 }
129 
130 bool
131 MooseEnum::operator==(const char * name) const
132 {
133  std::string upper(MooseUtils::toUpper(name));
134 
135  mooseAssert(_allow_out_of_range || find(upper) != _items.end(),
136  "Invalid string comparison \"" + upper +
137  "\" in MooseEnum. Valid options (not case-sensitive) are \"" + getRawNames() +
138  "\".");
139 
140  return _current == upper;
141 }
142 
143 bool
144 MooseEnum::operator!=(const char * name) const
145 {
146  return !(*this == name);
147 }
148 
149 bool
150 MooseEnum::operator==(int value) const
151 {
152  return value == _current;
153 }
154 
155 bool
156 MooseEnum::operator!=(int value) const
157 {
158  return value != _current;
159 }
160 
161 bool
162 MooseEnum::operator==(unsigned short value) const
163 {
164  return value == _current;
165 }
166 
167 bool
168 MooseEnum::operator!=(unsigned short value) const
169 {
170  return value != _current;
171 }
172 
173 bool
175 {
176  switch (mode)
177  {
179  return (_current.id() == other._current.id()) && (_current.name() == other._current.name());
181  return _current.name() == other._current.name();
183  return _current.id() == other._current.id();
184  }
185  return false;
186 }
187 
188 bool
189 MooseEnum::operator==(const MooseEnum & value) const
190 {
191  mooseDeprecated("This method will be removed because the meaning is not well defined, please use "
192  "the 'compareCurrent' method instead.");
193  return value._current.name() == _current.name();
194 }
195 
196 bool
197 MooseEnum::operator!=(const MooseEnum & value) const
198 {
199  mooseDeprecated("This method will be removed because the meaning is not well defined, please use "
200  "the 'compareCurrent' method instead.");
201  return value._current.name() != _current.name();
202 }
203 
204 void
206 {
208 }
std::string name(const ElemQuality q)
int getNextValidID() const
Compute the next valid ID.
bool operator!=(const char *value) const
Definition: MooseEnum.C:144
const std::set< MooseEnumItem > & items() const
Return the complete set of available flags.
Definition: MooseEnumBase.h:93
const std::string & name() const
Definition: MooseEnumItem.h:35
CompareMode
Enum item for controlling comparison in the compareCurrent method.
Definition: MooseEnum.h:60
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:311
static const int INVALID_ID
Definition: MooseEnumItem.h:21
virtual void checkDeprecated() const override
Check whether the current value is deprecated when called.
Definition: MooseEnum.C:205
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
std::string toUpper(std::string name)
Convert supplied string to upper case.
MooseEnumItem _current
The current id.
Definition: MooseEnum.h:167
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
MooseEnum()
Constructor for use by libmesh::Parameters and ReporterMode.
Definition: MooseEnum.C:36
std::string trim(const std::string &str, const std::string &white_space=" \\\)
Standard scripting language trim function.
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:54
void mooseDeprecated(Args &&... args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition: MooseError.h:363
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:64
void assign(const std::string &name, const std::optional< std::string > &context={})
Definition: MooseEnum.C:60
std::set< MooseEnumItem > _items
Storage for the assigned items.
const int & id() const
Return the numeric, name, or raw name.
Definition: MooseEnumItem.h:34
MooseEnum & operator=(const MooseEnum &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 char *value) const
Comparison operators for comparing with character constants, MooseEnums or integer values...
Definition: MooseEnum.C:131
bool compareCurrent(const MooseEnum &other, CompareMode mode=CompareMode::COMPARE_NAME) const
Method for comparing currently set values between MooseEnum.
Definition: MooseEnum.C:174
std::set< MooseEnumItem >::const_iterator find(const MooseEnumItem &other) const
Locate an item.
bool _allow_out_of_range
Flag to enable enumeration items not previously defined.