diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2022-11-19 15:18:02 +0100 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2022-11-19 15:18:02 +0100 |
commit | 67760d797c7dac09077e59a87fa3c27a610a5fc4 (patch) | |
tree | 62d2c53bf5a5f3935bbfc059ba49dbe5b1c6e29d | |
parent | 17ee71b537bf65923acc021a1ed382ab76df9f98 (diff) |
entity: make `constantly` more generic
-rw-r--r-- | entity/metadata.hpp | 11 | ||||
-rw-r--r-- | test/entity.cpp | 10 |
2 files changed, 11 insertions, 10 deletions
diff --git a/entity/metadata.hpp b/entity/metadata.hpp index 60e9c458..c9b9a1d5 100644 --- a/entity/metadata.hpp +++ b/entity/metadata.hpp @@ -70,18 +70,17 @@ struct find_reader<Obj, Type, Default, I, F, Fs...> { using type = F; static con namespace floormat::entities { -template<typename Obj, auto constant> -constexpr auto constantly = [](const Obj&) constexpr { return constant; }; +template<typename... Ts> constexpr auto constantly(const auto& x) noexcept { return [x](const Ts&...) constexpr { return x; }; } template<typename Obj, typename Type> struct entity_field_base {}; template<typename Obj, typename Type, FieldReader<Obj, Type> R, FieldWriter<Obj, Type> W, typename... Ts> struct entity_field : entity_field_base<Obj, Type> { private: - static constexpr auto default_predicate = constantly<Obj, field_status::enabled>; - static constexpr auto default_c_range = constantly<Obj, constraints::range<Type>{}>; - static constexpr auto default_c_length = constantly<Obj, constraints::length{std::size_t(-1)}>; - static constexpr auto default_c_group = [](const Obj&) constexpr { return StringView{}; }; + static constexpr auto default_predicate = constantly<Obj>(field_status::enabled); + static constexpr auto default_c_range = constantly<Obj>(constraints::range<Type>{}); + static constexpr auto default_c_length = constantly<Obj>(constraints::length{std::size_t(-1)}); + static constexpr auto default_c_group = constantly<Obj>(StringView{}); using default_predicate_t = std::decay_t<decltype(default_predicate)>; using default_c_range_t = std::decay_t<decltype(default_c_range)>; using default_c_length_t = std::decay_t<decltype(default_c_length)>; diff --git a/test/entity.cpp b/test/entity.cpp index d09c94ba..4e0935f2 100644 --- a/test/entity.cpp +++ b/test/entity.cpp @@ -189,10 +189,12 @@ constexpr bool test_names() constexpr void test_range() { constexpr auto x = TestAccessors{}; - constexpr auto foo = entity::type<int>::field{"foo"_s, &TestAccessors::foo, &TestAccessors::foo, - constantly<TestAccessors, constraints::length{42}>, - constantly<TestAccessors, constraints::range<int>{37, 42}>, - [](const TestAccessors&) constexpr -> constraints::group { return "foo"_s; }}; + constexpr auto foo = entity::type<int>::field{ + "foo"_s, &TestAccessors::foo, &TestAccessors::foo, + constantly<TestAccessors>(constraints::length{42}), + constantly<TestAccessors>(constraints::range<int>{37, 42}), + constantly<TestAccessors>(constraints::group{"foo"_s}) + }; using limits = std::numeric_limits<int>; |