$darkmode
confable.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  */
25 #pragma once
26 
27 #include <string> // for string
28 #include <utility> // for move
29 
30 #include <fable/fable_fwd.hpp> // for Confable
31 #include <fable/schema/interface.hpp> // for Interface
32 
33 namespace fable::schema {
34 
35 template <typename T, std::enable_if_t<std::is_base_of_v<Confable, T>, int> = 0>
36 class FromConfable : public Base<FromConfable<T>> {
37  public: // Types and Constructors
38  using Type = T;
39 
40  template <std::enable_if_t<std::is_default_constructible_v<T>, int> = 0>
41  FromConfable(std::string desc = "")
42  : Base<FromConfable<T>>(std::move(desc)), schema_(Type().schema()) {
43  schema_.reset_ptr(); // type was temporary
44  this->type_ = schema_.type();
45  }
46 
47  FromConfable(Type* ptr, std::string desc)
48  : Base<FromConfable<Type>>(ptr->schema().type(), std::move(desc))
49  , schema_(ptr->schema())
50  , ptr_(ptr) {
51  assert(ptr != nullptr);
52  }
53 
54  public: // Special
55  [[nodiscard]] Box get_confable_schema() const { return schema_.clone(); }
56 
57  public: // Overrides
58  [[nodiscard]] Json json_schema() const override {
59  Json j = schema_.json_schema();
60  this->augment_schema(j);
61  return j;
62  }
63 
64  bool validate(const Conf& c, std::optional<SchemaError>& err) const override {
65  if (ptr_ == nullptr) {
66  return schema_.validate(c, err);
67  }
68  return ptr_->validate(c, err);
69  }
70 
71  using Interface::to_json;
72  void to_json(Json& j) const override {
73  assert(ptr_ != nullptr);
74  ptr_->to_json(j);
75  }
76 
77  void from_conf(const Conf& c) override {
78  assert(ptr_ != nullptr);
79  ptr_->from_conf(c);
80  }
81 
82  [[nodiscard]] Json serialize(const Type& x) const { return x.to_json(); }
83 
84  [[nodiscard]] Type deserialize(const Conf& c) const {
85  Type tmp;
86  tmp.from_conf(c);
87  return tmp;
88  }
89 
90  void serialize_into(Json& j, const Type& x) const { x.to_json(j); }
91 
92  void deserialize_into(const Conf& c, Type& x) const { x.from_conf(c); }
93 
94  void reset_ptr() override {
95  ptr_ = nullptr;
96  schema_.reset_ptr();
97  }
98 
99  private:
100  Box schema_;
101  Type* ptr_{nullptr};
102 };
103 
104 template <
105  typename T, typename S,
106  std::enable_if_t<std::is_base_of_v<Confable, T> && std::is_default_constructible_v<T>, int> = 0>
107 FromConfable<T> make_schema(T* ptr, S&& desc) {
108  if (ptr == nullptr) {
109  return {std::forward<S>(desc)};
110  }
111  return {ptr, std::forward<S>(desc)};
112 }
113 
114 template <typename T, typename S,
115  std::enable_if_t<std::is_base_of_v<Confable, T> && !std::is_default_constructible_v<T>,
116  int> = 0>
117 FromConfable<T> make_schema(T* ptr, S&& desc) {
118  if (ptr == nullptr) {
119  // Since T is a struct with a user-defined schema, we need an instance in order
120  // to extract its schema, and T is not default constructible.
121  throw std::invalid_argument("make_schema(FromConfable*, S&&) requires a valid pointer");
122  }
123  return {ptr, std::forward<S>(desc)};
124 }
125 
126 } // namespace fable::schema
Definition: conf.hpp:82
Definition: interface.hpp:398
Definition: interface.hpp:297
Json json_schema() const override
Definition: interface.hpp:375
void reset_ptr() override
Definition: interface.hpp:381
std::unique_ptr< Interface > clone() const override
Definition: interface.hpp:368
JsonType type() const override
Definition: interface.hpp:369
bool validate(const Conf &c, std::optional< SchemaError > &err) const override
Definition: interface.hpp:376
Definition: confable.hpp:36
bool validate(const Conf &c, std::optional< SchemaError > &err) const override
Definition: confable.hpp:64
void to_json(Json &j) const override
Definition: confable.hpp:72
Json json_schema() const override
Definition: confable.hpp:58
void from_conf(const Conf &c) override
Definition: confable.hpp:77
void reset_ptr() override
Definition: confable.hpp:94
virtual Json to_json() const
Definition: interface.hpp:254
nlohmann::json Json
Definition: fable_fwd.hpp:35