https://mooseframework.inl.gov
Loading...
Searching...
No Matches
MooseEnum.h
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#pragma once
11
12// MOOSE includes
13#include "MooseEnumBase.h"
14#include "MooseStringUtils.h"
15#include "MooseUtils.h"
16
17#include <optional>
18#include <cctype>
19#include <algorithm>
20
21#define CreateMooseEnumClass(EnumName, /* enumerators */...) \
22 static inline std::string get##EnumName##Options() \
23 { \
24 /* 1) stringify the enumerator list */ \
25 std::string s = #__VA_ARGS__; \
26 /* 2) normalize to the format MooseEnum expects: "A B C=3 D ..." */ \
27 std::vector<std::string> elements; \
28 MooseUtils::tokenize(s, elements, 1, ","); \
29 for (auto & elem : elements) \
30 elem.erase(std::remove_if(elem.begin(), elem.end(), isspace), elem.end()); \
31 return MooseUtils::join(elements, " "); \
32 } \
33 enum class EnumName \
34 { \
35 __VA_ARGS__ \
36 }
37
38// Forward declarations
39namespace libMesh
40{
41class Parameters;
42}
43
55{
56public:
60 enum class CompareMode
61 {
65 };
66
75 MooseEnum(std::string names, std::string default_name = "", bool allow_out_of_range = false);
76
81 MooseEnum(const MooseEnum & other_enum);
82
87 MooseEnum & operator=(const MooseEnum & other_enum) = default;
88
89 virtual ~MooseEnum() = default;
90
95 operator int() const { return _current.id(); }
96 operator std::string() const { return _current.rawName(); }
97
104 bool operator==(const char * value) const;
105 bool operator!=(const char * value) const;
106
107 bool operator==(int value) const;
108 bool operator!=(int value) const;
109
110 bool operator==(unsigned short value) const;
111 bool operator!=(unsigned short value) const;
112
113 bool operator==(const MooseEnum & value) const;
114 bool operator!=(const MooseEnum & value) const;
115
119 bool compareCurrent(const MooseEnum & other, CompareMode mode = CompareMode::COMPARE_NAME) const;
120
122
127 MooseEnum & operator=(const std::string & name);
128 MooseEnum & operator=(int value);
129 MooseEnum & operator=(const MooseEnumItem & item);
130 void assign(const std::string & name, const std::optional<std::string> & context = {});
131 void assign(int value);
132 void assign(const MooseEnumItem & item);
134
139 virtual bool isValid() const override { return _current.id() > MooseEnumItem::INVALID_ID; }
140
141 // InputParameters is allowed to create an empty enum but is responsible for
142 // filling it in after the fact
144
146 friend std::ostream & operator<<(std::ostream & out, const MooseEnum & obj)
147 {
148 out << obj._current.rawName();
149 return out;
150 }
151
153 template <typename T>
154 T getEnum() const;
155
156protected:
158 virtual void checkDeprecated() const override;
159
163 MooseEnum();
164
165private:
168};
169
170template <typename T>
171T
173{
174#ifdef LIBMESH_HAVE_CXX11_TYPE_TRAITS
175 static_assert(std::is_enum<T>::value == true,
176 "The type requested from MooseEnum::getEnum must be an enum type!\n\n");
177#endif
178 return static_cast<T>(_current.id());
179}
void ErrorVector unsigned int
The base class for both the MooseEnum and MultiMooseEnum classes.
Class for containing MooseEnum item information.
const int & id() const
Return the numeric, name, or raw name.
const std::string & rawName() const
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.
friend std::ostream & operator<<(std::ostream &out, const MooseEnum &obj)
Operator for printing to iostreams.
Definition MooseEnum.h:146
MooseEnumItem _current
The current id.
Definition MooseEnum.h:167
virtual bool isValid() const override
IsValid.
Definition MooseEnum.h:139
virtual ~MooseEnum()=default
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
T getEnum() const
get the current value cast to the enum type T
Definition MooseEnum.h:172
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...