www.mooseframework.org
MooseEnum.C
Go to the documentation of this file.
1 /****************************************************************/
2 /* DO NOT MODIFY THIS HEADER */
3 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
4 /* */
5 /* (c) 2010 Battelle Energy Alliance, LLC */
6 /* ALL RIGHTS RESERVED */
7 /* */
8 /* Prepared by Battelle Energy Alliance, LLC */
9 /* Under Contract No. DE-AC07-05ID14517 */
10 /* With the U. S. Department of Energy */
11 /* */
12 /* See COPYRIGHT for full restrictions */
13 /****************************************************************/
14 
15 #include "MooseEnum.h"
16 #include "MooseUtils.h"
17 #include "MooseError.h"
18 
19 #include <sstream>
20 #include <algorithm>
21 #include <iterator>
22 #include <limits>
23 #include <string>
24 #include <iostream>
25 
26 MooseEnum::MooseEnum(std::string names, std::string default_name, bool allow_out_of_range)
27  : MooseEnumBase(names, allow_out_of_range), _current("", INVALID_ID)
28 {
29  *this = default_name;
30 }
31 
32 MooseEnum::MooseEnum(const MooseEnum & other_enum)
33  : MooseEnumBase(other_enum), _current(other_enum._current)
34 {
35 }
36 
41 
42 MooseEnum &
43 MooseEnum::operator=(const std::string & name)
44 {
45  if (name == "")
46  {
48  return *this;
49  }
50 
51  std::string upper(MooseUtils::toUpper(name));
52  checkDeprecatedBase(upper);
53 
54  std::set<MooseEnumItem>::const_iterator iter = find(upper);
55  if (iter == _items.end())
56  {
57  if (_out_of_range_index == 0) // Are out of range values allowed?
58  mooseError(std::string("Invalid option \"") + upper +
59  "\" in MooseEnum. Valid options (not case-sensitive) are \"" + getRawNames() +
60  "\".");
61  else
62  {
64  _items.insert(_current);
65  }
66  }
67  else
68  _current = *iter;
69 
70  return *this;
71 }
72 
73 bool
74 MooseEnum::operator==(const char * name) const
75 {
76  std::string upper(MooseUtils::toUpper(name));
77 
78  mooseAssert(_out_of_range_index != 0 || find(upper) != _items.end(),
79  std::string("Invalid string comparison \"") + upper +
80  "\" in MooseEnum. Valid options (not case-sensitive) are \"" + getRawNames() +
81  "\".");
82 
83  return _current == upper;
84 }
85 
86 bool
87 MooseEnum::operator!=(const char * name) const
88 {
89  return !(*this == name);
90 }
91 
92 bool
93 MooseEnum::operator==(int value) const
94 {
95  return value == _current;
96 }
97 
98 bool
99 MooseEnum::operator!=(int value) const
100 {
101  return value != _current;
102 }
103 
104 bool
105 MooseEnum::operator==(unsigned short value) const
106 {
107  return value == _current;
108 }
109 
110 bool
111 MooseEnum::operator!=(unsigned short value) const
112 {
113  return value != _current;
114 }
115 
116 bool
118 {
119  switch (mode)
120  {
122  return (_current.id() == other._current.id()) && (_current.name() == other._current.name());
124  return _current.name() == other._current.name();
126  return _current.id() == other._current.id();
127  }
128  return false;
129 }
130 
131 bool
132 MooseEnum::operator==(const MooseEnum & value) const
133 {
134  mooseDeprecated("This method will be removed becuase the meaning is not well defined, please use "
135  "the 'compareCurrent' method instead.");
136  return value._current.name() == _current.name();
137 }
138 
139 bool
140 MooseEnum::operator!=(const MooseEnum & value) const
141 {
142  mooseDeprecated("This method will be removed becuase the meaning is not well defined, please use "
143  "the 'compareCurrent' method instead.");
144  return value._current.name() != _current.name();
145 }
146 
147 void
149 {
151 }
void checkDeprecatedBase(const std::string &name_upper) const
Check and warn deprecated values.
Definition: MooseEnumBase.C:99
std::string toUpper(const std::string &name)
Convert supplied string to upper case.
Definition: MooseUtils.C:555
CompareMode
Enum item for controlling comparison in the compareCurrent method.
Definition: MooseEnum.h:43
const int & id() const
Return the numeric, name, or raw name.
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:182
void mooseDeprecated(Args &&...args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition: MooseError.h:202
int _out_of_range_index
The index of values assigned that are NOT values in this enum.
The base class for both the MooseEnum and MultiMooseEnum classes.
Definition: MooseEnumBase.h:30
MooseEnumItem _current
The current id.
Definition: MooseEnum.h:138
bool operator!=(const char *value) const
Definition: MooseEnum.C:87
MooseEnum()
Private constructor for use by libmesh::Parameters.
Definition: MooseEnum.C:40
static const int INVALID_ID
Constants.
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:37
const std::string & name() const
Definition: MooseEnumItem.h:39
std::set< MooseEnumItem > _items
Storage for the assigned items.
std::string getRawNames() const
Method for returning the raw name strings for this instance.
bool operator==(const char *value) const
Comparison operators for comparing with character constants, MooseEnums or integer values...
Definition: MooseEnum.C:74
virtual void checkDeprecated() const override
Check whether the current value is deprecated when called.
Definition: MooseEnum.C:148
Class for containing MooseEnum item information.
Definition: MooseEnumItem.h:24
bool compareCurrent(const MooseEnum &other, CompareMode mode=CompareMode::COMPARE_NAME) const
Method for comparing currently set values between MooseEnum.
Definition: MooseEnum.C:117
std::set< MooseEnumItem >::const_iterator find(const MooseEnumItem &other) const
Locate an item.
MooseEnum & operator=(const std::string &name)
Assignment operators TODO: Perhaps we should implement an int assignment operator.
Definition: MooseEnum.C:43