mutable handle

This commit is contained in:
Michele Caini
2019-05-21 15:02:30 +02:00
parent c126b64892
commit d4a64e93e0
3 changed files with 38 additions and 21 deletions

View File

@@ -26,6 +26,7 @@ Kerndog73
Paolo-Oliverio
pgruenbacher
prowolf
The5-1
vblanco20-1
willtunnels
WizardIke

View File

@@ -51,30 +51,25 @@ public:
return *resource;
}
/**
* @brief Casts a handle and gets a reference to the managed resource.
*
* @warning
* The behavior is undefined if the handle doesn't contain a resource.<br/>
* An assertion will abort the execution at runtime in debug mode if the
* handle is empty.
*/
inline operator const Resource &() const ENTT_NOEXCEPT { return get(); }
/*! @copydoc get */
Resource & get() ENTT_NOEXCEPT {
return const_cast<Resource &>(std::as_const(*this).get());
}
/**
* @brief Dereferences a handle to obtain the managed resource.
*
* @warning
* The behavior is undefined if the handle doesn't contain a resource.<br/>
* An assertion will abort the execution at runtime in debug mode if the
* handle is empty.
*
* @return A reference to the managed resource.
*/
/*! @copydoc get */
inline operator const Resource & () const ENTT_NOEXCEPT { return get(); }
/*! @copydoc get */
inline operator Resource & () ENTT_NOEXCEPT { return get(); }
/*! @copydoc get */
inline const Resource & operator *() const ENTT_NOEXCEPT { return get(); }
/*! @copydoc get */
inline Resource & operator *() ENTT_NOEXCEPT { return get(); }
/**
* @brief Gets a pointer to the managed resource from a handle.
* @brief Gets a pointer to the managed resource.
*
* @warning
* The behavior is undefined if the handle doesn't contain a resource.<br/>
@@ -89,6 +84,11 @@ public:
return resource.get();
}
/*! @copydoc operator-> */
inline Resource * operator->() ENTT_NOEXCEPT {
return const_cast<Resource *>(std::as_const(*this).operator->());
}
/**
* @brief Returns true if a handle contains a resource, false otherwise.
* @return True if the handle contains a resource, false otherwise.

View File

@@ -2,7 +2,7 @@
#include <gtest/gtest.h>
#include <entt/resource/cache.hpp>
struct resource { const int value; };
struct resource { int value; };
struct loader: entt::resource_loader<loader, resource> {
std::shared_ptr<resource> load(int value) const {
@@ -91,3 +91,19 @@ TEST(Resource, Functionalities) {
ASSERT_TRUE(std::is_copy_assignable_v<entt::resource_handle<resource>>);
ASSERT_TRUE(std::is_move_assignable_v<entt::resource_handle<resource>>);
}
TEST(Resource, MutableHandle) {
entt::resource_cache<resource> cache;
constexpr auto hs = entt::hashed_string{"res"};
auto handle = cache.load<loader>(hs, 0);
ASSERT_TRUE(handle);
++handle.get().value;
++static_cast<resource &>(handle).value;
++(*handle).value;
++handle->value;
ASSERT_EQ(cache.handle(hs)->value, 4);
}