$darkmode
const.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_CONST_HPP_
27 #define FABLE_SCHEMA_CONST_HPP_
28 
29 #include <string> // for string
30 #include <utility> // for move
31 
32 #include <fable/schema/interface.hpp> // for Base<>, Box
33 #include <fable/schema/string.hpp> // for String
34 
35 namespace fable {
36 namespace schema {
37 
38 template <typename T, typename P>
39 class Const : public Base<Const<T, P>> {
40  public: // Types and Constructors
41  using Type = T;
42  using PrototypeSchema = P;
43 
44  Const(const Type& constant, std::string&& desc);
45  Const(const Type& constant, const PrototypeSchema& prototype, std::string&& desc)
46  : Base<Const<T, P>>(prototype.type(), std::move(desc))
47  , prototype_(prototype)
48  , constant_(constant) {
49  prototype_.reset_ptr();
50  }
51 
52 #if 0
53  // This is defined in: fable/schema/magic.hpp
54  Const(const T& constant, std::string&& desc)
55  : Const(constant, make_prototype<T>(), std::move(desc)) {}
56 #endif
57 
58  public: // Overrides
59  Json json_schema() const override {
60  Json j{
61  {"const", constant_},
62  };
63  this->augment_schema(j);
64  return j;
65  }
66 
67  void validate(const Conf& c) const override {
68  Type tmp = prototype_.deserialize(c);
69  if (tmp != constant_) {
70  this->throw_error(c, "expected const value {}, got {}", constant_, tmp);
71  }
72  }
73 
74  using Interface::to_json;
75  void to_json(Json& j) const override { j = serialize(constant_); }
76 
77  void from_conf(const Conf& c) override { validate(c); }
78 
79  Json serialize(const Type& x) const { return prototype_.serialize(x); }
80 
81  Type deserialize(const Conf& c) const {
82  validate(c);
83  return constant_;
84  }
85 
86  void reset_ptr() override {}
87 
88  private:
89  PrototypeSchema prototype_;
90  const Type constant_;
91 };
92 
93 template <typename T, typename P>
94 Const<T, P> make_const_schema(const T& constant, const P& prototype, std::string&& desc) {
95  return Const<T, P>(constant, prototype, std::move(desc));
96 }
97 
98 inline Const<std::string, String> make_const_str(const std::string& constant, std::string&& desc) {
99  return Const<std::string, String>(constant, std::move(desc));
100 }
101 
102 inline Const<std::string, String> make_const_str(const char* constant, std::string&& desc) {
103  return Const<std::string, String>(constant, std::move(desc));
104 }
105 
106 } // namespace schema
107 } // namespace fable
108 
109 #endif // FABLE_SCHEMA_CONST_HPP_
Definition: conf.hpp:74
void to_json(Json &j) const override
Definition: const.hpp:75
void reset_ptr() override
Definition: const.hpp:86
nlohmann::json Json
Definition: json.hpp:62
Json json_schema() const override
Definition: const.hpp:59
Definition: const.hpp:39
void validate(const Conf &c) const override
Definition: const.hpp:67
Definition: conf.hpp:70
virtual Json to_json() const
Definition: interface.hpp:220
void from_conf(const Conf &c) override
Definition: const.hpp:77
Definition: interface.hpp:354