diff --git a/include/rfl.hpp b/include/rfl.hpp index e38806c0..5b7ae588 100644 --- a/include/rfl.hpp +++ b/include/rfl.hpp @@ -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" diff --git a/include/rfl/Constrained.hpp b/include/rfl/Constrained.hpp new file mode 100644 index 00000000..de2c1638 --- /dev/null +++ b/include/rfl/Constrained.hpp @@ -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 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 diff --git a/include/rfl/constraints.hpp b/include/rfl/constraints.hpp new file mode 100644 index 00000000..80f3815e --- /dev/null +++ b/include/rfl/constraints.hpp @@ -0,0 +1,58 @@ +#ifndef RFL_CONSTRAINTS_HPP_ +#define RFL_CONSTRAINTS_HPP_ + +#include "rfl/Result.hpp" + +namespace rfl { + +template +struct greater_than { + template + static Result check(T _value) noexcept { + if (_value <= static_cast(_threshold)) { + return Error("Value expected to be greater than " + + std::to_string(_threshold) + ", but got " + + std::to_string(_value) + "."); + } + return _value; + } +}; + +template +struct less_than { + template + static Result check(T _value) noexcept { + if (_value >= static_cast(_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 +struct all_of { + template + static rfl::Result check(T _value) noexcept { + return check_impl(_value); + } + + private: + template + static rfl::Result check_impl(T _value) noexcept { + if constexpr (sizeof...(Tail) == 0) { + return Head::check(_value); + } else { + return Head::check(_value).and_then(check_impl); + } + } +}; + +/// Requires that any of the contraints C and Cs be true. +// ... + +} // namespace rfl + +#endif diff --git a/tests/test_all_of.hpp b/tests/test_all_of.hpp new file mode 100644 index 00000000..b6f0d8bc --- /dev/null +++ b/tests/test_all_of.hpp @@ -0,0 +1,26 @@ +#include +#include +#include +#include +#include + +#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::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})"); +} diff --git a/tests/tests.cpp b/tests/tests.cpp index ebb3de95..00b68559 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -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();