$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 = 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))
51  , prototype_(std::move(prototype))
52  , ptr_(ptr) {
53  prototype_.reset_ptr();
54  }
55 
56  public: // Overrides
57  [[nodiscard]] std::string type_string() const override { return prototype_.type_string(); }
58 
59  [[nodiscard]] Json json_schema() const override {
60  Json j = prototype_.json_schema();
61  this->augment_schema(j);
62  return j;
63  }
64 
65  bool validate(const Conf& c, std::optional<SchemaError>& err) const override {
66  // Let the prototype do all the validation, since Passthru doesn't have
67  // enough information to do a correct validation.
68  return prototype_.validate(c, err);
69  }
70 
71  using Interface::to_json;
72  void to_json(Json& j) const override {
73  assert(ptr_ != nullptr);
74  j = **ptr_;
75  }
76 
77  void from_conf(const Conf& c) override {
78  assert(ptr_ != nullptr);
79  *ptr_ = deserialize(c);
80  }
81 
82  [[nodiscard]] Json serialize(const Type& x) const { return *x; }
83 
84  [[nodiscard]] Type deserialize(const Conf& c) const { return c; }
85 
86  void serialize_into(Json& j, const Type& x) const { j = serialize(x); }
87 
88  void deserialize_into(const Conf& c, Type& x) const { x = deserialize(c); }
89 
90  void reset_ptr() override { ptr_ = nullptr; }
91 
92  private:
93  PrototypeSchema prototype_;
94  Type* ptr_{nullptr};
95 };
96 
97 inline Passthru<Ignore> make_schema(Conf* ptr, std::string desc) {
98  return {ptr, Ignore(), std::move(desc)};
99 }
100 
101 template <typename P>
102 Passthru<P> make_schema(Conf* ptr, P prototype, std::string desc) {
103  return {ptr, std::move(prototype), std::move(desc)};
104 }
105 
106 } // namespace fable::schema
Definition: conf.hpp:76
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:57
void from_conf(const Conf &c) override
Definition: passthru.hpp:77
void to_json(Json &j) const override
Definition: passthru.hpp:72
Json json_schema() const override
Definition: passthru.hpp:59
bool validate(const Conf &c, std::optional< SchemaError > &err) const override
Definition: passthru.hpp:65
void reset_ptr() override
Definition: passthru.hpp:90
nlohmann::json Json
Definition: fable_fwd.hpp:35