$darkmode
optional.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_OPTIONAL_HPP_
27 #define FABLE_SCHEMA_OPTIONAL_HPP_
28 
29 #include <string> // for string
30 #include <utility> // for move
31 
32 #include <boost/optional.hpp> // for optional<>
33 
34 #include <fable/json/with_boost.hpp> // for to_json, from_json
35 #include <fable/schema/interface.hpp> // for Base<>, Box
36 
37 namespace fable {
38 namespace schema {
39 
40 template <typename T, typename P>
41 class Optional : public Base<Optional<T, P>> {
42  public: // Types and Constructors
43  using Type = boost::optional<T>;
44  using PrototypeSchema = P;
45 
46  Optional(Type* ptr, std::string&& desc);
47  Optional(Type* ptr, const PrototypeSchema& prototype, std::string&& desc)
48  : Base<Optional<T, P>>(prototype.type(), std::move(desc)), prototype_(prototype), ptr_(ptr) {
49  prototype_.reset_ptr();
50  }
51 
52 #if 0
53  // This is defined in: fable/schema/magic.hpp
54  Optional(Type* ptr, std::string&& desc)
55  : Optional(ptr, make_prototype<T>(), std::move(desc)) {}
56 #endif
57 
58  public: // Overrides
59  std::string type_string() const override { return prototype_.type_string() + "?"; }
60  bool is_variant() const override { return true; }
61 
62  Json json_schema() const override {
63  Json j{{
64  "oneOf",
65  {
66  Json{
67  {"type", "null"},
68  },
69  prototype_.json_schema(),
70  },
71  }};
72  this->augment_schema(j);
73  return j;
74  }
75 
76  void validate(const Conf& c) const override {
77  if (c->type() == JsonType::null) {
78  return;
79  }
80  this->validate_type(c);
81  prototype_.validate(c);
82  }
83 
84  using Interface::to_json;
85  void to_json(Json& j) const override {
86  assert(ptr_ != nullptr);
87  j = serialize(*ptr_);
88  }
89 
90  void from_conf(const Conf& c) override {
91  assert(ptr_ != nullptr);
92  *ptr_ = deserialize(c);
93  }
94 
95  Json serialize(const Type& x) const {
96  if (x) {
97  return prototype_.serialize(x.get());
98  } else {
99  return nullptr;
100  }
101  }
102 
103  Type deserialize(const Conf& c) const {
104  if (c->type() == JsonType::null) {
105  return boost::none;
106  }
107  return prototype_.deserialize(c);
108  }
109 
110  void reset_ptr() override { ptr_ = nullptr; }
111 
112  private:
113  PrototypeSchema prototype_;
114  Type* ptr_{nullptr};
115 };
116 
117 template <typename T, typename P>
118 Optional<T, P> make_schema(boost::optional<T>* ptr, const P& prototype, std::string&& desc) {
119  return Optional<T, P>(ptr, prototype, std::move(desc));
120 }
121 
122 } // namespace schema
123 } // namespace fable
124 
125 #endif // FABLE_SCHEMA_OPTIONAL_HPP_
Definition: conf.hpp:74
Definition: optional.hpp:41
std::string type_string() const override
Definition: optional.hpp:59
bool is_variant() const override
Definition: optional.hpp:60
void reset_ptr() override
Definition: optional.hpp:110
nlohmann::json Json
Definition: json.hpp:62
void from_conf(const Conf &c) override
Definition: optional.hpp:90
Definition: conf.hpp:70
virtual Json to_json() const
Definition: interface.hpp:220
void validate(const Conf &c) const override
Definition: optional.hpp:76
Json json_schema() const override
Definition: optional.hpp:62
Definition: interface.hpp:354
void to_json(Json &j) const override
Definition: optional.hpp:85