diff --git a/headers/meta.hpp/meta_base.hpp b/headers/meta.hpp/meta_base.hpp index 5f08db5..0ec657c 100644 --- a/headers/meta.hpp/meta_base.hpp +++ b/headers/meta.hpp/meta_base.hpp @@ -19,7 +19,6 @@ #include #include #include -#include #include #include #include diff --git a/headers/meta.hpp/meta_states.hpp b/headers/meta.hpp/meta_states.hpp index 43287bf..578024f 100644 --- a/headers/meta.hpp/meta_states.hpp +++ b/headers/meta.hpp/meta_states.hpp @@ -211,10 +211,10 @@ namespace meta_hpp [[nodiscard]] const std::string& get_name() const noexcept; template < typename... Args > - std::optional invoke(Args&&... args) const; + value invoke(Args&&... args) const; template < typename... Args > - std::optional operator()(Args&&... args) const; + value operator()(Args&&... args) const; template < typename... Args > [[nodiscard]] bool is_invocable_with() const noexcept; @@ -277,10 +277,10 @@ namespace meta_hpp [[nodiscard]] const std::string& get_name() const noexcept; template < typename Instance, typename... Args > - std::optional invoke(Instance&& instance, Args&&... args) const; + value invoke(Instance&& instance, Args&&... args) const; template < typename Instance, typename... Args > - std::optional operator()(Instance&& instance, Args&&... args) const; + value operator()(Instance&& instance, Args&&... args) const; template < typename Instance, typename... Args > [[nodiscard]] bool is_invocable_with() const noexcept; @@ -388,7 +388,7 @@ namespace meta_hpp::detail }; struct function_state final { - using invoke_impl = std::function(std::span)>; + using invoke_impl = std::function)>; using is_invocable_with_impl = std::function)>; const function_index index; @@ -417,7 +417,7 @@ namespace meta_hpp::detail }; struct method_state final { - using invoke_impl = std::function(const inst&, std::span)>; + using invoke_impl = std::function)>; using is_invocable_with_impl = std::function)>; const method_index index; diff --git a/headers/meta.hpp/meta_states/function.hpp b/headers/meta.hpp/meta_states/function.hpp index 3f5154b..1d8a25c 100644 --- a/headers/meta.hpp/meta_states/function.hpp +++ b/headers/meta.hpp/meta_states/function.hpp @@ -14,7 +14,7 @@ namespace meta_hpp::detail { template < function_policy_kind Policy, function_kind Function > - std::optional raw_function_invoke(Function function, std::span args) { + value raw_function_invoke(Function function, std::span args) { using ft = function_traits; using return_type = typename ft::return_type; using argument_types = typename ft::argument_types; @@ -40,7 +40,7 @@ namespace meta_hpp::detail return std::invoke([ args, function = std::move(function) // NOLINTNEXTLINE(readability-named-parameter) - ](std::index_sequence) -> std::optional { + ](std::index_sequence) -> value { if ( !(... && (args.data() + Is)->can_cast_to>()) ) { throw std::logic_error("an attempt to call a function with incorrect argument types"); } @@ -49,7 +49,7 @@ namespace meta_hpp::detail std::ignore = std::invoke( std::move(function), (args.data() + Is)->cast>()...); - return std::nullopt; + return {}; } else { return_type&& return_value = std::invoke( std::move(function), @@ -134,7 +134,7 @@ namespace meta_hpp } template < typename... Args > - std::optional function::invoke(Args&&... args) const { + value function::invoke(Args&&... args) const { if constexpr ( sizeof...(Args) > 0 ) { using namespace detail; const std::array vargs{arg{std::forward(args)}...}; @@ -145,7 +145,7 @@ namespace meta_hpp } template < typename... Args > - std::optional function::operator()(Args&&... args) const { + value function::operator()(Args&&... args) const { return invoke(std::forward(args)...); } diff --git a/headers/meta.hpp/meta_states/method.hpp b/headers/meta.hpp/meta_states/method.hpp index 1f2c1f8..762b0d4 100644 --- a/headers/meta.hpp/meta_states/method.hpp +++ b/headers/meta.hpp/meta_states/method.hpp @@ -14,7 +14,7 @@ namespace meta_hpp::detail { template < method_policy_kind Policy, method_kind Method > - std::optional raw_method_invoke(Method method, const inst& inst, std::span args) { + value raw_method_invoke(Method method, const inst& inst, std::span args) { using mt = method_traits; using return_type = typename mt::return_type; using qualified_type = typename mt::qualified_type; @@ -46,7 +46,7 @@ namespace meta_hpp::detail &inst, &args, method = std::move(method) // NOLINTNEXTLINE(readability-named-parameter) - ](std::index_sequence) -> std::optional { + ](std::index_sequence) -> value { if ( !(... && (args.data() + Is)->can_cast_to>()) ) { throw std::logic_error("an attempt to call a method with incorrect argument types"); } @@ -56,7 +56,7 @@ namespace meta_hpp::detail std::move(method), inst.cast(), (args.data() + Is)->cast>()...); - return std::nullopt; + return {}; } else { return_type&& return_value = std::invoke( std::move(method), @@ -147,7 +147,7 @@ namespace meta_hpp } template < typename Instance, typename... Args > - std::optional method::invoke(Instance&& instance, Args&&... args) const { + value method::invoke(Instance&& instance, Args&&... args) const { using namespace detail; const inst vinst{std::forward(instance)}; if constexpr ( sizeof...(Args) > 0 ) { @@ -159,7 +159,7 @@ namespace meta_hpp } template < typename Instance, typename... Args > - std::optional method::operator()(Instance&& instance, Args&&... args) const { + value method::operator()(Instance&& instance, Args&&... args) const { return invoke(std::forward(instance), std::forward(args)...); } diff --git a/headers/meta.hpp/meta_types.hpp b/headers/meta.hpp/meta_types.hpp index d0a6d90..dd7b014 100644 --- a/headers/meta.hpp/meta_types.hpp +++ b/headers/meta.hpp/meta_types.hpp @@ -220,10 +220,10 @@ namespace meta_hpp [[nodiscard]] const variable_map& get_variables() const noexcept; template < typename... Args > - [[nodiscard]] std::optional create(Args&&... args) const; + [[nodiscard]] value create(Args&&... args) const; template < typename... Args > - [[nodiscard]] std::optional operator()(Args&&... args) const; + [[nodiscard]] value operator()(Args&&... args) const; template < typename Arg > bool destroy(Arg&& ptr) const; @@ -315,8 +315,8 @@ namespace meta_hpp [[nodiscard]] evalue get_evalue(std::string_view name) const noexcept; template < typename Value > - [[nodiscard]] std::optional value_to_name(Value&& value) const noexcept; - [[nodiscard]] std::optional name_to_value(std::string_view name) const noexcept; + [[nodiscard]] std::string_view value_to_name(Value&& value) const noexcept; + [[nodiscard]] value name_to_value(std::string_view name) const noexcept; private: detail::enum_type_data_ptr data_; friend auto detail::data_access(const enum_type&); diff --git a/headers/meta.hpp/meta_types/class_type.hpp b/headers/meta.hpp/meta_types/class_type.hpp index 4925fda..de4aebc 100644 --- a/headers/meta.hpp/meta_types/class_type.hpp +++ b/headers/meta.hpp/meta_types/class_type.hpp @@ -103,17 +103,17 @@ namespace meta_hpp } template < typename... Args > - std::optional class_type::create(Args&&... args) const { + value class_type::create(Args&&... args) const { for ( auto&& ctor : data_->ctors ) { if ( ctor.second.is_invocable_with(std::forward(args)...) ) { return ctor.second.invoke(std::forward(args)...); } } - return std::nullopt; + return {}; } template < typename... Args > - std::optional class_type::operator()(Args&&... args) const { + value class_type::operator()(Args&&... args) const { return create(std::forward(args)...); } diff --git a/headers/meta.hpp/meta_types/enum_type.hpp b/headers/meta.hpp/meta_types/enum_type.hpp index f2088a5..1b84bb1 100644 --- a/headers/meta.hpp/meta_types/enum_type.hpp +++ b/headers/meta.hpp/meta_types/enum_type.hpp @@ -71,11 +71,11 @@ namespace meta_hpp } template < typename Value > - std::optional enum_type::value_to_name(Value&& value) const noexcept { + std::string_view enum_type::value_to_name(Value&& value) const noexcept { const detail::arg value_arg{std::forward(value)}; if ( value_arg.get_raw_type() != *this ) { - return std::nullopt; + return std::string_view{}; } for ( auto&& evalue : data_->evalues ) { @@ -84,13 +84,14 @@ namespace meta_hpp } } - return std::nullopt; + return std::string_view{}; } - inline std::optional enum_type::name_to_value(std::string_view name) const noexcept { + inline value enum_type::name_to_value(std::string_view name) const noexcept { if ( const evalue value = get_evalue(name); value ) { return value.get_value(); } - return std::nullopt; + + return value{}; } } diff --git a/headers/meta.hpp/meta_utilities/vinvoke.hpp b/headers/meta.hpp/meta_utilities/vinvoke.hpp index 66cb0ef..5c786e8 100644 --- a/headers/meta.hpp/meta_utilities/vinvoke.hpp +++ b/headers/meta.hpp/meta_utilities/vinvoke.hpp @@ -13,12 +13,12 @@ namespace meta_hpp { template < typename... Args > - std::optional invoke(const function& function, Args&&... args) { + value invoke(const function& function, Args&&... args) { return function.invoke(std::forward(args)...); } template < detail::function_kind Function, typename... Args > - std::optional invoke(Function&& function, Args&&... args) { + value invoke(Function&& function, Args&&... args) { using namespace detail; if constexpr ( sizeof...(Args) > 0 ) { const std::array vargs{arg{std::forward(args)}...}; @@ -32,12 +32,12 @@ namespace meta_hpp namespace meta_hpp { template < typename Instance > - std::optional invoke(const member& member, Instance&& instance) { + value invoke(const member& member, Instance&& instance) { return member.get(std::forward(instance)); } template < detail::member_kind Member, typename Instance > - std::optional invoke(Member&& member, Instance&& instance) { + value invoke(Member&& member, Instance&& instance) { using namespace detail; const inst vinst{std::forward(instance)}; return raw_member_getter(std::forward(member), vinst); @@ -47,12 +47,12 @@ namespace meta_hpp namespace meta_hpp { template < typename Instance, typename... Args > - std::optional invoke(const method& method, Instance&& instance, Args&&... args) { + value invoke(const method& method, Instance&& instance, Args&&... args) { return method.invoke(std::forward(instance), std::forward(args)...); } template < detail::method_kind Method, typename Instance, typename... Args > - std::optional invoke(Method&& method, Instance&& instance, Args&&... args) { + value invoke(Method&& method, Instance&& instance, Args&&... args) { using namespace detail; const inst vinst{std::forward(instance)}; if constexpr ( sizeof...(Args) > 0 ) { diff --git a/untests/meta_states/ctor_tests.cpp b/untests/meta_states/ctor_tests.cpp index ac5f36c..60d1267 100644 --- a/untests/meta_states/ctor_tests.cpp +++ b/untests/meta_states/ctor_tests.cpp @@ -89,7 +89,8 @@ TEST_CASE("meta/meta_states/ctor") { REQUIRE(clazz_type); { - const meta::value v = clazz_type.create(10).value(); + CHECK_FALSE(clazz_type.create(10, 20)); + const meta::value v = clazz_type.create(10); CHECK(v.get_type() == meta::resolve_type>()); CHECK(v.cast>().i == 10); @@ -108,7 +109,8 @@ TEST_CASE("meta/meta_states/ctor") { REQUIRE(clazz_type); { - const meta::value v = clazz_type.create(20).value(); + CHECK_FALSE(clazz_type.create(10, 20)); + const meta::value v = clazz_type.create(20); CHECK(v.get_type() == meta::resolve_type*>()); CHECK(v.cast*>()->i == 20); CHECK(clazz_type.destroy(v)); @@ -128,7 +130,8 @@ TEST_CASE("meta/meta_states/ctor") { REQUIRE(clazz_type); { - const meta::value v = clazz_type.create(30).value(); + CHECK_FALSE(clazz_type.create(10, 20)); + const meta::value v = clazz_type.create(30); CHECK(v.get_type() == meta::resolve_type>>()); CHECK(v.cast>>()->i == 30); diff --git a/untests/meta_states/function_tests.cpp b/untests/meta_states/function_tests.cpp index f58b630..c7bdd78 100644 --- a/untests/meta_states/function_tests.cpp +++ b/untests/meta_states/function_tests.cpp @@ -88,7 +88,7 @@ TEST_CASE("meta/meta_states/function") { CHECK_THROWS(func.invoke(ivec2{}, ivec2{}, 42)); CHECK(func.invoke(ivec2{1,2}, ivec2{3,4})); - CHECK(func.invoke(ivec2{1,2}, ivec2{3,4}).value() == ivec2{4,6}); + CHECK(func.invoke(ivec2{1,2}, ivec2{3,4}) == ivec2{4,6}); } SUBCASE("ilength2") { @@ -113,7 +113,7 @@ TEST_CASE("meta/meta_states/function") { CHECK_THROWS(func.invoke(ivec2{}, 42)); CHECK(func.invoke(ivec2{2,3})); - CHECK(func.invoke(ivec2{2,3}).value() == 13); + CHECK(func.invoke(ivec2{2,3}) == 13); } SUBCASE("arg_null") { diff --git a/untests/meta_states/method_tests.cpp b/untests/meta_states/method_tests.cpp index 884a049..fac12d3 100644 --- a/untests/meta_states/method_tests.cpp +++ b/untests/meta_states/method_tests.cpp @@ -130,9 +130,9 @@ TEST_CASE("meta/meta_states/method") { { clazz cl; - CHECK(mi.invoke(cl).value() == 1); + CHECK(mi.invoke(cl) == 1); CHECK_THROWS(mi.invoke(std::as_const(cl))); - CHECK(mi.invoke(std::move(cl)).value() == 1); + CHECK(mi.invoke(std::move(cl)) == 1); CHECK_THROWS(mi.invoke(std::move(std::as_const(cl)))); } @@ -175,9 +175,9 @@ TEST_CASE("meta/meta_states/method") { { clazz cl; - CHECK(mi.invoke(cl).value() == 2); + CHECK(mi.invoke(cl) == 2); CHECK_THROWS(mi.invoke(std::as_const(cl))); - CHECK(mi.invoke(std::move(cl)).value() == 2); + CHECK(mi.invoke(std::move(cl)) == 2); CHECK_THROWS(mi.invoke(std::move(std::as_const(cl)))); } @@ -220,10 +220,10 @@ TEST_CASE("meta/meta_states/method") { { clazz cl; - CHECK(mi.invoke(cl).value() == 3); - CHECK(mi.invoke(std::as_const(cl)).value() == 3); - CHECK(mi.invoke(std::move(cl)).value() == 3); - CHECK(mi.invoke(std::move(std::as_const(cl))).value() == 3); + CHECK(mi.invoke(cl) == 3); + CHECK(mi.invoke(std::as_const(cl)) == 3); + CHECK(mi.invoke(std::move(cl)) == 3); + CHECK(mi.invoke(std::move(std::as_const(cl))) == 3); } static_assert(std::is_invocable_v); @@ -265,10 +265,10 @@ TEST_CASE("meta/meta_states/method") { { clazz cl; - CHECK(mi.invoke(cl).value() == 4); - CHECK(mi.invoke(std::as_const(cl)).value() == 4); - CHECK(mi.invoke(std::move(cl)).value() == 4); - CHECK(mi.invoke(std::move(std::as_const(cl))).value() == 4); + CHECK(mi.invoke(cl) == 4); + CHECK(mi.invoke(std::as_const(cl)) == 4); + CHECK(mi.invoke(std::move(cl)) == 4); + CHECK(mi.invoke(std::move(std::as_const(cl))) == 4); } static_assert(std::is_invocable_v); @@ -310,7 +310,7 @@ TEST_CASE("meta/meta_states/method") { { clazz cl; - CHECK(mi.invoke(cl).value() == 5); + CHECK(mi.invoke(cl) == 5); CHECK_THROWS(mi.invoke(std::as_const(cl))); CHECK_THROWS(mi.invoke(std::move(cl))); CHECK_THROWS(mi.invoke(std::move(std::as_const(cl)))); @@ -355,7 +355,7 @@ TEST_CASE("meta/meta_states/method") { { clazz cl; - CHECK(mi.invoke(cl).value() == 6); + CHECK(mi.invoke(cl) == 6); CHECK_THROWS(mi.invoke(std::as_const(cl))); CHECK_THROWS(mi.invoke(std::move(cl))); CHECK_THROWS(mi.invoke(std::move(std::as_const(cl)))); @@ -400,8 +400,8 @@ TEST_CASE("meta/meta_states/method") { { clazz cl; - CHECK(mi.invoke(cl).value() == 7); - CHECK(mi.invoke(std::as_const(cl)).value() == 7); + CHECK(mi.invoke(cl) == 7); + CHECK(mi.invoke(std::as_const(cl)) == 7); CHECK(mi.invoke(std::move(cl)) == 7); CHECK(mi.invoke(std::move(std::as_const(cl))) == 7); } @@ -445,8 +445,8 @@ TEST_CASE("meta/meta_states/method") { { clazz cl; - CHECK(mi.invoke(cl).value() == 8); - CHECK(mi.invoke(std::as_const(cl)).value() == 8); + CHECK(mi.invoke(cl) == 8); + CHECK(mi.invoke(std::as_const(cl)) == 8); CHECK(mi.invoke(std::move(cl)) == 8); CHECK(mi.invoke(std::move(std::as_const(cl))) == 8); } @@ -492,7 +492,7 @@ TEST_CASE("meta/meta_states/method") { CHECK_THROWS(mi.invoke(cl)); CHECK_THROWS(mi.invoke(std::as_const(cl))); - CHECK(mi.invoke(std::move(cl)).value() == 9); + CHECK(mi.invoke(std::move(cl)) == 9); CHECK_THROWS(mi.invoke(std::move(std::as_const(cl)))); } @@ -537,7 +537,7 @@ TEST_CASE("meta/meta_states/method") { CHECK_THROWS(mi.invoke(cl)); CHECK_THROWS(mi.invoke(std::as_const(cl))); - CHECK(mi.invoke(std::move(cl)).value() == 10); + CHECK(mi.invoke(std::move(cl)) == 10); CHECK_THROWS(mi.invoke(std::move(std::as_const(cl)))); } @@ -582,8 +582,8 @@ TEST_CASE("meta/meta_states/method") { CHECK_THROWS(mi.invoke(cl)); CHECK_THROWS(mi.invoke(std::as_const(cl))); - CHECK(mi.invoke(std::move(cl)).value() == 11); - CHECK(mi.invoke(std::move(std::as_const(cl))).value() == 11); + CHECK(mi.invoke(std::move(cl)) == 11); + CHECK(mi.invoke(std::move(std::as_const(cl))) == 11); } static_assert(!std::is_invocable_v); @@ -627,8 +627,8 @@ TEST_CASE("meta/meta_states/method") { CHECK_THROWS(mi.invoke(cl)); CHECK_THROWS(mi.invoke(std::as_const(cl))); - CHECK(mi.invoke(std::move(cl)).value() == 12); - CHECK(mi.invoke(std::move(std::as_const(cl))).value() == 12); + CHECK(mi.invoke(std::move(cl)) == 12); + CHECK(mi.invoke(std::move(std::as_const(cl))) == 12); } static_assert(!std::is_invocable_v); diff --git a/untests/meta_types/enum_type_tests.cpp b/untests/meta_types/enum_type_tests.cpp index b18cd0b..c50d603 100644 --- a/untests/meta_types/enum_type_tests.cpp +++ b/untests/meta_types/enum_type_tests.cpp @@ -120,42 +120,20 @@ TEST_CASE("meta/meta_types/enum_type") { const meta::enum_type color_type = meta::resolve_type(); REQUIRE(color_type); - { - REQUIRE(color_type.value_to_name(color::red)); - CHECK(color_type.value_to_name(color::red) == "red"); - } - - { - - REQUIRE(color_type.value_to_name(meta::value{color::blue})); - CHECK(color_type.value_to_name(color::blue) == "blue"); - } - - { - REQUIRE_FALSE(color_type.value_to_name(100500)); - REQUIRE_FALSE(color_type.value_to_name(color{100500})); - } + CHECK(color_type.value_to_name(color::red) == "red"); + CHECK(color_type.value_to_name(color::blue) == "blue"); + CHECK(color_type.value_to_name(100500).empty()); + CHECK(color_type.value_to_name(color{100500}).empty()); } SUBCASE("ecolor/value_to_name") { const meta::enum_type ecolor_type = meta::resolve_type(); REQUIRE(ecolor_type); - { - REQUIRE(ecolor_type.value_to_name(ecolor_red)); - CHECK(ecolor_type.value_to_name(ecolor_red) == "red"); - } - - { - - REQUIRE(ecolor_type.value_to_name(meta::value{ecolor_blue})); - CHECK(ecolor_type.value_to_name(ecolor_blue) == "blue"); - } - - { - REQUIRE_FALSE(ecolor_type.value_to_name(100500)); - REQUIRE_FALSE(ecolor_type.value_to_name(ecolor{100500})); - } + CHECK(ecolor_type.value_to_name(ecolor_red) == "red"); + CHECK(ecolor_type.value_to_name(meta::value{ecolor_blue}) == "blue"); + CHECK(ecolor_type.value_to_name(100500).empty()); + CHECK(ecolor_type.value_to_name(ecolor{100500}).empty()); } SUBCASE("color/name_to_value") {