fix uvalue deref traits

ref: https://github.com/BlackMATov/meta.hpp/issues/100
This commit is contained in:
BlackMATov
2024-09-05 07:18:15 +07:00
parent 5d5ba52045
commit 41855b3ed7
2 changed files with 40 additions and 5 deletions

View File

@@ -107,11 +107,17 @@ namespace
bool operator==(const ivec2_big& l, const ivec2_big& r) noexcept {
return l.x == r.x && l.y == r.y;
}
struct deref_custom_class {
int operator*() const { return 42; }
};
}
META_HPP_DECLARE_COPY_TRAITS_FOR(ivec2)
META_HPP_DECLARE_COPY_TRAITS_FOR(ivec2_big)
META_HPP_DECLARE_DEREF_TRAITS_FOR(deref_custom_class)
TEST_CASE("meta/meta_utilities/value/_") {
namespace meta = meta_hpp;
@@ -645,6 +651,18 @@ TEST_CASE("meta/meta_utilities/value") {
meta::uvalue v{std::make_unique<int>(42)};
CHECK((*v).as<int>() == 42);
}
{
CHECK(meta::uvalue{std::make_shared<std::vector<std::shared_ptr<int>>>()}.has_deref_op());
CHECK_FALSE(meta::uvalue{std::make_shared<std::vector<std::unique_ptr<int>>>()}.has_deref_op());
}
{
const meta::uvalue v{deref_custom_class{}};
CHECK(v.has_deref_op());
const meta::uvalue u = *v;
CHECK(u.get_type() == meta::resolve_type<int>());
CHECK(u.as<int>() == 42);
}
}
SUBCASE("less/equal") {

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 deref_traits<T*> {
uvalue operator()(T* v) const {
return v != nullptr ? uvalue{*v} : uvalue{};
@@ -30,18 +32,33 @@ namespace meta_hpp::detail
};
template < typename T >
requires std::is_copy_constructible_v<T>
requires has_copy_traits<T>
struct deref_traits<std::shared_ptr<T>> {
uvalue operator()(const std::shared_ptr<T>& v) const {
using value_t = std::shared_ptr<T>;
uvalue operator()(const value_t& v) const {
return v != nullptr ? uvalue{*v} : uvalue{};
}
};
template < typename T, typename Deleter >
requires std::is_copy_constructible_v<T>
requires has_copy_traits<T>
struct deref_traits<std::unique_ptr<T, Deleter>> {
uvalue operator()(const std::unique_ptr<T, Deleter>& v) const {
using value_t = std::unique_ptr<T, Deleter>;
uvalue operator()(const value_t& v) const {
return v != nullptr ? uvalue{*v} : uvalue{};
}
};
}
#define META_HPP_DECLARE_DEREF_TRAITS_FOR(T) \
namespace meta_hpp::detail \
{ \
template <> \
struct deref_traits<T> { \
uvalue operator()(const T& v) const { \
return uvalue{*v}; \
} \
}; \
}