https://mooseframework.inl.gov
Capabilities.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 "Capabilities.h"
11 #include "MooseUtils.h"
12 #include "Conversion.h"
13 
14 #include "nlohmann/json.h"
15 
16 #include <vector>
17 
18 namespace Moose
19 {
20 
21 Capabilities &
23 {
24  // We need a naked new here (_not_ a smart pointer or object instance) due to what seems like a
25  // bug in clang's static object destruction when using dynamic library loading.
26  static Capabilities * capability_registry = nullptr;
27  if (!capability_registry)
28  capability_registry = new Capabilities();
29  return *capability_registry;
30 }
31 
32 void
33 Capabilities::add(const std::string & raw_capability,
35  const std::string & doc)
36 {
37  const auto capability = MooseUtils::toLower(raw_capability);
38  if (std::holds_alternative<std::string>(value))
39  value = MooseUtils::toLower(std::get<std::string>(value));
40 
41  auto it_pair = _capability_registry.lower_bound(capability);
42  if (it_pair == _capability_registry.end() || it_pair->first != capability ||
43  it_pair->second.first == value)
44  _capability_registry.emplace_hint(it_pair, capability, std::make_pair(value, doc));
45  else
46  mooseError("Capability '",
47  capability,
48  "' was already registered with a different value. ('",
49  Moose::stringify(it_pair->second.first),
50  "' instead of '",
52  "')");
53 }
54 
55 void
56 Capabilities::add(const std::string & capability, const char * value, const char * doc)
57 {
58  add(capability, std::string(value), std::string(doc));
59 }
60 
61 std::string
63 {
64  nlohmann::json root;
65  for (const auto & [capability, value_doc] : _capability_registry)
66  {
67  const auto & value = value_doc.first;
68  const auto & doc = value_doc.second;
69  if (std::holds_alternative<bool>(value))
70  root[capability] = {std::get<bool>(value), doc};
71  else if (std::holds_alternative<int>(value))
72  root[capability] = {std::get<int>(value), doc};
73  else if (std::holds_alternative<std::string>(value))
74  root[capability] = {std::get<std::string>(value), doc};
75  else
76  mooseError("Unknown type in capabilities registry");
77  }
78  return root.dump(2);
79 }
80 
82 Capabilities::check(const std::string & requested_capabilities) const
83 {
84  const auto result = CapabilityUtils::check(requested_capabilities, _capability_registry);
85  return result;
86 }
87 
88 } // namespace Moose
This singleton class holds a registry for capabilities supported by the current app.
Definition: Capabilities.h:35
std::string toLower(const std::string &name)
Convert supplied string to lower case.
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
CapabilityUtils::Result check(const std::string &requested_capabilities) const
check if the given required capabilities are fulfilled, returns a bool, a reason, and a verbose docum...
Definition: Capabilities.C:82
std::tuple< CheckState, std::string, std::string > Result
Result from a capability check: the state, the reason, and the documentation.
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
std::string dump() const
create a JSON dump of the capabilities registry
Definition: Capabilities.C:62
std::string stringify(const T &t)
conversion to string
Definition: Conversion.h:64
static Capabilities & getCapabilityRegistry()
Definition: Capabilities.C:22
std::variant< bool, int, std::string > Type
A capability can have a bool, int, or string value.
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...
void add(const std::string &capability, CapabilityUtils::Type value, const std::string &doc)
register a new capability
Definition: Capabilities.C:33
Result check(std::string requirements, const Registry &capabilities)
Checks if a set of requirements is satisified by the given capability registry.
std::map< std::string, std::pair< CapabilityUtils::Type, std::string > > _capability_registry
Capability registry.
Definition: Capabilities.h:66