Simo 0.0.1
Loading...
Searching...
No Matches
Parameter.h
1// Copyright 2026 Matteo Fusi and Contributors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef SIMO_PARAMETER_HH
16#define SIMO_PARAMETER_HH
17
18#include <Simo/compiler/BoostTypeIndexRuntimeCast.h>
19
20#include <algorithm>
21#include <functional>
22#include <glaze/json/generic.hpp>
23#include <string>
24#include <utility>
25
26#include "Simo/compiler/Compiler.h"
27
28namespace Simo::Parameter {
29
31class Parameter {
32 public:
33 Parameter() = default;
34
35 virtual ~Parameter() = default;
36
37 BOOST_TYPE_INDEX_REGISTER_RUNTIME_CLASS()
38
39 [[nodiscard]] boost::typeindex::type_index type() const {
40 return boost::typeindex::type_id_runtime(*this);
41 }
42
43 [[nodiscard]] virtual bool validate() const = 0;
44
45 [[nodiscard]] virtual std::unique_ptr<Parameter> clone() const = 0;
46
47 [[nodiscard]]
48 bool has_value() const {
49 return has_value_;
50 }
51
52 template <typename Self>
53 Self& has_value(this Self& self, const bool new_val) {
54 self.has_value_ = new_val;
55 return self;
56 }
57
59 [[nodiscard]]
60 virtual std::expected<Parameter*, std::string> value_from_generic(
61 const glz::generic_u64& glz_value) = 0;
62
64 [[nodiscard]]
65 virtual std::expected<glz::generic_u64, std::string> value_to_generic()
66 const = 0;
67
68 protected:
69 bool has_value_ = false;
70};
71
73template <typename T>
74class ParameterTyped : public Parameter {
75 public:
76 ~ParameterTyped() override = default;
77 BOOST_TYPE_INDEX_REGISTER_RUNTIME_CLASS(Parameter);
78
79 using Validator = std::function<bool(const T&)>;
80
81 ParameterTyped() = default;
82
83 explicit ParameterTyped(const T& value) : value_(value) { has_value_ = true; }
84
85 explicit ParameterTyped(const T&& value) : value_(value) {
86 has_value_ = true;
87 }
88
89 ParameterTyped& validator(Validator validator) {
90 validator_ = std::move(validator);
91 return *this;
92 }
93
94 ParameterTyped& value(const T& value) {
95 value_ = value;
96 has_value_ = true;
97 return *this;
98 }
99
100 [[nodiscard]]
101 std::expected<Parameter*, std::string> value_from_generic(
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));
105 }
106 has_value_ = true;
107 return this;
108 }
109
110 std::expected<glz::generic_u64, std::string> value_to_generic()
111 const override {
112 if (!has_value()) {
113 return glz::generic_u64(nullptr);
114 }
115 glz::generic_u64 out;
116 out = value_;
117 return out;
118 }
119
120 [[nodiscard]] T value() const { return value_; }
121
122 [[nodiscard]] bool validate() const override {
123 if (!has_value_) {
124 return false;
125 }
126 if (validator_ == nullptr) {
127 return true;
128 }
129 return validator_(value_);
130 }
131
132 [[nodiscard]] std::unique_ptr<Parameter> clone() const override {
133 return std::make_unique<ParameterTyped>(value_);
134 }
135
136 protected:
137 Validator validator_{};
138 T value_{};
139};
140} // namespace Simo::Parameter
141#endif // SIMO_PARAMETER_HH
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.