mirror of
https://github.com/BlackMATov/meta.hpp.git
synced 2025-12-16 22:17:02 +07:00
fix uvalue deref traits
ref: https://github.com/BlackMATov/meta.hpp/issues/100
This commit is contained in:
@@ -107,11 +107,17 @@ namespace
|
|||||||
bool operator==(const ivec2_big& l, const ivec2_big& r) noexcept {
|
bool operator==(const ivec2_big& l, const ivec2_big& r) noexcept {
|
||||||
return l.x == r.x && l.y == r.y;
|
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)
|
||||||
META_HPP_DECLARE_COPY_TRAITS_FOR(ivec2_big)
|
META_HPP_DECLARE_COPY_TRAITS_FOR(ivec2_big)
|
||||||
|
|
||||||
|
META_HPP_DECLARE_DEREF_TRAITS_FOR(deref_custom_class)
|
||||||
|
|
||||||
TEST_CASE("meta/meta_utilities/value/_") {
|
TEST_CASE("meta/meta_utilities/value/_") {
|
||||||
namespace meta = meta_hpp;
|
namespace meta = meta_hpp;
|
||||||
|
|
||||||
@@ -645,6 +651,18 @@ TEST_CASE("meta/meta_utilities/value") {
|
|||||||
meta::uvalue v{std::make_unique<int>(42)};
|
meta::uvalue v{std::make_unique<int>(42)};
|
||||||
CHECK((*v).as<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") {
|
SUBCASE("less/equal") {
|
||||||
|
|||||||
@@ -9,6 +9,8 @@
|
|||||||
#include "../../meta_base.hpp"
|
#include "../../meta_base.hpp"
|
||||||
#include "../../meta_uvalue.hpp"
|
#include "../../meta_uvalue.hpp"
|
||||||
|
|
||||||
|
#include "copy_traits.hpp"
|
||||||
|
|
||||||
namespace meta_hpp::detail
|
namespace meta_hpp::detail
|
||||||
{
|
{
|
||||||
template < typename T >
|
template < typename T >
|
||||||
@@ -22,7 +24,7 @@ namespace meta_hpp::detail
|
|||||||
namespace meta_hpp::detail
|
namespace meta_hpp::detail
|
||||||
{
|
{
|
||||||
template < typename T >
|
template < typename T >
|
||||||
requires std::is_copy_constructible_v<T>
|
requires has_copy_traits<T>
|
||||||
struct deref_traits<T*> {
|
struct deref_traits<T*> {
|
||||||
uvalue operator()(T* v) const {
|
uvalue operator()(T* v) const {
|
||||||
return v != nullptr ? uvalue{*v} : uvalue{};
|
return v != nullptr ? uvalue{*v} : uvalue{};
|
||||||
@@ -30,18 +32,33 @@ namespace meta_hpp::detail
|
|||||||
};
|
};
|
||||||
|
|
||||||
template < typename T >
|
template < typename T >
|
||||||
requires std::is_copy_constructible_v<T>
|
requires has_copy_traits<T>
|
||||||
struct deref_traits<std::shared_ptr<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{};
|
return v != nullptr ? uvalue{*v} : uvalue{};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template < typename T, typename Deleter >
|
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>> {
|
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{};
|
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}; \
|
||||||
|
} \
|
||||||
|
}; \
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user