$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  */
24 #pragma once
25 
26 #include <string> // for string
27 #include <utility> // for move
28 
29 #include <fable/schema/interface.hpp> // for Base<>, Box
30 #include <fable/schema/string.hpp> // for String
31 
32 namespace fable::schema {
33 
34 template <typename T, typename P>
35 class Const : public Base<Const<T, P>> {
36  public: // Types and Constructors
37  using Type = std::remove_cv_t<std::remove_reference_t<T>>;
38  using PrototypeSchema = std::remove_cv_t<std::remove_reference_t<P>>;
39 
40  Const(Type constant, std::string desc)
41  : Const(std::move(constant), make_prototype<Type>(), std::move(desc)) {}
42 
43  Const(Type constant, PrototypeSchema prototype, std::string desc)
44  : Base<Const<T, P>>(prototype.type(), std::move(desc))
45  , prototype_(std::move(prototype))
46  , constant_(std::move(constant)) {
47  prototype_.reset_ptr();
48  }
49 
50  public: // Overrides
51  [[nodiscard]] Json json_schema() const override {
52  Json j{
53  {"const", constant_},
54  };
55  this->augment_schema(j);
56  return j;
57  }
58 
59  bool validate(const Conf& c, std::optional<SchemaError>& err) const override {
60  Type tmp = prototype_.deserialize(c);
61  if (tmp != constant_) {
62  return this->set_error(err, c, "expected const value {}, got {}", constant_, tmp);
63  }
64  return true;
65  }
66 
67  using Interface::to_json;
68  void to_json(Json& j) const override { j = serialize(constant_); }
69 
70  void from_conf(const Conf& c) override { this->validate_or_throw(c); }
71 
72  [[nodiscard]] Json serialize(const Type& x) const { return prototype_.serialize(x); }
73 
74  [[nodiscard]] Type deserialize(const Conf& c) const {
75  this->validate_or_throw(c);
76  return constant_;
77  }
78 
79  void serialize_into(Json& j, const Type& x) const { prototype_.serialize_into(j, x); }
80 
81  void deserialize_into(const Conf& c, Type& x) const {
82  this->validate_or_throw(c);
83  x = constant_;
84  }
85 
86  void reset_ptr() override {}
87 
88  private:
89  PrototypeSchema prototype_;
90  Type constant_;
91 };
92 
93 template <typename T, typename P, typename S>
94 Const<T, P> make_const_schema(T&& constant, P&& prototype, S&& desc) {
95  return {std::forward<T>(constant), std::forward<P>(prototype), std::forward<S>(desc)};
96 }
97 
98 template <typename T, typename S>
99 Const<T, decltype(make_prototype<std::remove_cv_t<std::remove_reference_t<T>>>())> make_const_schema(T&& constant, S&& desc) {
100  return {std::forward<T>(constant), std::forward<S>(desc)};
101 }
102 
103 } // namespace fable::schema
Definition: conf.hpp:81
Definition: interface.hpp:398
Definition: const.hpp:35
void from_conf(const Conf &c) override
Definition: const.hpp:70
void reset_ptr() override
Definition: const.hpp:86
bool validate(const Conf &c, std::optional< SchemaError > &err) const override
Definition: const.hpp:59
void to_json(Json &j) const override
Definition: const.hpp:68
Json json_schema() const override
Definition: const.hpp:51
virtual void validate_or_throw(const Conf &c) const final
Definition: interface.hpp:218
virtual Json to_json() const
Definition: interface.hpp:254
nlohmann::json Json
Definition: fable_fwd.hpp:35