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 
16 #include "MooseError.h"
17 
18 #include "minijson/minijson.h"
19 
20 class WebServerControl;
21 
22 namespace Moose
23 {
31 {
32 public:
37 
41  class ValueBase
42  {
43  public:
49  ValueBase(const std::string & name, const std::string & type) : _name(name), _type(type) {}
50  virtual ~ValueBase() {}
51 
55  const std::string & name() const { return _name; }
59  const std::string & type() const { return _type; }
60 
67  virtual void setControllableValue(WebServerControl & control) = 0;
68 
72  struct Exception : public std::exception
73  {
74  public:
75  Exception(const std::string & message) : _message(message) {}
76  virtual const char * what() const noexcept override final { return _message.c_str(); }
77 
78  private:
79  const std::string _message;
80  };
81 
82  private:
84  const std::string _name;
86  const std::string _type;
87  };
88 
92  template <typename DerivedValueType>
93  static char add(const std::string & type_name)
94  {
95  static_assert(std::is_base_of_v<ValueBase, DerivedValueType>, "Is not derived from ValueBase");
96  getRegistry()._types.emplace(type_name, std::make_unique<Type<DerivedValueType>>(type_name));
97  return 0;
98  }
99 
103  static bool isRegistered(const std::string & type) { return getRegistry()._types.count(type); }
104 
108  static std::unique_ptr<ValueBase> build(const std::string & type, const std::string & name)
109  {
110  return get(type).build(name);
111  }
117  static std::unique_ptr<ValueBase>
118  build(const std::string & type, const std::string & name, const miniJson::Json & json_value)
119  {
120  return get(type).build(name, json_value);
121  }
122 
123 private:
127  class TypeBase
128  {
129  public:
130  TypeBase(const std::string & type) : _type(type) {}
131  virtual ~TypeBase() {}
132 
136  const std::string & type() const { return _type; }
143  virtual std::unique_ptr<ValueBase> build(const std::string & name,
144  const miniJson::Json & json_value) const = 0;
150  virtual std::unique_ptr<ValueBase> build(const std::string & name) const = 0;
151 
152  private:
154  const std::string _type;
155  };
156 
157  template <class DerivedValueType>
158  struct Type : public TypeBase
159  {
160  Type(const std::string & type) : TypeBase(type) {}
161 
162  virtual std::unique_ptr<ValueBase> build(const std::string & name) const override final
163  {
164  return std::make_unique<DerivedValueType>(name, type());
165  }
166  virtual std::unique_ptr<ValueBase> build(const std::string & name,
167  const miniJson::Json & json_value) const override final
168  {
169  return std::make_unique<DerivedValueType>(name, type(), json_value);
170  }
171  };
172 
176  static const TypeBase & get(const std::string & type)
177  {
178  auto & registry = getRegistry();
179  const auto it = registry._types.find(type);
180  if (it == registry._types.end())
181  mooseError("WebServerControlTypeRegistry: The type '", type, "' is not registered");
182  return *it->second;
183  }
184 
186  std::map<std::string, std::unique_ptr<TypeBase>> _types;
187 };
188 }
std::string name(const ElemQuality q)
std::map< std::string, std::unique_ptr< TypeBase > > _types
The registration data.
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 ...
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
virtual void setControllableValue(WebServerControl &control)=0
Sets the controllable value given the name and type via the controllable interface in control...
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.
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.