$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 #ifndef FABLE_SCHEMA_PASSTHRU_HPP_
26 #define FABLE_SCHEMA_PASSTHRU_HPP_
27 
28 #include <string> // for string
29 #include <utility> // for move
30 
31 #include <boost/optional.hpp> // for optional<>
32 
33 #include <fable/json/with_boost.hpp> // for to_json, from_json
34 #include <fable/schema/ignore.hpp> // for Ignore
35 #include <fable/schema/interface.hpp> // for Base<>, Box
36 
37 namespace fable {
38 namespace schema {
39 
47 template <typename P>
48 class Passthru : public Base<Passthru<P>> {
49  public: // Types and Constructors
50  using Type = Conf;
51  using PrototypeSchema = P;
52 
53  Passthru(Type* ptr, std::string&& desc)
54  : Passthru(ptr, PrototypeSchema(nullptr, ""), std::move(desc)) {}
55  Passthru(Type* ptr, const PrototypeSchema& prototype, std::string&& desc)
56  : Base<Passthru<P>>(prototype.type(), std::move(desc)), prototype_(prototype), ptr_(ptr) {
57  prototype_.reset_ptr();
58  }
59 
60  public: // Overrides
61  std::string type_string() const override { return prototype_.type_string(); }
62 
63  Json json_schema() const override {
64  Json j = prototype_.json_schema();
65  this->augment_schema(j);
66  return j;
67  }
68 
69  void validate(const Conf& c) const override {
70  // Let the prototype do all the validation, since Passthru doesn't have
71  // enough information to do a correct validation.
72  prototype_.validate(c);
73  }
74 
75  using Interface::to_json;
76  void to_json(Json& j) const override {
77  assert(ptr_ != nullptr);
78  j = **ptr_;
79  }
80 
81  void from_conf(const Conf& c) override {
82  assert(ptr_ != nullptr);
83  *ptr_ = deserialize(c);
84  }
85 
86  Json serialize(const Type& x) const { return *x; }
87 
88  Type deserialize(const Conf& c) const { return 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 Passthru<Ignore>(ptr, Ignore(), std::move(desc));
99 }
100 
101 template <typename P>
102 Passthru<P> make_schema(Conf* ptr, const P& prototype, std::string&& desc) {
103  return Passthru<P>(ptr, prototype, std::move(desc));
104 }
105 
106 } // namespace schema
107 } // namespace fable
108 
109 #endif // FABLE_SCHEMA_PASSTHRU_HPP_
Definition: conf.hpp:74
void to_json(Json &j) const override
Definition: passthru.hpp:76
Definition: ignore.hpp:46
std::string type_string() const override
Definition: passthru.hpp:61
void validate(const Conf &c) const override
Definition: passthru.hpp:69
nlohmann::json Json
Definition: json.hpp:62
Definition: conf.hpp:70
Definition: passthru.hpp:48
virtual Json to_json() const
Definition: interface.hpp:220
void from_conf(const Conf &c) override
Definition: passthru.hpp:81
void reset_ptr() override
Definition: passthru.hpp:90
Json json_schema() const override
Definition: passthru.hpp:63
Definition: interface.hpp:354