diff --git a/headers/meta.hpp/meta_uvalue.hpp b/headers/meta.hpp/meta_uvalue.hpp index d9101d9..e4600e9 100644 --- a/headers/meta.hpp/meta_uvalue.hpp +++ b/headers/meta.hpp/meta_uvalue.hpp @@ -76,9 +76,6 @@ namespace meta_hpp [[nodiscard]] auto try_get_as() const noexcept -> std::conditional_t, T, const T*>; - template < typename T > - [[nodiscard]] bool can_get_as() const noexcept; - friend bool operator<(const uvalue& l, const uvalue& r); friend bool operator==(const uvalue& l, const uvalue& r); friend std::istream& operator>>(std::istream& is, uvalue& v); diff --git a/headers/meta.hpp/meta_uvalue/uvalue.hpp b/headers/meta.hpp/meta_uvalue/uvalue.hpp index 5f4084e..0b8f693 100644 --- a/headers/meta.hpp/meta_uvalue/uvalue.hpp +++ b/headers/meta.hpp/meta_uvalue/uvalue.hpp @@ -489,23 +489,6 @@ namespace meta_hpp return nullptr; } - - template < typename T > - bool uvalue::can_get_as() const noexcept { - static_assert(std::is_same_v>); - - if constexpr ( detail::pointer_kind ) { - if ( T ptr = try_get_as(); ptr || get_type().is_nullptr() ) { - return true; - } - } else { - if ( const T* ptr = try_get_as() ) { - return true; - } - } - - return false; - } } namespace meta_hpp diff --git a/manuals/meta_examples/uvalue_example.cpp b/manuals/meta_examples/uvalue_example.cpp index def3347..e59c49d 100644 --- a/manuals/meta_examples/uvalue_example.cpp +++ b/manuals/meta_examples/uvalue_example.cpp @@ -75,9 +75,22 @@ TEST_CASE("meta/meta_examples/uvalue/usage") { // also, it supports upcasting for registered types CHECK(val.get_as().get_area() == 200); + // an exception will be thrown if we try to get a wrong type + CHECK_THROWS(std::ignore = val.get_as()); + + // but we can use try_get_as for safe access + CHECK(val.try_get_as()); + if ( shape* s = val.try_get_as() ) { + } + // upcasting is supported for pointers too rectangle rect{3, 5}; val = ▭ CHECK(val.get_as()->get_area() == 15); CHECK(val.get_type() == meta::resolve_type()); + + // but we can use try_get_as for pointers too + CHECK(val.try_get_as()); + if ( shape* s = val.try_get_as() ) { + } } diff --git a/untests/meta_utilities/value3_tests.cpp b/untests/meta_utilities/value3_tests.cpp index bef64a9..3543c37 100644 --- a/untests/meta_utilities/value3_tests.cpp +++ b/untests/meta_utilities/value3_tests.cpp @@ -324,107 +324,3 @@ TEST_CASE("meta/meta_utilities/value4/try_get_as") { } } } - -TEST_CASE("meta/meta_utilities/value4/can_get_as") { - namespace meta = meta_hpp; - - SUBCASE("derived to derived") { - { - meta::uvalue v{derived{}}; - CHECK(v.can_get_as()); - CHECK_FALSE(v.can_get_as()); - } - { - const meta::uvalue v{derived{}}; - CHECK(v.can_get_as()); - CHECK_FALSE(v.can_get_as()); - } - } - - SUBCASE("derived to base") { - { - meta::uvalue v{derived{}}; - CHECK(v.can_get_as()); - } - { - const meta::uvalue v{derived{}}; - CHECK(v.can_get_as()); - } - } - - SUBCASE("voidptr") { - { - derived d{}; - meta::uvalue v{&d}; - CHECK(v.can_get_as()); - CHECK(v.can_get_as()); - } - { - const derived d{}; - meta::uvalue v{&d}; - CHECK_FALSE(v.can_get_as()); - CHECK(v.can_get_as()); - } - { - meta::uvalue v{derived{}}; - CHECK_FALSE(v.can_get_as()); - CHECK_FALSE(v.can_get_as()); - } - } - - SUBCASE("nullptr") { - { - meta::uvalue v{nullptr}; - CHECK(v.can_get_as()); - CHECK(v.can_get_as()); - CHECK(v.can_get_as()); - CHECK(v.can_get_as()); - - CHECK_FALSE(v.can_get_as()); - } - { - const meta::uvalue v{nullptr}; - CHECK(v.can_get_as()); - CHECK(v.can_get_as()); - CHECK(v.can_get_as()); - CHECK(v.can_get_as()); - - CHECK_FALSE(v.can_get_as()); - } - } - - SUBCASE("derived* to derived*") { - { - derived d{}; - meta::uvalue v{&d}; - CHECK(v.can_get_as()); - CHECK(v.can_get_as()); - - CHECK_FALSE(v.can_get_as()); - CHECK_FALSE(v.can_get_as()); - } - { - const derived d{}; - meta::uvalue v{&d}; - CHECK(v.can_get_as()); - - CHECK_FALSE(v.can_get_as()); - } - } - - SUBCASE("derived* to base*") { - { - derived d{}; - meta::uvalue v{&d}; - CHECK(v.can_get_as()); - CHECK(v.can_get_as()); - } - - { - const derived d{}; - meta::uvalue v{&d}; - CHECK_FALSE(v.can_get_as()); - CHECK(v.can_get_as()); - } - } -} diff --git a/untests/meta_utilities/value_tests.cpp b/untests/meta_utilities/value_tests.cpp index 256a005..3539bb5 100644 --- a/untests/meta_utilities/value_tests.cpp +++ b/untests/meta_utilities/value_tests.cpp @@ -133,8 +133,8 @@ TEST_CASE("meta/meta_utilities/value") { CHECK_FALSE(*val); CHECK_FALSE(val[0]); - CHECK_FALSE(val.can_get_as()); - CHECK_FALSE(std::as_const(val).can_get_as()); + CHECK_FALSE(val.try_get_as()); + CHECK_FALSE(std::as_const(val).try_get_as()); CHECK_THROWS(std::ignore = val.get_as()); CHECK_THROWS(std::ignore = std::as_const(val).get_as()); @@ -207,8 +207,8 @@ TEST_CASE("meta/meta_utilities/value") { CHECK(val.get_as() == ivec2{1,2}); CHECK(std::as_const(val).get_as() == ivec2{1,2}); - CHECK_FALSE(val.can_get_as()); - CHECK_FALSE(std::as_const(val).can_get_as()); + CHECK_FALSE(val.try_get_as()); + CHECK_FALSE(std::as_const(val).try_get_as()); } SUBCASE("const ivec2&") { @@ -241,8 +241,8 @@ TEST_CASE("meta/meta_utilities/value") { CHECK(val.get_as() == ivec2{1,2}); CHECK(std::as_const(val).get_as() == ivec2{1,2}); - CHECK_FALSE(val.can_get_as()); - CHECK_FALSE(std::as_const(val).can_get_as()); + CHECK_FALSE(val.try_get_as()); + CHECK_FALSE(std::as_const(val).try_get_as()); } SUBCASE("ivec2&&") { @@ -269,8 +269,8 @@ TEST_CASE("meta/meta_utilities/value") { CHECK(val.get_as() == ivec2{1,2}); CHECK(std::as_const(val).get_as() == ivec2{1,2}); - CHECK_FALSE(val.can_get_as()); - CHECK_FALSE(std::as_const(val).can_get_as()); + CHECK_FALSE(val.try_get_as()); + CHECK_FALSE(std::as_const(val).try_get_as()); } SUBCASE("const ivec2&&") { @@ -297,8 +297,8 @@ TEST_CASE("meta/meta_utilities/value") { CHECK(val.get_as() == ivec2{1,2}); CHECK(std::as_const(val).get_as() == ivec2{1,2}); - CHECK_FALSE(val.can_get_as()); - CHECK_FALSE(std::as_const(val).can_get_as()); + CHECK_FALSE(val.try_get_as()); + CHECK_FALSE(std::as_const(val).try_get_as()); } SUBCASE("value(value&&)") {