Skip to content

Commit

Permalink
meta_any:
Browse files Browse the repository at this point in the history
* introduce meta_any_policy
* deprecate ::owner member
* add ::policy member
  • Loading branch information
skypjack committed Jun 30, 2023
1 parent 020060f commit 0472949
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/entt/meta/meta.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ class meta_associative_container {
any storage{};
};

/*! @brief Possible modes of a meta any object. */
using meta_any_policy = any_policy;

/*! @brief Opaque wrapper for values of any type. */
class meta_any {
enum class operation : std::uint8_t {
Expand Down Expand Up @@ -197,7 +200,7 @@ class meta_any {
}

void release() {
if(node.dtor.dtor && owner()) {
if(node.dtor.dtor && (storage.policy() == any_policy::owner)) {
node.dtor.dtor(storage.data());
}
}
Expand Down Expand Up @@ -455,7 +458,7 @@ class meta_any {
*/
[[nodiscard]] bool allow_cast(const meta_type &type) {
if(auto other = std::as_const(*this).allow_cast(type); other) {
if(other.owner()) {
if((other.storage.policy() == any_policy::owner)) {
std::swap(*this, other);
}

Expand Down Expand Up @@ -593,8 +596,16 @@ class meta_any {
}

/*! @copydoc any::owner */
[[nodiscard]] bool owner() const noexcept {
return storage.owner();
[[deprecated("use policy() and meta_any_policy instead")]] [[nodiscard]] bool owner() const noexcept {
return (storage.policy() == any_policy::owner);
}

/**
* @brief Returns the current mode of a meta any object.
* @return The current mode of the meta any object.
*/
[[nodiscard]] meta_any_policy policy() const noexcept {
return storage.policy();
}

private:
Expand Down
7 changes: 7 additions & 0 deletions test/entt/meta/meta_any.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ TEST_F(MetaAny, SBO) {

ASSERT_TRUE(any);
ASSERT_TRUE(any.owner());
ASSERT_EQ(any.policy(), entt::meta_any_policy::owner);
ASSERT_FALSE(any.try_cast<std::size_t>());
ASSERT_EQ(any.cast<char>(), 'c');
ASSERT_NE(any.data(), nullptr);
Expand All @@ -123,6 +124,7 @@ TEST_F(MetaAny, NoSBO) {

ASSERT_TRUE(any);
ASSERT_TRUE(any.owner());
ASSERT_EQ(any.policy(), entt::meta_any_policy::owner);
ASSERT_FALSE(any.try_cast<std::size_t>());
ASSERT_EQ(any.cast<fat_t>(), instance);
ASSERT_NE(any.data(), nullptr);
Expand Down Expand Up @@ -168,6 +170,7 @@ TEST_F(MetaAny, SBOAsRefConstruction) {

ASSERT_TRUE(any);
ASSERT_FALSE(any.owner());
ASSERT_EQ(any.policy(), entt::meta_any_policy::ref);
ASSERT_EQ(any.type(), entt::resolve<int>());

ASSERT_FALSE(any.try_cast<std::size_t>());
Expand Down Expand Up @@ -204,6 +207,7 @@ TEST_F(MetaAny, SBOAsConstRefConstruction) {

ASSERT_TRUE(any);
ASSERT_FALSE(any.owner());
ASSERT_EQ(any.policy(), entt::meta_any_policy::cref);
ASSERT_EQ(any.type(), entt::resolve<int>());

ASSERT_FALSE(any.try_cast<std::size_t>());
Expand Down Expand Up @@ -436,6 +440,7 @@ TEST_F(MetaAny, NoSBOAsRefConstruction) {

ASSERT_TRUE(any);
ASSERT_FALSE(any.owner());
ASSERT_EQ(any.policy(), entt::meta_any_policy::ref);
ASSERT_EQ(any.type(), entt::resolve<fat_t>());

ASSERT_FALSE(any.try_cast<std::size_t>());
Expand Down Expand Up @@ -470,6 +475,7 @@ TEST_F(MetaAny, NoSBOAsConstRefConstruction) {

ASSERT_TRUE(any);
ASSERT_FALSE(any.owner());
ASSERT_EQ(any.policy(), entt::meta_any_policy::cref);
ASSERT_EQ(any.type(), entt::resolve<fat_t>());

ASSERT_FALSE(any.try_cast<std::size_t>());
Expand Down Expand Up @@ -707,6 +713,7 @@ TEST_F(MetaAny, VoidInPlaceTypeConstruction) {

ASSERT_TRUE(any);
ASSERT_TRUE(any.owner());
ASSERT_EQ(any.policy(), entt::meta_any_policy::owner);
ASSERT_FALSE(any.try_cast<char>());
ASSERT_EQ(any.data(), nullptr);
ASSERT_EQ(any.type(), entt::resolve<void>());
Expand Down

0 comments on commit 0472949

Please sign in to comment.