-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #89 from BlackMATov/dev
Dev
- Loading branch information
Showing
11 changed files
with
121 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(component) | ||
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::class_type derived_instance_type = derived_instance_ptr_type.as_pointer().get_data_type().as_class(); | ||
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.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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters