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
58 [[nodiscard]]
59 virtual std::expected<Parameter*, std::string> value_from_generic(
60 const glz::generic_u64& glz_value) = 0;
61
62 protected:
63 bool has_value_ = false;
64};
65
67template <typename T>
68class ParameterTyped : public Parameter {
69 public:
70 ~ParameterTyped() override = default;
71 BOOST_TYPE_INDEX_REGISTER_RUNTIME_CLASS(Parameter);
72
73 using Validator = std::function<bool(const T&)>;
74
75 ParameterTyped() = default;
76
77 explicit ParameterTyped(const T& value) : value_(value) { has_value_ = true; }
78
79 explicit ParameterTyped(const T&& value) : value_(value) {
80 has_value_ = true;
81 }
82
83 ParameterTyped& validator(Validator validator) {
84 validator_ = std::move(validator);
85 return *this;
86 }
87
88 ParameterTyped& value(const T& value) {
89 value_ = value;
90 has_value_ = true;
91 return *this;
92 }
93
94 [[nodiscard]]
95 std::expected<Parameter*, std::string> value_from_generic(
96 const glz::generic_u64& glz_value) override {
97 if (auto ec = glz::read_json(value_, glz_value)) {
98 return std::unexpected(glz::format_error(ec));
99 }
100 has_value_ = true;
101 return this;
102 }
103
104 [[nodiscard]] T value() const { return value_; }
105
106 [[nodiscard]] bool validate() const override {
107 if (!has_value_) {
108 return false;
109 }
110 if (validator_ == nullptr) {
111 return true;
112 }
113 return validator_(value_);
114 }
115
116 [[nodiscard]] std::unique_ptr<Parameter> clone() const override {
117 return std::make_unique<ParameterTyped>(value_);
118 }
119
120 protected:
121 Validator validator_{};
122 T value_{};
123};
124} // namespace Simo::Parameter
125#endif // SIMO_PARAMETER_HH