$darkmode
custom.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2021 Robert Bosch GmbH
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * SPDX-License-Identifier: Apache-2.0
17  */
24 #pragma once
25 
26 #include <functional> // for function<>
27 
28 #include <fable/schema/interface.hpp> // for Interface, Box
29 
30 namespace fable::schema {
31 
41  public: // Constructors
42  CustomDeserializer(Box&& s) : impl_(std::move(s).reset_pointer().get()) {}
43  CustomDeserializer(Box&& s, std::function<void(CustomDeserializer*, const Conf&)> deserialize_fn)
44  : impl_(std::move(s).reset_pointer().get()), from_conf_fn_(std::move(deserialize_fn)) {}
45 
46  public: // Special
47  [[nodiscard]] operator Box() const { return Box{this->clone()}; }
48 
49  void set_from_conf(std::function<void(CustomDeserializer*, const Conf&)> deserialize_fn) {
50  from_conf_fn_ = std::move(deserialize_fn);
51  }
52 
53  [[nodiscard]] CustomDeserializer with_from_conf(
54  std::function<void(CustomDeserializer*, const Conf&)> deserialize_fn) && {
55  set_from_conf(std::move(deserialize_fn));
56  return std::move(*this);
57  }
58 
59  public: // Overrides
60  [[nodiscard]] std::unique_ptr<Interface> clone() const override {
61  return std::make_unique<CustomDeserializer>(*this);
62  }
63 
64  using Interface::to_json;
65  [[nodiscard]] JsonType type() const override { return impl_->type(); }
66  [[nodiscard]] std::string type_string() const override { return impl_->type_string(); }
67  [[nodiscard]] bool is_required() const override { return impl_->is_required(); }
68  [[nodiscard]] const std::string& description() const override { return impl_->description(); }
69  void set_description(std::string s) override { return impl_->set_description(std::move(s)); }
70  [[nodiscard]] Json usage() const override { return impl_->usage(); }
71  [[nodiscard]] Json json_schema() const override { return impl_->json_schema(); };
72  bool validate(const Conf& c, std::optional<SchemaError>& err) const override {
73  return impl_->validate(c, err);
74  }
75  void to_json(Json& j) const override { impl_->to_json(j); }
76 
77  void from_conf(const Conf& c) override {
78  if (!from_conf_fn_) {
79  throw Error("no deserializer configured");
80  }
81  from_conf_fn_(this, c);
82  }
83 
84  void reset_ptr() override {
85  impl_->reset_ptr();
86  from_conf_fn_ = [](CustomDeserializer*, const Conf&) {
87  throw Error("cannot deserialize after reset_ptr is called");
88  };
89  }
90 
91  friend void to_json(Json& j, const CustomDeserializer& b) { b.impl_->to_json(j); }
92 
93  // NOTE: The following methods cannot be implemented because
94  // CustomDeserializer does not have an associated type:
95  //
96  // Json serialize(const Type&) const;
97  // void serialize_into(Json&, const Type&) const;
98  // Type deserialize(const Conf&) const;
99  // void deserialize_into(const Conf&, Type&) const;
100  //
101  // This means that `CustomDeserializer` cannot be used as a prototype schema
102  // directly, for example with `optional`. Use `Confable` instead.
103 
104  private:
105  std::shared_ptr<schema::Interface> impl_{nullptr};
106  std::function<void(CustomDeserializer*, const Conf&)> from_conf_fn_{};
107 };
108 
109 } // namespace fable::schema
Definition: conf.hpp:82
Definition: error.hpp:40
Definition: interface.hpp:297
Definition: custom.hpp:40
const std::string & description() const override
Definition: custom.hpp:68
void from_conf(const Conf &c) override
Definition: custom.hpp:77
virtual Json to_json() const
Definition: interface.hpp:254
void set_description(std::string s) override
Definition: custom.hpp:69
std::string type_string() const override
Definition: custom.hpp:66
bool validate(const Conf &c, std::optional< SchemaError > &err) const override
Definition: custom.hpp:72
Json usage() const override
Definition: custom.hpp:70
void reset_ptr() override
Definition: custom.hpp:84
JsonType type() const override
Definition: custom.hpp:65
std::unique_ptr< Interface > clone() const override
Definition: custom.hpp:60
void to_json(Json &j) const override
Definition: custom.hpp:75
bool is_required() const override
Definition: custom.hpp:67
Json json_schema() const override
Definition: custom.hpp:71
Definition: interface.hpp:67
virtual Json to_json() const
Definition: interface.hpp:254
nlohmann::json Json
Definition: fable_fwd.hpp:35
nlohmann::json::value_t JsonType
Definition: json.hpp:78