fix uvalue index traits

ref: https://github.com/BlackMATov/meta.hpp/issues/100
This commit is contained in:
BlackMATov
2024-09-05 07:28:05 +07:00
parent 41855b3ed7
commit 774d0c04c2
3 changed files with 44 additions and 6 deletions

View File

@@ -46,6 +46,8 @@ namespace
};
}
META_HPP_DECLARE_COPY_TRAITS_FOR(clazz_throw_dtor)
TEST_CASE("meta/meta_utilities/value4") {
namespace meta = meta_hpp;

View File

@@ -38,6 +38,10 @@ namespace
return *this;
}
int operator[](std::size_t i) const noexcept {
return i == 0 ? x : y;
}
ivec2& operator=(ivec2&& other) = delete;
ivec2& operator=(const ivec2& other) = delete;
public:
@@ -118,6 +122,8 @@ META_HPP_DECLARE_COPY_TRAITS_FOR(ivec2_big)
META_HPP_DECLARE_DEREF_TRAITS_FOR(deref_custom_class)
META_HPP_DECLARE_INDEX_TRAITS_FOR(ivec2)
TEST_CASE("meta/meta_utilities/value/_") {
namespace meta = meta_hpp;
@@ -978,6 +984,23 @@ TEST_CASE("meta/meta_utilities/value/arrays") {
CHECK(v[2].as<int>() == 3);
CHECK_FALSE(v[3]);
}
SUBCASE("ivec2/ivec3") {
{
meta::uvalue v{ivec2{1,2}};
CHECK(v.get_type() == meta::resolve_type<ivec2>());
CHECK(v.has_index_op());
CHECK(v[0].as<int>() == 1);
CHECK(v[1].as<int>() == 2);
}
{
meta::uvalue v{ivec3{1,2,3}};
CHECK(v.get_type() == meta::resolve_type<ivec3>());
CHECK_FALSE(v.has_index_op());
CHECK_THROWS(std::ignore = v[0]);
}
}
}
TEST_CASE("meta/meta_utilities/value/functions") {

View File

@@ -9,6 +9,8 @@
#include "../../meta_base.hpp"
#include "../../meta_uvalue.hpp"
#include "copy_traits.hpp"
namespace meta_hpp::detail
{
template < typename T >
@@ -22,7 +24,7 @@ namespace meta_hpp::detail
namespace meta_hpp::detail
{
template < typename T >
requires std::is_copy_constructible_v<T>
requires has_copy_traits<T>
struct index_traits<T*> {
uvalue operator()(T* v, std::size_t i) const {
// NOLINTNEXTLINE(*-pointer-arithmetic)
@@ -31,7 +33,7 @@ namespace meta_hpp::detail
};
template < typename T, std::size_t Size >
requires std::is_copy_constructible_v<T>
requires has_copy_traits<T>
struct index_traits<std::array<T, Size>> {
uvalue operator()(const std::array<T, Size>& v, std::size_t i) const {
return i < v.size() ? uvalue{v[i]} : uvalue{};
@@ -39,7 +41,7 @@ namespace meta_hpp::detail
};
template < typename T, typename Traits, typename Allocator >
requires std::is_copy_constructible_v<T>
requires has_copy_traits<T>
struct index_traits<std::basic_string<T, Traits, Allocator>> {
uvalue operator()(const std::basic_string<T, Traits, Allocator>& v, std::size_t i) const {
return i < v.size() ? uvalue{v[i]} : uvalue{};
@@ -47,7 +49,7 @@ namespace meta_hpp::detail
};
template < typename T, typename Traits >
requires std::is_copy_constructible_v<T>
requires has_copy_traits<T>
struct index_traits<std::basic_string_view<T, Traits>> {
uvalue operator()(const std::basic_string_view<T, Traits>& v, std::size_t i) const {
return i < v.size() ? uvalue{v[i]} : uvalue{};
@@ -55,7 +57,7 @@ namespace meta_hpp::detail
};
template < typename T, std::size_t Extent >
requires std::is_copy_constructible_v<T>
requires has_copy_traits<T>
struct index_traits<std::span<T, Extent>> {
uvalue operator()(const std::span<T, Extent>& v, std::size_t i) const {
return i < v.size() ? uvalue{v[i]} : uvalue{};
@@ -63,10 +65,21 @@ namespace meta_hpp::detail
};
template < typename T, typename Allocator >
requires std::is_copy_constructible_v<T>
requires has_copy_traits<T>
struct index_traits<std::vector<T, Allocator>> {
uvalue operator()(const std::vector<T, Allocator>& v, std::size_t i) const {
return i < v.size() ? uvalue{v[i]} : uvalue{};
}
};
}
#define META_HPP_DECLARE_INDEX_TRAITS_FOR(T) \
namespace meta_hpp::detail \
{ \
template <> \
struct index_traits<T> { \
uvalue operator()(const T& v, std::size_t i) const { \
return uvalue{v[i]}; \
} \
}; \
}