www.mooseframework.org
MooseEnum.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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 
15 // Forward declarations
16 namespace libMesh
17 {
18 class Parameters;
19 }
20 
31 class MooseEnum : public MooseEnumBase
32 {
33 public:
37  enum class CompareMode
38  {
40  COMPARE_ID,
42  };
43 
52  MooseEnum(std::string names, std::string default_name = "", bool allow_out_of_range = false);
53 
58  MooseEnum(const MooseEnum & other_enum);
59 
60  virtual ~MooseEnum() = default;
61 
66  operator int() const { return _current.id(); }
67  operator std::string() const { return _current.rawName(); }
68 
75  bool operator==(const char * value) const;
76  bool operator!=(const char * value) const;
77 
78  bool operator==(int value) const;
79  bool operator!=(int value) const;
80 
81  bool operator==(unsigned short value) const;
82  bool operator!=(unsigned short value) const;
83 
84  bool operator==(const MooseEnum & value) const;
85  bool operator!=(const MooseEnum & value) const;
86 
90  bool compareCurrent(const MooseEnum & other, CompareMode mode = CompareMode::COMPARE_NAME) const;
91 
93 
98  MooseEnum & operator=(const std::string & name);
99  MooseEnum & operator=(int value);
101 
106  virtual bool isValid() const override { return _current.id() > MooseEnumItem::INVALID_ID; }
107 
108  // InputParameters is allowed to create an empty enum but is responsible for
109  // filling it in after the fact
110  friend class libMesh::Parameters;
111 
113  friend std::ostream & operator<<(std::ostream & out, const MooseEnum & obj)
114  {
115  out << obj._current.rawName();
116  return out;
117  }
118 
120  template <typename T>
121  T getEnum() const;
122 
123 protected:
125  virtual void checkDeprecated() const override;
126 
127 private:
131  MooseEnum();
132 
135 };
136 
137 template <typename T>
138 T
140 {
141 #ifdef LIBMESH_HAVE_CXX11_TYPE_TRAITS
142  static_assert(std::is_enum<T>::value == true,
143  "The type requested from MooseEnum::getEnum must be an enum type!\n\n");
144 #endif
145  return static_cast<T>(_current.id());
146 }
147 
friend class libMesh::Parameters
Definition: MooseEnum.h:110
bool operator!=(const char *value) const
Definition: MooseEnum.C:108
CompareMode
Enum item for controlling comparison in the compareCurrent method.
Definition: MooseEnum.h:37
static const int INVALID_ID
Definition: MooseEnumItem.h:24
T getEnum() const
get the current value cast to the enum type T
Definition: MooseEnum.h:139
virtual void checkDeprecated() const override
Check whether the current value is deprecated when called.
Definition: MooseEnum.C:169
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:24
MooseEnumItem _current
The current id.
Definition: MooseEnum.h:134
MooseEnum()
Private constructor for use by libmesh::Parameters.
Definition: MooseEnum.C:36
virtual ~MooseEnum()=default
This is a "smart" enum class intended to replace many of the shortcomings in the C++ enum type It sho...
Definition: MooseEnum.h:31
const std::string & rawName() const
Definition: MooseEnumItem.h:38
friend std::ostream & operator<<(std::ostream &out, const MooseEnum &obj)
Operator for printing to iostreams.
Definition: MooseEnum.h:113
const int & id() const
Return the numeric, name, or raw name.
Definition: MooseEnumItem.h:36
Class for containing MooseEnum item information.
Definition: MooseEnumItem.h:21
bool operator==(const char *value) const
Comparison operators for comparing with character constants, MooseEnums or integer values...
Definition: MooseEnum.C:95
bool compareCurrent(const MooseEnum &other, CompareMode mode=CompareMode::COMPARE_NAME) const
Method for comparing currently set values between MooseEnum.
Definition: MooseEnum.C:138
virtual bool isValid() const override
IsValid.
Definition: MooseEnum.h:106
MooseEnum & operator=(const std::string &name)
Assignment operators.
Definition: MooseEnum.C:39