| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327 |
- #include <algorithm>
- #include <iterator>
- #include <tuple>
- #include <type_traits>
- #include <utility>
- #include <gtest/gtest.h>
- #include <entt/entity/registry.hpp>
- #include <entt/entity/view.hpp>
- struct empty_type {};
- struct stable_type {
- static constexpr auto in_place_delete = true;
- int value;
- };
- TEST(SingleComponentView, Functionalities) {
- entt::registry registry;
- auto view = registry.view<char>();
- auto cview = std::as_const(registry).view<const char>();
- const auto e0 = registry.create();
- const auto e1 = registry.create();
- ASSERT_TRUE(view.empty());
- registry.emplace<int>(e1);
- registry.emplace<char>(e1);
- ASSERT_NO_FATAL_FAILURE(view.begin()++);
- ASSERT_NO_FATAL_FAILURE(++cview.begin());
- ASSERT_NO_FATAL_FAILURE([](auto it) { return it++; }(view.rbegin()));
- ASSERT_NO_FATAL_FAILURE([](auto it) { return ++it; }(cview.rbegin()));
- ASSERT_NE(view.begin(), view.end());
- ASSERT_NE(cview.begin(), cview.end());
- ASSERT_NE(view.rbegin(), view.rend());
- ASSERT_NE(cview.rbegin(), cview.rend());
- ASSERT_EQ(view.size(), 1u);
- ASSERT_FALSE(view.empty());
- registry.emplace<char>(e0);
- ASSERT_EQ(view.size(), 2u);
- view.get<char>(e0) = '1';
- std::get<0>(view.get(e1)) = '2';
- ASSERT_EQ(view.get<0u>(e0), '1');
- ASSERT_EQ(cview.get<0u>(e0), view.get<char>(e0));
- ASSERT_EQ(view.get<char>(e1), '2');
- for(auto entity: view) {
- ASSERT_TRUE(entity == e0 || entity == e1);
- ASSERT_TRUE(entity != e0 || cview.get<const char>(entity) == '1');
- ASSERT_TRUE(entity != e1 || std::get<const char &>(cview.get(entity)) == '2');
- }
- registry.erase<char>(e0);
- registry.erase<char>(e1);
- ASSERT_EQ(view.begin(), view.end());
- ASSERT_EQ(view.rbegin(), view.rend());
- ASSERT_TRUE(view.empty());
- decltype(view) invalid{};
- ASSERT_TRUE(view);
- ASSERT_TRUE(cview);
- ASSERT_FALSE(invalid);
- }
- TEST(SingleComponentView, Constructors) {
- entt::storage<int> storage{};
- entt::view<entt::get_t<int>> invalid{};
- entt::basic_view from_storage{storage};
- entt::basic_view from_tuple{std::forward_as_tuple(storage)};
- ASSERT_FALSE(invalid);
- ASSERT_TRUE(from_storage);
- ASSERT_TRUE(from_tuple);
- ASSERT_EQ(&from_storage.handle(), &from_tuple.handle());
- }
- TEST(SingleComponentView, Handle) {
- entt::registry registry;
- const auto entity = registry.create();
- auto view = registry.view<int>();
- auto &&handle = view.handle();
- ASSERT_TRUE(handle.empty());
- ASSERT_FALSE(handle.contains(entity));
- ASSERT_EQ(&handle, &view.handle());
- registry.emplace<int>(entity);
- ASSERT_FALSE(handle.empty());
- ASSERT_TRUE(handle.contains(entity));
- ASSERT_EQ(&handle, &view.handle());
- }
- TEST(SingleComponentView, LazyTypeFromConstRegistry) {
- entt::registry registry{};
- auto eview = std::as_const(registry).view<const empty_type>();
- auto cview = std::as_const(registry).view<const int>();
- const auto entity = registry.create();
- registry.emplace<empty_type>(entity);
- registry.emplace<int>(entity);
- ASSERT_TRUE(cview);
- ASSERT_TRUE(eview);
- ASSERT_TRUE(cview.empty());
- ASSERT_EQ(eview.size(), 0u);
- ASSERT_FALSE(cview.contains(entity));
- ASSERT_EQ(cview.begin(), cview.end());
- ASSERT_EQ(eview.rbegin(), eview.rend());
- ASSERT_EQ(eview.find(entity), eview.end());
- ASSERT_NE(cview.front(), entity);
- ASSERT_NE(eview.back(), entity);
- }
- TEST(SingleComponentView, ElementAccess) {
- entt::registry registry;
- auto view = registry.view<int>();
- auto cview = std::as_const(registry).view<const int>();
- const auto e0 = registry.create();
- registry.emplace<int>(e0, 42);
- const auto e1 = registry.create();
- registry.emplace<int>(e1, 3);
- for(auto i = 0u; i < view.size(); ++i) {
- ASSERT_EQ(view[i], i ? e0 : e1);
- ASSERT_EQ(cview[i], i ? e0 : e1);
- }
- ASSERT_EQ(view[e0], 42);
- ASSERT_EQ(cview[e1], 3);
- }
- TEST(SingleComponentView, Contains) {
- entt::registry registry;
- const auto e0 = registry.create();
- registry.emplace<int>(e0);
- const auto e1 = registry.create();
- registry.emplace<int>(e1);
- registry.destroy(e0);
- auto view = registry.view<int>();
- ASSERT_FALSE(view.contains(e0));
- ASSERT_TRUE(view.contains(e1));
- }
- TEST(SingleComponentView, Empty) {
- entt::registry registry;
- const auto e0 = registry.create();
- registry.emplace<char>(e0);
- registry.emplace<double>(e0);
- const auto e1 = registry.create();
- registry.emplace<char>(e1);
- auto view = registry.view<int>();
- ASSERT_EQ(view.size(), 0u);
- ASSERT_EQ(view.begin(), view.end());
- ASSERT_EQ(view.rbegin(), view.rend());
- }
- TEST(SingleComponentView, Each) {
- entt::registry registry;
- entt::entity entity[2]{registry.create(), registry.create()};
- auto view = registry.view<int>(entt::exclude<double>);
- auto cview = std::as_const(registry).view<const int>();
- registry.emplace<int>(entity[0u], 0);
- registry.emplace<int>(entity[1u], 1);
- auto iterable = view.each();
- auto citerable = cview.each();
- ASSERT_NE(citerable.begin(), citerable.end());
- ASSERT_NO_FATAL_FAILURE(iterable.begin()->operator=(*iterable.begin()));
- ASSERT_EQ(decltype(iterable.end()){}, iterable.end());
- auto it = iterable.begin();
- ASSERT_EQ(it.base(), view.begin());
- ASSERT_EQ((it++, ++it), iterable.end());
- ASSERT_EQ(it.base(), view.end());
- view.each([expected = 1](auto entt, int &value) mutable {
- ASSERT_EQ(static_cast<int>(entt::to_integral(entt)), expected);
- ASSERT_EQ(value, expected);
- --expected;
- });
- cview.each([expected = 1](const int &value) mutable {
- ASSERT_EQ(value, expected);
- --expected;
- });
- ASSERT_EQ(std::get<0>(*iterable.begin()), entity[1u]);
- ASSERT_EQ(std::get<0>(*++citerable.begin()), entity[0u]);
- static_assert(std::is_same_v<decltype(std::get<1>(*iterable.begin())), int &>);
- static_assert(std::is_same_v<decltype(std::get<1>(*citerable.begin())), const int &>);
- // do not use iterable, make sure an iterable view works when created from a temporary
- for(auto [entt, value]: view.each()) {
- ASSERT_EQ(static_cast<int>(entt::to_integral(entt)), value);
- }
- }
- TEST(SingleComponentView, ConstNonConstAndAllInBetween) {
- entt::registry registry;
- auto view = registry.view<int>();
- auto cview = std::as_const(registry).view<const int>();
- ASSERT_EQ(view.size(), 0u);
- ASSERT_EQ(cview.size(), 0u);
- registry.emplace<int>(registry.create(), 0);
- ASSERT_EQ(view.size(), 1u);
- ASSERT_EQ(cview.size(), 1u);
- static_assert(std::is_same_v<decltype(view.get<0u>({})), int &>);
- static_assert(std::is_same_v<decltype(view.get<int>({})), int &>);
- static_assert(std::is_same_v<decltype(view.get({})), std::tuple<int &>>);
- static_assert(std::is_same_v<decltype(cview.get<0u>({})), const int &>);
- static_assert(std::is_same_v<decltype(cview.get<const int>({})), const int &>);
- static_assert(std::is_same_v<decltype(cview.get({})), std::tuple<const int &>>);
- static_assert(std::is_same_v<decltype(std::as_const(registry).view<int>()), decltype(cview)>);
- view.each([](auto &&i) {
- static_assert(std::is_same_v<decltype(i), int &>);
- });
- cview.each([](auto &&i) {
- static_assert(std::is_same_v<decltype(i), const int &>);
- });
- for(auto [entt, iv]: view.each()) {
- static_assert(std::is_same_v<decltype(entt), entt::entity>);
- static_assert(std::is_same_v<decltype(iv), int &>);
- }
- for(auto [entt, iv]: cview.each()) {
- static_assert(std::is_same_v<decltype(entt), entt::entity>);
- static_assert(std::is_same_v<decltype(iv), const int &>);
- }
- }
- TEST(SingleComponentView, ConstNonConstAndAllInBetweenWithEmptyType) {
- entt::registry registry;
- auto view = registry.view<empty_type>();
- auto cview = std::as_const(registry).view<const empty_type>();
- ASSERT_EQ(view.size(), 0u);
- ASSERT_EQ(cview.size(), 0u);
- registry.emplace<empty_type>(registry.create());
- ASSERT_EQ(view.size(), 1u);
- ASSERT_EQ(cview.size(), 1u);
- static_assert(std::is_same_v<decltype(view.get({})), std::tuple<>>);
- static_assert(std::is_same_v<decltype(cview.get({})), std::tuple<>>);
- static_assert(std::is_same_v<decltype(std::as_const(registry).view<empty_type>()), decltype(cview)>);
- for(auto [entt]: view.each()) {
- static_assert(std::is_same_v<decltype(entt), entt::entity>);
- }
- for(auto [entt]: cview.each()) {
- static_assert(std::is_same_v<decltype(entt), entt::entity>);
- }
- }
- TEST(SingleComponentView, Find) {
- entt::registry registry;
- auto view = registry.view<int>();
- const auto e0 = registry.create();
- registry.emplace<int>(e0);
- const auto e1 = registry.create();
- registry.emplace<int>(e1);
- const auto e2 = registry.create();
- registry.emplace<int>(e2);
- const auto e3 = registry.create();
- registry.emplace<int>(e3);
- registry.erase<int>(e1);
- ASSERT_NE(view.find(e0), view.end());
- ASSERT_EQ(view.find(e1), view.end());
- ASSERT_NE(view.find(e2), view.end());
- ASSERT_NE(view.find(e3), view.end());
- auto it = view.find(e2);
- ASSERT_EQ(*it, e2);
- ASSERT_EQ(*(++it), e3);
- ASSERT_EQ(*(++it), e0);
- ASSERT_EQ(++it, view.end());
- ASSERT_EQ(++view.find(e0), view.end());
- const auto e4 = registry.create();
- registry.destroy(e4);
- const auto e5 = registry.create();
- registry.emplace<int>(e5);
- ASSERT_NE(view.find(e5), view.end());
- ASSERT_EQ(view.find(e4), view.end());
- }
- TEST(SingleComponentView, EmptyTypes) {
- entt::registry registry;
- entt::entity entities[2u];
- registry.create(std::begin(entities), std::end(entities));
- registry.emplace<int>(entities[0u], 0);
- registry.emplace<empty_type>(entities[0u]);
- registry.emplace<char>(entities[1u], 'c');
- registry.view<empty_type>().each([&](const auto entt) {
- ASSERT_EQ(entities[0u], entt);
- });
- registry.view<empty_type>().each([check = true]() mutable {
- ASSERT_TRUE(check);
- check = false;
- });
- for(auto [entt]: registry.view<empty_type>().each()) {
- static_assert(std::is_same_v<decltype(entt), entt::entity>);
- ASSERT_EQ(entities[0u], entt);
- }
- registry.view<int>().each([&](const auto entt, int) {
- ASSERT_EQ(entities[0u], entt);
- });
- registry.view<int>().each([check = true](int) mutable {
- ASSERT_TRUE(check);
- check = false;
- });
- for(auto [entt, iv]: registry.view<int>().each()) {
- static_assert(std::is_same_v<decltype(entt), entt::entity>);
- static_assert(std::is_same_v<decltype(iv), int &>);
- ASSERT_EQ(entities[0u], entt);
- }
- }
- TEST(SingleComponentView, FrontBack) {
- entt::registry registry;
- auto view = registry.view<const int>();
- ASSERT_EQ(view.front(), static_cast<entt::entity>(entt::null));
- ASSERT_EQ(view.back(), static_cast<entt::entity>(entt::null));
- const auto e0 = registry.create();
- registry.emplace<int>(e0);
- const auto e1 = registry.create();
- registry.emplace<int>(e1);
- ASSERT_EQ(view.front(), e1);
- ASSERT_EQ(view.back(), e0);
- }
- TEST(SingleComponentView, DeductionGuide) {
- entt::registry registry;
- entt::storage_type_t<int> istorage;
- entt::storage_type_t<stable_type> sstorage;
- static_assert(std::is_same_v<entt::basic_view<entt::get_t<entt::storage_type_t<int>>, entt::exclude_t<>>, decltype(entt::basic_view{istorage})>);
- static_assert(std::is_same_v<entt::basic_view<entt::get_t<const entt::storage_type_t<int>>, entt::exclude_t<>>, decltype(entt::basic_view{std::as_const(istorage)})>);
- static_assert(std::is_same_v<entt::basic_view<entt::get_t<entt::storage_type_t<stable_type>>, entt::exclude_t<>>, decltype(entt::basic_view{sstorage})>);
- static_assert(std::is_same_v<entt::basic_view<entt::get_t<entt::storage_type_t<int>>, entt::exclude_t<>>, decltype(entt::basic_view{std::forward_as_tuple(istorage), std::make_tuple()})>);
- static_assert(std::is_same_v<entt::basic_view<entt::get_t<const entt::storage_type_t<int>>, entt::exclude_t<>>, decltype(entt::basic_view{std::forward_as_tuple(std::as_const(istorage))})>);
- static_assert(std::is_same_v<entt::basic_view<entt::get_t<entt::storage_type_t<stable_type>>, entt::exclude_t<>>, decltype(entt::basic_view{std::forward_as_tuple(sstorage)})>);
- }
- TEST(SingleComponentView, IterableViewAlgorithmCompatibility) {
- entt::registry registry;
- const auto entity = registry.create();
- registry.emplace<int>(entity);
- const auto view = registry.view<int>();
- const auto iterable = view.each();
- const auto it = std::find_if(iterable.begin(), iterable.end(), [entity](auto args) { return std::get<0>(args) == entity; });
- ASSERT_EQ(std::get<0>(*it), entity);
- }
- TEST(SingleComponentView, StableType) {
- entt::registry registry;
- auto view = registry.view<stable_type>();
- const auto entity = registry.create();
- const auto other = registry.create();
- registry.emplace<stable_type>(entity);
- registry.emplace<stable_type>(other);
- registry.destroy(entity);
- ASSERT_EQ(view.size_hint(), 2u);
- ASSERT_FALSE(view.contains(entity));
- ASSERT_TRUE(view.contains(other));
- ASSERT_EQ(view.front(), other);
- ASSERT_EQ(view.back(), other);
- ASSERT_EQ(*view.begin(), other);
- ASSERT_EQ(++view.begin(), view.end());
- view.each([other](const auto entt, stable_type) {
- ASSERT_EQ(other, entt);
- });
- view.each([check = true](stable_type) mutable {
- ASSERT_TRUE(check);
- check = false;
- });
- for(auto [entt, st]: view.each()) {
- static_assert(std::is_same_v<decltype(entt), entt::entity>);
- static_assert(std::is_same_v<decltype(st), stable_type &>);
- ASSERT_EQ(other, entt);
- }
- registry.compact();
- ASSERT_EQ(view.size_hint(), 1u);
- }
- TEST(SingleComponentView, Storage) {
- entt::registry registry;
- const auto entity = registry.create();
- const auto view = registry.view<int>();
- const auto cview = registry.view<const char>();
- static_assert(std::is_same_v<decltype(view.storage()), entt::storage_type_t<int> &>);
- static_assert(std::is_same_v<decltype(view.storage<0u>()), entt::storage_type_t<int> &>);
- static_assert(std::is_same_v<decltype(view.storage<int>()), entt::storage_type_t<int> &>);
- static_assert(std::is_same_v<decltype(view.storage<const int>()), entt::storage_type_t<int> &>);
- static_assert(std::is_same_v<decltype(cview.storage()), const entt::storage_type_t<char> &>);
- static_assert(std::is_same_v<decltype(cview.storage<0u>()), const entt::storage_type_t<char> &>);
- static_assert(std::is_same_v<decltype(cview.storage<char>()), const entt::storage_type_t<char> &>);
- static_assert(std::is_same_v<decltype(cview.storage<const char>()), const entt::storage_type_t<char> &>);
- ASSERT_EQ(view.size(), 0u);
- ASSERT_EQ(cview.size(), 0u);
- view.storage().emplace(entity);
- registry.emplace<char>(entity);
- ASSERT_EQ(view.size(), 1u);
- ASSERT_EQ(cview.size(), 1u);
- ASSERT_TRUE(view.storage<int>().contains(entity));
- ASSERT_TRUE(cview.storage<0u>().contains(entity));
- ASSERT_TRUE((registry.all_of<int, char>(entity)));
- view.storage().erase(entity);
- ASSERT_EQ(view.size(), 0u);
- ASSERT_EQ(cview.size(), 1u);
- ASSERT_FALSE(view.storage<0u>().contains(entity));
- ASSERT_TRUE(cview.storage<const char>().contains(entity));
- ASSERT_FALSE((registry.all_of<int, char>(entity)));
- }
- TEST(MultiComponentView, Functionalities) {
- entt::registry registry;
- auto view = registry.view<int, char>();
- auto cview = std::as_const(registry).view<const int, const char>();
- const auto e0 = registry.create();
- registry.emplace<char>(e0, '1');
- const auto e1 = registry.create();
- registry.emplace<int>(e1, 42);
- registry.emplace<char>(e1, '2');
- ASSERT_EQ(*view.begin(), e1);
- ASSERT_EQ(*cview.begin(), e1);
- ASSERT_EQ(++view.begin(), (view.end()));
- ASSERT_EQ(++cview.begin(), (cview.end()));
- ASSERT_NO_FATAL_FAILURE((view.begin()++));
- ASSERT_NO_FATAL_FAILURE((++cview.begin()));
- ASSERT_NE(view.begin(), view.end());
- ASSERT_NE(cview.begin(), cview.end());
- ASSERT_EQ(view.size_hint(), 1u);
- for(auto entity: view) {
- ASSERT_EQ(std::get<0>(cview.get<const int, const char>(entity)), 42);
- ASSERT_EQ(std::get<0>(cview.get<0u, 1u>(entity)), 42);
- ASSERT_EQ(std::get<1>(view.get<int, char>(entity)), '2');
- ASSERT_EQ(std::get<1>(view.get<0u, 1u>(entity)), '2');
- ASSERT_EQ(cview.get<const char>(entity), '2');
- ASSERT_EQ(cview.get<1u>(entity), '2');
- }
- decltype(view) invalid{};
- ASSERT_TRUE(view);
- ASSERT_TRUE(cview);
- ASSERT_FALSE(invalid);
- }
- TEST(MultiComponentView, Constructors) {
- entt::storage<int> storage{};
- entt::view<entt::get_t<int, int>> invalid{};
- entt::basic_view from_storage{storage, storage};
- entt::basic_view from_tuple{std::forward_as_tuple(storage, storage)};
- ASSERT_FALSE(invalid);
- ASSERT_TRUE(from_storage);
- ASSERT_TRUE(from_tuple);
- ASSERT_EQ(&from_storage.handle(), &from_tuple.handle());
- }
- TEST(MultiComponentView, Handle) {
- entt::registry registry;
- const auto entity = registry.create();
- auto view = registry.view<int, char>();
- auto &&handle = view.handle();
- ASSERT_TRUE(handle.empty());
- ASSERT_FALSE(handle.contains(entity));
- ASSERT_EQ(&handle, &view.handle());
- registry.emplace<int>(entity);
- ASSERT_FALSE(handle.empty());
- ASSERT_TRUE(handle.contains(entity));
- ASSERT_EQ(&handle, &view.handle());
- view = view.refresh();
- auto &&other = view.handle();
- ASSERT_TRUE(other.empty());
- ASSERT_FALSE(other.contains(entity));
- ASSERT_EQ(&other, &view.handle());
- ASSERT_NE(&handle, &other);
- view = view.use<int>();
- ASSERT_NE(&other, &view.handle());
- ASSERT_EQ(&handle, &view.handle());
- }
- TEST(MultiComponentView, LazyTypesFromConstRegistry) {
- entt::registry registry{};
- auto view = std::as_const(registry).view<const empty_type, const int>();
- const auto entity = registry.create();
- registry.emplace<empty_type>(entity);
- registry.emplace<int>(entity);
- ASSERT_TRUE(view);
- ASSERT_EQ(view.size_hint(), 0u);
- ASSERT_FALSE(view.contains(entity));
- ASSERT_EQ(view.begin(), view.end());
- ASSERT_EQ(view.find(entity), view.end());
- ASSERT_NE(view.front(), entity);
- ASSERT_NE(view.back(), entity);
- }
- TEST(MultiComponentView, LazyExcludedTypeFromConstRegistry) {
- entt::registry registry;
- auto entity = registry.create();
- registry.emplace<int>(entity);
- auto view = std::as_const(registry).view<const int>(entt::exclude<char>);
- ASSERT_TRUE(view);
- ASSERT_EQ(view.size_hint(), 1u);
- ASSERT_TRUE(view.contains(entity));
- ASSERT_NE(view.begin(), view.end());
- ASSERT_NE(view.find(entity), view.end());
- ASSERT_EQ(view.front(), entity);
- ASSERT_EQ(view.back(), entity);
- }
- TEST(MultiComponentView, Iterator) {
- entt::registry registry;
- const entt::entity entity[2]{registry.create(), registry.create()};
- registry.insert<int>(std::begin(entity), std::end(entity));
- registry.insert<char>(std::begin(entity), std::end(entity));
- const auto view = registry.view<int, char>();
- using iterator = typename decltype(view)::iterator;
- iterator end{view.begin()};
- iterator begin{};
- begin = view.end();
- std::swap(begin, end);
- ASSERT_EQ(begin, view.begin());
- ASSERT_EQ(end, view.end());
- ASSERT_NE(begin, end);
- ASSERT_EQ(*begin, entity[1u]);
- ASSERT_EQ(*begin.operator->(), entity[1u]);
- ASSERT_EQ(begin++, view.begin());
- ASSERT_EQ(*begin, entity[0u]);
- ASSERT_EQ(*begin.operator->(), entity[0u]);
- ASSERT_EQ(++begin, view.end());
- }
- TEST(MultiComponentView, ElementAccess) {
- entt::registry registry;
- auto view = registry.view<int, char>();
- auto cview = std::as_const(registry).view<const int, const char>();
- const auto e0 = registry.create();
- registry.emplace<int>(e0, 42);
- registry.emplace<char>(e0, '0');
- const auto e1 = registry.create();
- registry.emplace<int>(e1, 3);
- registry.emplace<char>(e1, '1');
- ASSERT_EQ(view[e0], std::make_tuple(42, '0'));
- ASSERT_EQ(cview[e1], std::make_tuple(3, '1'));
- }
- TEST(MultiComponentView, Contains) {
- entt::registry registry;
- const auto e0 = registry.create();
- registry.emplace<int>(e0);
- registry.emplace<char>(e0);
- const auto e1 = registry.create();
- registry.emplace<int>(e1);
- registry.emplace<char>(e1);
- registry.destroy(e0);
- auto view = registry.view<int, char>();
- ASSERT_FALSE(view.contains(e0));
- ASSERT_TRUE(view.contains(e1));
- }
- TEST(MultiComponentView, SizeHint) {
- entt::registry registry;
- const auto e0 = registry.create();
- registry.emplace<double>(e0);
- registry.emplace<int>(e0);
- registry.emplace<float>(e0);
- const auto e1 = registry.create();
- registry.emplace<char>(e1);
- registry.emplace<float>(e1);
- auto view = registry.view<char, int, float>();
- ASSERT_EQ(view.size_hint(), 1u);
- ASSERT_EQ(view.begin(), view.end());
- }
- TEST(MultiComponentView, Each) {
- entt::registry registry;
- entt::entity entity[2]{registry.create(), registry.create()};
- auto view = registry.view<int, char>(entt::exclude<double>);
- auto cview = std::as_const(registry).view<const int, const char>();
- registry.emplace<int>(entity[0u], 0);
- registry.emplace<char>(entity[0u], static_cast<char>(0));
- registry.emplace<int>(entity[1u], 1);
- registry.emplace<char>(entity[1u], static_cast<char>(1));
- auto iterable = view.each();
- auto citerable = cview.each();
- ASSERT_NE(citerable.begin(), citerable.end());
- ASSERT_NO_FATAL_FAILURE(iterable.begin()->operator=(*iterable.begin()));
- ASSERT_EQ(decltype(iterable.end()){}, iterable.end());
- auto it = iterable.begin();
- ASSERT_EQ(it.base(), view.begin());
- ASSERT_EQ((it++, ++it), iterable.end());
- ASSERT_EQ(it.base(), view.end());
- view.each([expected = 1](auto entt, int &ivalue, char &cvalue) mutable {
- ASSERT_EQ(static_cast<int>(entt::to_integral(entt)), expected);
- ASSERT_EQ(ivalue, expected);
- ASSERT_EQ(cvalue, expected);
- --expected;
- });
- cview.each([expected = 1](const int &ivalue, const char &cvalue) mutable {
- ASSERT_EQ(ivalue, expected);
- ASSERT_EQ(cvalue, expected);
- --expected;
- });
- ASSERT_EQ(std::get<0>(*iterable.begin()), entity[1u]);
- ASSERT_EQ(std::get<0>(*++citerable.begin()), entity[0u]);
- static_assert(std::is_same_v<decltype(std::get<1>(*iterable.begin())), int &>);
- static_assert(std::is_same_v<decltype(std::get<2>(*citerable.begin())), const char &>);
- // do not use iterable, make sure an iterable view works when created from a temporary
- for(auto [entt, ivalue, cvalue]: registry.view<int, char>().each()) {
- ASSERT_EQ(static_cast<int>(entt::to_integral(entt)), ivalue);
- ASSERT_EQ(static_cast<char>(entt::to_integral(entt)), cvalue);
- }
- }
- TEST(MultiComponentView, EachWithSuggestedType) {
- entt::registry registry;
- for(auto i = 0; i < 3; ++i) {
- const auto entity = registry.create();
- registry.emplace<int>(entity, i);
- registry.emplace<char>(entity);
- }
- // makes char a better candidate during iterations
- const auto entity = registry.create();
- registry.emplace<int>(entity, 99);
- registry.view<int, char>().use<int>().each([value = 2](const auto curr, const auto) mutable {
- ASSERT_EQ(curr, value--);
- });
- registry.sort<int>([](const auto lhs, const auto rhs) {
- return lhs < rhs;
- });
- registry.view<int, char>().use<0u>().each([value = 0](const auto curr, const auto) mutable {
- ASSERT_EQ(curr, value++);
- });
- registry.sort<int>([](const auto lhs, const auto rhs) {
- return lhs > rhs;
- });
- auto value = registry.view<int, char>().size_hint();
- for(auto &&curr: registry.view<int, char>().each()) {
- ASSERT_EQ(std::get<1>(curr), static_cast<int>(--value));
- }
- registry.sort<int>([](const auto lhs, const auto rhs) {
- return lhs < rhs;
- });
- value = {};
- for(auto &&curr: registry.view<int, char>().use<int>().each()) {
- ASSERT_EQ(std::get<1>(curr), static_cast<int>(value++));
- }
- }
- TEST(MultiComponentView, EachWithHoles) {
- entt::registry registry;
- const auto e0 = registry.create();
- const auto e1 = registry.create();
- const auto e2 = registry.create();
- registry.emplace<char>(e0, '0');
- registry.emplace<char>(e1, '1');
- registry.emplace<int>(e0, 0);
- registry.emplace<int>(e2, 2);
- auto view = registry.view<char, int>();
- view.each([e0](auto entity, const char &c, const int &i) {
- ASSERT_EQ(entity, e0);
- ASSERT_EQ(c, '0');
- ASSERT_EQ(i, 0);
- });
- for(auto &&curr: view.each()) {
- ASSERT_EQ(std::get<0>(curr), e0);
- ASSERT_EQ(std::get<1>(curr), '0');
- ASSERT_EQ(std::get<2>(curr), 0);
- }
- }
- TEST(MultiComponentView, ConstNonConstAndAllInBetween) {
- entt::registry registry;
- auto view = registry.view<int, empty_type, const char>();
- ASSERT_EQ(view.size_hint(), 0u);
- const auto entity = registry.create();
- registry.emplace<int>(entity, 0);
- registry.emplace<empty_type>(entity);
- registry.emplace<char>(entity, 'c');
- ASSERT_EQ(view.size_hint(), 1u);
- static_assert(std::is_same_v<decltype(view.get<0u>({})), int &>);
- static_assert(std::is_same_v<decltype(view.get<2u>({})), const char &>);
- static_assert(std::is_same_v<decltype(view.get<0u, 2u>({})), std::tuple<int &, const char &>>);
- static_assert(std::is_same_v<decltype(view.get<int>({})), int &>);
- static_assert(std::is_same_v<decltype(view.get<const char>({})), const char &>);
- static_assert(std::is_same_v<decltype(view.get<int, const char>({})), std::tuple<int &, const char &>>);
- static_assert(std::is_same_v<decltype(view.get({})), std::tuple<int &, const char &>>);
- static_assert(std::is_same_v<decltype(std::as_const(registry).view<char, int>()), decltype(std::as_const(registry).view<const char, const int>())>);
- static_assert(std::is_same_v<decltype(std::as_const(registry).view<char, const int>()), decltype(std::as_const(registry).view<const char, const int>())>);
- static_assert(std::is_same_v<decltype(std::as_const(registry).view<const char, int>()), decltype(std::as_const(registry).view<const char, const int>())>);
- view.each([](auto &&i, auto &&c) {
- static_assert(std::is_same_v<decltype(i), int &>);
- static_assert(std::is_same_v<decltype(c), const char &>);
- });
- for(auto [entt, iv, cv]: view.each()) {
- static_assert(std::is_same_v<decltype(entt), entt::entity>);
- static_assert(std::is_same_v<decltype(iv), int &>);
- static_assert(std::is_same_v<decltype(cv), const char &>);
- }
- }
- TEST(MultiComponentView, Find) {
- entt::registry registry;
- auto view = registry.view<int, const char>();
- const auto e0 = registry.create();
- registry.emplace<int>(e0);
- registry.emplace<char>(e0);
- const auto e1 = registry.create();
- registry.emplace<int>(e1);
- registry.emplace<char>(e1);
- const auto e2 = registry.create();
- registry.emplace<int>(e2);
- registry.emplace<char>(e2);
- const auto e3 = registry.create();
- registry.emplace<int>(e3);
- registry.emplace<char>(e3);
- registry.erase<int>(e1);
- ASSERT_NE(view.find(e0), view.end());
- ASSERT_EQ(view.find(e1), view.end());
- ASSERT_NE(view.find(e2), view.end());
- ASSERT_NE(view.find(e3), view.end());
- auto it = view.find(e2);
- ASSERT_EQ(*it, e2);
- ASSERT_EQ(*(++it), e3);
- ASSERT_EQ(*(++it), e0);
- ASSERT_EQ(++it, view.end());
- ASSERT_EQ(++view.find(e0), view.end());
- const auto e4 = registry.create();
- registry.destroy(e4);
- const auto e5 = registry.create();
- registry.emplace<int>(e5);
- registry.emplace<char>(e5);
- ASSERT_NE(view.find(e5), view.end());
- ASSERT_EQ(view.find(e4), view.end());
- }
- TEST(MultiComponentView, ExcludedComponents) {
- entt::registry registry;
- const auto e0 = registry.create();
- registry.emplace<int>(e0, 0);
- const auto e1 = registry.create();
- registry.emplace<int>(e1, 1);
- registry.emplace<char>(e1);
- const auto e2 = registry.create();
- registry.emplace<int>(e2, 2);
- const auto e3 = registry.create();
- registry.emplace<int>(e3, 3);
- registry.emplace<char>(e3);
- const auto view = std::as_const(registry).view<const int>(entt::exclude<char>);
- for(const auto entity: view) {
- ASSERT_TRUE(entity == e0 || entity == e2);
- if(entity == e0) {
- ASSERT_EQ(view.get<const int>(e0), 0);
- ASSERT_EQ(view.get<0u>(e0), 0);
- } else if(entity == e2) {
- ASSERT_EQ(std::get<0>(view.get(e2)), 2);
- }
- }
- registry.emplace<char>(e0);
- registry.emplace<char>(e2);
- registry.erase<char>(e1);
- registry.erase<char>(e3);
- for(const auto entity: view) {
- ASSERT_TRUE(entity == e1 || entity == e3);
- if(entity == e1) {
- ASSERT_EQ(std::get<0>(view.get(e1)), 1);
- } else if(entity == e3) {
- ASSERT_EQ(view.get<const int>(e3), 3);
- ASSERT_EQ(view.get<0u>(e3), 3);
- }
- }
- }
- TEST(MultiComponentView, EmptyTypes) {
- entt::registry registry;
- const auto entity = registry.create();
- registry.emplace<int>(entity);
- registry.emplace<char>(entity);
- registry.emplace<empty_type>(entity);
- const auto other = registry.create();
- registry.emplace<int>(other);
- registry.emplace<char>(other);
- registry.emplace<double>(other);
- registry.emplace<empty_type>(other);
- const auto ignored = registry.create();
- registry.emplace<int>(ignored);
- registry.emplace<char>(ignored);
- registry.view<int, char, empty_type>(entt::exclude<double>).each([entity](const auto entt, int, char) {
- ASSERT_EQ(entity, entt);
- });
- for(auto [entt, iv, cv]: registry.view<int, char, empty_type>(entt::exclude<double>).each()) {
- static_assert(std::is_same_v<decltype(entt), entt::entity>);
- static_assert(std::is_same_v<decltype(iv), int &>);
- static_assert(std::is_same_v<decltype(cv), char &>);
- ASSERT_EQ(entity, entt);
- }
- registry.view<int, empty_type, char>(entt::exclude<double>).each([check = true](int, char) mutable {
- ASSERT_TRUE(check);
- check = false;
- });
- for(auto [entt, iv, cv]: registry.view<int, empty_type, char>(entt::exclude<double>).each()) {
- static_assert(std::is_same_v<decltype(entt), entt::entity>);
- static_assert(std::is_same_v<decltype(iv), int &>);
- static_assert(std::is_same_v<decltype(cv), char &>);
- ASSERT_EQ(entity, entt);
- }
- registry.view<empty_type, int, char>(entt::exclude<double>).each([entity](const auto entt, int, char) {
- ASSERT_EQ(entity, entt);
- });
- for(auto [entt, iv, cv]: registry.view<empty_type, int, char>(entt::exclude<double>).each()) {
- static_assert(std::is_same_v<decltype(entt), entt::entity>);
- static_assert(std::is_same_v<decltype(iv), int &>);
- static_assert(std::is_same_v<decltype(cv), char &>);
- ASSERT_EQ(entity, entt);
- }
- registry.view<empty_type, int, char>(entt::exclude<double>).use<empty_type>().each([entity](const auto entt, int, char) {
- ASSERT_EQ(entity, entt);
- });
- for(auto [entt, iv, cv]: registry.view<empty_type, int, char>(entt::exclude<double>).use<0u>().each()) {
- static_assert(std::is_same_v<decltype(entt), entt::entity>);
- static_assert(std::is_same_v<decltype(iv), int &>);
- static_assert(std::is_same_v<decltype(cv), char &>);
- ASSERT_EQ(entity, entt);
- }
- registry.view<int, empty_type, char>(entt::exclude<double>).use<1u>().each([check = true](int, char) mutable {
- ASSERT_TRUE(check);
- check = false;
- });
- for(auto [entt, iv, cv]: registry.view<int, empty_type, char>(entt::exclude<double>).use<empty_type>().each()) {
- static_assert(std::is_same_v<decltype(entt), entt::entity>);
- static_assert(std::is_same_v<decltype(iv), int &>);
- static_assert(std::is_same_v<decltype(cv), char &>);
- ASSERT_EQ(entity, entt);
- }
- }
- TEST(MultiComponentView, FrontBack) {
- entt::registry registry;
- auto view = registry.view<const int, const char>();
- ASSERT_EQ(view.front(), static_cast<entt::entity>(entt::null));
- ASSERT_EQ(view.back(), static_cast<entt::entity>(entt::null));
- const auto e0 = registry.create();
- registry.emplace<int>(e0);
- registry.emplace<char>(e0);
- const auto e1 = registry.create();
- registry.emplace<int>(e1);
- registry.emplace<char>(e1);
- const auto entity = registry.create();
- registry.emplace<char>(entity);
- ASSERT_EQ(view.front(), e1);
- ASSERT_EQ(view.back(), e0);
- }
- TEST(MultiComponentView, ExtendedGet) {
- using type = decltype(std::declval<entt::registry>().view<int, empty_type, char>().get({}));
- static_assert(std::tuple_size_v<type> == 2u);
- static_assert(std::is_same_v<std::tuple_element_t<0, type>, int &>);
- static_assert(std::is_same_v<std::tuple_element_t<1, type>, char &>);
- }
- TEST(MultiComponentView, DeductionGuide) {
- entt::registry registry;
- entt::storage_type_t<int> istorage;
- entt::storage_type_t<double> dstorage;
- entt::storage_type_t<stable_type> sstorage;
- static_assert(std::is_same_v<entt::basic_view<entt::get_t<entt::storage_type_t<int>, entt::storage_type_t<double>>, entt::exclude_t<>>, decltype(entt::basic_view{istorage, dstorage})>);
- static_assert(std::is_same_v<entt::basic_view<entt::get_t<const entt::storage_type_t<int>, entt::storage_type_t<double>>, entt::exclude_t<>>, decltype(entt::basic_view{std::as_const(istorage), dstorage})>);
- static_assert(std::is_same_v<entt::basic_view<entt::get_t<entt::storage_type_t<int>, const entt::storage_type_t<double>>, entt::exclude_t<>>, decltype(entt::basic_view{istorage, std::as_const(dstorage)})>);
- static_assert(std::is_same_v<entt::basic_view<entt::get_t<const entt::storage_type_t<int>, const entt::storage_type_t<double>>, entt::exclude_t<>>, decltype(entt::basic_view{std::as_const(istorage), std::as_const(dstorage)})>);
- static_assert(std::is_same_v<entt::basic_view<entt::get_t<entt::storage_type_t<int>, entt::storage_type_t<stable_type>>, entt::exclude_t<>>, decltype(entt::basic_view{istorage, sstorage})>);
- static_assert(std::is_same_v<entt::basic_view<entt::get_t<entt::storage_type_t<int>, entt::storage_type_t<double>>, entt::exclude_t<>>, decltype(entt::basic_view{std::forward_as_tuple(istorage, dstorage), std::make_tuple()})>);
- static_assert(std::is_same_v<entt::basic_view<entt::get_t<const entt::storage_type_t<int>, entt::storage_type_t<double>>, entt::exclude_t<>>, decltype(entt::basic_view{std::forward_as_tuple(std::as_const(istorage), dstorage)})>);
- static_assert(std::is_same_v<entt::basic_view<entt::get_t<entt::storage_type_t<int>, const entt::storage_type_t<double>>, entt::exclude_t<>>, decltype(entt::basic_view{std::forward_as_tuple(istorage, std::as_const(dstorage))})>);
- static_assert(std::is_same_v<entt::basic_view<entt::get_t<const entt::storage_type_t<int>, const entt::storage_type_t<double>>, entt::exclude_t<>>, decltype(entt::basic_view{std::forward_as_tuple(std::as_const(istorage), std::as_const(dstorage))})>);
- static_assert(std::is_same_v<entt::basic_view<entt::get_t<entt::storage_type_t<int>, entt::storage_type_t<stable_type>>, entt::exclude_t<>>, decltype(entt::basic_view{std::forward_as_tuple(istorage, sstorage)})>);
- static_assert(std::is_same_v<entt::basic_view<entt::get_t<entt::storage_type_t<int>>, entt::exclude_t<entt::storage_type_t<double>>>, decltype(entt::basic_view{std::forward_as_tuple(istorage), std::forward_as_tuple(dstorage)})>);
- static_assert(std::is_same_v<entt::basic_view<entt::get_t<const entt::storage_type_t<int>>, entt::exclude_t<entt::storage_type_t<double>>>, decltype(entt::basic_view{std::forward_as_tuple(std::as_const(istorage)), std::forward_as_tuple(dstorage)})>);
- static_assert(std::is_same_v<entt::basic_view<entt::get_t<entt::storage_type_t<int>>, entt::exclude_t<const entt::storage_type_t<double>>>, decltype(entt::basic_view{std::forward_as_tuple(istorage), std::forward_as_tuple(std::as_const(dstorage))})>);
- static_assert(std::is_same_v<entt::basic_view<entt::get_t<const entt::storage_type_t<int>>, entt::exclude_t<const entt::storage_type_t<double>>>, decltype(entt::basic_view{std::forward_as_tuple(std::as_const(istorage)), std::forward_as_tuple(std::as_const(dstorage))})>);
- static_assert(std::is_same_v<entt::basic_view<entt::get_t<entt::storage_type_t<int>>, entt::exclude_t<entt::storage_type_t<stable_type>>>, decltype(entt::basic_view{std::forward_as_tuple(istorage), std::forward_as_tuple(sstorage)})>);
- }
- TEST(MultiComponentView, IterableViewAlgorithmCompatibility) {
- entt::registry registry;
- const auto entity = registry.create();
- registry.emplace<int>(entity);
- registry.emplace<char>(entity);
- const auto view = registry.view<int, char>();
- const auto iterable = view.each();
- const auto it = std::find_if(iterable.begin(), iterable.end(), [entity](auto args) { return std::get<0>(args) == entity; });
- ASSERT_EQ(std::get<0>(*it), entity);
- }
- TEST(MultiComponentView, StableType) {
- entt::registry registry;
- auto view = registry.view<int, stable_type>();
- const auto entity = registry.create();
- const auto other = registry.create();
- registry.emplace<int>(entity);
- registry.emplace<int>(other);
- registry.emplace<stable_type>(entity);
- registry.emplace<stable_type>(other);
- registry.destroy(entity);
- ASSERT_EQ(view.size_hint(), 1u);
- view = view.use<stable_type>();
- ASSERT_EQ(view.size_hint(), 2u);
- ASSERT_FALSE(view.contains(entity));
- ASSERT_TRUE(view.contains(other));
- ASSERT_EQ(view.front(), other);
- ASSERT_EQ(view.back(), other);
- ASSERT_EQ(*view.begin(), other);
- ASSERT_EQ(++view.begin(), view.end());
- view.each([other](const auto entt, int, stable_type) {
- ASSERT_EQ(other, entt);
- });
- view.each([check = true](int, stable_type) mutable {
- ASSERT_TRUE(check);
- check = false;
- });
- for(auto [entt, iv, st]: view.each()) {
- static_assert(std::is_same_v<decltype(entt), entt::entity>);
- static_assert(std::is_same_v<decltype(iv), int &>);
- static_assert(std::is_same_v<decltype(st), stable_type &>);
- ASSERT_EQ(other, entt);
- }
- registry.compact();
- ASSERT_EQ(view.size_hint(), 1u);
- }
- TEST(MultiComponentView, StableTypeWithExcludedComponent) {
- entt::registry registry;
- auto view = registry.view<stable_type>(entt::exclude<int>).use<stable_type>();
- const auto entity = registry.create();
- const auto other = registry.create();
- registry.emplace<stable_type>(entity, 0);
- registry.emplace<stable_type>(other, 42);
- registry.emplace<int>(entity);
- ASSERT_EQ(view.size_hint(), 2u);
- ASSERT_FALSE(view.contains(entity));
- ASSERT_TRUE(view.contains(other));
- registry.destroy(entity);
- ASSERT_EQ(view.size_hint(), 2u);
- ASSERT_FALSE(view.contains(entity));
- ASSERT_TRUE(view.contains(other));
- for(auto entt: view) {
- constexpr entt::entity tombstone = entt::tombstone;
- ASSERT_NE(entt, tombstone);
- ASSERT_EQ(entt, other);
- }
- for(auto [entt, comp]: view.each()) {
- constexpr entt::entity tombstone = entt::tombstone;
- ASSERT_NE(entt, tombstone);
- ASSERT_EQ(entt, other);
- ASSERT_EQ(comp.value, 42);
- }
- view.each([other](const auto entt, auto &&...) {
- constexpr entt::entity tombstone = entt::tombstone;
- ASSERT_NE(entt, tombstone);
- ASSERT_EQ(entt, other);
- });
- }
- TEST(MultiComponentView, SameComponentTypes) {
- entt::registry registry;
- entt::storage_type_t<int> storage;
- entt::storage_type_t<int> other;
- entt::basic_view view{storage, other};
- storage.bind(entt::forward_as_any(registry));
- other.bind(entt::forward_as_any(registry));
- const entt::entity e0{42u};
- const entt::entity e1{3u};
- storage.emplace(e0, 7);
- other.emplace(e0, 9);
- other.emplace(e1, 1);
- ASSERT_TRUE(view.contains(e0));
- ASSERT_FALSE(view.contains(e1));
- ASSERT_EQ((view.get<0u, 1u>(e0)), (std::make_tuple(7, 9)));
- ASSERT_EQ(view.get<1u>(e0), 9);
- for(auto entt: view) {
- ASSERT_EQ(entt, e0);
- }
- view.each([&](auto entt, auto &&first, auto &&second) {
- ASSERT_EQ(entt, e0);
- ASSERT_EQ(first, 7);
- ASSERT_EQ(second, 9);
- });
- for(auto [entt, first, second]: view.each()) {
- ASSERT_EQ(entt, e0);
- ASSERT_EQ(first, 7);
- ASSERT_EQ(second, 9);
- }
- ASSERT_EQ(&view.handle(), &storage);
- ASSERT_EQ(&view.use<1u>().handle(), &other);
- }
- TEST(MultiComponentView, Storage) {
- entt::registry registry;
- const auto entity = registry.create();
- const auto view = registry.view<int, const char>(entt::exclude<double, const float>);
- static_assert(std::is_same_v<decltype(view.storage<0u>()), entt::storage_type_t<int> &>);
- static_assert(std::is_same_v<decltype(view.storage<int>()), entt::storage_type_t<int> &>);
- static_assert(std::is_same_v<decltype(view.storage<const int>()), entt::storage_type_t<int> &>);
- static_assert(std::is_same_v<decltype(view.storage<1u>()), const entt::storage_type_t<char> &>);
- static_assert(std::is_same_v<decltype(view.storage<char>()), const entt::storage_type_t<char> &>);
- static_assert(std::is_same_v<decltype(view.storage<const char>()), const entt::storage_type_t<char> &>);
- static_assert(std::is_same_v<decltype(view.storage<2u>()), entt::storage_type_t<double> &>);
- static_assert(std::is_same_v<decltype(view.storage<double>()), entt::storage_type_t<double> &>);
- static_assert(std::is_same_v<decltype(view.storage<const double>()), entt::storage_type_t<double> &>);
- static_assert(std::is_same_v<decltype(view.storage<3u>()), const entt::storage_type_t<float> &>);
- static_assert(std::is_same_v<decltype(view.storage<float>()), const entt::storage_type_t<float> &>);
- static_assert(std::is_same_v<decltype(view.storage<const float>()), const entt::storage_type_t<float> &>);
- ASSERT_EQ(view.size_hint(), 0u);
- view.storage<int>().emplace(entity);
- view.storage<double>().emplace(entity);
- registry.emplace<char>(entity);
- registry.emplace<float>(entity);
- ASSERT_EQ(view.size_hint(), 1u);
- ASSERT_EQ(view.begin(), view.end());
- ASSERT_TRUE(view.storage<int>().contains(entity));
- ASSERT_TRUE(view.storage<const char>().contains(entity));
- ASSERT_TRUE(view.storage<double>().contains(entity));
- ASSERT_TRUE(view.storage<const float>().contains(entity));
- ASSERT_TRUE((registry.all_of<int, char, double, float>(entity)));
- view.storage<double>().erase(entity);
- registry.erase<float>(entity);
- ASSERT_EQ(view.size_hint(), 1u);
- ASSERT_NE(view.begin(), view.end());
- ASSERT_TRUE(view.storage<const int>().contains(entity));
- ASSERT_TRUE(view.storage<char>().contains(entity));
- ASSERT_FALSE(view.storage<const double>().contains(entity));
- ASSERT_FALSE(view.storage<float>().contains(entity));
- ASSERT_TRUE((registry.all_of<int, char>(entity)));
- ASSERT_FALSE((registry.any_of<double, float>(entity)));
- view.storage<0u>().erase(entity);
- ASSERT_EQ(view.size_hint(), 0u);
- ASSERT_EQ(view.begin(), view.end());
- ASSERT_FALSE(view.storage<0u>().contains(entity));
- ASSERT_TRUE(view.storage<1u>().contains(entity));
- ASSERT_FALSE(view.storage<2u>().contains(entity));
- ASSERT_FALSE(view.storage<3u>().contains(entity));
- ASSERT_TRUE((registry.all_of<char>(entity)));
- ASSERT_FALSE((registry.any_of<int, double, float>(entity)));
- }
- TEST(View, Pipe) {
- entt::registry registry;
- const auto entity = registry.create();
- const auto other = registry.create();
- registry.emplace<int>(entity);
- registry.emplace<char>(entity);
- registry.emplace<double>(entity);
- registry.emplace<empty_type>(entity);
- registry.emplace<int>(other);
- registry.emplace<char>(other);
- registry.emplace<stable_type>(other);
- const auto view1 = registry.view<int>(entt::exclude<const double>);
- const auto view2 = registry.view<const char>(entt::exclude<float>);
- const auto view3 = registry.view<empty_type>();
- const auto view4 = registry.view<stable_type>();
- static_assert(std::is_same_v<entt::basic_view<entt::get_t<entt::storage_type_t<int>, const entt::storage_type_t<char>>, entt::exclude_t<const entt::storage_type_t<double>, entt::storage_type_t<float>>>, decltype(view1 | view2)>);
- static_assert(std::is_same_v<entt::basic_view<entt::get_t<const entt::storage_type_t<char>, entt::storage_type_t<int>>, entt::exclude_t<entt::storage_type_t<float>, const entt::storage_type_t<double>>>, decltype(view2 | view1)>);
- static_assert(std::is_same_v<decltype((view3 | view2) | view1), decltype(view3 | (view2 | view1))>);
- ASSERT_FALSE((view1 | view2).contains(entity));
- ASSERT_TRUE((view1 | view2).contains(other));
- ASSERT_TRUE((view3 | view2).contains(entity));
- ASSERT_FALSE((view3 | view2).contains(other));
- ASSERT_FALSE((view1 | view2 | view3).contains(entity));
- ASSERT_FALSE((view1 | view2 | view3).contains(other));
- ASSERT_FALSE((view1 | view4 | view2).contains(entity));
- ASSERT_TRUE((view1 | view4 | view2).contains(other));
- }
|