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 "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 & 22 2376015 : Capabilities::getCapabilityRegistry() 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 2376015 : if (!capability_registry) 28 51208 : capability_registry = new Capabilities(); 29 2376015 : return *capability_registry; 30 : } 31 : 32 : void 33 2373144 : Capabilities::add(const std::string & raw_capability, 34 : CapabilityUtils::Type value, 35 : const std::string & doc) 36 : { 37 2373144 : const auto capability = MooseUtils::toLower(raw_capability); 38 2373144 : if (std::holds_alternative<std::string>(value)) 39 571778 : value = MooseUtils::toLower(std::get<std::string>(value)); 40 : 41 2373144 : auto it_pair = _capability_registry.lower_bound(capability); 42 2801641 : if (it_pair == _capability_registry.end() || it_pair->first != capability || 43 428497 : it_pair->second.first == value) 44 2373144 : _capability_registry.emplace_hint(it_pair, capability, std::make_pair(value, doc)); 45 : else 46 0 : mooseError("Capability '", 47 : capability, 48 : "' was already registered with a different value. ('", 49 0 : Moose::stringify(it_pair->second.first), 50 : "' instead of '", 51 0 : Moose::stringify(value), 52 : "')"); 53 2373144 : } 54 : 55 : void 56 4 : Capabilities::add(const std::string & capability, const char * value, const char * doc) 57 : { 58 4 : add(capability, std::string(value), std::string(doc)); 59 4 : } 60 : 61 : std::string 62 34 : Capabilities::dump() const 63 : { 64 34 : nlohmann::json root; 65 1332 : for (const auto & [capability, value_doc] : _capability_registry) 66 : { 67 1298 : const auto & value = value_doc.first; 68 1298 : const auto & doc = value_doc.second; 69 1298 : if (std::holds_alternative<bool>(value)) 70 2742 : root[capability] = {std::get<bool>(value), doc}; 71 384 : else if (std::holds_alternative<int>(value)) 72 204 : root[capability] = {std::get<int>(value), doc}; 73 316 : else if (std::holds_alternative<std::string>(value)) 74 948 : root[capability] = {std::get<std::string>(value), doc}; 75 : else 76 0 : mooseError("Unknown type in capabilities registry"); 77 : } 78 68 : return root.dump(2); 79 1332 : } 80 : 81 : CapabilityUtils::Result 82 2964 : Capabilities::check(const std::string & requested_capabilities) const 83 : { 84 2964 : const auto result = CapabilityUtils::check(requested_capabilities, _capability_registry); 85 2951 : return result; 86 : } 87 : 88 : } // namespace Moose