Compare commits

...

9 Commits

Author SHA1 Message Date
Michele Caini
3a3a2c2702 doc: updated links and references to similar projects 2020-09-22 08:03:55 +02:00
Michele Caini
be38919758 doc: removed references to entt::component (close #557) 2020-09-22 08:03:44 +02:00
Michele Caini
a816ccf6ee meta_any: support for pointer-like types to non-const, non-copyable types (close #556) 2020-09-22 08:03:39 +02:00
Michele Caini
5e85068e35 meta: @Innokentiy-Alaytsev was right and I was not :) 2020-09-22 08:03:33 +02:00
Michele Caini
77865ab4a1 meta: update doc + minor stylistic changes 2020-09-22 08:03:28 +02:00
Innokentiy Alaytsev
5fc6ee27f6 Fixed trailing spaces
Signed-off-by: Innokentiy Alaytsev <alaitsev@gmail.com>
2020-09-22 08:03:21 +02:00
Innokentiy Alaytsev
5995adf8b6 Implemented shortcuts for meta-functions and meta-data
The shortcut for meta-functions allows invoking them directly from an instance
of meta_type or meta_any. If the meta-function was not found or the arguments
don't match its signature an invalid meta_any object is returned.

The shortcut for meta-data allows getting and setting meta-data directly from an
instance of meta_type of meta_any. If the meta_data was not found or
getting/setting it failed and invalid meta_any object is returned.

Signed-off-by: Innokentiy Alaytsev <alaitsev@gmail.com>
2020-09-22 08:03:12 +02:00
Michele Caini
c9f47ed89a sparse_set: minor changes 2020-09-22 08:02:23 +02:00
Michele Caini
d6325ffcfa now working on v3.5.2 2020-09-22 08:02:04 +02:00
9 changed files with 229 additions and 16 deletions

View File

@@ -1281,7 +1281,7 @@ thrown away. The reasons for this go far beyond the scope of this document.<br/>
To iterate a runtime view, either use it in a range-for loop:
```cpp
entt::component types[] = { entt::type_info<position>::id(), entt::type_info<velocity>::id() };
entt::id_type types[] = { entt::type_info<position>::id(), entt::type_info<velocity>::id() };
auto view = registry.runtime_view(std::cbegin(types), std::cend(types));
for(auto entity: view) {
@@ -1299,7 +1299,7 @@ for(auto entity: view) {
Or rely on the `each` member function to iterate entities:
```cpp
entt::component types[] = { entt::type_info<position>::id(), entt::type_info<velocity>::id() };
entt::id_type types[] = { entt::type_info<position>::id(), entt::type_info<velocity>::id() };
registry.runtime_view(std::cbegin(types), std::cend(types)).each([](auto entity) {
// ...
@@ -1310,8 +1310,8 @@ Performance are exactly the same in both cases.<br/>
Filtering entities by components is also supported for this kind of views:
```cpp
entt::component components[] = { entt::type_info<position>::id() };
entt::component filter[] = { entt::type_info<velocity>::id() };
entt::id_type components[] = { entt::type_info<position>::id() };
entt::id_type filter[] = { entt::type_info<velocity>::id() };
auto view = registry.runtime_view(std::cbegin(components), std::cend(components), std::cbegin(filter), std::cend(filter));
```

View File

@@ -117,6 +117,7 @@ I hope this list can grow much more in the future:
- [Native Scripting](https://www.youtube.com/watch?v=iIUhg88MK5M).
- [Native Scripting (now with virtual functions!)](https://www.youtube.com/watch?v=1cHEcrIn8IQ).
- [Scene Hierarchy Panel](https://www.youtube.com/watch?v=wziDnE8guvI).
- [Properties Panel](https://www.youtube.com/watch?v=NBpB0qscF3E).
* [Ability Creator](https://www.erichildebrand.net/blog/ability-creator-project-retrospect):
project retrospect by [Eric Hildebrand](https://www.erichildebrand.net/).

View File

@@ -42,5 +42,8 @@ I hope this list can grow much more in the future:
* [Specs](https://github.com/amethyst/specs): a parallel ECS based mainly on
hierarchical bitsets that allows different types of storage as needed.
* Zig
* [zig-ecs](https://github.com/prime31/zig-ecs): a _zig-ification_ of `EnTT`.
If you know of other resources out there that can be of interest for the reader,
feel free to open an issue or a PR and I'll be glad to add them to this page.

View File

@@ -4,7 +4,7 @@
#define ENTT_VERSION_MAJOR 3
#define ENTT_VERSION_MINOR 5
#define ENTT_VERSION_PATCH 1
#define ENTT_VERSION_PATCH 2
#endif

View File

@@ -386,7 +386,7 @@ public:
*/
void emplace(const entity_type entt) {
ENTT_ASSERT(!contains(entt));
assure(page(entt))[offset(entt)] = entity_type(static_cast<typename traits_type::entity_type>(packed.size()));
assure(page(entt))[offset(entt)] = entity_type{static_cast<typename traits_type::entity_type>(packed.size())};
packed.push_back(entt);
}
@@ -411,7 +411,7 @@ public:
while(first != last) {
const auto entt = *(first++);
ENTT_ASSERT(!contains(entt));
assure(page(entt))[offset(entt)] = entity_type(next++);
assure(page(entt))[offset(entt)] = entity_type{next++};
}
}
@@ -505,7 +505,7 @@ public:
algo(from, to, std::move(compare), std::forward<Args>(args)...);
for(size_type pos = skip, end = skip+length; pos < end; ++pos) {
sparse[page(packed[pos])][offset(packed[pos])] = entity_type(static_cast<typename traits_type::entity_type>(pos));
sparse[page(packed[pos])][offset(packed[pos])] = entity_type{static_cast<typename traits_type::entity_type>(pos)};
}
}
@@ -554,7 +554,7 @@ public:
while(curr != next) {
apply(packed[curr], packed[next]);
sparse[page(packed[curr])][offset(packed[curr])] = entity_type(static_cast<typename traits_type::entity_type>(curr));
sparse[page(packed[curr])][offset(packed[curr])] = entity_type{static_cast<typename traits_type::entity_type>(curr)};
curr = next;
next = index(packed[curr]);

View File

@@ -167,10 +167,14 @@ class meta_any {
template<typename Type>
[[nodiscard]] static meta_any dereference_operator(meta_any &any) {
if constexpr(is_meta_pointer_like_v<Type>) {
if constexpr(std::is_const_v<std::remove_reference_t<decltype(*std::declval<Type>())>>) {
return *any.cast<Type>();
} else {
using pointed_type = std::remove_reference_t<decltype(*std::declval<Type>())>;
if constexpr(std::is_const_v<pointed_type> && std::is_copy_constructible_v<pointed_type>) {
return std::as_const(*any.cast<Type>());
} else if constexpr(!std::is_const_v<pointed_type>) {
return std::ref(*any.cast<Type>());
} else {
return {};
}
} else {
return {};
@@ -296,6 +300,40 @@ public:
return storage.data();
}
/**
* @brief Invokes the underlying function, if possible.
*
* @sa invoke
*
* @tparam Args Types of arguments to use to invoke the function.
* @param id Unique identifier.
* @param args Parameters to use to invoke the function.
* @return A meta any containing the returned value, if any.
*/
template<typename... Args>
meta_any invoke(const id_type id, Args &&... args) const;
/**
* @brief Sets the value of a given variable.
*
* The type of the value must be such that a cast or conversion to the type
* of the variable is possible. Otherwise, invoking the setter does nothing.
*
* @tparam Type Type of value to assign.
* @param id Unique identifier.
* @param value Parameter to use to set the underlying variable.
* @return True in case of success, false otherwise.
*/
template<typename Type>
bool set(const id_type id, Type &&value) const;
/**
* @brief Gets the value of a given variable.
* @param id Unique identifier.
* @return A meta any containing the value of the underlying variable.
*/
[[nodiscard]] meta_any get(const id_type id) const;
/**
* @brief Tries to cast an instance to a given type.
* @tparam Type Type to which to cast the instance.
@@ -916,7 +954,7 @@ struct meta_func {
* @param sz Number of parameters to use to invoke the function.
* @return A meta any containing the returned value, if any.
*/
[[nodiscard]] meta_any invoke(meta_handle instance, meta_any * const args, const std::size_t sz) const {
meta_any invoke(meta_handle instance, meta_any * const args, const std::size_t sz) const {
return sz == size() ? node->invoke(instance, args) : meta_any{};
}
@@ -1305,6 +1343,80 @@ public:
return construct(arguments.data(), sizeof...(Args));
}
/**
* @brief Invokes the function with the given identifier, if possible.
*
* To invoke a meta function, the parameters must be such that a cast or
* conversion to the required types is possible. Otherwise, an empty and
* thus invalid wrapper is returned.<br/>
* It must be possible to cast the instance to the parent type of the meta
* function. Otherwise, invoking the underlying function results in an
* undefined behavior.
*
* @param id Unique identifier.
* @param instance An opaque instance of the underlying type.
* @param args Parameters to use to invoke the function.
* @param sz Number of parameters to use to invoke the function.
* @return A meta any containing the returned value, if any.
*/
meta_any invoke(const id_type id, meta_handle instance, meta_any * const args, const std::size_t sz) const {
auto const candidate = func(id);
return candidate ? candidate.invoke(std::move(instance), args, sz) : meta_any{};
}
/**
* @copybrief invoke
*
* @sa invoke
*
* @param id Unique identifier.
* @tparam Args Types of arguments to use to invoke the function.
* @param instance An opaque instance of the underlying type.
* @param args Parameters to use to invoke the function.
* @return A meta any containing the new instance, if any.
*/
template<typename... Args>
meta_any invoke(const id_type id, meta_handle instance, Args &&... args) const {
std::array<meta_any, sizeof...(Args)> arguments{std::forward<Args>(args)...};
return invoke(id, std::move(instance), arguments.data(), sizeof...(Args));
}
/**
* @brief Sets the value of a given variable.
*
* It must be possible to cast the instance to the parent type of the meta
* data. Otherwise, invoking the setter results in an undefined
* behavior.<br/>
* The type of the value must be such that a cast or conversion to the type
* of the variable is possible. Otherwise, invoking the setter does nothing.
*
* @tparam Type Type of value to assign.
* @param id Unique identifier.
* @param instance An opaque instance of the underlying type.
* @param value Parameter to use to set the underlying variable.
* @return True in case of success, false otherwise.
*/
template<typename Type>
bool set(const id_type id, meta_handle instance, Type &&value) const {
auto const candidate = data(id);
return candidate ? candidate.set(std::move(instance), std::forward<Type>(value)) : false;
}
/**
* @brief Gets the value of a given variable.
*
* It must be possible to cast the instance to the parent type of the meta
* data. Otherwise, invoking the getter results in an undefined behavior.
*
* @param id Unique identifier.
* @param instance An opaque instance of the underlying type.
* @return A meta any containing the value of the underlying variable.
*/
[[nodiscard]] meta_any get(const id_type id, meta_handle instance) const {
auto const candidate = data(id);
return candidate ? candidate.get(std::move(instance)) : meta_any{};
}
/**
* @brief Returns a range to use to visit top-level meta properties.
* @return An iterable range to use to visit top-level meta properties.
@@ -1352,7 +1464,7 @@ public:
* functions and properties, as well as its constructors, destructors and
* conversion functions if any.<br/>
* Base classes aren't reset but the link between the two types is removed.
*
*
* The meta type is also removed from the list of searchable types.
*/
void reset() ENTT_NOEXCEPT {
@@ -1376,14 +1488,14 @@ public:
}
}
};
unregister_all(&node->prop);
unregister_all(&node->base);
unregister_all(&node->conv);
unregister_all(&node->ctor, &internal::meta_ctor_node::prop);
unregister_all(&node->data, &internal::meta_data_node::prop);
unregister_all(&node->func, &internal::meta_func_node::prop);
node->id = {};
node->dtor = nullptr;
}
@@ -1409,6 +1521,23 @@ private:
}
template<typename... Args>
meta_any meta_any::invoke(const id_type id, Args &&... args) const {
return type().invoke(id, *this, std::forward<Args>(args)...);
}
template<typename Type>
bool meta_any::set(const id_type id, Type &&value) const {
return type().set(id, *this, std::forward<Type>(value));
}
[[nodiscard]] inline meta_any meta_any::get(const id_type id) const {
return type().get(id, *this);
}
[[nodiscard]] inline meta_type meta_base::parent() const ENTT_NOEXCEPT {
return node->parent;
}

View File

@@ -5,6 +5,14 @@
#include <entt/meta/meta.hpp>
#include <entt/meta/resolve.hpp>
struct clazz_t {
void member(int i) { value = i; }
static void func() { c = 'd'; }
static inline char c = 'c';
int value = 0;
};
struct empty_t {
virtual ~empty_t() = default;
static void destroy(empty_t &) {
@@ -46,6 +54,12 @@ struct MetaAny: ::testing::Test {
entt::meta<double>().conv<int>();
entt::meta<empty_t>().dtor<&empty_t::destroy>();
entt::meta<fat_t>().base<empty_t>().dtor<&fat_t::destroy>();
entt::meta<clazz_t>()
.type("clazz"_hs)
.data<&clazz_t::value>("value"_hs)
.func<&clazz_t::member>("member"_hs)
.func<&clazz_t::func>("func"_hs);
}
void SetUp() override {
@@ -622,3 +636,32 @@ TEST_F(MetaAny, UnmanageableType) {
ASSERT_TRUE(std::as_const(any).convert<unmanageable_t>());
ASSERT_FALSE(std::as_const(any).convert<int>());
}
TEST_F(MetaAny, Invoke) {
clazz_t instance;
entt::meta_any any{std::ref(instance)};
ASSERT_TRUE(any.invoke("func"_hs));
ASSERT_TRUE(any.invoke("member"_hs, 42));
ASSERT_FALSE(any.invoke("non_existent"_hs, 42));
ASSERT_EQ(clazz_t::c, 'd');
ASSERT_EQ(instance.value, 42);
}
TEST_F(MetaAny, SetGet) {
clazz_t instance;
entt::meta_any any{std::ref(instance)};
ASSERT_TRUE(any.set("value"_hs, 42));
const auto value = any.get("value"_hs);
ASSERT_TRUE(value);
ASSERT_TRUE(value.try_cast<int>());
ASSERT_EQ(value.cast<int>(), 42);
ASSERT_EQ(instance.value, 42);
ASSERT_FALSE(any.set("non_existent"_hs, 42));
ASSERT_FALSE(any.get("non_existent"_hs));
}

View File

@@ -4,6 +4,14 @@
#include <entt/meta/pointer.hpp>
#include <entt/meta/resolve.hpp>
struct not_copyable_t {
not_copyable_t() = default;
not_copyable_t(const not_copyable_t &) = delete;
not_copyable_t(not_copyable_t &&) = default;
not_copyable_t & operator=(const not_copyable_t &) = delete;
not_copyable_t & operator=(not_copyable_t &&) = default;
};
TEST(MetaPointerLike, DereferenceOperatorInvalidType) {
int value = 0;
entt::meta_any any{value};
@@ -79,3 +87,11 @@ TEST(MetaPointerLike, DereferenceOperatorSmartPointer) {
ASSERT_EQ(*any.cast<std::shared_ptr<int>>(), 42);
ASSERT_EQ(*value, 42);
}
TEST(MetaPointerLike, PointerToMoveOnlyType) {
const not_copyable_t instance;
entt::meta_any any{&instance};
ASSERT_TRUE(any);
ASSERT_FALSE(*any);
}

View File

@@ -272,6 +272,27 @@ TEST_F(MetaType, Func) {
ASSERT_TRUE(type.func("func"_hs).invoke({}));
}
TEST_F(MetaType, Invoke) {
auto type = entt::resolve<clazz_t>();
clazz_t instance{};
ASSERT_TRUE(type.invoke("member"_hs, instance));
ASSERT_FALSE(type.invoke("rebmem"_hs, {}));
}
TEST_F(MetaType, SetGet) {
auto type = entt::resolve<clazz_t>();
clazz_t instance{};
ASSERT_TRUE(type.set("value"_hs, instance, 42));
ASSERT_FALSE(type.set("eulav"_hs, instance, 3));
ASSERT_EQ(instance.value, 42);
ASSERT_FALSE(type.get("eulav"_hs, instance));
ASSERT_TRUE(type.get("value"_hs, instance));
ASSERT_EQ(type.get("value"_hs, instance).cast<int>(), 42);
}
TEST_F(MetaType, Construct) {
auto any = entt::resolve<clazz_t>().construct(base_t{}, 42);