https://mooseframework.inl.gov
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 <string>
13 #include <map>
14 #include <type_traits>
15 #include <typeindex>
16 
17 #include "MooseError.h"
18 #include "MooseUtils.h"
19 
20 #include "minijson/minijson.h"
21 
22 class WebServerControl;
23 
24 namespace Moose
25 {
33 {
34 public:
39 
43  class ValueBase
44  {
45  public:
51  ValueBase(const std::string & name, const std::string & type) : _name(name), _type(type) {}
52  virtual ~ValueBase() {}
53 
57  const std::string & name() const { return _name; }
61  const std::string & type() const { return _type; }
62 
69  virtual void setControllableValue(WebServerControl & control) = 0;
70 
74  struct Exception : public std::exception
75  {
76  public:
77  Exception(const std::string & message) : _message(message) {}
78  virtual const char * what() const noexcept override final { return _message.c_str(); }
79 
80  private:
81  const std::string _message;
82  };
83 
84  private:
86  const std::string _name;
88  const std::string _type;
89  };
90 
94  template <typename DerivedValueType>
95  static char add(const std::string & type_name)
96  {
97  static_assert(std::is_base_of_v<ValueBase, DerivedValueType>, "Is not derived from ValueBase");
98  using value_type = typename DerivedValueType::value_type;
99  static const std::type_index index = typeid(value_type);
100  if (!getRegistry()
101  ._name_map.emplace(type_name, std::make_unique<Type<DerivedValueType>>(type_name))
102  .second)
103  ::mooseError("WebServerControlTypeRegistry: The string type \"",
104  type_name,
105  "\" is already registered.");
106  if (!getRegistry()._value_types.insert(index).second)
107  ::mooseError("WebServerControlRegistry: The type \"",
108  MooseUtils::prettyCppType<value_type>(),
109  "\" is already registered");
110  return 0;
111  }
112 
116  static bool isRegistered(const std::string & type) { return getRegistry()._name_map.count(type); }
117 
121  static std::unique_ptr<ValueBase> build(const std::string & type, const std::string & name)
122  {
123  return get(type).build(name);
124  }
130  static std::unique_ptr<ValueBase>
131  build(const std::string & type, const std::string & name, const miniJson::Json & json_value)
132  {
133  return get(type).build(name, json_value);
134  }
135 
136 private:
140  class TypeBase
141  {
142  public:
143  TypeBase(const std::string & type) : _type(type) {}
144  virtual ~TypeBase() {}
145 
149  const std::string & type() const { return _type; }
156  virtual std::unique_ptr<ValueBase> build(const std::string & name,
157  const miniJson::Json & json_value) const = 0;
163  virtual std::unique_ptr<ValueBase> build(const std::string & name) const = 0;
164 
165  private:
167  const std::string _type;
168  };
169 
170  template <class DerivedValueType>
171  struct Type : public TypeBase
172  {
173  Type(const std::string & type) : TypeBase(type) {}
174 
175  using value_type = typename DerivedValueType::value_type;
176 
177  virtual std::unique_ptr<ValueBase> build(const std::string & name) const override final
178  {
179  return std::make_unique<DerivedValueType>(name, type());
180  }
181  virtual std::unique_ptr<ValueBase> build(const std::string & name,
182  const miniJson::Json & json_value) const override final
183  {
184  return std::make_unique<DerivedValueType>(name, type(), json_value);
185  }
186  };
187 
191  static const TypeBase & get(const std::string & type)
192  {
193  auto & registry = getRegistry();
194  const auto it = registry._name_map.find(type);
195  if (it == registry._name_map.end())
196  mooseError("WebServerControlTypeRegistry: The type '", type, "' is not registered");
197  return *it->second;
198  }
199 
201  std::map<std::string, std::unique_ptr<TypeBase>> _name_map;
204  std::set<std::type_index> _value_types;
205 };
206 }
std::string name(const ElemQuality q)
static bool isRegistered(const std::string &type)
static WebServerControlTypeRegistry & getRegistry()
Starts a webserver that an external process can connect to in order to send JSON messages to control ...
std::map< std::string, std::unique_ptr< TypeBase > > _name_map
The registration data.
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:333
virtual void setControllableValue(WebServerControl &control)=0
Sets the controllable value given the name and type via the controllable interface in control...
typename DerivedValueType::value_type value_type
The base class for a value that is produced by this registry.
Common exception for parsing related errors in converting JSON to a value.
const std::string _name
The name that the value is for.
std::set< std::type_index > _value_types
The registered value types, to avoid registering the same underlying value type multiple times...
A static registry used to register and build values of different types for the WebServerControl.
virtual std::unique_ptr< ValueBase > build(const std::string &name, const miniJson::Json &json_value) const override final
Builds a value with the given type, name name, and JSON value json_value.
virtual const char * what() const noexcept override final
ValueBase(const std::string &name, const std::string &type)
Constructor.
static std::unique_ptr< ValueBase > build(const std::string &type, const std::string &name, const miniJson::Json &json_value)
Builds a value with the type type, name name, and a value parsed from json_value. ...
virtual std::unique_ptr< ValueBase > 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< ValueBase > build(const std::string &name, const miniJson::Json &json_value) const =0
Builds a value with the given type, name name, and JSON value json_value.
Base registry class for a type that is used to build values.
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...
const std::string _type
The string representation of the type.
static std::unique_ptr< ValueBase > build(const std::string &type, const std::string &name)
Builds a value with the type type, name name, and a default value.
const std::string _type
The string representation of the underlying type.
static char add(const std::string &type_name)
Registers a type with string name type_name and the given derived type.