| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323 |
- #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++, ++it), iterable.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++, ++it), iterable.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(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));
- }
- 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)));
- }
|