www.mooseframework.org
MooseEnum.h
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 #ifndef MOOSEENUM_H
16 #define MOOSEENUM_H
17 
18 // MOOSE includes
19 #include "MooseEnumBase.h"
20 
21 // Forward declarations
22 namespace libMesh
23 {
24 class Parameters;
25 }
26 
37 class MooseEnum : public MooseEnumBase
38 {
39 public:
43  enum class CompareMode
44  {
45  COMPARE_NAME,
46  COMPARE_ID,
47  COMPARE_BOTH
48  };
49 
58  MooseEnum(std::string names, std::string default_name = "", bool allow_out_of_range = false);
59 
64  MooseEnum(const MooseEnum & other_enum);
65 
66  virtual ~MooseEnum() = default;
67 
72  operator int() const { return _current.id(); }
73  operator std::string() const { return _current.rawName(); }
74 
81  bool operator==(const char * value) const;
82  bool operator!=(const char * value) const;
83 
84  bool operator==(int value) const;
85  bool operator!=(int value) const;
86 
87  bool operator==(unsigned short value) const;
88  bool operator!=(unsigned short value) const;
89 
90  bool operator==(const MooseEnum & value) const;
91  bool operator!=(const MooseEnum & value) const;
92 
96  bool compareCurrent(const MooseEnum & other, CompareMode mode = CompareMode::COMPARE_NAME) const;
97 
104  MooseEnum & operator=(const std::string & name);
105 
110  virtual bool isValid() const override { return _current.id() > INVALID_ID; }
111 
112  // InputParameters is allowed to create an empty enum but is responsible for
113  // filling it in after the fact
114  friend class libMesh::Parameters;
115 
117  friend std::ostream & operator<<(std::ostream & out, const MooseEnum & obj)
118  {
119  out << obj._current.rawName();
120  return out;
121  }
122 
124  template <typename T>
125  T getEnum() const;
126 
127 protected:
129  virtual void checkDeprecated() const override;
130 
131 private:
135  MooseEnum();
136 
139 };
140 
141 template <typename T>
142 T
144 {
145 #ifdef LIBMESH_HAVE_CXX11_TYPE_TRAITS
146  static_assert(std::is_enum<T>::value == true,
147  "The type requested from MooseEnum::getEnum must be an enum type!\n\n");
148 #endif
149  return static_cast<T>(_current.id());
150 }
151 
152 #endif // MOOSEENUM_H
virtual bool isValid() const override
IsValid.
Definition: MooseEnum.h:110
CompareMode
Enum item for controlling comparison in the compareCurrent method.
Definition: MooseEnum.h:43
const std::string & rawName() const
Definition: MooseEnumItem.h:40
T getEnum() const
get the current value cast to the enum type T
Definition: MooseEnum.h:143
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...
The base class for both the MooseEnum and MultiMooseEnum classes.
Definition: MooseEnumBase.h:30
MooseEnumItem _current
The current id.
Definition: MooseEnum.h:138
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:37
friend std::ostream & operator<<(std::ostream &out, const MooseEnum &obj)
Operator for printing to iostreams.
Definition: MooseEnum.h:117
Class for containing MooseEnum item information.
Definition: MooseEnumItem.h:24