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 19649790 : MooseEnum::MooseEnum(std::string names, std::string default_name, bool allow_out_of_range) 23 39299580 : : MooseEnumBase(names, allow_out_of_range), _current("", MooseEnumItem::INVALID_ID) 24 : { 25 19649790 : *this = default_name; 26 19649790 : } 27 : 28 1188775 : MooseEnum::MooseEnum(const MooseEnum & other_enum) 29 1188775 : : MooseEnumBase(other_enum), _current(other_enum._current) 30 : { 31 1188775 : } 32 : 33 : /** 34 : * Private constuctor for use by libmesh::Parameters 35 : */ 36 96713010 : MooseEnum::MooseEnum() : _current("", MooseEnumItem::INVALID_ID) {} 37 : 38 : MooseEnum & 39 21087246 : MooseEnum::operator=(const std::string & name) 40 : { 41 21087246 : assign(name); 42 21087227 : return *this; 43 : } 44 : 45 : MooseEnum & 46 263093 : MooseEnum::operator=(int value) 47 : { 48 263093 : assign(value); 49 263092 : 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 21087246 : MooseEnum::assign(const std::string & name, const std::optional<std::string> & context) 61 : { 62 21087246 : if (name == "") 63 : { 64 3469259 : _current = MooseEnumItem("", MooseEnumItem::INVALID_ID); 65 3469259 : return; 66 : } 67 : 68 17617987 : std::set<MooseEnumItem>::const_iterator iter = find(name); 69 17617987 : if (iter == _items.end()) 70 : { 71 258 : if (!_allow_out_of_range) // Are out of range values allowed? 72 38 : mooseError(context ? (*context + ":\n\n") : std::string(""), 73 : "Invalid option \"", 74 : name, 75 : "\" in MooseEnum. Valid options (not case-sensitive) are \"", 76 20 : getRawNames(), 77 : "\"."); 78 : else 79 : { 80 240 : _current = MooseEnumItem(name, getNextValidID()); 81 240 : _items.insert(_current); 82 : } 83 : } 84 : else 85 17617729 : _current = *iter; 86 : 87 17617969 : checkDeprecated(); 88 : } 89 : 90 : void 91 263093 : MooseEnum::assign(int value) 92 : { 93 263093 : if (value == MooseEnumItem::INVALID_ID) 94 : { 95 0 : _current = MooseEnumItem("", MooseEnumItem::INVALID_ID); 96 0 : return; 97 : } 98 : 99 263093 : std::set<MooseEnumItem>::const_iterator iter = find(value); 100 263093 : if (iter == _items.end()) 101 1 : mooseError("Invalid id \"", 102 : value, 103 : "\" in MooseEnum. Valid ids are \"", 104 7 : Moose::stringify(getIDs()), 105 : "\"."); 106 : else 107 263092 : _current = *iter; 108 : 109 263092 : checkDeprecated(); 110 : } 111 : 112 : void 113 79428 : MooseEnum::assign(const MooseEnumItem & item) 114 : { 115 79428 : std::set<MooseEnumItem>::const_iterator iter = find(item); 116 79428 : 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 79428 : _current = *iter; 124 : 125 79428 : checkDeprecated(); 126 79428 : } 127 : 128 : bool 129 51076535 : MooseEnum::operator==(const char * name) const 130 : { 131 51076535 : 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 102153070 : return _current == upper; 139 51076535 : } 140 : 141 : bool 142 1031907 : MooseEnum::operator!=(const char * name) const 143 : { 144 1031907 : return !(*this == name); 145 : } 146 : 147 : bool 148 49597627 : MooseEnum::operator==(int value) const 149 : { 150 49597627 : 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 120884 : MooseEnum::compareCurrent(const MooseEnum & other, CompareMode mode) const 173 : { 174 120884 : switch (mode) 175 : { 176 5 : case CompareMode::COMPARE_BOTH: 177 5 : return (_current.id() == other._current.id()) && (_current.name() == other._current.name()); 178 120874 : case CompareMode::COMPARE_NAME: 179 120874 : return _current.name() == other._current.name(); 180 5 : case CompareMode::COMPARE_ID: 181 5 : 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 17960490 : MooseEnum::checkDeprecated() const 204 : { 205 17960490 : MooseEnumBase::checkDeprecated(_current); 206 17960489 : }