$darkmode
number.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 <initializer_list> // for initializer_list<>
27 #include <set> // for set<>
28 #include <string> // for string
29 #include <type_traits> // for enable_if_t<>, is_arithmetic<>
30 
31 #include <fable/schema/interface.hpp> // for Base<>
32 
33 namespace fable::schema {
34 
35 template <typename T>
36 class Number : public Base<Number<T>> {
37  static_assert(std::is_arithmetic_v<T>, "arithmetic value required");
38 
39  public: // Types and Constructors
40  using Type = T;
41 
42  template <typename X = T, std::enable_if_t<std::is_integral_v<X> && std::is_unsigned_v<X>, int> = 0>
43  Number(Type* ptr, std::string desc)
44  : Base<Number<T>>(JsonType::number_unsigned, std::move(desc)), ptr_(ptr) {}
45 
46  template <typename X = T, std::enable_if_t<std::is_integral_v<X> && std::is_signed_v<X>, int> = 0>
47  Number(Type* ptr, std::string desc)
48  : Base<Number<T>>(JsonType::number_integer, std::move(desc)), ptr_(ptr) {}
49 
50  template <typename X = T, std::enable_if_t<std::is_floating_point_v<X>, int> = 0>
51  Number(Type* ptr, std::string desc)
52  : Base<Number<T>>(JsonType::number_float, std::move(desc)), ptr_(ptr) {}
53 
54  public: // Special
55  [[nodiscard]] T minimum() const { return value_min_; }
56  [[nodiscard]] bool exclusive_minimum() const { return exclusive_min_; }
57  Number<T> minimum(T value) &&;
58  Number<T> exclusive_minimum(T value) &&;
59 
60  [[nodiscard]] T maximum() const { return value_max_; }
61  [[nodiscard]] bool exclusive_maximum() const { return exclusive_max_; }
62  Number<T> maximum(T value) &&;
63  Number<T> exclusive_maximum(T value) &&;
64 
65  [[nodiscard]] std::pair<T, T> bounds() const;
66  Number<T> bounds(T min, T max) &&;
67  Number<T> bounds_with(T min, T max, std::initializer_list<T> whitelisted) &&;
68 
69  [[nodiscard]] const std::set<T>& whitelist() const { return whitelist_; }
70  Number<T> whitelist(T x) &&;
71  Number<T> whitelist(std::initializer_list<T> xs) &&;
72  void insert_whitelist(T x);
73 
74  [[nodiscard]] const std::set<T>& blacklist() const { return blacklist_; }
75  Number<T> blacklist(T x) &&;
76  Number<T> blacklist(std::initializer_list<T> xs) &&;
77  void insert_blacklist(T x);
78 
79  public: // Overrides
80  [[nodiscard]] Json json_schema() const override;
81  bool validate(const Conf& c, std::optional<SchemaError>& err) const override;
82  using Interface::to_json;
83  void to_json(Json& j) const override;
84  void from_conf(const Conf& c) override;
85  [[nodiscard]] Json serialize(const Type& x) const;
86  [[nodiscard]] Type deserialize(const Conf& c) const;
87  void serialize_into(Json& j, const Type& x) const;
88  void deserialize_into(const Conf& c, Type& x) const;
89  void reset_ptr() override;
90 
91  private:
92  template <typename B>
93  bool validate_bounds(const Conf& c, std::optional<SchemaError>& err) const;
94 
95  private:
96  bool exclusive_min_{false};
97  bool exclusive_max_{false};
98  T value_min_{std::numeric_limits<T>::lowest()};
99  T value_max_{std::numeric_limits<T>::max()};
100  std::set<T> whitelist_{};
101  std::set<T> blacklist_{};
102  Type* ptr_{nullptr};
103 };
104 
105 template <typename T, std::enable_if_t<std::is_arithmetic_v<T> && !std::is_enum_v<T>, int> = 0>
106 inline Number<T> make_schema(T* ptr, std::string desc) {
107  return Number<T>(ptr, std::move(desc));
108 }
109 
110 } // namespace fable::schema
Definition: conf.hpp:76
Definition: interface.hpp:398
virtual Json to_json() const
Definition: interface.hpp:254
Definition: number.hpp:36
void from_conf(const Conf &c) override
Definition: number_impl.hpp:198
virtual Json to_json() const
Definition: interface.hpp:254
Json json_schema() const override
Definition: number_impl.hpp:147
void reset_ptr() override
Definition: number_impl.hpp:224
bool validate(const Conf &c, std::optional< SchemaError > &err) const override
Definition: number_impl.hpp:174
nlohmann::json Json
Definition: fable_fwd.hpp:35