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 : #pragma once 11 : 12 : #include <functional> 13 : #include <vector> 14 : 15 : #include "nlohmann/json.h" 16 : 17 : #include "WebServerControlTypeRegistry.h" 18 : #include "WebServerControl.h" 19 : 20 : #define registerWebServerControlCombine1(X, Y) X##Y 21 : #define registerWebServerControlCombine(X, Y) registerWebServerControlCombine1(X, Y) 22 : // Register a generic type 23 : #define registerWebServerControlType(T, parse_function) \ 24 : static char registerWebServerControlCombine(wsc_type, __COUNTER__) = \ 25 : Moose::WebServerControlTypeRegistration::registerType<T>(#T, parse_function) 26 : // Register a scalar type 27 : #define registerWebServerControlScalar(T) \ 28 : static char registerWebServerControlCombine(wsc_scalar, __COUNTER__) = \ 29 : Moose::WebServerControlTypeRegistration::registerScalarType<T>(#T) 30 : // Register a vector type 31 : #define registerWebServerControlVector(T) \ 32 : static char registerWebServerControlCombine(wsc_vector, __COUNTER__) = \ 33 : Moose::WebServerControlTypeRegistration::registerVectorType<T>(#T) 34 : 35 : /** 36 : * Defines classes for registering values that can be parsed, 37 : * communicated, and stored in the WebServerControl. 38 : */ 39 : namespace Moose::WebServerControlTypeRegistration 40 : { 41 : 42 : /** 43 : * Register a generic type to be controlled by the WebServerControl. 44 : * 45 : * This should be used through the registerWebServerControlType macro. 46 : * 47 : * @param type_name Name of the type; this is the name that the client 48 : * will associate with the value when it is communicated 49 : * @param parse_function Function that parses the value from a JSON value 50 : */ 51 : template <class T> 52 : char 53 458936 : registerType(const std::string type_name, 54 : std::function<T(const nlohmann::json &)> && parse_function) 55 : { 56 1376808 : return Moose::WebServerControlTypeRegistry().add<WebServerControl::ControlledValue<T>, T>( 57 1376808 : type_name, std::move(parse_function)); 58 : } 59 : 60 : /** 61 : * Register a scalar type to be controlled by the WebServerControl. 62 : * 63 : * This should be used through the registerWebServerControlScalar macro. 64 : * 65 : * Uses nlohmann::json to perform the type conversion. 66 : * 67 : * @param type_name Name of the type; this is the name that the client 68 : * will associate with the value when it is communicated 69 : */ 70 : template <class T> 71 : char 72 229468 : registerScalarType(const std::string & type_name) 73 : { 74 74 : const auto parse_function = [](const nlohmann::json & json_value) -> T 75 74 : { return json_value.get<T>(); }; 76 229468 : return registerType<T>(type_name, std::move(parse_function)); 77 : } 78 : 79 : /** 80 : * Register a vector type to be controlled by the WebServerControl. 81 : * 82 : * This should be used through the registerWebServerControlVector macro. 83 : * 84 : * Uses nlohmann::json to perform the type conversion. 85 : * 86 : * @param type_name Name of the type in the vector; this is the name that 87 : * the client will associate with the value when it is communicated 88 : */ 89 : template <class T> 90 : char 91 172101 : registerVectorType(const std::string & type_name) 92 : { 93 : using vector_T = std::vector<T>; 94 46 : const auto parse_function = [](const nlohmann::json & json_value) -> vector_T 95 46 : { return json_value.get<vector_T>(); }; 96 172101 : return registerType<vector_T>("std::vector<" + type_name + ">", std::move(parse_function)); 97 : } 98 : 99 : }