$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 #ifndef FABLE_SCHEMA_CUSTOM_HPP_
26 #define FABLE_SCHEMA_CUSTOM_HPP_
27 
28 #include <functional> // for function<>
29 
30 #include <fable/schema/interface.hpp> // for Interface, Box
31 
32 namespace fable {
33 namespace schema {
34 
44  public: // Constructors
45  CustomDeserializer(Box&& s) : impl_(std::move(s).reset_pointer().get()) {}
46  CustomDeserializer(Box&& s, std::function<void(CustomDeserializer*, const Conf&)> f)
47  : impl_(std::move(s).reset_pointer().get()), from_conf_fn_(f) {}
48 
49  public: // Special
50  operator Box() const { return Box{this->clone()}; }
51 
52  void set_from_conf(std::function<void(CustomDeserializer*, const Conf&)> f) { from_conf_fn_ = f; }
53 
54  CustomDeserializer with_from_conf(std::function<void(CustomDeserializer*, const Conf&)> f) && {
55  set_from_conf(f);
56  return std::move(*this);
57  }
58 
59  public: // Overrides
60  Interface* clone() const override { return new CustomDeserializer(*this); }
61 
62  using Interface::to_json;
63  JsonType type() const override { return impl_->type(); }
64  std::string type_string() const override { return impl_->type_string(); }
65  bool is_required() const override { return impl_->is_required(); }
66  const std::string& description() const override { return impl_->description(); }
67  void set_description(const std::string& s) override { return impl_->set_description(s); }
68  Json usage() const override { return impl_->usage(); }
69  Json json_schema() const override { return impl_->json_schema(); };
70  void validate(const Conf& c) const override { impl_->validate(c); }
71  void to_json(Json& j) const override { impl_->to_json(j); }
72 
73  void from_conf(const Conf& c) override {
74  if (!from_conf_fn_) {
75  throw Error("no deserializer configured");
76  }
77  from_conf_fn_(this, c);
78  }
79 
80  void reset_ptr() override {
81  impl_->reset_ptr();
82  from_conf_fn_ = [](CustomDeserializer*, const Conf&) {
83  throw Error("cannot deserialize after reset_ptr is called");
84  };
85  }
86 
87  friend void to_json(Json& j, const CustomDeserializer& b) { b.impl_->to_json(j); }
88 
89  private:
90  std::shared_ptr<schema::Interface> impl_{nullptr};
91  std::function<void(CustomDeserializer*, const Conf&)> from_conf_fn_{};
92 };
93 
94 } // namespace schema
95 } // namespace fable
96 
97 #endif // FABLE_SCHEMA_CUSTOM_HPP_
Definition: conf.hpp:74
bool is_required() const override
Definition: custom.hpp:65
Json usage() const override
Definition: custom.hpp:68
void set_description(const std::string &s) override
Definition: custom.hpp:67
void from_conf(const Conf &c) override
Definition: custom.hpp:73
JsonType type() const override
Definition: custom.hpp:63
Definition: interface.hpp:263
Interface * clone() const override
Definition: custom.hpp:60
void to_json(Json &j) const override
Definition: custom.hpp:71
void validate(const Conf &c) const override
Definition: custom.hpp:70
nlohmann::json Json
Definition: json.hpp:62
Json::value_t JsonType
Definition: json.hpp:78
Definition: conf.hpp:70
Definition: interface.hpp:72
void reset_ptr() override
Definition: custom.hpp:80
Definition: custom.hpp:43
virtual Json to_json() const
Definition: interface.hpp:220
Definition: error.hpp:40
Json json_schema() const override
Definition: custom.hpp:69
const std::string & description() const override
Definition: custom.hpp:66
std::string type_string() const override
Definition: custom.hpp:64