$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 #ifndef FABLE_SCHEMA_CONFABLE_HPP_
27 #define FABLE_SCHEMA_CONFABLE_HPP_
28 
29 #include <memory> // for shared_ptr<>
30 #include <string> // for string
31 #include <utility> // for move
32 
33 #include <fable/schema/interface.hpp> // for Interface
34 
35 namespace fable {
36 
37 // Forward declarations:
38 class Confable;
39 
40 namespace schema {
41 
42 template <typename T, std::enable_if_t<std::is_base_of<Confable, T>::value, int> = 0>
43 class FromConfable : public Base<FromConfable<T>> {
44  public: // Types and Constructors
45  using Type = T;
46 
47  explicit FromConfable(std::string&& desc = "") {
48  schema_ = Type().schema();
49  schema_.reset_ptr();
50  this->type_ = schema_.type();
51  this->desc_ = std::move(desc);
52  }
53 
54  FromConfable(Type* ptr, std::string&& desc)
55  : Base<FromConfable<Type>>(ptr->schema().type(), std::move(desc))
56  , schema_(ptr->schema())
57  , ptr_(ptr) {
58  assert(ptr != nullptr);
59  }
60 
61  public: // Special
62  Box get_confable_schema() const { return schema_.clone(); }
63 
64  public: // Overrides
65  Json json_schema() const override {
66  Json j = schema_.json_schema();
67  this->augment_schema(j);
68  return j;
69  }
70 
71  void validate(const Conf& c) const override {
72  if (ptr_ == nullptr) {
73  schema_.validate(c);
74  } else {
75  ptr_->validate(c);
76  }
77  }
78 
79  using Interface::to_json;
80  void to_json(Json& j) const override {
81  assert(ptr_ != nullptr);
82  ptr_->to_json(j);
83  }
84 
85  void from_conf(const Conf& c) override {
86  assert(ptr_ != nullptr);
87  ptr_->from_conf(c);
88  }
89 
90  Json serialize(const Type& x) const { return x.to_json(); }
91 
92  Type deserialize(const Conf& c) const {
93  Type tmp;
94  tmp.from_conf(c);
95  return tmp;
96  }
97 
98  void reset_ptr() override { ptr_ = nullptr; }
99 
100  private:
101  Box schema_;
102  Type* ptr_{nullptr};
103 };
104 
105 template <typename T>
106 FromConfable<T> make_schema(T* ptr, std::string&& desc) {
107  assert(ptr != nullptr);
108  return FromConfable<T>(ptr, std::move(desc));
109 }
110 
111 } // namespace schema
112 } // namespace fable
113 
114 #endif // FABLE_SCHEMA_CONFABLE_HPP_
Definition: conf.hpp:74
void validate(const Conf &c) const override
Definition: confable.hpp:71
Definition: interface.hpp:263
Json json_schema() const override
Definition: confable.hpp:65
nlohmann::json Json
Definition: json.hpp:62
void reset_ptr() override
Definition: interface.hpp:343
void reset_ptr() override
Definition: confable.hpp:98
Definition: conf.hpp:70
void from_conf(const Conf &c) override
Definition: confable.hpp:85
void to_json(Json &j) const override
Definition: confable.hpp:80
Definition: confable.hpp:43
virtual Json to_json() const
Definition: interface.hpp:220
Interface * clone() const override
Definition: interface.hpp:332
JsonType type() const override
Definition: interface.hpp:333
void validate(const Conf &c) const override
Definition: interface.hpp:340
Definition: interface.hpp:354
Json json_schema() const override
Definition: interface.hpp:339