Squashed 'external/entt/entt/' content from commit fef92113
git-subtree-dir: external/entt/entt git-subtree-split: fef921132cae7588213d0f9bcd2fb9c8ffd8b7fc
This commit is contained in:
53
test/example/custom_identifier.cpp
Normal file
53
test/example/custom_identifier.cpp
Normal file
@ -0,0 +1,53 @@
|
||||
#include <cstdint>
|
||||
#include <gtest/gtest.h>
|
||||
#include <entt/entity/entity.hpp>
|
||||
#include <entt/entity/registry.hpp>
|
||||
|
||||
struct entity_id {
|
||||
using entity_type = std::uint32_t;
|
||||
static constexpr auto null = entt::null;
|
||||
|
||||
constexpr entity_id(entity_type value = null) noexcept
|
||||
: entt{value} {}
|
||||
|
||||
constexpr entity_id(const entity_id &other) noexcept
|
||||
: entt{other.entt} {}
|
||||
|
||||
constexpr operator entity_type() const noexcept {
|
||||
return entt;
|
||||
}
|
||||
|
||||
private:
|
||||
entity_type entt;
|
||||
};
|
||||
|
||||
TEST(Example, CustomIdentifier) {
|
||||
entt::basic_registry<entity_id> registry{};
|
||||
entity_id entity{};
|
||||
|
||||
ASSERT_FALSE(registry.valid(entity));
|
||||
ASSERT_TRUE(entity == entt::null);
|
||||
|
||||
entity = registry.create();
|
||||
|
||||
ASSERT_TRUE(registry.valid(entity));
|
||||
ASSERT_TRUE(entity != entt::null);
|
||||
|
||||
ASSERT_FALSE((registry.all_of<int, char>(entity)));
|
||||
ASSERT_EQ(registry.try_get<int>(entity), nullptr);
|
||||
|
||||
registry.emplace<int>(entity, 42);
|
||||
|
||||
ASSERT_TRUE((registry.any_of<int, char>(entity)));
|
||||
ASSERT_EQ(registry.get<int>(entity), 42);
|
||||
|
||||
registry.destroy(entity);
|
||||
|
||||
ASSERT_FALSE(registry.valid(entity));
|
||||
ASSERT_TRUE(entity != entt::null);
|
||||
|
||||
entity = registry.create();
|
||||
|
||||
ASSERT_TRUE(registry.valid(entity));
|
||||
ASSERT_TRUE(entity != entt::null);
|
||||
}
|
78
test/example/entity_copy.cpp
Normal file
78
test/example/entity_copy.cpp
Normal file
@ -0,0 +1,78 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <entt/core/hashed_string.hpp>
|
||||
#include <entt/entity/registry.hpp>
|
||||
|
||||
enum class my_entity : entt::id_type {};
|
||||
|
||||
TEST(Example, EntityCopy) {
|
||||
using namespace entt::literals;
|
||||
|
||||
entt::registry registry{};
|
||||
auto &&custom = registry.storage<double>("custom"_hs);
|
||||
|
||||
const auto src = registry.create();
|
||||
const auto dst = registry.create();
|
||||
const auto other = registry.create();
|
||||
|
||||
custom.emplace(src, 1.);
|
||||
registry.emplace<int>(src, 42);
|
||||
registry.emplace<char>(src, 'c');
|
||||
registry.emplace<double>(other, 3.);
|
||||
|
||||
ASSERT_TRUE(custom.contains(src));
|
||||
ASSERT_FALSE(registry.all_of<double>(src));
|
||||
ASSERT_TRUE((registry.all_of<int, char>(src)));
|
||||
ASSERT_FALSE((registry.any_of<int, char, double>(dst)));
|
||||
ASSERT_FALSE(custom.contains(dst));
|
||||
|
||||
for(auto [id, storage]: registry.storage()) {
|
||||
// discard the custom storage because why not, this is just an example after all
|
||||
if(id != "custom"_hs && storage.contains(src)) {
|
||||
storage.emplace(dst, storage.get(src));
|
||||
}
|
||||
}
|
||||
|
||||
ASSERT_TRUE((registry.all_of<int, char>(dst)));
|
||||
ASSERT_FALSE((registry.all_of<double>(dst)));
|
||||
ASSERT_FALSE(custom.contains(dst));
|
||||
|
||||
ASSERT_EQ(registry.get<int>(dst), 42);
|
||||
ASSERT_EQ(registry.get<char>(dst), 'c');
|
||||
}
|
||||
|
||||
TEST(Example, DifferentRegistryTypes) {
|
||||
using namespace entt::literals;
|
||||
|
||||
entt::basic_registry<entt::entity> src{};
|
||||
entt::basic_registry<my_entity> dst{};
|
||||
|
||||
/*
|
||||
TODO These are currently needed to ensure that the source and
|
||||
target registries have the proper storage initialized
|
||||
prior to copying, as this isn't done automatically
|
||||
when emplacing storages (as is done below).
|
||||
|
||||
There is an open issue about this, and these two
|
||||
lines should be removed when a fix is properly landed.
|
||||
https://github.com/skypjack/entt/issues/827
|
||||
*/
|
||||
static_cast<void>(src.storage<double>());
|
||||
static_cast<void>(dst.storage<int>());
|
||||
|
||||
const auto entity = src.create();
|
||||
const auto copy = dst.create();
|
||||
|
||||
src.emplace<int>(entity, 42);
|
||||
src.emplace<char>(entity, 'c');
|
||||
|
||||
for(auto [id, storage]: src.storage()) {
|
||||
if(auto *other = dst.storage(id); other && storage.contains(entity)) {
|
||||
other->emplace(copy, storage.get(entity));
|
||||
}
|
||||
}
|
||||
|
||||
ASSERT_TRUE((src.all_of<int, char>(entity)));
|
||||
ASSERT_FALSE(dst.all_of<char>(copy));
|
||||
ASSERT_TRUE(dst.all_of<int>(copy));
|
||||
ASSERT_EQ(dst.get<int>(copy), 42);
|
||||
}
|
43
test/example/signal_less.cpp
Normal file
43
test/example/signal_less.cpp
Normal file
@ -0,0 +1,43 @@
|
||||
#include <iterator>
|
||||
#include <type_traits>
|
||||
#include <gtest/gtest.h>
|
||||
#include <entt/entity/entity.hpp>
|
||||
#include <entt/entity/registry.hpp>
|
||||
|
||||
template<typename Type, typename Entity>
|
||||
struct entt::storage_type<Type, Entity> {
|
||||
// no signal regardless of component type ...
|
||||
using type = basic_storage<Type, Entity>;
|
||||
};
|
||||
|
||||
template<typename Entity>
|
||||
struct entt::storage_type<char, Entity> {
|
||||
// ... unless it's char, because yes.
|
||||
using type = sigh_storage_mixin<basic_storage<char, Entity>>;
|
||||
};
|
||||
|
||||
template<typename, typename, typename = void>
|
||||
struct has_on_construct: std::false_type {};
|
||||
|
||||
template<typename Entity, typename Type>
|
||||
struct has_on_construct<Entity, Type, std::void_t<decltype(&entt::storage_type_t<Type>::on_construct)>>: std::true_type {};
|
||||
|
||||
template<typename Entity, typename Type>
|
||||
inline constexpr auto has_on_construct_v = has_on_construct<Entity, Type>::value;
|
||||
|
||||
TEST(Example, SignalLess) {
|
||||
// invoking registry::on_construct<int> is a compile-time error
|
||||
static_assert(!has_on_construct_v<entt::entity, int>);
|
||||
static_assert(has_on_construct_v<entt::entity, char>);
|
||||
|
||||
entt::registry registry;
|
||||
const entt::entity entity[1u]{registry.create()};
|
||||
|
||||
// literally a test for storage_adapter_mixin
|
||||
registry.emplace<int>(entity[0], 0);
|
||||
registry.erase<int>(entity[0]);
|
||||
registry.insert<int>(std::begin(entity), std::end(entity), 3);
|
||||
registry.patch<int>(entity[0], [](auto &value) { value = 42; });
|
||||
|
||||
ASSERT_EQ(registry.get<int>(entity[0]), 42);
|
||||
}
|
Reference in New Issue
Block a user