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;
59 virtual std::expected<Parameter*, std::string> value_from_generic(
60 const glz::generic_u64& glz_value) = 0;
63 bool has_value_ =
false;
68class ParameterTyped :
public Parameter {
70 ~ParameterTyped()
override =
default;
71 BOOST_TYPE_INDEX_REGISTER_RUNTIME_CLASS(Parameter);
73 using Validator = std::function<bool(
const T&)>;
75 ParameterTyped() =
default;
77 explicit ParameterTyped(
const T& value) : value_(value) { has_value_ =
true; }
79 explicit ParameterTyped(
const T&& value) : value_(value) {
83 ParameterTyped& validator(Validator validator) {
84 validator_ = std::move(validator);
88 ParameterTyped& value(
const T& value) {
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));
104 [[nodiscard]] T value()
const {
return value_; }
106 [[nodiscard]]
bool validate()
const override {
110 if (validator_ ==
nullptr) {
113 return validator_(value_);
116 [[nodiscard]] std::unique_ptr<Parameter> clone()
const override {
117 return std::make_unique<ParameterTyped>(value_);
121 Validator validator_{};