Added the basic functionality for constrained numeric values

This commit is contained in:
Patrick Urbanke
2023-10-31 20:11:54 +01:00
parent 463fa175fc
commit 92d2d92c74
5 changed files with 123 additions and 0 deletions
+2
View File
@@ -2,6 +2,7 @@
#define RFL_RFL_HPP_
#include "rfl/Box.hpp"
#include "rfl/Constrained.hpp"
#include "rfl/Field.hpp"
#include "rfl/Flatten.hpp"
#include "rfl/Literal.hpp"
@@ -11,6 +12,7 @@
#include "rfl/Timestamp.hpp"
#include "rfl/always_false.hpp"
#include "rfl/as.hpp"
#include "rfl/constraints.hpp"
#include "rfl/default.hpp"
#include "rfl/define_literal.hpp"
#include "rfl/define_named_tuple.hpp"
+35
View File
@@ -0,0 +1,35 @@
#ifndef RFL_CONSTRAINED_HPP_
#define RFL_CONSTRAINED_HPP_
#include "rfl/Result.hpp"
namespace rfl {
/// A constrained type is a numeric value that is constrained is some way, such
/// as it must be greater than a specific threshold known at compile time.
template <class T, class C>
class Constrained {
public:
using ReflectionType = T;
Constrained(T _value) : value_(C::check(_value).value()) {}
~Constrained() = default;
/// Exposes the underlying value.
T& value() { return value_; }
/// Exposes the underlying value.
T value() const { return value_; }
/// Necessary for the serialization to work.
T reflection() const { return value_; }
private:
/// The underlying value.
T value_;
};
} // namespace rfl
#endif
+58
View File
@@ -0,0 +1,58 @@
#ifndef RFL_CONSTRAINTS_HPP_
#define RFL_CONSTRAINTS_HPP_
#include "rfl/Result.hpp"
namespace rfl {
template <int _threshold>
struct greater_than {
template <class T>
static Result<T> check(T _value) noexcept {
if (_value <= static_cast<T>(_threshold)) {
return Error("Value expected to be greater than " +
std::to_string(_threshold) + ", but got " +
std::to_string(_value) + ".");
}
return _value;
}
};
template <int _threshold>
struct less_than {
template <class T>
static Result<T> check(T _value) noexcept {
if (_value >= static_cast<T>(_threshold)) {
return Error("Value expected to be less than " +
std::to_string(_threshold) + ", but got " +
std::to_string(_value) + ".");
}
return _value;
}
};
/// Requires that all of the contraints C and Cs be true.
template <class C, class... Cs>
struct all_of {
template <class T>
static rfl::Result<T> check(T _value) noexcept {
return check_impl<T, C, Cs...>(_value);
}
private:
template <class T, class Head, class... Tail>
static rfl::Result<T> check_impl(T _value) noexcept {
if constexpr (sizeof...(Tail) == 0) {
return Head::check(_value);
} else {
return Head::check(_value).and_then(check_impl<T, Tail...>);
}
}
};
/// Requires that any of the contraints C and Cs be true.
// ...
} // namespace rfl
#endif
+26
View File
@@ -0,0 +1,26 @@
#include <iostream>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <string>
#include <vector>
#include "write_and_read.hpp"
void test_all_of() {
std::cout << "test_all_of" << std::endl;
using Age = rfl::Constrained<
int, rfl::all_of<rfl::greater_than<0>, rfl::less_than<130>>>;
struct Person {
rfl::Field<"firstName", std::string> first_name;
rfl::Field<"lastName", std::string> last_name;
rfl::Field<"age", Age> age;
};
const auto homer =
Person{.first_name = "Homer", .last_name = "Simpson", .age = 45};
write_and_read(homer,
R"({"firstName":"Homer","lastName":"Simpson","age":45})");
}
+2
View File
@@ -1,3 +1,4 @@
#include "test_all_of.hpp"
#include "test_array.hpp"
#include "test_as.hpp"
#include "test_as_flatten.hpp"
@@ -66,6 +67,7 @@ int main() {
test_string_map();
test_string_unordered_map();
test_error_messages();
test_all_of();
test_custom_class1();
test_custom_class2();