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 : #include "MooseEnum.h" 11 : #include "MooseUtils.h" 12 : #include "MooseError.h" 13 : #include "Conversion.h" 14 : 15 : #include <sstream> 16 : #include <algorithm> 17 : #include <iterator> 18 : #include <limits> 19 : #include <string> 20 : #include <iostream> 21 : 22 30256323 : MooseEnum::MooseEnum(std::string names, std::string default_name, bool allow_out_of_range) 23 60512646 : : MooseEnumBase(names, allow_out_of_range), _current("", MooseEnumItem::INVALID_ID) 24 : { 25 30256323 : *this = default_name; 26 30256323 : } 27 : 28 766865 : MooseEnum::MooseEnum(const MooseEnum & other_enum) 29 766865 : : MooseEnumBase(other_enum), _current(other_enum._current) 30 : { 31 766865 : } 32 : 33 : /** 34 : * Private constuctor for use by libmesh::Parameters 35 : */ 36 161034129 : MooseEnum::MooseEnum() : _current("", MooseEnumItem::INVALID_ID) {} 37 : 38 : MooseEnum & 39 31762088 : MooseEnum::operator=(const std::string & name) 40 : { 41 31762088 : assign(name); 42 31762067 : return *this; 43 : } 44 : 45 : MooseEnum & 46 263245 : MooseEnum::operator=(int value) 47 : { 48 263245 : assign(value); 49 263243 : return *this; 50 : } 51 : 52 : MooseEnum & 53 0 : MooseEnum::operator=(const MooseEnumItem & item) 54 : { 55 0 : assign(item); 56 0 : return *this; 57 : } 58 : 59 : void 60 31762088 : MooseEnum::assign(const std::string & name, const std::optional<std::string> & context) 61 : { 62 31762088 : if (name == "") 63 : { 64 3586819 : _current = MooseEnumItem("", MooseEnumItem::INVALID_ID); 65 3586819 : return; 66 : } 67 : 68 28175269 : std::set<MooseEnumItem>::const_iterator iter = find(name); 69 28175269 : if (iter == _items.end()) 70 : { 71 261 : if (!_allow_out_of_range) // Are out of range values allowed? 72 74 : mooseError(context ? (*context + ":\n\n") : std::string(""), 73 : "Invalid option \"", 74 : name, 75 : "\" in MooseEnum. Valid options (not case-sensitive) are \"", 76 55 : getRawNames(), 77 : "\"."); 78 : else 79 : { 80 242 : _current = MooseEnumItem(name, getNextValidID()); 81 242 : _items.insert(_current); 82 : } 83 : } 84 : else 85 28175008 : _current = *iter; 86 : 87 28175250 : checkDeprecated(); 88 : } 89 : 90 : void 91 263245 : MooseEnum::assign(int value) 92 : { 93 263245 : if (value == MooseEnumItem::INVALID_ID) 94 : { 95 0 : _current = MooseEnumItem("", MooseEnumItem::INVALID_ID); 96 0 : return; 97 : } 98 : 99 263245 : std::set<MooseEnumItem>::const_iterator iter = find(value); 100 263245 : if (iter == _items.end()) 101 2 : mooseError("Invalid id \"", 102 : value, 103 : "\" in MooseEnum. Valid ids are \"", 104 14 : Moose::stringify(getIDs()), 105 : "\"."); 106 : else 107 263243 : _current = *iter; 108 : 109 263243 : checkDeprecated(); 110 : } 111 : 112 : void 113 80655 : MooseEnum::assign(const MooseEnumItem & item) 114 : { 115 80655 : std::set<MooseEnumItem>::const_iterator iter = find(item); 116 80655 : if (iter == _items.end()) 117 0 : mooseError("Invalid item \"", 118 : item, 119 : "\" in MooseEnum. Valid ids are \"", 120 0 : Moose::stringify(items()), 121 : "\"."); 122 : else 123 80655 : _current = *iter; 124 : 125 80655 : checkDeprecated(); 126 80655 : } 127 : 128 : bool 129 51434544 : MooseEnum::operator==(const char * name) const 130 : { 131 51434544 : std::string upper(MooseUtils::toUpper(name)); 132 : 133 : mooseAssert(_allow_out_of_range || find(upper) != _items.end(), 134 : "Invalid string comparison \"" + upper + 135 : "\" in MooseEnum. Valid options (not case-sensitive) are \"" + getRawNames() + 136 : "\"."); 137 : 138 102869088 : return _current == upper; 139 51434544 : } 140 : 141 : bool 142 1050206 : MooseEnum::operator!=(const char * name) const 143 : { 144 1050206 : return !(*this == name); 145 : } 146 : 147 : bool 148 49602029 : MooseEnum::operator==(int value) const 149 : { 150 49602029 : return value == _current; 151 : } 152 : 153 : bool 154 822 : MooseEnum::operator!=(int value) const 155 : { 156 822 : return value != _current; 157 : } 158 : 159 : bool 160 0 : MooseEnum::operator==(unsigned short value) const 161 : { 162 0 : return value == _current; 163 : } 164 : 165 : bool 166 0 : MooseEnum::operator!=(unsigned short value) const 167 : { 168 0 : return value != _current; 169 : } 170 : 171 : bool 172 122774 : MooseEnum::compareCurrent(const MooseEnum & other, CompareMode mode) const 173 : { 174 122774 : switch (mode) 175 : { 176 10 : case CompareMode::COMPARE_BOTH: 177 10 : return (_current.id() == other._current.id()) && (_current.name() == other._current.name()); 178 122754 : case CompareMode::COMPARE_NAME: 179 122754 : return _current.name() == other._current.name(); 180 10 : case CompareMode::COMPARE_ID: 181 10 : return _current.id() == other._current.id(); 182 : } 183 0 : return false; 184 : } 185 : 186 : bool 187 0 : MooseEnum::operator==(const MooseEnum & value) const 188 : { 189 0 : mooseDeprecated("This method will be removed because the meaning is not well defined, please use " 190 : "the 'compareCurrent' method instead."); 191 0 : return value._current.name() == _current.name(); 192 : } 193 : 194 : bool 195 0 : MooseEnum::operator!=(const MooseEnum & value) const 196 : { 197 0 : mooseDeprecated("This method will be removed because the meaning is not well defined, please use " 198 : "the 'compareCurrent' method instead."); 199 0 : return value._current.name() != _current.name(); 200 : } 201 : 202 : void 203 28519150 : MooseEnum::checkDeprecated() const 204 : { 205 28519150 : MooseEnumBase::checkDeprecated(_current); 206 28519148 : }