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 "Capability.h" 11 : #include "CapabilityException.h" 12 : 13 : #include "MooseStringUtils.h" 14 : 15 : #include <regex> 16 : 17 : namespace Moose 18 : { 19 3100560 : Capability::Capability(const std::string_view name, 20 : const Capability::Value & value, 21 3100560 : const std::string_view doc) 22 15502800 : : _name(name), _doc(doc), _value(value), _explicit(false) 23 : { 24 : // Check name validity 25 3100560 : if (getName().empty()) 26 2 : throw CapabilityException("Capability has empty name"); 27 3100558 : if (!std::regex_match(getName(), std::regex("[a-z0-9_-]+"))) 28 : throw CapabilityException( 29 4 : "Capability '" + getName() + 30 6 : "': Name has unallowed characters; allowed characters = 'a-z, 0-9, _, -'"); 31 : 32 : // String value validity 33 3100556 : if (const auto string_ptr = queryStringValue()) 34 : { 35 1335119 : const std::regex string_regex("[a-z0-9_.-]+"); 36 1335119 : if (!std::regex_match(*string_ptr, string_regex)) 37 : throw CapabilityException( 38 4 : "String capability '" + getName() + "': value '" + *string_ptr + 39 6 : "' has unallowed characters; allowed characters = 'a-z, 0-9, _, ., -'"); 40 1335119 : } 41 3100578 : } 42 : 43 : bool 44 383133 : Capability::hasEnumeration(const std::string & value) const 45 : { 46 383133 : return _enumeration ? _enumeration->count(value) : true; 47 : } 48 : 49 : const std::set<std::string> & 50 4 : Capability::getEnumeration() const 51 : { 52 4 : if (!_enumeration) 53 4 : throw CapabilityException("Capability::getEnumeration(): Capability '" + getName() + 54 6 : "' does not have an enumeration"); 55 2 : return *_enumeration; 56 : } 57 : 58 : Capability & 59 568311 : Capability::setExplicit() 60 : { 61 568311 : if (hasBoolValue()) 62 4 : throw CapabilityException("Capability::setExplicit(): Capability '" + getName() + 63 6 : "' is bool-valued and cannot be set as explicit"); 64 568309 : _explicit = true; 65 568309 : return *this; 66 : } 67 : 68 : Capability & 69 393103 : Capability::setEnumeration(const std::set<std::string> & enumeration) 70 : { 71 501881 : static const std::string error_prefix = "Capability::setEnumeration(): "; 72 : 73 393103 : const auto string_ptr = queryStringValue(); 74 393103 : if (!string_ptr) 75 8 : throw CapabilityException(error_prefix + "Capability '" + getName() + 76 12 : "' is not string-valued and cannot have an enumeration"); 77 : 78 393099 : if (_enumeration) 79 : { 80 12104 : if (*_enumeration == enumeration) 81 12102 : return *this; 82 4 : throw CapabilityException(error_prefix + "Capability '" + getName() + 83 6 : "' already has an enumeration set"); 84 : } 85 : 86 380995 : if (enumeration.empty()) 87 2 : throw CapabilityException(error_prefix + "Enumeration is empty for '" + getName() + "'"); 88 : 89 1632791 : for (const auto & value : enumeration) 90 1251802 : if (!std::regex_match(value, std::regex("[a-z0-9_-]+"))) 91 8 : throw CapabilityException(error_prefix + "Enumeration value '" + value + 92 8 : "' for capability '" + getName() + "'" + 93 12 : " has unallowed characters; allowed characters = 'a-z, 0-9, _, -'"); 94 : 95 380989 : _enumeration = enumeration; 96 : 97 380989 : if (!hasEnumeration(*string_ptr)) 98 4 : throw CapabilityException(error_prefix + "Capability " + toString() + 99 6 : " value not within enumeration"); 100 : 101 380987 : return *this; 102 : } 103 : 104 : bool 105 14 : Capability::getBoolValue() const 106 : { 107 14 : if (const auto bool_ptr = queryBoolValue()) 108 10 : return *bool_ptr; 109 8 : throw CapabilityException("Capability::getBoolValue(): Capability " + toString() + 110 12 : " is not a bool"); 111 : } 112 : 113 : int 114 8 : Capability::getIntValue() const 115 : { 116 8 : if (const auto int_ptr = queryIntValue()) 117 2 : return *int_ptr; 118 12 : throw CapabilityException("Capability::getIntValue(): Capability " + toString() + 119 18 : " is not an integer"); 120 : } 121 : 122 : const std::string & 123 34 : Capability::getStringValue() const 124 : { 125 34 : if (const auto string_ptr = queryStringValue()) 126 28 : return *string_ptr; 127 12 : throw CapabilityException("Capability::getStringValue(): Capability " + toString() + 128 18 : " is not a string"); 129 : } 130 : 131 : std::string 132 50 : Capability::valueToString() const 133 : { 134 50 : if (const auto bool_ptr = queryBoolValue()) 135 44 : return *bool_ptr ? "true" : "false"; 136 28 : if (const auto string_ptr = queryStringValue()) 137 16 : return *string_ptr; 138 12 : if (const auto int_ptr = queryIntValue()) 139 12 : return std::to_string(*int_ptr); 140 0 : throw CapabilityException("Capability::valueToString(): Invalid type"); 141 : } 142 : 143 : std::string 144 42 : Capability::toString() const 145 : { 146 42 : return getName() + "=" + valueToString(); 147 : } 148 : 149 : std::string 150 15 : Capability::enumerationToString() const 151 : { 152 15 : if (_enumeration) 153 : return MooseUtils::stringJoin( 154 52 : std::vector<std::string>(_enumeration->begin(), _enumeration->end()), ", "); 155 : throw CapabilityException("Capability::enumerationToString(): Capability '", 156 2 : getName(), 157 2 : "' does not have an enumeration"); 158 : } 159 : } // namespace Moose