$darkmode
struct.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_STRUCT_HPP_
26 #define FABLE_SCHEMA_STRUCT_HPP_
27 
28 #include <map> // for map<>
29 #include <memory> // for shared_ptr<>
30 #include <string> // for string
31 #include <type_traits> // for enable_if_t<>, is_same<>
32 #include <utility> // for move, forward<>, pair<>
33 #include <vector> // for vector<>
34 
35 #include <fable/schema/interface.hpp> // for Base<>
36 
37 namespace fable {
38 namespace schema {
39 
56 template <typename S = Box>
57 using PropertyList = std::initializer_list<std::pair<std::string const, S>>;
58 
59 template <typename T, typename S = Box>
60 using enable_if_property_list_t = std::enable_if_t<std::is_same<PropertyList<S>, T>::value>;
61 
73 class Struct : public Base<Struct> {
74  public: // Constructors
75  explicit Struct(std::string&& desc = "") : Base(JsonType::object, std::move(desc)) {}
76 
77  Struct(std::string&& desc, PropertyList<Box> props) : Base(JsonType::object, std::move(desc)) {
78  set_properties(props);
79  }
80 
81  Struct(PropertyList<Box> props) : Struct("", props) {} // NOLINT
82 
83  // Inheriting constructors
108  Struct(std::string&& desc, const Box& base, PropertyList<Box> props)
109  : Struct(*base.template as<Struct>()) {
110  desc_ = std::move(desc);
111  set_properties(props);
112  }
113 
114  Struct(const Box& base, PropertyList<Box> props) : Struct("", base, props) {}
115 
116  public: // Special
122  void set_property(const std::string& key, Box&& s);
123 
124  Struct property(const std::string& key, Box&& s) && {
125  set_property(key, std::move(s));
126  return std::move(*this);
127  }
128 
134  void set_properties(PropertyList<Box> props);
135 
141  void set_properties_from(const Struct& s) { set_properties(s.properties_); }
142 
143  void set_properties_from(const Box& s) { set_properties_from(*s.template as<Struct>()); }
144 
145  template <typename T, typename = enable_if_confable_t<T>>
146  void set_properties_from(const T* x) {
147  set_properties_from(x->schema());
148  }
149 
150  template <typename T>
151  Struct properties_from(const T x) {
153  return std::move(*this);
154  }
155 
162  void set_require(std::initializer_list<std::string> init);
163  Struct require(std::initializer_list<std::string> init) &&;
164 
173  void set_require_all();
174  Struct require_all() &&;
175 
183  additional_properties_ = v;
184  return std::move(*this);
185  }
186 
187  template <typename S, typename = enable_if_schema_t<S>>
188  void set_additional_properties(const S& s) {
189  additional_properties_ = true;
190  additional_prototype_.reset(s.clone());
191  additional_prototype_->reset_ptr();
192  }
193 
194  template <typename S, typename = enable_if_schema_t<S>>
195  Struct additional_properties(const S& s) && {
196  set_additional_properties(s);
197  return std::move(*this);
198  }
199 
200  bool additional_properties() const { return additional_properties_; }
201 
202  void reset_ptr() override;
203 
204  public: // Overrides
205  using Interface::to_json;
206  Json usage() const override;
207  Json json_schema() const override;
208  void validate(const Conf& c) const override;
209  void to_json(Json& j) const override;
210  void from_conf(const Conf& c) override;
211 
212  private:
213  void set_properties(const std::map<std::string, Box>& props);
214 
215  private:
216  std::map<std::string, Box> properties_{};
217  std::vector<std::string> properties_required_{};
218  std::shared_ptr<Interface> additional_prototype_{};
219  bool additional_properties_{false};
220 };
221 
222 template <typename T, typename = enable_if_property_list_t<T>>
223 inline Struct make_schema(T&& props) {
224  return Struct(std::forward<T>(props));
225 }
226 
227 template <typename T, typename = enable_if_property_list_t<T>>
228 inline Struct make_schema(std::string&& desc, T&& props) {
229  return Struct(std::move(desc), std::forward<T>(props));
230 }
231 
232 template <typename T, typename = enable_if_property_list_t<T>>
233 inline Struct make_schema(std::string&& desc, const Box& base, T&& props) {
234  return Struct(std::move(desc), base, std::forward<T>(props));
235 }
236 
237 template <typename T, typename = enable_if_property_list_t<T>>
238 inline Struct make_schema(std::string&& desc, const Struct& base, T&& props) {
239  return Struct(std::move(desc), base, std::forward<T>(props));
240 }
241 
242 } // namespace schema
243 } // namespace fable
244 
245 #endif // FABLE_SCHEMA_STRUCT_HPP_
Definition: conf.hpp:74
Struct(std::string &&desc, const Box &base, PropertyList< Box > props)
Definition: struct.hpp:108
void from_conf(const Conf &c) override
Definition: struct.cpp:137
void reset_ptr() override
Definition: struct.cpp:172
Definition: interface.hpp:263
Struct additional_properties(bool v) &&
Definition: struct.hpp:182
nlohmann::json Json
Definition: json.hpp:62
Definition: conf.hpp:70
void set_require(std::initializer_list< std::string > init)
Definition: struct.cpp:58
Json usage() const override
Definition: struct.cpp:84
void set_properties_from(const Struct &s)
Definition: struct.hpp:141
virtual Json to_json() const
Definition: interface.hpp:220
void validate(const Conf &c) const override
Definition: struct.cpp:112
Definition: struct.hpp:73
Definition: interface.hpp:354
void set_properties(PropertyList< Box > props)
Definition: struct.cpp:46
void set_require_all()
Definition: struct.cpp:72
void set_property(const std::string &key, Box &&s)
Definition: struct.cpp:32
Json json_schema() const override
Definition: struct.cpp:92