26 #ifndef FABLE_SCHEMA_DURATION_HPP_ 27 #define FABLE_SCHEMA_DURATION_HPP_ 39 template <
typename T,
typename Period>
42 using Type = std::chrono::duration<T, Period>;
44 template <
typename X = T,
45 std::enable_if_t<std::is_integral<X>::value && std::is_unsigned<X>::value,
int> = 0>
46 Duration(Type* ptr, std::string&& desc)
49 template <
typename X = T,
50 std::enable_if_t<std::is_integral<X>::value && std::is_signed<X>::value,
int> = 0>
51 Duration(Type* ptr, std::string&& desc)
54 template <typename X = T, std::enable_if_t<std::is_floating_point<X>::value,
int> = 0>
55 Duration(Type* ptr, std::string&& desc)
59 T minimum()
const {
return value_min_; }
60 bool exclusive_minimum()
const {
return exclusive_min_; }
63 exclusive_min_ =
false;
64 return std::move(*
this);
68 exclusive_min_ =
true;
69 return std::move(*
this);
72 T maximum()
const {
return value_max_; }
73 bool exclusive_maximum()
const {
return exclusive_max_; }
76 exclusive_max_ =
false;
77 return std::move(*
this);
81 exclusive_max_ =
true;
82 return std::move(*
this);
85 std::pair<T, T> bounds()
const {
return std::make_pair(value_min_, value_max_); }
87 exclusive_min_ =
false;
89 exclusive_max_ =
false;
91 return std::move(*
this);
98 {exclusive_min_ ?
"exclusiveMinimum" :
"minimum", value_min_},
99 {exclusive_max_ ?
"exclusiveMaximum" :
"maximum", value_max_},
101 this->augment_schema(j);
107 case JsonType::number_unsigned: {
108 check_bounds<uint64_t>(c);
111 case JsonType::number_integer: {
112 check_bounds<int64_t>(c);
115 case JsonType::number_float: {
116 if (this->
type() != JsonType::number_float) {
117 this->throw_wrong_type(c);
119 check_bounds<double>(c);
124 this->throw_wrong_type(c);
130 assert(ptr_ !=
nullptr);
131 j = serialize(*ptr_);
135 assert(ptr_ !=
nullptr);
136 *ptr_ = deserialize(c);
139 Json serialize(
const Type& x)
const {
return x.count(); }
141 Type deserialize(
const Conf& c)
const {
return Type(c.
get<T>()); }
149 template <
typename B>
150 void check_bounds(
const Conf& c)
const {
152 if (!std::numeric_limits<B>::is_signed && value_min_ < 0) {
157 }
else if (exclusive_min_) {
158 if (v <= static_cast<B>(value_min_)) {
159 this->throw_error(c,
"expected exclusive minimum of {}, got {}", value_min_, v);
162 if (v < static_cast<B>(value_min_)) {
163 this->throw_error(c,
"expected minimum of {}, got {}", value_min_, v);
167 if (!std::numeric_limits<B>::is_signed && value_max_ < 0) {
170 this->throw_error(c,
"expected {}maximum of {}, got {}", (exclusive_max_ ?
"exclusive " :
""),
172 }
else if (exclusive_max_) {
173 if (v >= static_cast<B>(value_max_)) {
174 this->throw_error(c,
"expected exclusive maximum of {}, got {}", value_max_, v);
177 if (v > static_cast<B>(value_max_)) {
178 this->throw_error(c,
"expected maximum of {}, got {}", value_max_, v);
184 bool exclusive_min_{
false};
185 bool exclusive_max_{
false};
186 T value_min_{std::numeric_limits<T>::lowest()};
187 T value_max_{std::numeric_limits<T>::max()};
191 template <
typename Rep,
typename Period>
193 std::string&& desc) {
200 #endif // FABLE_SCHEMA_DURATION_HPP_
JsonType type() const override
Definition: interface.hpp:365
void reset_ptr() override
Definition: duration.hpp:143
T get() const
Definition: conf.hpp:164
nlohmann::json Json
Definition: json.hpp:62
Definition: duration.hpp:40
virtual Json to_json() const
Definition: interface.hpp:220
void validate(const Conf &c) const override
Definition: duration.hpp:105
void from_conf(const Conf &c) override
Definition: duration.hpp:134
void to_json(Json &j) const override
Definition: duration.hpp:129
Json json_schema() const override
Definition: duration.hpp:95
Definition: interface.hpp:354
std::string type_string() const override
Definition: interface.hpp:366