15#ifndef SIMO_PARAMETER_HH
16#define SIMO_PARAMETER_HH
18#include <Simo/compiler/BoostTypeIndexRuntimeCast.h>
22#include <glaze/json/generic.hpp>
26#include "Simo/compiler/Compiler.h"
28namespace Simo::Parameter {
33 Parameter() =
default;
35 virtual ~Parameter() =
default;
37 BOOST_TYPE_INDEX_REGISTER_RUNTIME_CLASS()
39 [[nodiscard]] boost::typeindex::type_index type()
const {
40 return boost::typeindex::type_id_runtime(*
this);
43 [[nodiscard]]
virtual bool validate()
const = 0;
45 [[nodiscard]]
virtual std::unique_ptr<Parameter> clone()
const = 0;
48 bool has_value()
const {
52 template <
typename Self>
53 Self& has_value(
this Self& self,
const bool new_val) {
54 self.has_value_ = new_val;
61 const glz::generic_u64& glz_value) = 0;
69 bool has_value_ =
false;
74class ParameterTyped :
public Parameter {
76 ~ParameterTyped()
override =
default;
77 BOOST_TYPE_INDEX_REGISTER_RUNTIME_CLASS(Parameter);
79 using Validator = std::function<bool(
const T&)>;
81 ParameterTyped() =
default;
83 explicit ParameterTyped(
const T& value) : value_(value) { has_value_ =
true; }
85 explicit ParameterTyped(
const T&& value) : value_(value) {
89 ParameterTyped& validator(Validator validator) {
90 validator_ = std::move(validator);
94 ParameterTyped& value(
const T& value) {
102 const glz::generic_u64& glz_value)
override {
103 if (
auto ec = glz::read_json(value_, glz_value)) {
104 return std::unexpected(glz::format_error(ec));
113 return glz::generic_u64(
nullptr);
115 glz::generic_u64 out;
120 [[nodiscard]] T value()
const {
return value_; }
122 [[nodiscard]]
bool validate()
const override {
126 if (validator_ ==
nullptr) {
129 return validator_(value_);
132 [[nodiscard]] std::unique_ptr<Parameter> clone()
const override {
133 return std::make_unique<ParameterTyped>(value_);
137 Validator validator_{};
std::expected< Parameter *, std::string > value_from_generic(const glz::generic_u64 &glz_value) override
Set stored value from glaze generic representation.
Definition Parameter.h:101
std::expected< glz::generic_u64, std::string > value_to_generic() const override
Produce glaze generic representation of the value.
Definition Parameter.h:110
virtual std::expected< Parameter *, std::string > value_from_generic(const glz::generic_u64 &glz_value)=0
Set stored value from glaze generic representation.
virtual std::expected< glz::generic_u64, std::string > value_to_generic() const =0
Produce glaze generic representation of the value.