https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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
22MooseEnum::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
29 : MooseEnumBase(other_enum), _current(other_enum._current)
30{
31}
32
36MooseEnum::MooseEnum() : _current("", MooseEnumItem::INVALID_ID) {}
37
39MooseEnum::operator=(const std::string & name)
40{
41 assign(name);
42 return *this;
43}
44
47{
48 assign(value);
49 return *this;
50}
51
54{
55 assign(item);
56 return *this;
57}
58
59void
60MooseEnum::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 \"",
84 "\".");
85
86 _current = MooseEnumItem(name, integer ? value : getNextValidID());
87 _items.insert(_current);
88 }
89
91}
92
93void
95{
96 if (value == MooseEnumItem::INVALID_ID)
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
112}
113
114void
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
128}
129
130bool
131MooseEnum::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
143bool
144MooseEnum::operator!=(const char * name) const
145{
146 return !(*this == name);
147}
148
149bool
150MooseEnum::operator==(int value) const
151{
152 return value == _current;
153}
154
155bool
156MooseEnum::operator!=(int value) const
157{
158 return value != _current;
159}
160
161bool
162MooseEnum::operator==(unsigned short value) const
163{
164 return value == _current;
165}
166
167bool
168MooseEnum::operator!=(unsigned short value) const
169{
170 return value != _current;
171}
172
173bool
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
188bool
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
196bool
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
204void
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
void mooseDeprecated(Args &&... args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition MooseError.h:363
The base class for both the MooseEnum and MultiMooseEnum classes.
bool _allow_out_of_range
Flag to enable enumeration items not previously defined.
std::set< MooseEnumItem > _items
Storage for the assigned items.
int getNextValidID() const
Compute the next valid ID.
std::set< MooseEnumItem >::const_iterator find(const MooseEnumItem &other) const
Locate an item.
virtual void checkDeprecated() const =0
Method that must be implemented to check derived class values against the _deprecated_names.
std::string getRawNames() const
Method for returning the raw name strings for this instance.
const std::set< MooseEnumItem > & items() const
Return the complete set of available flags.
std::vector< int > getIDs() const
Method for returning a vector of ids for this instance.
Class for containing MooseEnum item information.
const std::string & name() const
const int & id() const
Return the numeric, name, or raw name.
static const int INVALID_ID
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition MooseEnum.h:55
CompareMode
Enum item for controlling comparison in the compareCurrent method.
Definition MooseEnum.h:61
virtual void checkDeprecated() const override
Check whether the current value is deprecated when called.
Definition MooseEnum.C:205
MooseEnum & operator=(const MooseEnum &other_enum)=default
Copy Assignment operator must be explicitly defined when a copy ctor exists and this method is used.
MooseEnumItem _current
The current id.
Definition MooseEnum.h:167
bool operator!=(const char *value) const
Definition MooseEnum.C:144
void assign(const std::string &name, const std::optional< std::string > &context={})
Definition MooseEnum.C:60
bool operator==(const char *value) const
Comparison operators for comparing with character constants, MooseEnums or integer values.
Definition MooseEnum.C:131
MooseEnum()
Constructor for use by libmesh::Parameters and ReporterMode.
Definition MooseEnum.C:36
bool compareCurrent(const MooseEnum &other, CompareMode mode=CompareMode::COMPARE_NAME) const
Method for comparing currently set values between MooseEnum.
Definition MooseEnum.C:174
std::string toUpper(std::string name)
Convert supplied string to upper case.
std::string trim(const std::string &str, const std::string &white_space=" \t\n\v\f\r")
Standard scripting language trim function.
std::string stringify(const T &t)
conversion to string
Definition Conversion.h:64