$darkmode
passthru.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020 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 <string> // for string
27 #include <utility> // for move
28 
29 #include <fable/schema/ignore.hpp> // for Ignore
30 #include <fable/schema/interface.hpp> // for Base<>, Box
31 
32 namespace fable::schema {
33 
41 template <typename P>
42 class Passthru : public Base<Passthru<P>> {
43  public: // Types and Constructors
44  using Type = Conf;
45  using PrototypeSchema = std::remove_cv_t<std::remove_reference_t<P>>;
46 
47  Passthru(Type* ptr, std::string desc)
48  : Passthru(ptr, PrototypeSchema(nullptr, ""), std::move(desc)) {}
49  Passthru(Type* ptr, PrototypeSchema prototype, std::string desc)
50  : Base<Passthru<P>>(prototype.type(), std::move(desc)), prototype_(std::move(prototype)), ptr_(ptr) {
51  prototype_.reset_ptr();
52  }
53 
54  public: // Overrides
55  [[nodiscard]] std::string type_string() const override { return prototype_.type_string(); }
56 
57  [[nodiscard]] Json json_schema() const override {
58  Json j = prototype_.json_schema();
59  this->augment_schema(j);
60  return j;
61  }
62 
63  bool validate(const Conf& c, std::optional<SchemaError>& err) const override {
64  // Let the prototype do all the validation, since Passthru doesn't have
65  // enough information to do a correct validation.
66  return prototype_.validate(c, err);
67  }
68 
69  using Interface::to_json;
70  void to_json(Json& j) const override {
71  assert(ptr_ != nullptr);
72  j = **ptr_;
73  }
74 
75  void from_conf(const Conf& c) override {
76  assert(ptr_ != nullptr);
77  *ptr_ = deserialize(c);
78  }
79 
80  [[nodiscard]] Json serialize(const Type& x) const { return *x; }
81 
82  [[nodiscard]] Type deserialize(const Conf& c) const { return c; }
83 
84  void serialize_into(Json& j, const Type& x) const { j = serialize(x); }
85 
86  void deserialize_into(const Conf& c, Type& x) const { x = deserialize(c); }
87 
88  void reset_ptr() override { ptr_ = nullptr; }
89 
90  private:
91  PrototypeSchema prototype_;
92  Type* ptr_{nullptr};
93 };
94 
95 template <typename S>
96 Passthru<Ignore> make_schema(Conf* ptr, S&& desc) {
97  return {ptr, Ignore(), std::forward<S>(desc)};
98 }
99 
100 template <typename P, typename S>
101 Passthru<P> make_schema(Conf* ptr, P&& prototype, S&& desc) {
102  return {ptr, std::forward<P>(prototype), std::forward<S>(desc)};
103 }
104 
105 } // namespace fable::schema
Definition: conf.hpp:82
Definition: interface.hpp:398
virtual Json to_json() const
Definition: interface.hpp:254
Definition: passthru.hpp:42
std::string type_string() const override
Definition: passthru.hpp:55
void from_conf(const Conf &c) override
Definition: passthru.hpp:75
void to_json(Json &j) const override
Definition: passthru.hpp:70
Json json_schema() const override
Definition: passthru.hpp:57
bool validate(const Conf &c, std::optional< SchemaError > &err) const override
Definition: passthru.hpp:63
void reset_ptr() override
Definition: passthru.hpp:88
nlohmann::json Json
Definition: fable_fwd.hpp:35