rewrite uvalue traits

This commit is contained in:
BlackMATov
2023-12-30 12:32:22 +07:00
parent e25af84b3f
commit 50d6ab4974
8 changed files with 21 additions and 158 deletions

View File

@@ -10,11 +10,11 @@
#include <array>
#include <cassert>
#include <compare>
#include <concepts>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <exception>
#include <functional>
#include <initializer_list>
@@ -8991,7 +8991,7 @@ namespace meta_hpp::detail
namespace meta_hpp::detail
{
template < typename T >
requires std::is_copy_constructible_v<T>
requires requires(const T& v) { uvalue{v}; }
struct copy_traits<T> {
uvalue operator()(const T& v) const {
return uvalue{v};
@@ -9014,26 +9014,10 @@ namespace meta_hpp::detail
namespace meta_hpp::detail
{
template < typename T >
requires std::is_copy_constructible_v<T>
struct deref_traits<T*> {
uvalue operator()(T* v) const {
return v != nullptr ? uvalue{*v} : uvalue{};
}
};
template < typename T >
requires std::is_copy_constructible_v<T>
struct deref_traits<std::shared_ptr<T>> {
uvalue operator()(const std::shared_ptr<T>& v) const {
return v != nullptr ? uvalue{*v} : uvalue{};
}
};
template < typename T >
requires std::is_copy_constructible_v<T>
struct deref_traits<std::unique_ptr<T>> {
uvalue operator()(const std::unique_ptr<T>& v) const {
return v != nullptr ? uvalue{*v} : uvalue{};
requires requires(const T& v) { uvalue{*v}; }
struct deref_traits<T> {
uvalue operator()(const T& v) const {
return uvalue{*v};
}
};
}
@@ -9053,51 +9037,10 @@ namespace meta_hpp::detail
namespace meta_hpp::detail
{
template < typename T >
requires std::is_copy_constructible_v<T>
struct index_traits<T*> {
uvalue operator()(T* v, std::size_t i) const {
// NOLINTNEXTLINE(*-pointer-arithmetic)
return v != nullptr ? uvalue{v[i]} : uvalue{};
}
};
template < typename T, std::size_t Size >
requires std::is_copy_constructible_v<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{};
}
};
template < typename T, typename Allocator >
requires std::is_copy_constructible_v<T>
struct index_traits<std::deque<T, Allocator>> {
uvalue operator()(const std::deque<T, Allocator>& v, std::size_t i) {
return i < v.size() ? uvalue{v[i]} : uvalue{};
}
};
template < typename T, std::size_t Extent >
requires std::is_copy_constructible_v<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{};
}
};
template < typename T, typename Traits, typename Allocator >
requires std::is_copy_constructible_v<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{};
}
};
template < typename T, typename Allocator >
requires std::is_copy_constructible_v<T>
struct index_traits<std::vector<T, Allocator>> {
uvalue operator()(const std::vector<T, Allocator>& v, std::size_t i) {
return i < v.size() ? uvalue{v[i]} : uvalue{};
requires requires(const T& v, std::size_t i) { uvalue{v[i]}; }
struct index_traits<T> {
uvalue operator()(const T& v, std::size_t i) const {
return uvalue{v[i]};
}
};
}

View File

@@ -34,7 +34,7 @@ TEST_CASE("meta/meta_issues/random/3") {
{
meta::uvalue v{&int_func};
CHECK_FALSE(v.has_deref_op());
CHECK(v.has_deref_op());
CHECK(v.get_type() == meta::resolve_type<int(*)()>());
CHECK((v.as<decltype(&int_func)>() == &int_func));
}

View File

@@ -584,9 +584,6 @@ TEST_CASE("meta/meta_utilities/value") {
CHECK(meta::uvalue{p1}.has_deref_op());
CHECK(meta::uvalue{p2}.has_deref_op());
CHECK_FALSE(*meta::uvalue{p1});
CHECK_FALSE(*meta::uvalue{p2});
}
{
ivec2 v{1,2};
@@ -657,8 +654,6 @@ TEST_CASE("meta/meta_utilities/value/arrays") {
meta::uvalue v{arr};
CHECK(v.get_type() == meta::resolve_type<int*>());
CHECK(v.has_index_op());
CHECK_FALSE(v[0]);
}
}
@@ -678,8 +673,6 @@ TEST_CASE("meta/meta_utilities/value/arrays") {
meta::uvalue v{arr};
CHECK(v.get_type() == meta::resolve_type<const int*>());
CHECK(v.has_index_op());
CHECK_FALSE(v[0]);
}
}
@@ -691,18 +684,6 @@ TEST_CASE("meta/meta_utilities/value/arrays") {
CHECK(v[0].as<int>() == 1);
CHECK(v[1].as<int>() == 2);
CHECK(v[2].as<int>() == 3);
CHECK_FALSE(v[3]);
}
SUBCASE("std::deque") {
const meta::uvalue v{std::deque{1,2,3}};
CHECK(v.get_type() == meta::resolve_type<std::deque<int>>());
CHECK(v.has_index_op());
CHECK(v[0].as<int>() == 1);
CHECK(v[1].as<int>() == 2);
CHECK(v[2].as<int>() == 3);
CHECK_FALSE(v[3]);
}
SUBCASE("std::string") {
@@ -713,7 +694,6 @@ TEST_CASE("meta/meta_utilities/value/arrays") {
CHECK(v[0].as<char>() == 'h');
CHECK(v[1].as<char>() == 'i');
CHECK(v[2].as<char>() == '!');
CHECK_FALSE(v[3]);
}
SUBCASE("std::span") {
@@ -725,7 +705,6 @@ TEST_CASE("meta/meta_utilities/value/arrays") {
CHECK(v[0].as<int>() == 1);
CHECK(v[1].as<int>() == 2);
CHECK(v[2].as<int>() == 3);
CHECK_FALSE(v[3]);
}
SUBCASE("std::vector") {
@@ -736,7 +715,6 @@ TEST_CASE("meta/meta_utilities/value/arrays") {
CHECK(v[0].as<int>() == 1);
CHECK(v[1].as<int>() == 2);
CHECK(v[2].as<int>() == 3);
CHECK_FALSE(v[3]);
}
}