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 (name == "")
63  {
65  return;
66  }
67 
68  std::set<MooseEnumItem>::const_iterator iter = find(name);
69  if (iter == _items.end())
70  {
71  if (!_allow_out_of_range) // Are out of range values allowed?
72  mooseError(context ? (*context + ":\n\n") : std::string(""),
73  "Invalid option \"",
74  name,
75  "\" in MooseEnum. Valid options (not case-sensitive) are \"",
76  getRawNames(),
77  "\".");
78  else
79  {
81  _items.insert(_current);
82  }
83  }
84  else
85  _current = *iter;
86 
88 }
89 
90 void
92 {
94  {
96  return;
97  }
98 
99  std::set<MooseEnumItem>::const_iterator iter = find(value);
100  if (iter == _items.end())
101  mooseError("Invalid id \"",
102  value,
103  "\" in MooseEnum. Valid ids are \"",
105  "\".");
106  else
107  _current = *iter;
108 
109  checkDeprecated();
110 }
111 
112 void
114 {
115  std::set<MooseEnumItem>::const_iterator iter = find(item);
116  if (iter == _items.end())
117  mooseError("Invalid item \"",
118  item,
119  "\" in MooseEnum. Valid ids are \"",
121  "\".");
122  else
123  _current = *iter;
124 
125  checkDeprecated();
126 }
127 
128 bool
129 MooseEnum::operator==(const char * name) const
130 {
131  std::string upper(MooseUtils::toUpper(name));
132 
133  mooseAssert(_allow_out_of_range || find(upper) != _items.end(),
134  "Invalid string comparison \"" + upper +
135  "\" in MooseEnum. Valid options (not case-sensitive) are \"" + getRawNames() +
136  "\".");
137 
138  return _current == upper;
139 }
140 
141 bool
142 MooseEnum::operator!=(const char * name) const
143 {
144  return !(*this == name);
145 }
146 
147 bool
148 MooseEnum::operator==(int value) const
149 {
150  return value == _current;
151 }
152 
153 bool
154 MooseEnum::operator!=(int value) const
155 {
156  return value != _current;
157 }
158 
159 bool
160 MooseEnum::operator==(unsigned short value) const
161 {
162  return value == _current;
163 }
164 
165 bool
166 MooseEnum::operator!=(unsigned short value) const
167 {
168  return value != _current;
169 }
170 
171 bool
173 {
174  switch (mode)
175  {
177  return (_current.id() == other._current.id()) && (_current.name() == other._current.name());
179  return _current.name() == other._current.name();
181  return _current.id() == other._current.id();
182  }
183  return false;
184 }
185 
186 bool
187 MooseEnum::operator==(const MooseEnum & value) const
188 {
189  mooseDeprecated("This method will be removed because the meaning is not well defined, please use "
190  "the 'compareCurrent' method instead.");
191  return value._current.name() == _current.name();
192 }
193 
194 bool
195 MooseEnum::operator!=(const MooseEnum & value) const
196 {
197  mooseDeprecated("This method will be removed because the meaning is not well defined, please use "
198  "the 'compareCurrent' method instead.");
199  return value._current.name() != _current.name();
200 }
201 
202 void
204 {
206 }
std::string name(const ElemQuality q)
int getNextValidID() const
Compute the next valid ID.
bool operator!=(const char *value) const
Definition: MooseEnum.C:142
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
std::string toUpper(const std::string &name)
Convert supplied string to upper case.
CompareMode
Enum item for controlling comparison in the compareCurrent method.
Definition: MooseEnum.h:39
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
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:203
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:146
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
MooseEnum()
Constructor for use by libmesh::Parameters and ReporterMode.
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:33
void mooseDeprecated(Args &&... args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition: MooseError.h:353
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:129
bool compareCurrent(const MooseEnum &other, CompareMode mode=CompareMode::COMPARE_NAME) const
Method for comparing currently set values between MooseEnum.
Definition: MooseEnum.C:172
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.