mirror of
https://github.com/BlackMATov/meta.hpp.git
synced 2025-12-16 14:09:02 +07:00
new meta_poly_ptr method in META_HPP_ENABLE_POLY_INFO macro
This commit is contained in:
@@ -3,6 +3,7 @@
|
|||||||
## Backlog
|
## Backlog
|
||||||
|
|
||||||
- add coverage information
|
- add coverage information
|
||||||
|
- add an ucast function with a dynamic `to` argument
|
||||||
|
|
||||||
## Version 1.0
|
## Version 1.0
|
||||||
|
|
||||||
|
|||||||
@@ -18,11 +18,15 @@ namespace
|
|||||||
// like `ucast` or `resolve_type(T&&)`
|
// like `ucast` or `resolve_type(T&&)`
|
||||||
|
|
||||||
struct A {
|
struct A {
|
||||||
|
A() = default;
|
||||||
|
A(const A&) = default;
|
||||||
virtual ~A() = default;
|
virtual ~A() = default;
|
||||||
META_HPP_ENABLE_POLY_INFO()
|
META_HPP_ENABLE_POLY_INFO()
|
||||||
};
|
};
|
||||||
|
|
||||||
struct B {
|
struct B {
|
||||||
|
B() = default;
|
||||||
|
B(const B&) = default;
|
||||||
virtual ~B() = default;
|
virtual ~B() = default;
|
||||||
META_HPP_ENABLE_POLY_INFO()
|
META_HPP_ENABLE_POLY_INFO()
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -4679,6 +4679,12 @@ private:
|
|||||||
META_HPP_ENABLE_BASE_INFO(__VA_ARGS__) \
|
META_HPP_ENABLE_BASE_INFO(__VA_ARGS__) \
|
||||||
public: \
|
public: \
|
||||||
META_HPP_DETAIL_IGNORE_OVERRIDE_WARNINGS_PUSH() \
|
META_HPP_DETAIL_IGNORE_OVERRIDE_WARNINGS_PUSH() \
|
||||||
|
virtual ::meta_hpp::uvalue meta_poly_ptr() { \
|
||||||
|
return ::meta_hpp::uvalue{this}; \
|
||||||
|
} \
|
||||||
|
virtual ::meta_hpp::uvalue meta_poly_ptr() const { \
|
||||||
|
return ::meta_hpp::uvalue{this}; \
|
||||||
|
} \
|
||||||
virtual ::meta_hpp::detail::poly_info meta_poly_info(::meta_hpp::detail::type_registry& registry) const { \
|
virtual ::meta_hpp::detail::poly_info meta_poly_info(::meta_hpp::detail::type_registry& registry) const { \
|
||||||
using self_type = std::remove_cvref_t<decltype(*this)>; \
|
using self_type = std::remove_cvref_t<decltype(*this)>; \
|
||||||
return ::meta_hpp::detail::poly_info{.ptr = this, .type = registry.resolve_by_type<self_type>()}; \
|
return ::meta_hpp::detail::poly_info{.ptr = this, .type = registry.resolve_by_type<self_type>()}; \
|
||||||
|
|||||||
74
develop/untests/meta_issues/github_discussion_88.cpp
Normal file
74
develop/untests/meta_issues/github_discussion_88.cpp
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
/*******************************************************************************
|
||||||
|
* This file is part of the "https://github.com/blackmatov/meta.hpp"
|
||||||
|
* For conditions of distribution and use, see copyright notice in LICENSE.md
|
||||||
|
* Copyright (C) 2021-2024, by Matvey Cherevko (blackmatov@gmail.com)
|
||||||
|
******************************************************************************/
|
||||||
|
|
||||||
|
#include <meta.hpp/meta_all.hpp>
|
||||||
|
#include <doctest/doctest.h>
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
struct component {
|
||||||
|
component() = default;
|
||||||
|
component(const component&) = default;
|
||||||
|
virtual ~component() = default;
|
||||||
|
META_HPP_ENABLE_POLY_INFO()
|
||||||
|
};
|
||||||
|
|
||||||
|
struct position_component : component {
|
||||||
|
META_HPP_ENABLE_POLY_INFO()
|
||||||
|
public:
|
||||||
|
explicit position_component(int nx, int ny) : x{nx}, y{ny} {}
|
||||||
|
|
||||||
|
int x{};
|
||||||
|
int y{};
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("meta/meta_discussion/88") {
|
||||||
|
namespace meta = meta_hpp;
|
||||||
|
|
||||||
|
meta::class_<position_component>()
|
||||||
|
.member_("x", &position_component::x, meta::member_policy::as_pointer)
|
||||||
|
.member_("y", &position_component::y, meta::member_policy::as_pointer);
|
||||||
|
|
||||||
|
position_component position{21, 42};
|
||||||
|
|
||||||
|
std::vector<component*> components{&position};
|
||||||
|
|
||||||
|
for (component* c : components) {
|
||||||
|
// meta_poly_ptr returns a pointer of the most derived type (position_component* in this case)
|
||||||
|
meta::uvalue derived_instance_ptr = c->meta_poly_ptr();
|
||||||
|
meta::any_type derived_instance_ptr_type = derived_instance_ptr.get_type();
|
||||||
|
CHECK(derived_instance_ptr_type == meta::resolve_type<position_component*>());
|
||||||
|
|
||||||
|
// to get all members of the component we should extract a class type from the pointer (position_component)
|
||||||
|
meta::any_type derived_instance_type = derived_instance_ptr_type.as_pointer().get_data_type();
|
||||||
|
CHECK(derived_instance_type == meta::resolve_type<position_component>());
|
||||||
|
|
||||||
|
// for each member we can show our debug inspector (imgui input for example)
|
||||||
|
for (const meta::member& m : derived_instance_type.as_class().get_members()) {
|
||||||
|
// we registered members as pointers (member_policy::as_pointer), so the type of the member is a pointer
|
||||||
|
CHECK(m.get_type() == meta::resolve_type<int position_component::*>());
|
||||||
|
CHECK(m.get_type().get_value_type() == meta::resolve_type<int>());
|
||||||
|
|
||||||
|
// Note:
|
||||||
|
// We can register members without any policy then member::get will return copy of this member value,
|
||||||
|
// and to change this member we will have to call member::set function.
|
||||||
|
// But in this case we will change values in-place by a pointer
|
||||||
|
|
||||||
|
meta::uvalue value = m.get(derived_instance_ptr);
|
||||||
|
CHECK(value.get_type() == meta::resolve_type<int*>());
|
||||||
|
|
||||||
|
int* raw_value = value.as<int*>();
|
||||||
|
|
||||||
|
// here we can show the value on the screen or change it
|
||||||
|
CHECK((*raw_value == 21 || *raw_value == 42));
|
||||||
|
*raw_value = 84;
|
||||||
|
}
|
||||||
|
|
||||||
|
CHECK(position.x == 84);
|
||||||
|
CHECK(position.y == 84);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -13,6 +13,8 @@ namespace
|
|||||||
struct derived : base {};
|
struct derived : base {};
|
||||||
|
|
||||||
struct poly_base {
|
struct poly_base {
|
||||||
|
poly_base() = default;
|
||||||
|
poly_base(const poly_base&) = default;
|
||||||
virtual ~poly_base() = default;
|
virtual ~poly_base() = default;
|
||||||
META_HPP_ENABLE_POLY_INFO()
|
META_HPP_ENABLE_POLY_INFO()
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -9,6 +9,8 @@
|
|||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
struct A {
|
struct A {
|
||||||
|
A() = default;
|
||||||
|
A(const A&) = default;
|
||||||
virtual ~A() = default;
|
virtual ~A() = default;
|
||||||
META_HPP_ENABLE_POLY_INFO()
|
META_HPP_ENABLE_POLY_INFO()
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -8,6 +8,7 @@
|
|||||||
|
|
||||||
#include "../meta_base.hpp"
|
#include "../meta_base.hpp"
|
||||||
#include "../meta_types.hpp"
|
#include "../meta_types.hpp"
|
||||||
|
#include "../meta_uvalue.hpp"
|
||||||
|
|
||||||
#include "type_registry.hpp"
|
#include "type_registry.hpp"
|
||||||
|
|
||||||
@@ -45,6 +46,12 @@ private:
|
|||||||
META_HPP_ENABLE_BASE_INFO(__VA_ARGS__) \
|
META_HPP_ENABLE_BASE_INFO(__VA_ARGS__) \
|
||||||
public: \
|
public: \
|
||||||
META_HPP_DETAIL_IGNORE_OVERRIDE_WARNINGS_PUSH() \
|
META_HPP_DETAIL_IGNORE_OVERRIDE_WARNINGS_PUSH() \
|
||||||
|
virtual ::meta_hpp::uvalue meta_poly_ptr() { \
|
||||||
|
return ::meta_hpp::uvalue{this}; \
|
||||||
|
} \
|
||||||
|
virtual ::meta_hpp::uvalue meta_poly_ptr() const { \
|
||||||
|
return ::meta_hpp::uvalue{this}; \
|
||||||
|
} \
|
||||||
virtual ::meta_hpp::detail::poly_info meta_poly_info(::meta_hpp::detail::type_registry& registry) const { \
|
virtual ::meta_hpp::detail::poly_info meta_poly_info(::meta_hpp::detail::type_registry& registry) const { \
|
||||||
using self_type = std::remove_cvref_t<decltype(*this)>; \
|
using self_type = std::remove_cvref_t<decltype(*this)>; \
|
||||||
return ::meta_hpp::detail::poly_info{.ptr = this, .type = registry.resolve_by_type<self_type>()}; \
|
return ::meta_hpp::detail::poly_info{.ptr = this, .type = registry.resolve_by_type<self_type>()}; \
|
||||||
|
|||||||
Reference in New Issue
Block a user