28 #ifndef FABLE_SCHEMA_NUMBER_IMPL_HPP_ 29 #define FABLE_SCHEMA_NUMBER_IMPL_HPP_ 31 #include <initializer_list> 35 #include <type_traits> 45 Number<T> Number<T>::minimum(T value) && {
47 exclusive_min_ =
false;
48 return std::move(*
this);
52 Number<T> Number<T>::exclusive_minimum(T value) && {
54 exclusive_min_ =
true;
55 return std::move(*
this);
59 Number<T> Number<T>::maximum(T value) && {
61 exclusive_max_ =
false;
62 return std::move(*
this);
66 Number<T> Number<T>::exclusive_maximum(T value) && {
68 exclusive_max_ =
true;
69 return std::move(*
this);
73 std::pair<T, T> Number<T>::bounds()
const {
return std::make_pair(value_min_, value_max_); }
76 Number<T> Number<T>::bounds(T min, T max) && {
77 exclusive_min_ =
false;
79 exclusive_max_ =
false;
81 return std::move(*
this);
85 Number<T> Number<T>::bounds_with(T min, T max, std::initializer_list<T> whitelisted) && {
86 exclusive_min_ =
false;
88 exclusive_max_ =
false;
90 for (
auto x : whitelisted) {
93 return std::move(*
this);
97 Number<T> Number<T>::whitelist(T x) && {
99 return std::move(*
this);
102 template <
typename T>
103 Number<T> Number<T>::whitelist(std::initializer_list<T> xs) && {
107 return std::move(*
this);
110 template <
typename T>
111 void Number<T>::insert_whitelist(T x) {
112 if (std::is_floating_point<T>::value) {
113 throw std::logic_error(
"cannot whitelist floating-point numbers");
115 if (blacklist_.count(x)) {
116 throw std::logic_error(
"cannot add blacklisted value to whitelist: " + std::to_string(x));
118 whitelist_.insert(x);
121 template <
typename T>
122 Number<T> Number<T>::blacklist(T x) && {
124 return std::move(*
this);
127 template <
typename T>
128 Number<T> Number<T>::blacklist(std::initializer_list<T> xs) && {
132 return std::move(*
this);
135 template <
typename T>
136 void Number<T>::insert_blacklist(T x) {
137 if (std::is_floating_point<T>::value) {
138 throw std::logic_error(
"cannot blacklist floating-point numbers");
140 if (blacklist_.count(x)) {
141 throw std::logic_error(
"cannot add whitelisted value to blacklist: " + std::to_string(x));
143 blacklist_.insert(x);
146 template <
typename T>
149 {
"type", this->type_string()},
150 {exclusive_min_ ?
"exclusiveMinimum" :
"minimum", value_min_},
151 {exclusive_max_ ?
"exclusiveMaximum" :
"maximum", value_max_},
154 if (!std::is_floating_point<T>::value) {
155 auto write_list = [&j](
auto name,
auto xlist) {
156 if (!xlist.empty()) {
158 for (
auto x : xlist) {
165 write_list(
"whitelist", whitelist_);
166 write_list(
"blacklist", blacklist_);
169 this->augment_schema(j);
173 template <
typename T>
176 case JsonType::number_unsigned: {
177 check_bounds<uint64_t>(c);
180 case JsonType::number_integer: {
181 check_bounds<int64_t>(c);
184 case JsonType::number_float: {
185 if (this->type() != JsonType::number_float) {
186 this->throw_wrong_type(c);
188 check_bounds<double>(c);
192 this->throw_wrong_type(c);
196 template <
typename T>
198 assert(ptr_ !=
nullptr);
199 j = serialize(*ptr_);
202 template <
typename T>
204 assert(ptr_ !=
nullptr);
205 *ptr_ = deserialize(c);
208 template <
typename T>
211 template <
typename T>
214 template <
typename T>
220 template <
typename T>
221 template <
typename B>
226 if (!std::is_floating_point<T>::value) {
227 if (whitelist_.count(v)) {
230 if (blacklist_.count(v)) {
231 this->throw_error(c,
"unexpected blacklisted value {}", v);
235 if (!std::numeric_limits<B>::is_signed && value_min_ < 0) {
240 }
else if (exclusive_min_) {
241 if (v <= static_cast<B>(value_min_)) {
242 this->throw_error(c,
"expected exclusive minimum of {}, got {}", value_min_, v);
245 if (v < static_cast<B>(value_min_)) {
246 this->throw_error(c,
"expected minimum of {}, got {}", value_min_, v);
250 if (!std::numeric_limits<B>::is_signed && value_max_ < 0) {
253 this->throw_error(c,
"expected {}maximum of {}, got {}", (exclusive_max_ ?
"exclusive " :
""),
255 }
else if (exclusive_max_) {
256 if (v >= static_cast<B>(value_max_)) {
257 this->throw_error(c,
"expected exclusive maximum of {}, got {}", value_max_, v);
260 if (v > static_cast<B>(value_max_)) {
261 this->throw_error(c,
"expected maximum of {}, got {}", value_max_, v);
269 #endif // FABLE_SCHEMA_NUMBER_IMPL_HPP_
void from_conf(const Conf &c) override
Definition: number_impl.hpp:203
Definition: number.hpp:39
T get() const
Definition: conf.hpp:164
Json json_schema() const override
Definition: number_impl.hpp:147
nlohmann::json Json
Definition: json.hpp:62
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