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