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 : #include <optional> 13 : #include <set> 14 : #include <string> 15 : #include <variant> 16 : 17 : #ifdef MOOSE_UNIT_TEST 18 : // forward declare unit tests 19 : #include "gtest/gtest.h" 20 : class GTEST_TEST_CLASS_NAME_(CapabilityTest, negateValue); 21 : class GTEST_TEST_CLASS_NAME_(CapabilitiesTest, isInstallationType); 22 : #endif 23 : 24 : namespace Moose 25 : { 26 : /** 27 : * An entry for a single capability. 28 : */ 29 : class Capability 30 : { 31 : public: 32 : /// A capability can have a bool, int, or string value 33 : using Value = std::variant<bool, int, std::string>; 34 : 35 : Capability() = delete; 36 : Capability(const std::string_view name, 37 : const Capability::Value & value, 38 : const std::string_view doc); 39 : 40 : /** 41 : * @return The name of the capability. 42 : */ 43 6210780 : const std::string & getName() const { return _name; } 44 : 45 : /** 46 : * @return The documentation string. 47 : */ 48 86521 : const std::string & getDoc() const { return _doc; } 49 : 50 : /** 51 : * @return The capability value. 52 : */ 53 86541 : const Capability::Value & getValue() const { return _value; } 54 : 55 : /** 56 : * @return Whether or not the capability is explicit. 57 : * 58 : * Explicit implies that the capability cannot be compared as a boolean. 59 : */ 60 6527 : bool getExplicit() const { return _explicit; } 61 : 62 : /** 63 : * @return The enumeration, if set. 64 : * 65 : * Only string-valued capabilites can have an enumeration. 66 : */ 67 1845 : const std::optional<std::set<std::string>> & queryEnumeration() const { return _enumeration; } 68 : 69 : /** 70 : * @return The enumeration. 71 : * 72 : * Will error if the capability does not have an enumeration. 73 : * 74 : * Only string-valued capabilites can have an enumeration. 75 : */ 76 : const std::set<std::string> & getEnumeration() const; 77 : 78 : /** 79 : * @return Whether or not the capability has the given enumeration \p value. 80 : * 81 : * This is only valid for string-valued capabilities. 82 : * 83 : * If the capability has no enumerations, this will always return true. 84 : */ 85 : bool hasEnumeration(const std::string & value) const; 86 : 87 : /** 88 : * Set the capability to be explicit. 89 : * 90 : * Explicit implies that the capability cannot be compared as a boolean. 91 : * 92 : * This is only valid for non-bool valued capabilities. 93 : */ 94 : Capability & setExplicit(); 95 : 96 : /** 97 : * Set the enumeration (allowed values) for the capability. 98 : * 99 : * This is only valid for string-valued capabilities. 100 : */ 101 : Capability & setEnumeration(const std::set<std::string> & enumeration); 102 : 103 : #if defined(MOOSE_UNIT_TEST) || defined(FOR_PYCAPABILITIES) 104 : /** 105 : * Negate a Capability value. 106 : * 107 : * This should only be used by pycapabilities via the 108 : * TestHarness when it needs to augment capabilities 109 : * to check on if a check depeneds on a capability or not. 110 : */ 111 : inline void negateValue(); 112 : #endif 113 : 114 : /** 115 : * @return The boolean capability value if it is a boolean. 116 : */ 117 9582 : const bool * queryBoolValue() const { return std::get_if<bool>(&_value); } 118 : /** 119 : * @return The integer capability value if it is an integer. 120 : */ 121 1878 : const int * queryIntValue() const { return std::get_if<int>(&_value); } 122 : /** 123 : * @return The string capability value if it is a string. 124 : */ 125 3497217 : const std::string * queryStringValue() const { return std::get_if<std::string>(&_value); } 126 : 127 : /** 128 : * @return Whether or not the capability value is a boolean. 129 : */ 130 570126 : bool hasBoolValue() const { return std::holds_alternative<bool>(_value); } 131 : 132 : /** 133 : * @return Whether or not the capability value is an integer. 134 : */ 135 8 : bool hasIntValue() const { return std::holds_alternative<int>(_value); } 136 : 137 : /** 138 : * @return Whether or not the capability value is a string. 139 : */ 140 8 : bool hasStringValue() const { return std::holds_alternative<std::string>(_value); } 141 : 142 : /** 143 : * @return The boolean capability value. 144 : * 145 : * Will error if the value is not a boolean. 146 : */ 147 : bool getBoolValue() const; 148 : /** 149 : * @return The boolean capability value. 150 : * 151 : * Will error if the value is not an integer. 152 : */ 153 : int getIntValue() const; 154 : /** 155 : * @return The string capability value. 156 : * 157 : * Will error if the value is not a string. 158 : */ 159 : const std::string & getStringValue() const; 160 : 161 : /** 162 : * @return The capability value as a string. 163 : */ 164 : std::string valueToString() const; 165 : 166 : /** 167 : * @return The capability as a string in the form of "name=value". 168 : */ 169 : std::string toString() const; 170 : 171 : /** 172 : * @return The enumeration as a string of comma separated values. 173 : * 174 : * This is only valid for a capability that has an enumeration. 175 : */ 176 : std::string enumerationToString() const; 177 : 178 : private: 179 : #ifdef MOOSE_UNIT_TEST 180 : FRIEND_TEST(::CapabilityTest, negateValue); 181 : FRIEND_TEST(::CapabilitiesTest, isInstallationType); 182 : #endif 183 : 184 : /// The name of capability 185 : std::string _name; 186 : /// Description for the capability 187 : std::string _doc; 188 : /// The value the capability is set to 189 : Capability::Value _value; 190 : /// Whether or not this capability must be compared explicitly 191 : /// (not as a boolean check) 192 : bool _explicit; 193 : /// Possible enumeration for the capability, if any (string capabilities only) 194 : std::optional<std::set<std::string>> _enumeration; 195 : }; 196 : 197 : #if defined(MOOSE_UNIT_TEST) || defined(FOR_PYCAPABILITIES) 198 : void 199 16 : Capability::negateValue() 200 : { 201 16 : _explicit = false; 202 16 : _enumeration.reset(); 203 16 : _value = false; 204 16 : } 205 : #endif 206 : }