$darkmode
variant.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_VARIANT_HPP_
27 #define FABLE_SCHEMA_VARIANT_HPP_
28 
29 #include <string> // for string
30 #include <utility> // for move
31 #include <vector> // for vector<>
32 
33 #include <fable/schema/interface.hpp> // for Interface
34 
35 namespace fable {
36 namespace schema {
37 
38 using BoxVec = std::vector<Box>;
39 using BoxList = std::initializer_list<Box>;
40 
59 class Variant : public Interface {
60  public: // Constructors
61  Variant(std::initializer_list<Box> vec) : Variant("", vec) {}
62  Variant(std::string&& desc, std::initializer_list<Box> vec)
63  : Variant(std::move(desc), std::vector<Box>(vec)) {}
64 
65  Variant(std::vector<Box>&& vec) : Variant("", std::move(vec)) {} // NOLINT(runtime/explicit)
66  Variant(std::string&& desc, std::vector<Box>&& vec);
67 
68  public: // Base
69  Interface* clone() const override { return new Variant(*this); }
70  operator Box() const { return Box{this->clone()}; }
71  JsonType type() const override { return type_; }
72  std::string type_string() const override { return type_string_; }
73  bool is_variant() const override { return true; }
74  Json usage() const override;
75 
76  bool is_required() const override { return required_; }
77  Variant require() && {
78  required_ = true;
79  return std::move(*this);
80  }
81  Variant required(bool value) && {
82  required_ = value;
83  return std::move(*this);
84  }
85 
86  bool has_description() const { return !desc_.empty(); }
87  void set_description(const std::string& s) override { desc_ = s; }
88  void set_description(std::string&& s) { desc_ = std::move(s); }
89  const std::string& description() const override { return desc_; }
90  Variant description(std::string&& desc) && {
91  desc_ = std::move(desc);
92  return std::move(*this);
93  }
94 
95  public: // Special
96  Variant unique_match(bool value) && {
97  unique_match_ = value;
98  return std::move(*this);
99  }
100 
101  Variant reset_pointer() && {
102  reset_ptr();
103  return std::move(*this);
104  }
105 
106  public: // Overrides
107  using Interface::to_json;
108  Json json_schema() const override;
109  void validate(const Conf& c) const override { validate_index(c); }
110  void to_json(Json& j) const override { schemas_[0].to_json(j); }
111  void from_conf(const Conf& c) override {
112  auto index = validate_index(c);
113  schemas_[index].from_conf(c);
114  }
115 
116  void reset_ptr() override;
117 
118  private:
119  size_t validate_index(const Conf& c) const;
120 
121  private:
122  std::string desc_{};
123  std::vector<Box> schemas_;
124  bool required_{false};
125  JsonType type_{JsonType::null};
126  std::string type_string_{};
127  bool unique_match_{false};
128 };
129 
130 inline Variant make_schema(std::initializer_list<Box> vec) { return Variant(vec); }
131 inline Variant make_schema(std::string&& desc, std::initializer_list<Box> vec) {
132  return Variant(std::move(desc), std::vector<Box>(vec));
133 }
134 
135 inline Variant make_schema(std::vector<Box>&& vec) { return Variant(std::move(vec)); }
136 inline Variant make_schema(std::string&& desc, std::vector<Box>&& vec) {
137  return Variant(std::move(desc), std::move(vec));
138 }
139 
140 } // namespace schema
141 } // namespace fable
142 
143 #endif // FABLE_SCHEMA_VARIANT_HPP_
Definition: conf.hpp:74
Json usage() const override
Definition: variant.cpp:77
void validate(const Conf &c) const override
Definition: variant.hpp:109
std::string type_string() const override
Definition: variant.hpp:72
Interface * clone() const override
Definition: variant.hpp:69
void reset_ptr() override
Definition: variant.cpp:130
Definition: interface.hpp:263
Json json_schema() const override
Definition: variant.cpp:86
nlohmann::json Json
Definition: json.hpp:62
bool is_variant() const override
Definition: variant.hpp:73
Json::value_t JsonType
Definition: json.hpp:78
Definition: conf.hpp:70
Definition: interface.hpp:72
bool is_required() const override
Definition: variant.hpp:76
const std::string & description() const override
Definition: variant.hpp:89
void to_json(Json &j) const override
Definition: variant.hpp:110
JsonType type() const override
Definition: variant.hpp:71
virtual Json to_json() const
Definition: interface.hpp:220
void from_conf(const Conf &c) override
Definition: variant.hpp:111
Definition: variant.hpp:59
void set_description(const std::string &s) override
Definition: variant.hpp:87