https://mooseframework.inl.gov
Loading...
Searching...
No Matches
Capability.C
Go to the documentation of this file.
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
17namespace Moose
18{
19Capability::Capability(const std::string_view name,
20 const Capability::Value & value,
21 const std::string_view doc)
22 : _name(name), _doc(doc), _value(value), _explicit(false)
23{
24 // Check name validity
25 if (getName().empty())
26 throw CapabilityException("Capability has empty name");
27 if (!std::regex_match(getName(), std::regex("[a-z0-9_-]+")))
29 "Capability '" + getName() +
30 "': Name has unallowed characters; allowed characters = 'a-z, 0-9, _, -'");
31
32 // String value validity
33 if (const auto string_ptr = queryStringValue())
34 {
35 const std::regex string_regex("[a-z0-9_.-]+");
36 if (!std::regex_match(*string_ptr, string_regex))
38 "String capability '" + getName() + "': value '" + *string_ptr +
39 "' has unallowed characters; allowed characters = 'a-z, 0-9, _, ., -'");
40 }
41}
42
43bool
44Capability::hasEnumeration(const std::string & value) const
45{
46 return _enumeration ? _enumeration->count(value) : true;
47}
48
49const std::set<std::string> &
51{
52 if (!_enumeration)
53 throw CapabilityException("Capability::getEnumeration(): Capability '" + getName() +
54 "' does not have an enumeration");
55 return *_enumeration;
56}
57
60{
61 if (hasBoolValue())
62 throw CapabilityException("Capability::setExplicit(): Capability '" + getName() +
63 "' is bool-valued and cannot be set as explicit");
64 _explicit = true;
65 return *this;
66}
67
69Capability::setEnumeration(const std::set<std::string> & enumeration)
70{
71 static const std::string error_prefix = "Capability::setEnumeration(): ";
72
73 const auto string_ptr = queryStringValue();
74 if (!string_ptr)
75 throw CapabilityException(error_prefix + "Capability '" + getName() +
76 "' is not string-valued and cannot have an enumeration");
77
78 if (_enumeration)
79 {
80 if (*_enumeration == enumeration)
81 return *this;
82 throw CapabilityException(error_prefix + "Capability '" + getName() +
83 "' already has an enumeration set");
84 }
85
86 if (enumeration.empty())
87 throw CapabilityException(error_prefix + "Enumeration is empty for '" + getName() + "'");
88
89 for (const auto & value : enumeration)
90 if (!std::regex_match(value, std::regex("[a-z0-9_-]+")))
91 throw CapabilityException(error_prefix + "Enumeration value '" + value +
92 "' for capability '" + getName() + "'" +
93 " has unallowed characters; allowed characters = 'a-z, 0-9, _, -'");
94
95 _enumeration = enumeration;
96
97 if (!hasEnumeration(*string_ptr))
98 throw CapabilityException(error_prefix + "Capability " + toString() +
99 " value not within enumeration");
100
101 return *this;
102}
103
104bool
106{
107 if (const auto bool_ptr = queryBoolValue())
108 return *bool_ptr;
109 throw CapabilityException("Capability::getBoolValue(): Capability " + toString() +
110 " is not a bool");
111}
112
113int
115{
116 if (const auto int_ptr = queryIntValue())
117 return *int_ptr;
118 throw CapabilityException("Capability::getIntValue(): Capability " + toString() +
119 " is not an integer");
120}
121
122const std::string &
124{
125 if (const auto string_ptr = queryStringValue())
126 return *string_ptr;
127 throw CapabilityException("Capability::getStringValue(): Capability " + toString() +
128 " is not a string");
129}
130
131std::string
133{
134 if (const auto bool_ptr = queryBoolValue())
135 return *bool_ptr ? "true" : "false";
136 if (const auto string_ptr = queryStringValue())
137 return *string_ptr;
138 if (const auto int_ptr = queryIntValue())
139 return std::to_string(*int_ptr);
140 throw CapabilityException("Capability::valueToString(): Invalid type");
141}
142
143std::string
145{
146 return getName() + "=" + valueToString();
147}
148
149std::string
151{
152 if (_enumeration)
154 std::vector<std::string>(_enumeration->begin(), _enumeration->end()), ", ");
155 throw CapabilityException("Capability::enumerationToString(): Capability '",
156 getName(),
157 "' does not have an enumeration");
158}
159} // namespace Moose
Common execption to be thrown when interacting with capabilities.
An entry for a single capability.
Definition Capability.h:30
const std::string & getName() const
Definition Capability.h:43
std::string enumerationToString() const
Definition Capability.C:150
std::string toString() const
Definition Capability.C:144
std::optional< std::set< std::string > > _enumeration
Possible enumeration for the capability, if any (string capabilities only)
Definition Capability.h:194
const std::set< std::string > & getEnumeration() const
Definition Capability.C:50
std::variant< bool, int, std::string > Value
A capability can have a bool, int, or string value.
Definition Capability.h:33
int getIntValue() const
Definition Capability.C:114
bool _explicit
Whether or not this capability must be compared explicitly (not as a boolean check)
Definition Capability.h:192
const int * queryIntValue() const
Definition Capability.h:121
const std::string & getStringValue() const
Definition Capability.C:123
Capability & setExplicit()
Set the capability to be explicit.
Definition Capability.C:59
std::string valueToString() const
Definition Capability.C:132
bool hasBoolValue() const
Definition Capability.h:130
const bool * queryBoolValue() const
Definition Capability.h:117
Capability & setEnumeration(const std::set< std::string > &enumeration)
Set the enumeration (allowed values) for the capability.
Definition Capability.C:69
bool hasEnumeration(const std::string &value) const
Definition Capability.C:44
const std::string * queryStringValue() const
Definition Capability.h:125
bool getBoolValue() const
Definition Capability.C:105
std::string stringJoin(const std::vector< std::string > &values, const std::string &separator=" ")
Concatenates value into a single string separated by separator.
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...