https://mooseframework.inl.gov
Loading...
Searching...
No Matches
WebServerControlTypeRegistry.h
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#pragma once
11
12#include <functional>
13#include <string>
14#include <map>
15#include <type_traits>
16#include <typeindex>
17
18#include "nlohmann/json.h"
19
20#include "MooseError.h"
21#include "MooseUtils.h"
22
24
25namespace Moose
26{
34{
35public:
40
45 {
46 public:
52 ControlledValueBase(const std::string & name, const std::string & type);
53
55
59 const std::string & name() const { return _name; }
63 const std::string & type() const { return _type; }
64
71 virtual void setControllableValue(WebServerControl & control) = 0;
72
73 private:
75 const std::string _name;
77 const std::string _type;
78 };
79
84 {
85 public:
86 RegisteredTypeBase(const std::string & type);
87
89
93 const std::string & type() const { return _type; }
102 virtual std::unique_ptr<ControlledValueBase> build(const std::string & name,
103 const nlohmann::json & json_value) const = 0;
111 virtual std::unique_ptr<ControlledValueBase> build(const std::string & name) const = 0;
112
113 private:
115 const std::string _type;
116 };
117
127 template <class ControlledValue, class ValueType>
129 {
130 RegisteredType(const std::string & type,
131 std::function<ValueType(const nlohmann::json &)> && parse_function)
132 : RegisteredTypeBase(type), _parse_function(parse_function)
133 {
134 }
135
136 virtual std::unique_ptr<ControlledValueBase>
137 build(const std::string & name) const override final
138 {
139 return std::make_unique<ControlledValue>(name, type());
140 }
141 virtual std::unique_ptr<ControlledValueBase>
142 build(const std::string & name, const nlohmann::json & json_value) const override final
143 {
144 return std::make_unique<ControlledValue>(name, type(), _parse_function(json_value));
145 }
146
147 private:
149 const std::function<ValueType(const nlohmann::json &)> _parse_function;
150 };
151
161 template <class ControlledValue, class ValueType>
162 static char add(const std::string & type_name,
163 std::function<ValueType(const nlohmann::json &)> && parse_function);
164
168 static const RegisteredTypeBase * query(const std::string & type);
169
173 static const RegisteredTypeBase & get(const std::string & type);
174
175private:
177 std::map<std::string, std::unique_ptr<RegisteredTypeBase>> _name_map;
180 std::set<std::type_index> _value_types;
181};
182
183template <class ControlledValue, class ValueType>
184char
186 const std::string & type_name,
187 std::function<ValueType(const nlohmann::json &)> && parse_function)
188{
189 static_assert(std::is_base_of_v<ControlledValueBase, ControlledValue>,
190 "Is not derived from ControlledValueBase");
191 static_assert(std::is_same_v<typename ControlledValue::value_type, ValueType>, "Is not the same");
192
193 auto entry = std::make_unique<RegisteredType<ControlledValue, ValueType>>(
194 type_name, std::move(parse_function));
195 if (!getRegistry()._name_map.emplace(type_name, std::move(entry)).second)
197 "WebServerControlTypeRegistry: The string type \"", type_name, "\" is already registered.");
198
199 static const std::type_index index = typeid(ValueType);
200 if (!getRegistry()._value_types.insert(index).second)
201 ::mooseError("WebServerControlRegistry: The type \"",
202 MooseUtils::prettyCppType<ValueType>(),
203 "\" is already registered");
204 return 0;
205}
206}
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
The base class for a value that is produced by this registry.
virtual void setControllableValue(WebServerControl &control)=0
Sets the controllable value given the name and type via the controllable interface in control.
const std::string _type
The string representation of the type.
const std::string _name
The name that the value is for.
Base registry class for a type that is used to build values.
virtual std::unique_ptr< ControlledValueBase > build(const std::string &name, const nlohmann::json &json_value) const =0
Builds a value with the given type, name name, and JSON value json_value.
virtual std::unique_ptr< ControlledValueBase > build(const std::string &name) const =0
Builds a value with the given type, name name, and a default value.
const std::string _type
The string representation of the underlying type.
A static registry used to register and build values of different types for the WebServerControl.
static const RegisteredTypeBase & get(const std::string &type)
Get the registration for the given type, erroring if it isn't registered.
std::set< std::type_index > _value_types
The registered value types, to avoid registering the same underlying value type multiple times.
static const RegisteredTypeBase * query(const std::string &type)
Query the registration for the given type.
static WebServerControlTypeRegistry & getRegistry()
static char add(const std::string &type_name, std::function< ValueType(const nlohmann::json &)> &&parse_function)
Register a type in the registry.
std::map< std::string, std::unique_ptr< RegisteredTypeBase > > _name_map
The registration data.
Starts a webserver that an external process can connect to in order to send JSON messages to control ...
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...
virtual std::unique_ptr< ControlledValueBase > build(const std::string &name) const override final
Builds a value with the given type, name name, and a default value.
virtual std::unique_ptr< ControlledValueBase > build(const std::string &name, const nlohmann::json &json_value) const override final
Builds a value with the given type, name name, and JSON value json_value.
const std::function< ValueType(const nlohmann::json &)> _parse_function
Function that converts from json -> the value for the ValueType.
RegisteredType(const std::string &type, std::function< ValueType(const nlohmann::json &)> &&parse_function)