Line data Source code
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 : 15 : #include <optional> 16 : 17 : // Forward declarations 18 : namespace libMesh 19 : { 20 : class Parameters; 21 : } 22 : 23 : /** 24 : * This is a "smart" enum class intended to replace many of the 25 : * shortcomings in the C++ enum type It should be initialized with a 26 : * space-delimited list of strings which become the enum values. You 27 : * may also optionally supply numeric ints for one or more values 28 : * similar to a C++ enum. This is done with the "=" sign (no 29 : * spaces). It can be used any place where an integer (switch 30 : * statements), const char* or std::string is expected. In addition 31 : * the InputParameters system has full support for this Enum type 32 : */ 33 : class MooseEnum : public MooseEnumBase 34 : { 35 : public: 36 : /** 37 : * Enum item for controlling comparison in the compareCurrent method. 38 : */ 39 : enum class CompareMode 40 : { 41 : COMPARE_NAME, 42 : COMPARE_ID, 43 : COMPARE_BOTH 44 : }; 45 : 46 : /** 47 : * Constructor that takes a list of enumeration values, and a separate string to set a default for 48 : * this instance 49 : * @param names - a list of names for this enumeration 50 : * @param default_name - the default value for this enumeration instance 51 : * @param allow_out_of_range - determines whether this enumeration will accept values outside of 52 : * it's range of defined values. 53 : */ 54 : MooseEnum(std::string names, std::string default_name = "", bool allow_out_of_range = false); 55 : 56 : /** 57 : * Copy Constructor for use when creating vectors of MooseEnums 58 : * @param other_enum - The other enumeration to copy state from 59 : */ 60 : MooseEnum(const MooseEnum & other_enum); 61 : 62 : /** 63 : * Copy Assignment operator must be explicitly defined when a copy ctor exists and this 64 : * method is used. 65 : */ 66 30887007 : MooseEnum & operator=(const MooseEnum & other_enum) = default; 67 : 68 50503316 : virtual ~MooseEnum() = default; 69 : 70 : /** 71 : * Cast operators to make this object behave as value_types and std::string 72 : * these methods can be used so that this class behaves more like a normal value_type enumeration 73 : */ 74 10946095 : operator int() const { return _current.id(); } 75 2092404 : operator std::string() const { return _current.rawName(); } 76 : 77 : /** 78 : * Comparison operators for comparing with character constants, MooseEnums 79 : * or integer values 80 : * @param value - RHS value to compare against 81 : * @return bool - the truth value for the comparison 82 : */ 83 : bool operator==(const char * value) const; 84 : bool operator!=(const char * value) const; 85 : 86 : bool operator==(int value) const; 87 : bool operator!=(int value) const; 88 : 89 : bool operator==(unsigned short value) const; 90 : bool operator!=(unsigned short value) const; 91 : 92 : bool operator==(const MooseEnum & value) const; 93 : bool operator!=(const MooseEnum & value) const; 94 : 95 : /** 96 : * Method for comparing currently set values between MooseEnum. 97 : */ 98 : bool compareCurrent(const MooseEnum & other, CompareMode mode = CompareMode::COMPARE_NAME) const; 99 : 100 : ///@{ 101 : /** 102 : * Assignment operators/methods 103 : * @param name/int - a string or int representing one of the enumeration values. 104 : * @return A reference to this object for chaining 105 : */ 106 : MooseEnum & operator=(const std::string & name); 107 : MooseEnum & operator=(int value); 108 : MooseEnum & operator=(const MooseEnumItem & item); 109 : void assign(const std::string & name, const std::optional<std::string> & context = {}); 110 : void assign(int value); 111 : void assign(const MooseEnumItem & item); 112 : ///@} 113 : 114 : /** 115 : * IsValid 116 : * @return - a Boolean indicating whether this Enumeration has been set 117 : */ 118 6735909 : virtual bool isValid() const override { return _current.id() > MooseEnumItem::INVALID_ID; } 119 : 120 : // InputParameters is allowed to create an empty enum but is responsible for 121 : // filling it in after the fact 122 : friend class libMesh::Parameters; 123 : 124 : /// Operator for printing to iostreams 125 1033741 : friend std::ostream & operator<<(std::ostream & out, const MooseEnum & obj) 126 : { 127 1033741 : out << obj._current.rawName(); 128 1033741 : return out; 129 : } 130 : 131 : /// get the current value cast to the enum type T 132 : template <typename T> 133 : T getEnum() const; 134 : 135 : protected: 136 : /// Check whether the current value is deprecated when called 137 : virtual void checkDeprecated() const override; 138 : 139 : /** 140 : * Constructor for use by libmesh::Parameters and ReporterMode 141 : */ 142 : MooseEnum(); 143 : 144 : private: 145 : /// The current id 146 : MooseEnumItem _current; 147 : }; 148 : 149 : template <typename T> 150 : T 151 427844 : MooseEnum::getEnum() const 152 : { 153 : #ifdef LIBMESH_HAVE_CXX11_TYPE_TRAITS 154 : static_assert(std::is_enum<T>::value == true, 155 : "The type requested from MooseEnum::getEnum must be an enum type!\n\n"); 156 : #endif 157 427844 : return static_cast<T>(_current.id()); 158 : }