view.cpp 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683
  1. #include <algorithm>
  2. #include <iterator>
  3. #include <tuple>
  4. #include <type_traits>
  5. #include <utility>
  6. #include <gtest/gtest.h>
  7. #include <entt/entity/registry.hpp>
  8. #include <entt/entity/view.hpp>
  9. struct empty_type {};
  10. struct stable_type {
  11. static constexpr auto in_place_delete = true;
  12. int value;
  13. };
  14. TEST(SingleComponentView, Functionalities) {
  15. entt::registry registry;
  16. auto view = registry.view<char>();
  17. auto cview = std::as_const(registry).view<const char>();
  18. const auto e0 = registry.create();
  19. const auto e1 = registry.create();
  20. ASSERT_TRUE(view.empty());
  21. registry.emplace<char>(e1);
  22. ASSERT_NO_FATAL_FAILURE(view.begin()++);
  23. ASSERT_NO_FATAL_FAILURE(++cview.begin());
  24. ASSERT_NO_FATAL_FAILURE([](auto it) { return it++; }(view.rbegin()));
  25. ASSERT_NO_FATAL_FAILURE([](auto it) { return ++it; }(cview.rbegin()));
  26. ASSERT_NE(view.begin(), view.end());
  27. ASSERT_NE(cview.begin(), cview.end());
  28. ASSERT_NE(view.rbegin(), view.rend());
  29. ASSERT_NE(cview.rbegin(), cview.rend());
  30. ASSERT_EQ(view.size(), 1u);
  31. ASSERT_FALSE(view.empty());
  32. registry.emplace<char>(e0);
  33. ASSERT_EQ(view.size(), 2u);
  34. view.get<char>(e0) = '1';
  35. std::get<0>(view.get(e1)) = '2';
  36. ASSERT_EQ(view.get<0u>(e0), '1');
  37. ASSERT_EQ(cview.get<0u>(e0), view.get<char>(e0));
  38. ASSERT_EQ(view.get<char>(e1), '2');
  39. for(auto entity: view) {
  40. ASSERT_TRUE(entity == e0 || entity == e1);
  41. ASSERT_TRUE(entity != e0 || cview.get<const char>(entity) == '1');
  42. ASSERT_TRUE(entity != e1 || std::get<const char &>(cview.get(entity)) == '2');
  43. }
  44. registry.erase<char>(e0);
  45. registry.erase<char>(e1);
  46. ASSERT_EQ(view.begin(), view.end());
  47. ASSERT_EQ(view.rbegin(), view.rend());
  48. ASSERT_TRUE(view.empty());
  49. decltype(view) invalid{};
  50. ASSERT_TRUE(view);
  51. ASSERT_TRUE(cview);
  52. ASSERT_FALSE(invalid);
  53. }
  54. TEST(SingleComponentView, InvalidView) {
  55. entt::basic_view<entt::get_t<entt::storage<int>>, entt::exclude_t<>> view{};
  56. ASSERT_FALSE(view);
  57. ASSERT_EQ(view.size(), 0u);
  58. ASSERT_TRUE(view.empty());
  59. ASSERT_FALSE(view.contains(entt::null));
  60. ASSERT_EQ(view.find(entt::null), view.end());
  61. ASSERT_EQ(view.front(), static_cast<entt::entity>(entt::null));
  62. ASSERT_EQ(view.back(), static_cast<entt::entity>(entt::null));
  63. ASSERT_EQ(view.begin(), typename decltype(view)::iterator{});
  64. ASSERT_EQ(view.begin(), view.end());
  65. ASSERT_EQ(view.rbegin(), typename decltype(view)::reverse_iterator{});
  66. ASSERT_EQ(view.rbegin(), view.rend());
  67. auto iterable = view.each();
  68. ASSERT_EQ(iterable.begin(), iterable.end());
  69. ASSERT_EQ(iterable.cbegin(), iterable.cend());
  70. view.each([](const int &) { FAIL(); });
  71. view.each([](const entt::entity, const int &) { FAIL(); });
  72. entt::storage<int> storage;
  73. view.storage(storage);
  74. ASSERT_TRUE(view);
  75. view.each([](const int &) { FAIL(); });
  76. view.each([](const entt::entity, const int &) { FAIL(); });
  77. }
  78. TEST(SingleComponentView, Constructors) {
  79. entt::storage<int> storage{};
  80. entt::view<entt::get_t<int>> invalid{};
  81. entt::basic_view from_storage{storage};
  82. entt::basic_view from_tuple{std::forward_as_tuple(storage)};
  83. ASSERT_FALSE(invalid);
  84. ASSERT_TRUE(from_storage);
  85. ASSERT_TRUE(from_tuple);
  86. ASSERT_NE(from_storage.handle(), nullptr);
  87. ASSERT_EQ(from_storage.handle(), from_tuple.handle());
  88. }
  89. TEST(SingleComponentView, Handle) {
  90. entt::registry registry;
  91. const auto entity = registry.create();
  92. auto view = registry.view<int>();
  93. auto *handle = view.handle();
  94. ASSERT_NE(handle, nullptr);
  95. ASSERT_TRUE(handle->empty());
  96. ASSERT_FALSE(handle->contains(entity));
  97. ASSERT_EQ(handle, view.handle());
  98. registry.emplace<int>(entity);
  99. ASSERT_FALSE(handle->empty());
  100. ASSERT_TRUE(handle->contains(entity));
  101. ASSERT_EQ(handle, view.handle());
  102. }
  103. TEST(SingleComponentView, LazyTypeFromConstRegistry) {
  104. entt::registry registry{};
  105. auto eview = std::as_const(registry).view<const empty_type>();
  106. auto cview = std::as_const(registry).view<const int>();
  107. const auto entity = registry.create();
  108. registry.emplace<empty_type>(entity);
  109. registry.emplace<int>(entity);
  110. ASSERT_FALSE(cview);
  111. ASSERT_FALSE(eview);
  112. ASSERT_TRUE(cview.empty());
  113. ASSERT_EQ(eview.size(), 0u);
  114. ASSERT_FALSE(cview.contains(entity));
  115. ASSERT_EQ(cview.begin(), cview.end());
  116. ASSERT_EQ(eview.rbegin(), eview.rend());
  117. ASSERT_EQ(eview.find(entity), eview.end());
  118. ASSERT_NE(cview.front(), entity);
  119. ASSERT_NE(eview.back(), entity);
  120. }
  121. TEST(SingleComponentView, ElementAccess) {
  122. entt::registry registry;
  123. auto view = registry.view<int>();
  124. auto cview = std::as_const(registry).view<const int>();
  125. const auto e0 = registry.create();
  126. registry.emplace<int>(e0, 42);
  127. const auto e1 = registry.create();
  128. registry.emplace<int>(e1, 3);
  129. for(auto i = 0u; i < view.size(); ++i) {
  130. ASSERT_EQ(view[i], i ? e0 : e1);
  131. ASSERT_EQ(cview[i], i ? e0 : e1);
  132. }
  133. ASSERT_EQ(view[e0], 42);
  134. ASSERT_EQ(cview[e1], 3);
  135. }
  136. TEST(SingleComponentView, Contains) {
  137. entt::registry registry;
  138. const auto e0 = registry.create();
  139. registry.emplace<int>(e0);
  140. const auto e1 = registry.create();
  141. registry.emplace<int>(e1);
  142. registry.destroy(e0);
  143. auto view = registry.view<int>();
  144. ASSERT_FALSE(view.contains(e0));
  145. ASSERT_TRUE(view.contains(e1));
  146. }
  147. TEST(SingleComponentView, Empty) {
  148. entt::registry registry;
  149. auto view = registry.view<int>();
  150. ASSERT_EQ(view.size(), 0u);
  151. ASSERT_EQ(view.begin(), view.end());
  152. ASSERT_EQ(view.rbegin(), view.rend());
  153. }
  154. TEST(SingleComponentView, Each) {
  155. entt::registry registry;
  156. entt::entity entity[2]{registry.create(), registry.create()};
  157. auto view = registry.view<int>(entt::exclude<double>);
  158. auto cview = std::as_const(registry).view<const int>();
  159. registry.emplace<int>(entity[0u], 0);
  160. registry.emplace<int>(entity[1u], 1);
  161. auto iterable = view.each();
  162. auto citerable = cview.each();
  163. ASSERT_NE(citerable.begin(), citerable.end());
  164. ASSERT_NO_FATAL_FAILURE(iterable.begin()->operator=(*iterable.begin()));
  165. ASSERT_EQ(decltype(iterable.end()){}, iterable.end());
  166. auto it = iterable.begin();
  167. ASSERT_EQ(it.base(), view.begin());
  168. ASSERT_EQ((it++, ++it), iterable.end());
  169. ASSERT_EQ(it.base(), view.end());
  170. view.each([expected = 1](auto entt, int &value) mutable {
  171. ASSERT_EQ(static_cast<int>(entt::to_integral(entt)), expected);
  172. ASSERT_EQ(value, expected);
  173. --expected;
  174. });
  175. cview.each([expected = 1](const int &value) mutable {
  176. ASSERT_EQ(value, expected);
  177. --expected;
  178. });
  179. ASSERT_EQ(std::get<0>(*iterable.begin()), entity[1u]);
  180. ASSERT_EQ(std::get<0>(*++citerable.begin()), entity[0u]);
  181. static_assert(std::is_same_v<decltype(std::get<1>(*iterable.begin())), int &>);
  182. static_assert(std::is_same_v<decltype(std::get<1>(*citerable.begin())), const int &>);
  183. // do not use iterable, make sure an iterable view works when created from a temporary
  184. for(auto [entt, value]: view.each()) {
  185. ASSERT_EQ(static_cast<int>(entt::to_integral(entt)), value);
  186. }
  187. }
  188. TEST(SingleComponentView, ConstNonConstAndAllInBetween) {
  189. entt::registry registry;
  190. auto view = registry.view<int>();
  191. auto cview = std::as_const(registry).view<const int>();
  192. ASSERT_EQ(view.size(), 0u);
  193. ASSERT_EQ(cview.size(), 0u);
  194. registry.emplace<int>(registry.create(), 0);
  195. ASSERT_EQ(view.size(), 1u);
  196. ASSERT_EQ(cview.size(), 1u);
  197. static_assert(std::is_same_v<decltype(view.get<0u>({})), int &>);
  198. static_assert(std::is_same_v<decltype(view.get<int>({})), int &>);
  199. static_assert(std::is_same_v<decltype(view.get({})), std::tuple<int &>>);
  200. static_assert(std::is_same_v<decltype(cview.get<0u>({})), const int &>);
  201. static_assert(std::is_same_v<decltype(cview.get<const int>({})), const int &>);
  202. static_assert(std::is_same_v<decltype(cview.get({})), std::tuple<const int &>>);
  203. static_assert(std::is_same_v<decltype(std::as_const(registry).view<int>()), decltype(cview)>);
  204. view.each([](auto &&i) {
  205. static_assert(std::is_same_v<decltype(i), int &>);
  206. });
  207. cview.each([](auto &&i) {
  208. static_assert(std::is_same_v<decltype(i), const int &>);
  209. });
  210. for(auto [entt, iv]: view.each()) {
  211. static_assert(std::is_same_v<decltype(entt), entt::entity>);
  212. static_assert(std::is_same_v<decltype(iv), int &>);
  213. }
  214. for(auto [entt, iv]: cview.each()) {
  215. static_assert(std::is_same_v<decltype(entt), entt::entity>);
  216. static_assert(std::is_same_v<decltype(iv), const int &>);
  217. }
  218. }
  219. TEST(SingleComponentView, ConstNonConstAndAllInBetweenWithEmptyType) {
  220. entt::registry registry;
  221. auto view = registry.view<empty_type>();
  222. auto cview = std::as_const(registry).view<const empty_type>();
  223. ASSERT_EQ(view.size(), 0u);
  224. ASSERT_EQ(cview.size(), 0u);
  225. registry.emplace<empty_type>(registry.create());
  226. ASSERT_EQ(view.size(), 1u);
  227. ASSERT_EQ(cview.size(), 1u);
  228. static_assert(std::is_same_v<decltype(view.get({})), std::tuple<>>);
  229. static_assert(std::is_same_v<decltype(cview.get({})), std::tuple<>>);
  230. static_assert(std::is_same_v<decltype(std::as_const(registry).view<empty_type>()), decltype(cview)>);
  231. for(auto [entt]: view.each()) {
  232. static_assert(std::is_same_v<decltype(entt), entt::entity>);
  233. }
  234. for(auto [entt]: cview.each()) {
  235. static_assert(std::is_same_v<decltype(entt), entt::entity>);
  236. }
  237. }
  238. TEST(SingleComponentView, Find) {
  239. entt::registry registry;
  240. auto view = registry.view<int>();
  241. const auto e0 = registry.create();
  242. registry.emplace<int>(e0);
  243. const auto e1 = registry.create();
  244. registry.emplace<int>(e1);
  245. const auto e2 = registry.create();
  246. registry.emplace<int>(e2);
  247. const auto e3 = registry.create();
  248. registry.emplace<int>(e3);
  249. registry.erase<int>(e1);
  250. ASSERT_NE(view.find(e0), view.end());
  251. ASSERT_EQ(view.find(e1), view.end());
  252. ASSERT_NE(view.find(e2), view.end());
  253. ASSERT_NE(view.find(e3), view.end());
  254. auto it = view.find(e2);
  255. ASSERT_EQ(*it, e2);
  256. ASSERT_EQ(*(++it), e3);
  257. ASSERT_EQ(*(++it), e0);
  258. ASSERT_EQ(++it, view.end());
  259. ASSERT_EQ(++view.find(e0), view.end());
  260. const auto e4 = registry.create();
  261. registry.destroy(e4);
  262. const auto e5 = registry.create();
  263. registry.emplace<int>(e5);
  264. ASSERT_NE(view.find(e5), view.end());
  265. ASSERT_EQ(view.find(e4), view.end());
  266. }
  267. TEST(SingleComponentView, EmptyTypes) {
  268. entt::registry registry;
  269. entt::entity entity = registry.create();
  270. registry.emplace<int>(entity, 0);
  271. registry.emplace<empty_type>(entity);
  272. registry.view<empty_type>().each([&](const auto entt) {
  273. ASSERT_EQ(entity, entt);
  274. });
  275. registry.view<empty_type>().each([check = true]() mutable {
  276. ASSERT_TRUE(check);
  277. check = false;
  278. });
  279. for(auto [entt]: registry.view<empty_type>().each()) {
  280. static_assert(std::is_same_v<decltype(entt), entt::entity>);
  281. ASSERT_EQ(entity, entt);
  282. }
  283. registry.view<int>().each([&](const auto entt, int) {
  284. ASSERT_EQ(entity, entt);
  285. });
  286. registry.view<int>().each([check = true](int) mutable {
  287. ASSERT_TRUE(check);
  288. check = false;
  289. });
  290. for(auto [entt, iv]: registry.view<int>().each()) {
  291. static_assert(std::is_same_v<decltype(entt), entt::entity>);
  292. static_assert(std::is_same_v<decltype(iv), int &>);
  293. ASSERT_EQ(entity, entt);
  294. }
  295. }
  296. TEST(SingleComponentView, FrontBack) {
  297. entt::registry registry;
  298. auto view = registry.view<const int>();
  299. ASSERT_EQ(view.front(), static_cast<entt::entity>(entt::null));
  300. ASSERT_EQ(view.back(), static_cast<entt::entity>(entt::null));
  301. const auto e0 = registry.create();
  302. registry.emplace<int>(e0);
  303. const auto e1 = registry.create();
  304. registry.emplace<int>(e1);
  305. ASSERT_EQ(view.front(), e1);
  306. ASSERT_EQ(view.back(), e0);
  307. }
  308. TEST(SingleComponentView, DeductionGuide) {
  309. entt::registry registry;
  310. entt::storage_type_t<int> istorage;
  311. entt::storage_type_t<stable_type> sstorage;
  312. 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})>);
  313. 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)})>);
  314. 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})>);
  315. 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()})>);
  316. 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))})>);
  317. 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)})>);
  318. }
  319. TEST(SingleComponentView, IterableViewAlgorithmCompatibility) {
  320. entt::registry registry;
  321. const auto entity = registry.create();
  322. registry.emplace<int>(entity);
  323. const auto view = registry.view<int>();
  324. const auto iterable = view.each();
  325. const auto it = std::find_if(iterable.begin(), iterable.end(), [entity](auto args) { return std::get<0>(args) == entity; });
  326. ASSERT_EQ(std::get<0>(*it), entity);
  327. }
  328. TEST(SingleComponentView, StableType) {
  329. entt::registry registry;
  330. auto view = registry.view<stable_type>();
  331. const auto entity = registry.create();
  332. const auto other = registry.create();
  333. registry.emplace<stable_type>(entity);
  334. registry.emplace<stable_type>(other);
  335. registry.destroy(entity);
  336. ASSERT_EQ(view.size_hint(), 2u);
  337. ASSERT_FALSE(view.contains(entity));
  338. ASSERT_TRUE(view.contains(other));
  339. ASSERT_EQ(view.front(), other);
  340. ASSERT_EQ(view.back(), other);
  341. ASSERT_EQ(*view.begin(), other);
  342. ASSERT_EQ(++view.begin(), view.end());
  343. view.each([other](const auto entt, stable_type) {
  344. ASSERT_EQ(other, entt);
  345. });
  346. view.each([check = true](stable_type) mutable {
  347. ASSERT_TRUE(check);
  348. check = false;
  349. });
  350. for(auto [entt, st]: view.each()) {
  351. static_assert(std::is_same_v<decltype(entt), entt::entity>);
  352. static_assert(std::is_same_v<decltype(st), stable_type &>);
  353. ASSERT_EQ(other, entt);
  354. }
  355. registry.compact();
  356. ASSERT_EQ(view.size_hint(), 1u);
  357. }
  358. TEST(SingleComponentView, Storage) {
  359. entt::registry registry;
  360. const auto entity = registry.create();
  361. auto view = registry.view<int>();
  362. auto cview = registry.view<const char>();
  363. static_assert(std::is_same_v<decltype(view.storage()), entt::storage_type_t<int> *>);
  364. static_assert(std::is_same_v<decltype(view.storage<0u>()), entt::storage_type_t<int> *>);
  365. static_assert(std::is_same_v<decltype(view.storage<int>()), entt::storage_type_t<int> *>);
  366. static_assert(std::is_same_v<decltype(view.storage<const int>()), entt::storage_type_t<int> *>);
  367. static_assert(std::is_same_v<decltype(cview.storage()), const entt::storage_type_t<char> *>);
  368. static_assert(std::is_same_v<decltype(cview.storage<0u>()), const entt::storage_type_t<char> *>);
  369. static_assert(std::is_same_v<decltype(cview.storage<char>()), const entt::storage_type_t<char> *>);
  370. static_assert(std::is_same_v<decltype(cview.storage<const char>()), const entt::storage_type_t<char> *>);
  371. ASSERT_TRUE(view);
  372. ASSERT_TRUE(cview);
  373. ASSERT_NE(view.storage<int>(), nullptr);
  374. ASSERT_NE(cview.storage<0u>(), nullptr);
  375. ASSERT_EQ(view.size(), 0u);
  376. ASSERT_EQ(cview.size(), 0u);
  377. view.storage()->emplace(entity);
  378. registry.emplace<char>(entity);
  379. ASSERT_EQ(view.size(), 1u);
  380. ASSERT_EQ(cview.size(), 1u);
  381. ASSERT_TRUE(view.storage<int>()->contains(entity));
  382. ASSERT_TRUE(cview.storage<0u>()->contains(entity));
  383. ASSERT_TRUE((registry.all_of<int, char>(entity)));
  384. view.storage()->erase(entity);
  385. ASSERT_EQ(view.size(), 0u);
  386. ASSERT_EQ(cview.size(), 1u);
  387. ASSERT_FALSE(view.storage<0u>()->contains(entity));
  388. ASSERT_TRUE(cview.storage<const char>()->contains(entity));
  389. ASSERT_FALSE((registry.all_of<int, char>(entity)));
  390. view = {};
  391. cview = {};
  392. ASSERT_FALSE(view);
  393. ASSERT_FALSE(cview);
  394. ASSERT_EQ(view.storage<0u>(), nullptr);
  395. ASSERT_EQ(cview.storage<const char>(), nullptr);
  396. }
  397. TEST(SingleComponentView, SwapStorage) {
  398. using namespace entt::literals;
  399. entt::registry registry;
  400. entt::basic_view<entt::get_t<entt::storage<int>>, entt::exclude_t<>> view;
  401. entt::basic_view<entt::get_t<const entt::storage<int>>, entt::exclude_t<>> cview;
  402. ASSERT_FALSE(view);
  403. ASSERT_FALSE(cview);
  404. ASSERT_EQ(view.storage<0u>(), nullptr);
  405. ASSERT_EQ(cview.storage<const int>(), nullptr);
  406. const entt::entity entity{42u};
  407. registry.emplace<int>(entity);
  408. view.storage(registry.storage<int>());
  409. cview.storage(registry.storage<int>());
  410. ASSERT_TRUE(view);
  411. ASSERT_TRUE(cview);
  412. ASSERT_NE(view.storage<0u>(), nullptr);
  413. ASSERT_NE(cview.storage<const int>(), nullptr);
  414. ASSERT_EQ(view.size(), 1u);
  415. ASSERT_EQ(cview.size(), 1u);
  416. ASSERT_TRUE(view.contains(entity));
  417. ASSERT_TRUE(cview.contains(entity));
  418. view.storage(registry.storage<int>("other"_hs));
  419. cview.storage(registry.storage<int>("other"_hs));
  420. ASSERT_TRUE(view.empty());
  421. ASSERT_TRUE(cview.empty());
  422. }
  423. TEST(SingleComponentView, StorageEntity) {
  424. entt::registry registry;
  425. auto view = registry.view<entt::entity>();
  426. const auto entity = registry.create();
  427. const auto other = registry.create();
  428. registry.destroy(entity, entt::to_version(entity));
  429. ASSERT_EQ(view.size(), 2u);
  430. ASSERT_NE(view.begin(), view.end());
  431. // returns all matching identifiers, both in-use and available ones
  432. ASSERT_EQ(std::distance(view.begin(), view.end()), 2);
  433. ASSERT_EQ(*view.begin(), entity);
  434. ASSERT_EQ(*(++view.begin()), other);
  435. // skips available identifiers automatically, only returns in-use elements
  436. for(auto [entt]: view.each()) {
  437. ASSERT_EQ(entt, other);
  438. }
  439. // skips available identifiers automatically, only returns in-use elements
  440. view.each([other](auto entt) {
  441. ASSERT_EQ(entt, other);
  442. });
  443. }
  444. TEST(MultiComponentView, Functionalities) {
  445. entt::registry registry;
  446. auto view = registry.view<int, char>();
  447. auto cview = std::as_const(registry).view<const int, const char>();
  448. const auto e0 = registry.create();
  449. registry.emplace<char>(e0, '1');
  450. const auto e1 = registry.create();
  451. registry.emplace<int>(e1, 42);
  452. registry.emplace<char>(e1, '2');
  453. ASSERT_EQ(*view.begin(), e1);
  454. ASSERT_EQ(*cview.begin(), e1);
  455. ASSERT_EQ(++view.begin(), (view.end()));
  456. ASSERT_EQ(++cview.begin(), (cview.end()));
  457. ASSERT_NO_FATAL_FAILURE((view.begin()++));
  458. ASSERT_NO_FATAL_FAILURE((++cview.begin()));
  459. ASSERT_NE(view.begin(), view.end());
  460. ASSERT_NE(cview.begin(), cview.end());
  461. ASSERT_EQ(view.size_hint(), 1u);
  462. for(auto entity: view) {
  463. ASSERT_EQ(std::get<0>(cview.get<const int, const char>(entity)), 42);
  464. ASSERT_EQ(std::get<0>(cview.get<0u, 1u>(entity)), 42);
  465. ASSERT_EQ(std::get<1>(view.get<int, char>(entity)), '2');
  466. ASSERT_EQ(std::get<1>(view.get<0u, 1u>(entity)), '2');
  467. ASSERT_EQ(cview.get<const char>(entity), '2');
  468. ASSERT_EQ(cview.get<1u>(entity), '2');
  469. }
  470. decltype(view) invalid{};
  471. ASSERT_TRUE(view);
  472. ASSERT_TRUE(cview);
  473. ASSERT_FALSE(invalid);
  474. }
  475. TEST(MultiComponentView, InvalidView) {
  476. entt::basic_view<entt::get_t<entt::storage<int>>, entt::exclude_t<entt::storage<char>>> view{};
  477. ASSERT_FALSE(view);
  478. ASSERT_EQ(view.size_hint(), 0u);
  479. ASSERT_FALSE(view.contains(entt::null));
  480. ASSERT_EQ(view.find(entt::null), view.end());
  481. ASSERT_EQ(view.front(), static_cast<entt::entity>(entt::null));
  482. ASSERT_EQ(view.back(), static_cast<entt::entity>(entt::null));
  483. ASSERT_EQ(view.begin(), typename decltype(view)::iterator{});
  484. ASSERT_EQ(view.begin(), view.end());
  485. auto iterable = view.each();
  486. ASSERT_EQ(iterable.begin(), iterable.end());
  487. ASSERT_EQ(iterable.cbegin(), iterable.cend());
  488. view.each([](const int &) { FAIL(); });
  489. view.each([](const entt::entity, const int &) { FAIL(); });
  490. entt::storage<int> storage;
  491. view.storage(storage);
  492. ASSERT_FALSE(view);
  493. view.each([](const int &) { FAIL(); });
  494. view.each([](const entt::entity, const int &) { FAIL(); });
  495. entt::storage<char> other;
  496. view.storage(other);
  497. ASSERT_TRUE(view);
  498. view.each([](const int &) { FAIL(); });
  499. view.each([](const entt::entity, const int &) { FAIL(); });
  500. }
  501. TEST(MultiComponentView, Constructors) {
  502. entt::storage<int> storage{};
  503. entt::view<entt::get_t<int, int>> invalid{};
  504. entt::basic_view from_storage{storage, storage};
  505. entt::basic_view from_tuple{std::forward_as_tuple(storage, storage)};
  506. ASSERT_FALSE(invalid);
  507. ASSERT_TRUE(from_storage);
  508. ASSERT_TRUE(from_tuple);
  509. ASSERT_NE(from_storage.handle(), nullptr);
  510. ASSERT_EQ(from_storage.handle(), from_tuple.handle());
  511. }
  512. TEST(MultiComponentView, Handle) {
  513. entt::registry registry;
  514. const auto entity = registry.create();
  515. auto view = registry.view<int, char>();
  516. auto *handle = view.handle();
  517. ASSERT_NE(handle, nullptr);
  518. ASSERT_TRUE(handle->empty());
  519. ASSERT_FALSE(handle->contains(entity));
  520. ASSERT_EQ(handle, view.handle());
  521. registry.emplace<int>(entity);
  522. ASSERT_FALSE(handle->empty());
  523. ASSERT_TRUE(handle->contains(entity));
  524. ASSERT_EQ(handle, view.handle());
  525. view.refresh();
  526. auto *other = view.handle();
  527. ASSERT_NE(other, nullptr);
  528. ASSERT_TRUE(other->empty());
  529. ASSERT_FALSE(other->contains(entity));
  530. ASSERT_EQ(other, view.handle());
  531. ASSERT_NE(handle, other);
  532. view.use<int>();
  533. ASSERT_NE(other, view.handle());
  534. ASSERT_EQ(handle, view.handle());
  535. }
  536. TEST(MultiComponentView, LazyTypesFromConstRegistry) {
  537. entt::registry registry{};
  538. auto view = std::as_const(registry).view<const empty_type, const int>();
  539. const auto entity = registry.create();
  540. registry.emplace<empty_type>(entity);
  541. registry.emplace<int>(entity);
  542. ASSERT_FALSE(view);
  543. ASSERT_EQ(view.size_hint(), 0u);
  544. ASSERT_FALSE(view.contains(entity));
  545. ASSERT_EQ(view.begin(), view.end());
  546. ASSERT_EQ(view.find(entity), view.end());
  547. ASSERT_NE(view.front(), entity);
  548. ASSERT_NE(view.back(), entity);
  549. }
  550. TEST(MultiComponentView, LazyExcludedTypeFromConstRegistry) {
  551. entt::registry registry;
  552. auto entity = registry.create();
  553. registry.emplace<int>(entity);
  554. auto view = std::as_const(registry).view<const int>(entt::exclude<char>);
  555. ASSERT_FALSE(view);
  556. ASSERT_EQ(view.size_hint(), 1u);
  557. ASSERT_TRUE(view.contains(entity));
  558. ASSERT_NE(view.begin(), view.end());
  559. ASSERT_NE(view.find(entity), view.end());
  560. ASSERT_EQ(view.front(), entity);
  561. ASSERT_EQ(view.back(), entity);
  562. }
  563. TEST(MultiComponentView, Iterator) {
  564. entt::registry registry;
  565. const entt::entity entity[2]{registry.create(), registry.create()};
  566. registry.insert<int>(std::begin(entity), std::end(entity));
  567. registry.insert<char>(std::begin(entity), std::end(entity));
  568. const auto view = registry.view<int, char>();
  569. using iterator = typename decltype(view)::iterator;
  570. iterator end{view.begin()};
  571. iterator begin{};
  572. begin = view.end();
  573. std::swap(begin, end);
  574. ASSERT_EQ(begin, view.begin());
  575. ASSERT_EQ(end, view.end());
  576. ASSERT_NE(begin, end);
  577. ASSERT_EQ(*begin, entity[1u]);
  578. ASSERT_EQ(*begin.operator->(), entity[1u]);
  579. ASSERT_EQ(begin++, view.begin());
  580. ASSERT_EQ(*begin, entity[0u]);
  581. ASSERT_EQ(*begin.operator->(), entity[0u]);
  582. ASSERT_EQ(++begin, view.end());
  583. }
  584. TEST(MultiComponentView, ElementAccess) {
  585. entt::registry registry;
  586. auto view = registry.view<int, char>();
  587. auto cview = std::as_const(registry).view<const int, const char>();
  588. const auto e0 = registry.create();
  589. registry.emplace<int>(e0, 42);
  590. registry.emplace<char>(e0, '0');
  591. const auto e1 = registry.create();
  592. registry.emplace<int>(e1, 3);
  593. registry.emplace<char>(e1, '1');
  594. ASSERT_EQ(view[e0], std::make_tuple(42, '0'));
  595. ASSERT_EQ(cview[e1], std::make_tuple(3, '1'));
  596. }
  597. TEST(MultiComponentView, Contains) {
  598. entt::registry registry;
  599. const auto e0 = registry.create();
  600. registry.emplace<int>(e0);
  601. registry.emplace<char>(e0);
  602. const auto e1 = registry.create();
  603. registry.emplace<int>(e1);
  604. registry.emplace<char>(e1);
  605. registry.destroy(e0);
  606. auto view = registry.view<int, char>();
  607. ASSERT_FALSE(view.contains(e0));
  608. ASSERT_TRUE(view.contains(e1));
  609. }
  610. TEST(MultiComponentView, SizeHint) {
  611. entt::registry registry;
  612. const auto e0 = registry.create();
  613. registry.emplace<int>(e0);
  614. registry.emplace<float>(e0);
  615. const auto e1 = registry.create();
  616. registry.emplace<char>(e1);
  617. registry.emplace<float>(e1);
  618. auto view = registry.view<char, int, float>();
  619. ASSERT_EQ(view.size_hint(), 1u);
  620. ASSERT_EQ(view.begin(), view.end());
  621. }
  622. TEST(MultiComponentView, UseAndRefresh) {
  623. entt::registry registry;
  624. entt::entity entity[3]{registry.create(), registry.create(), registry.create()};
  625. registry.emplace<int>(entity[0u]);
  626. registry.emplace<int>(entity[1u]);
  627. registry.emplace<char>(entity[1u]);
  628. registry.emplace<char>(entity[0u]);
  629. registry.emplace<char>(entity[2u]);
  630. auto view = registry.view<int, char>(entt::exclude<double>);
  631. view.use<int>();
  632. ASSERT_EQ(view.handle()->type(), entt::type_id<int>());
  633. ASSERT_EQ(view.front(), entity[1u]);
  634. ASSERT_EQ(view.back(), entity[0u]);
  635. view.use<char>();
  636. ASSERT_EQ(view.handle()->type(), entt::type_id<char>());
  637. ASSERT_EQ(view.front(), entity[0u]);
  638. ASSERT_EQ(view.back(), entity[1u]);
  639. view.refresh();
  640. ASSERT_EQ(view.handle()->type(), entt::type_id<int>());
  641. }
  642. TEST(MultiComponentView, Each) {
  643. entt::registry registry;
  644. entt::entity entity[2]{registry.create(), registry.create()};
  645. auto view = registry.view<int, char>(entt::exclude<double>);
  646. auto cview = std::as_const(registry).view<const int, const char>();
  647. registry.emplace<int>(entity[0u], 0);
  648. registry.emplace<char>(entity[0u], static_cast<char>(0));
  649. registry.emplace<int>(entity[1u], 1);
  650. registry.emplace<char>(entity[1u], static_cast<char>(1));
  651. auto iterable = view.each();
  652. auto citerable = cview.each();
  653. ASSERT_NE(citerable.begin(), citerable.end());
  654. ASSERT_NO_FATAL_FAILURE(iterable.begin()->operator=(*iterable.begin()));
  655. ASSERT_EQ(decltype(iterable.end()){}, iterable.end());
  656. auto it = iterable.begin();
  657. ASSERT_EQ(it.base(), view.begin());
  658. ASSERT_EQ((it++, ++it), iterable.end());
  659. ASSERT_EQ(it.base(), view.end());
  660. view.each([expected = 1](auto entt, int &ivalue, char &cvalue) mutable {
  661. ASSERT_EQ(static_cast<int>(entt::to_integral(entt)), expected);
  662. ASSERT_EQ(ivalue, expected);
  663. ASSERT_EQ(cvalue, expected);
  664. --expected;
  665. });
  666. cview.each([expected = 1](const int &ivalue, const char &cvalue) mutable {
  667. ASSERT_EQ(ivalue, expected);
  668. ASSERT_EQ(cvalue, expected);
  669. --expected;
  670. });
  671. ASSERT_EQ(std::get<0>(*iterable.begin()), entity[1u]);
  672. ASSERT_EQ(std::get<0>(*++citerable.begin()), entity[0u]);
  673. static_assert(std::is_same_v<decltype(std::get<1>(*iterable.begin())), int &>);
  674. static_assert(std::is_same_v<decltype(std::get<2>(*citerable.begin())), const char &>);
  675. // do not use iterable, make sure an iterable view works when created from a temporary
  676. for(auto [entt, ivalue, cvalue]: registry.view<int, char>().each()) {
  677. ASSERT_EQ(static_cast<int>(entt::to_integral(entt)), ivalue);
  678. ASSERT_EQ(static_cast<char>(entt::to_integral(entt)), cvalue);
  679. }
  680. }
  681. TEST(MultiComponentView, EachWithSuggestedType) {
  682. entt::registry registry;
  683. auto view = registry.view<int, char>();
  684. for(auto i = 0; i < 3; ++i) {
  685. const auto entity = registry.create();
  686. registry.emplace<int>(entity, i);
  687. registry.emplace<char>(entity);
  688. }
  689. // makes char a better candidate during iterations
  690. const auto entity = registry.create();
  691. registry.emplace<int>(entity, 99);
  692. view.use<int>();
  693. view.each([value = 2](const auto curr, const auto) mutable {
  694. ASSERT_EQ(curr, value--);
  695. });
  696. registry.sort<int>([](const auto lhs, const auto rhs) {
  697. return lhs < rhs;
  698. });
  699. view.use<0u>();
  700. view.each([value = 0](const auto curr, const auto) mutable {
  701. ASSERT_EQ(curr, value++);
  702. });
  703. registry.sort<int>([](const auto lhs, const auto rhs) {
  704. return lhs > rhs;
  705. });
  706. auto value = registry.view<int, char>().size_hint();
  707. for(auto &&curr: registry.view<int, char>().each()) {
  708. ASSERT_EQ(std::get<1>(curr), static_cast<int>(--value));
  709. }
  710. registry.sort<int>([](const auto lhs, const auto rhs) {
  711. return lhs < rhs;
  712. });
  713. value = {};
  714. view.use<int>();
  715. for(auto &&curr: view.each()) {
  716. ASSERT_EQ(std::get<1>(curr), static_cast<int>(value++));
  717. }
  718. }
  719. TEST(MultiComponentView, EachWithHoles) {
  720. entt::registry registry;
  721. const auto e0 = registry.create();
  722. const auto e1 = registry.create();
  723. const auto e2 = registry.create();
  724. registry.emplace<char>(e0, '0');
  725. registry.emplace<char>(e1, '1');
  726. registry.emplace<int>(e0, 0);
  727. registry.emplace<int>(e2, 2);
  728. auto view = registry.view<char, int>();
  729. view.each([e0](auto entity, const char &c, const int &i) {
  730. ASSERT_EQ(entity, e0);
  731. ASSERT_EQ(c, '0');
  732. ASSERT_EQ(i, 0);
  733. });
  734. for(auto &&curr: view.each()) {
  735. ASSERT_EQ(std::get<0>(curr), e0);
  736. ASSERT_EQ(std::get<1>(curr), '0');
  737. ASSERT_EQ(std::get<2>(curr), 0);
  738. }
  739. }
  740. TEST(MultiComponentView, ConstNonConstAndAllInBetween) {
  741. entt::registry registry;
  742. auto view = registry.view<int, empty_type, const char>();
  743. ASSERT_EQ(view.size_hint(), 0u);
  744. const auto entity = registry.create();
  745. registry.emplace<int>(entity, 0);
  746. registry.emplace<empty_type>(entity);
  747. registry.emplace<char>(entity, 'c');
  748. ASSERT_EQ(view.size_hint(), 1u);
  749. static_assert(std::is_same_v<decltype(view.get<0u>({})), int &>);
  750. static_assert(std::is_same_v<decltype(view.get<2u>({})), const char &>);
  751. static_assert(std::is_same_v<decltype(view.get<0u, 2u>({})), std::tuple<int &, const char &>>);
  752. static_assert(std::is_same_v<decltype(view.get<int>({})), int &>);
  753. static_assert(std::is_same_v<decltype(view.get<const char>({})), const char &>);
  754. static_assert(std::is_same_v<decltype(view.get<int, const char>({})), std::tuple<int &, const char &>>);
  755. static_assert(std::is_same_v<decltype(view.get({})), std::tuple<int &, const char &>>);
  756. static_assert(std::is_same_v<decltype(std::as_const(registry).view<char, int>()), decltype(std::as_const(registry).view<const char, const int>())>);
  757. 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>())>);
  758. 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>())>);
  759. view.each([](auto &&i, auto &&c) {
  760. static_assert(std::is_same_v<decltype(i), int &>);
  761. static_assert(std::is_same_v<decltype(c), const char &>);
  762. });
  763. for(auto [entt, iv, cv]: view.each()) {
  764. static_assert(std::is_same_v<decltype(entt), entt::entity>);
  765. static_assert(std::is_same_v<decltype(iv), int &>);
  766. static_assert(std::is_same_v<decltype(cv), const char &>);
  767. }
  768. }
  769. TEST(MultiComponentView, Find) {
  770. entt::registry registry;
  771. auto view = registry.view<int, const char>();
  772. const auto e0 = registry.create();
  773. registry.emplace<int>(e0);
  774. registry.emplace<char>(e0);
  775. const auto e1 = registry.create();
  776. registry.emplace<int>(e1);
  777. registry.emplace<char>(e1);
  778. const auto e2 = registry.create();
  779. registry.emplace<int>(e2);
  780. registry.emplace<char>(e2);
  781. const auto e3 = registry.create();
  782. registry.emplace<int>(e3);
  783. registry.emplace<char>(e3);
  784. registry.erase<int>(e1);
  785. ASSERT_NE(view.find(e0), view.end());
  786. ASSERT_EQ(view.find(e1), view.end());
  787. ASSERT_NE(view.find(e2), view.end());
  788. ASSERT_NE(view.find(e3), view.end());
  789. auto it = view.find(e2);
  790. ASSERT_EQ(*it, e2);
  791. ASSERT_EQ(*(++it), e3);
  792. ASSERT_EQ(*(++it), e0);
  793. ASSERT_EQ(++it, view.end());
  794. ASSERT_EQ(++view.find(e0), view.end());
  795. const auto e4 = registry.create();
  796. registry.destroy(e4);
  797. const auto e5 = registry.create();
  798. registry.emplace<int>(e5);
  799. registry.emplace<char>(e5);
  800. ASSERT_NE(view.find(e5), view.end());
  801. ASSERT_EQ(view.find(e4), view.end());
  802. }
  803. TEST(MultiComponentView, ExcludedComponents) {
  804. entt::registry registry;
  805. const auto e0 = registry.create();
  806. registry.emplace<int>(e0, 0);
  807. const auto e1 = registry.create();
  808. registry.emplace<int>(e1, 1);
  809. registry.emplace<char>(e1);
  810. const auto e2 = registry.create();
  811. registry.emplace<int>(e2, 2);
  812. const auto e3 = registry.create();
  813. registry.emplace<int>(e3, 3);
  814. registry.emplace<char>(e3);
  815. const auto view = std::as_const(registry).view<const int>(entt::exclude<char>);
  816. for(const auto entity: view) {
  817. ASSERT_TRUE(entity == e0 || entity == e2);
  818. if(entity == e0) {
  819. ASSERT_EQ(view.get<const int>(e0), 0);
  820. ASSERT_EQ(view.get<0u>(e0), 0);
  821. } else if(entity == e2) {
  822. ASSERT_EQ(std::get<0>(view.get(e2)), 2);
  823. }
  824. }
  825. registry.emplace<char>(e0);
  826. registry.emplace<char>(e2);
  827. registry.erase<char>(e1);
  828. registry.erase<char>(e3);
  829. for(const auto entity: view) {
  830. ASSERT_TRUE(entity == e1 || entity == e3);
  831. if(entity == e1) {
  832. ASSERT_EQ(std::get<0>(view.get(e1)), 1);
  833. } else if(entity == e3) {
  834. ASSERT_EQ(view.get<const int>(e3), 3);
  835. ASSERT_EQ(view.get<0u>(e3), 3);
  836. }
  837. }
  838. }
  839. TEST(MultiComponentView, EmptyTypes) {
  840. entt::registry registry;
  841. auto v1 = registry.view<int, char, empty_type>(entt::exclude<double>);
  842. auto v2 = registry.view<int, empty_type, char>(entt::exclude<double>);
  843. auto v3 = registry.view<empty_type, int, char>(entt::exclude<double>);
  844. const auto entity = registry.create();
  845. registry.emplace<int>(entity);
  846. registry.emplace<char>(entity);
  847. registry.emplace<empty_type>(entity);
  848. const auto other = registry.create();
  849. registry.emplace<int>(other);
  850. registry.emplace<char>(other);
  851. registry.emplace<double>(other);
  852. registry.emplace<empty_type>(other);
  853. const auto ignored = registry.create();
  854. registry.emplace<int>(ignored);
  855. registry.emplace<char>(ignored);
  856. v1.each([entity](const auto entt, int, char) {
  857. ASSERT_EQ(entity, entt);
  858. });
  859. for(auto [entt, iv, cv]: v1.each()) {
  860. static_assert(std::is_same_v<decltype(entt), entt::entity>);
  861. static_assert(std::is_same_v<decltype(iv), int &>);
  862. static_assert(std::is_same_v<decltype(cv), char &>);
  863. ASSERT_EQ(entity, entt);
  864. }
  865. v2.each([check = true](int, char) mutable {
  866. ASSERT_TRUE(check);
  867. check = false;
  868. });
  869. for(auto [entt, iv, cv]: v2.each()) {
  870. static_assert(std::is_same_v<decltype(entt), entt::entity>);
  871. static_assert(std::is_same_v<decltype(iv), int &>);
  872. static_assert(std::is_same_v<decltype(cv), char &>);
  873. ASSERT_EQ(entity, entt);
  874. }
  875. v3.each([entity](const auto entt, int, char) {
  876. ASSERT_EQ(entity, entt);
  877. });
  878. for(auto [entt, iv, cv]: v3.each()) {
  879. static_assert(std::is_same_v<decltype(entt), entt::entity>);
  880. static_assert(std::is_same_v<decltype(iv), int &>);
  881. static_assert(std::is_same_v<decltype(cv), char &>);
  882. ASSERT_EQ(entity, entt);
  883. }
  884. v3.use<empty_type>();
  885. v3.each([entity](const auto entt, int, char) {
  886. ASSERT_EQ(entity, entt);
  887. });
  888. v3.use<0u>();
  889. for(auto [entt, iv, cv]: v3.each()) {
  890. static_assert(std::is_same_v<decltype(entt), entt::entity>);
  891. static_assert(std::is_same_v<decltype(iv), int &>);
  892. static_assert(std::is_same_v<decltype(cv), char &>);
  893. ASSERT_EQ(entity, entt);
  894. }
  895. v2.use<1u>();
  896. v2.each([check = true](int, char) mutable {
  897. ASSERT_TRUE(check);
  898. check = false;
  899. });
  900. v2.use<empty_type>();
  901. for(auto [entt, iv, cv]: v2.each()) {
  902. static_assert(std::is_same_v<decltype(entt), entt::entity>);
  903. static_assert(std::is_same_v<decltype(iv), int &>);
  904. static_assert(std::is_same_v<decltype(cv), char &>);
  905. ASSERT_EQ(entity, entt);
  906. }
  907. }
  908. TEST(MultiComponentView, FrontBack) {
  909. entt::registry registry;
  910. auto view = registry.view<const int, const char>();
  911. ASSERT_EQ(view.front(), static_cast<entt::entity>(entt::null));
  912. ASSERT_EQ(view.back(), static_cast<entt::entity>(entt::null));
  913. const auto e0 = registry.create();
  914. registry.emplace<int>(e0);
  915. registry.emplace<char>(e0);
  916. const auto e1 = registry.create();
  917. registry.emplace<int>(e1);
  918. registry.emplace<char>(e1);
  919. const auto entity = registry.create();
  920. registry.emplace<char>(entity);
  921. ASSERT_EQ(view.front(), e1);
  922. ASSERT_EQ(view.back(), e0);
  923. }
  924. TEST(MultiComponentView, ExtendedGet) {
  925. using type = decltype(std::declval<entt::registry>().view<int, empty_type, char>().get({}));
  926. static_assert(std::tuple_size_v<type> == 2u);
  927. static_assert(std::is_same_v<std::tuple_element_t<0, type>, int &>);
  928. static_assert(std::is_same_v<std::tuple_element_t<1, type>, char &>);
  929. }
  930. TEST(MultiComponentView, DeductionGuide) {
  931. entt::registry registry;
  932. entt::storage_type_t<int> istorage;
  933. entt::storage_type_t<double> dstorage;
  934. entt::storage_type_t<stable_type> sstorage;
  935. 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})>);
  936. 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})>);
  937. 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)})>);
  938. 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)})>);
  939. 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})>);
  940. 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()})>);
  941. 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)})>);
  942. 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))})>);
  943. 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))})>);
  944. 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)})>);
  945. 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)})>);
  946. 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)})>);
  947. 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))})>);
  948. 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))})>);
  949. 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)})>);
  950. }
  951. TEST(MultiComponentView, IterableViewAlgorithmCompatibility) {
  952. entt::registry registry;
  953. const auto entity = registry.create();
  954. registry.emplace<int>(entity);
  955. registry.emplace<char>(entity);
  956. const auto view = registry.view<int, char>();
  957. const auto iterable = view.each();
  958. const auto it = std::find_if(iterable.begin(), iterable.end(), [entity](auto args) { return std::get<0>(args) == entity; });
  959. ASSERT_EQ(std::get<0>(*it), entity);
  960. }
  961. TEST(MultiComponentView, StableType) {
  962. entt::registry registry;
  963. auto view = registry.view<int, stable_type>();
  964. const auto entity = registry.create();
  965. const auto other = registry.create();
  966. registry.emplace<int>(entity);
  967. registry.emplace<int>(other);
  968. registry.emplace<stable_type>(entity);
  969. registry.emplace<stable_type>(other);
  970. registry.destroy(entity);
  971. ASSERT_EQ(view.size_hint(), 1u);
  972. view.use<stable_type>();
  973. ASSERT_EQ(view.size_hint(), 2u);
  974. ASSERT_FALSE(view.contains(entity));
  975. ASSERT_TRUE(view.contains(other));
  976. ASSERT_EQ(view.front(), other);
  977. ASSERT_EQ(view.back(), other);
  978. ASSERT_EQ(*view.begin(), other);
  979. ASSERT_EQ(++view.begin(), view.end());
  980. view.each([other](const auto entt, int, stable_type) {
  981. ASSERT_EQ(other, entt);
  982. });
  983. view.each([check = true](int, stable_type) mutable {
  984. ASSERT_TRUE(check);
  985. check = false;
  986. });
  987. for(auto [entt, iv, st]: view.each()) {
  988. static_assert(std::is_same_v<decltype(entt), entt::entity>);
  989. static_assert(std::is_same_v<decltype(iv), int &>);
  990. static_assert(std::is_same_v<decltype(st), stable_type &>);
  991. ASSERT_EQ(other, entt);
  992. }
  993. registry.compact();
  994. ASSERT_EQ(view.size_hint(), 1u);
  995. }
  996. TEST(MultiComponentView, StableTypeWithExcludedComponent) {
  997. entt::registry registry;
  998. auto view = registry.view<stable_type>(entt::exclude<int>);
  999. const auto entity = registry.create();
  1000. const auto other = registry.create();
  1001. registry.emplace<stable_type>(entity, 0);
  1002. registry.emplace<stable_type>(other, 42);
  1003. registry.emplace<int>(entity);
  1004. ASSERT_EQ(view.size_hint(), 2u);
  1005. ASSERT_FALSE(view.contains(entity));
  1006. ASSERT_TRUE(view.contains(other));
  1007. registry.destroy(entity);
  1008. ASSERT_EQ(view.size_hint(), 2u);
  1009. ASSERT_FALSE(view.contains(entity));
  1010. ASSERT_TRUE(view.contains(other));
  1011. for(auto entt: view) {
  1012. constexpr entt::entity tombstone = entt::tombstone;
  1013. ASSERT_NE(entt, tombstone);
  1014. ASSERT_EQ(entt, other);
  1015. }
  1016. for(auto [entt, comp]: view.each()) {
  1017. constexpr entt::entity tombstone = entt::tombstone;
  1018. ASSERT_NE(entt, tombstone);
  1019. ASSERT_EQ(entt, other);
  1020. ASSERT_EQ(comp.value, 42);
  1021. }
  1022. view.each([other](const auto entt, auto &&...) {
  1023. constexpr entt::entity tombstone = entt::tombstone;
  1024. ASSERT_NE(entt, tombstone);
  1025. ASSERT_EQ(entt, other);
  1026. });
  1027. }
  1028. TEST(MultiComponentView, SameComponentTypes) {
  1029. entt::registry registry;
  1030. entt::storage_type_t<int> storage;
  1031. entt::storage_type_t<int> other;
  1032. entt::basic_view view{storage, other};
  1033. storage.bind(entt::forward_as_any(registry));
  1034. other.bind(entt::forward_as_any(registry));
  1035. const entt::entity e0{42u};
  1036. const entt::entity e1{3u};
  1037. storage.emplace(e0, 7);
  1038. other.emplace(e0, 9);
  1039. other.emplace(e1, 1);
  1040. ASSERT_TRUE(view.contains(e0));
  1041. ASSERT_FALSE(view.contains(e1));
  1042. ASSERT_EQ((view.get<0u, 1u>(e0)), (std::make_tuple(7, 9)));
  1043. ASSERT_EQ(view.get<1u>(e0), 9);
  1044. for(auto entt: view) {
  1045. ASSERT_EQ(entt, e0);
  1046. }
  1047. view.each([&](auto entt, auto &&first, auto &&second) {
  1048. ASSERT_EQ(entt, e0);
  1049. ASSERT_EQ(first, 7);
  1050. ASSERT_EQ(second, 9);
  1051. });
  1052. for(auto [entt, first, second]: view.each()) {
  1053. ASSERT_EQ(entt, e0);
  1054. ASSERT_EQ(first, 7);
  1055. ASSERT_EQ(second, 9);
  1056. }
  1057. ASSERT_EQ(view.handle(), &storage);
  1058. view.use<1u>();
  1059. ASSERT_EQ(view.handle(), &other);
  1060. }
  1061. TEST(MultiComponentView, Storage) {
  1062. entt::registry registry;
  1063. const auto entity = registry.create();
  1064. auto view = registry.view<int, const char>(entt::exclude<double, const float>);
  1065. static_assert(std::is_same_v<decltype(view.storage<0u>()), entt::storage_type_t<int> *>);
  1066. static_assert(std::is_same_v<decltype(view.storage<int>()), entt::storage_type_t<int> *>);
  1067. static_assert(std::is_same_v<decltype(view.storage<const int>()), entt::storage_type_t<int> *>);
  1068. static_assert(std::is_same_v<decltype(view.storage<1u>()), const entt::storage_type_t<char> *>);
  1069. static_assert(std::is_same_v<decltype(view.storage<char>()), const entt::storage_type_t<char> *>);
  1070. static_assert(std::is_same_v<decltype(view.storage<const char>()), const entt::storage_type_t<char> *>);
  1071. static_assert(std::is_same_v<decltype(view.storage<2u>()), entt::storage_type_t<double> *>);
  1072. static_assert(std::is_same_v<decltype(view.storage<double>()), entt::storage_type_t<double> *>);
  1073. static_assert(std::is_same_v<decltype(view.storage<const double>()), entt::storage_type_t<double> *>);
  1074. static_assert(std::is_same_v<decltype(view.storage<3u>()), const entt::storage_type_t<float> *>);
  1075. static_assert(std::is_same_v<decltype(view.storage<float>()), const entt::storage_type_t<float> *>);
  1076. static_assert(std::is_same_v<decltype(view.storage<const float>()), const entt::storage_type_t<float> *>);
  1077. ASSERT_TRUE(view);
  1078. ASSERT_NE(view.storage<int>(), nullptr);
  1079. ASSERT_NE(view.storage<1u>(), nullptr);
  1080. ASSERT_NE(view.storage<double>(), nullptr);
  1081. ASSERT_NE(view.storage<3u>(), nullptr);
  1082. ASSERT_EQ(view.size_hint(), 0u);
  1083. view.storage<int>()->emplace(entity);
  1084. view.storage<double>()->emplace(entity);
  1085. registry.emplace<char>(entity);
  1086. registry.emplace<float>(entity);
  1087. ASSERT_EQ(view.size_hint(), 1u);
  1088. ASSERT_EQ(view.begin(), view.end());
  1089. ASSERT_TRUE(view.storage<int>()->contains(entity));
  1090. ASSERT_TRUE(view.storage<const char>()->contains(entity));
  1091. ASSERT_TRUE(view.storage<double>()->contains(entity));
  1092. ASSERT_TRUE(view.storage<const float>()->contains(entity));
  1093. ASSERT_TRUE((registry.all_of<int, char, double, float>(entity)));
  1094. view.storage<double>()->erase(entity);
  1095. registry.erase<float>(entity);
  1096. ASSERT_EQ(view.size_hint(), 1u);
  1097. ASSERT_NE(view.begin(), view.end());
  1098. ASSERT_TRUE(view.storage<const int>()->contains(entity));
  1099. ASSERT_TRUE(view.storage<char>()->contains(entity));
  1100. ASSERT_FALSE(view.storage<const double>()->contains(entity));
  1101. ASSERT_FALSE(view.storage<float>()->contains(entity));
  1102. ASSERT_TRUE((registry.all_of<int, char>(entity)));
  1103. ASSERT_FALSE((registry.any_of<double, float>(entity)));
  1104. view.storage<0u>()->erase(entity);
  1105. ASSERT_EQ(view.size_hint(), 0u);
  1106. ASSERT_EQ(view.begin(), view.end());
  1107. ASSERT_FALSE(view.storage<0u>()->contains(entity));
  1108. ASSERT_TRUE(view.storage<1u>()->contains(entity));
  1109. ASSERT_FALSE(view.storage<2u>()->contains(entity));
  1110. ASSERT_FALSE(view.storage<3u>()->contains(entity));
  1111. ASSERT_TRUE((registry.all_of<char>(entity)));
  1112. ASSERT_FALSE((registry.any_of<int, double, float>(entity)));
  1113. view = {};
  1114. ASSERT_FALSE(view);
  1115. ASSERT_EQ(view.storage<0u>(), nullptr);
  1116. ASSERT_EQ(view.storage<const char>(), nullptr);
  1117. ASSERT_EQ(view.storage<2u>(), nullptr);
  1118. ASSERT_EQ(view.storage<const float>(), nullptr);
  1119. }
  1120. TEST(MultiComponentView, SwapStorage) {
  1121. using namespace entt::literals;
  1122. entt::registry registry;
  1123. entt::basic_view<entt::get_t<entt::storage<int>>, entt::exclude_t<const entt::storage<char>>> view;
  1124. ASSERT_FALSE(view);
  1125. ASSERT_EQ(view.storage<0u>(), nullptr);
  1126. ASSERT_EQ(view.storage<const char>(), nullptr);
  1127. const entt::entity entity{42u};
  1128. registry.emplace<int>(entity);
  1129. registry.emplace<char>(entity);
  1130. view.storage(registry.storage<int>());
  1131. view.storage<1u>(registry.storage<char>());
  1132. ASSERT_TRUE(view);
  1133. ASSERT_NE(view.storage<int>(), nullptr);
  1134. ASSERT_NE(view.storage<1u>(), nullptr);
  1135. ASSERT_EQ(view.size_hint(), 1u);
  1136. ASSERT_FALSE(view.contains(entity));
  1137. view.storage(registry.storage<char>("other"_hs));
  1138. ASSERT_EQ(view.size_hint(), 1u);
  1139. ASSERT_TRUE(view.contains(entity));
  1140. view.storage(registry.storage<int>("empty"_hs));
  1141. ASSERT_EQ(view.size_hint(), 0u);
  1142. }
  1143. TEST(MultiComponentView, StorageEntity) {
  1144. entt::registry registry;
  1145. auto view = registry.view<entt::entity, int>();
  1146. const auto entity = registry.create();
  1147. const auto other = registry.create();
  1148. registry.emplace<int>(entity);
  1149. registry.emplace<int>(other);
  1150. registry.destroy(entity, entt::to_version(entity));
  1151. ASSERT_EQ(view.size_hint(), 2u);
  1152. ASSERT_NE(view.begin(), view.end());
  1153. ASSERT_EQ(std::distance(view.begin(), view.end()), 1);
  1154. ASSERT_EQ(*view.begin(), other);
  1155. for(auto elem: view.each()) {
  1156. ASSERT_EQ(std::get<0>(elem), other);
  1157. }
  1158. view.each([other](auto entt, auto &&...) {
  1159. ASSERT_EQ(entt, other);
  1160. });
  1161. }
  1162. TEST(MultiComponentView, StorageEntityWithExcludedComponent) {
  1163. entt::registry registry;
  1164. auto view = registry.view<entt::entity, int>(entt::exclude<char>);
  1165. const auto entity = registry.create();
  1166. const auto other = registry.create();
  1167. const auto excluded = registry.create();
  1168. registry.emplace<int>(entity);
  1169. registry.emplace<int>(other);
  1170. registry.emplace<int>(excluded);
  1171. registry.emplace<char>(excluded);
  1172. registry.destroy(entity, entt::to_version(entity));
  1173. ASSERT_EQ(view.size_hint(), 3u);
  1174. ASSERT_NE(view.begin(), view.end());
  1175. ASSERT_EQ(std::distance(view.begin(), view.end()), 1);
  1176. ASSERT_EQ(*view.begin(), other);
  1177. for(auto elem: view.each()) {
  1178. ASSERT_EQ(std::get<0>(elem), other);
  1179. }
  1180. view.each([other](auto entt, auto &&...) {
  1181. ASSERT_EQ(entt, other);
  1182. });
  1183. }
  1184. TEST(MultiComponentView, StorageEntityExcludeOnly) {
  1185. entt::registry registry;
  1186. auto view = registry.view<entt::entity>(entt::exclude<int>);
  1187. const auto entity = registry.create();
  1188. const auto other = registry.create();
  1189. const auto excluded = registry.create();
  1190. registry.emplace<int>(excluded);
  1191. registry.destroy(entity, entt::to_version(entity));
  1192. ASSERT_EQ(view.size_hint(), 3u);
  1193. ASSERT_NE(view.begin(), view.end());
  1194. // returns all matching identifiers, both in-use and available ones
  1195. ASSERT_EQ(std::distance(view.begin(), view.end()), 2);
  1196. ASSERT_EQ(*view.begin(), entity);
  1197. ASSERT_EQ(*(++view.begin()), other);
  1198. // skips available identifiers automatically, only returns in-use elements
  1199. for(auto [entt]: view.each()) {
  1200. ASSERT_EQ(entt, other);
  1201. }
  1202. // skips available identifiers automatically, only returns in-use elements
  1203. view.each([other](auto entt) {
  1204. ASSERT_EQ(entt, other);
  1205. });
  1206. }
  1207. TEST(View, Pipe) {
  1208. entt::registry registry;
  1209. const auto entity = registry.create();
  1210. const auto other = registry.create();
  1211. registry.emplace<int>(entity);
  1212. registry.emplace<char>(entity);
  1213. registry.emplace<double>(entity);
  1214. registry.emplace<empty_type>(entity);
  1215. registry.emplace<int>(other);
  1216. registry.emplace<char>(other);
  1217. registry.emplace<stable_type>(other);
  1218. auto view1 = registry.view<int>(entt::exclude<const double>);
  1219. auto view2 = registry.view<const char>(entt::exclude<float>);
  1220. auto view3 = registry.view<empty_type>();
  1221. auto view4 = registry.view<stable_type>();
  1222. 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)>);
  1223. 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)>);
  1224. static_assert(std::is_same_v<decltype((view3 | view2) | view1), decltype(view3 | (view2 | view1))>);
  1225. ASSERT_FALSE((view1 | view2).contains(entity));
  1226. ASSERT_TRUE((view1 | view2).contains(other));
  1227. ASSERT_TRUE((view3 | view2).contains(entity));
  1228. ASSERT_FALSE((view3 | view2).contains(other));
  1229. ASSERT_FALSE((view1 | view2 | view3).contains(entity));
  1230. ASSERT_FALSE((view1 | view2 | view3).contains(other));
  1231. ASSERT_FALSE((view1 | view4 | view2).contains(entity));
  1232. ASSERT_TRUE((view1 | view4 | view2).contains(other));
  1233. view1 = {};
  1234. view3 = {};
  1235. ASSERT_FALSE(view1);
  1236. ASSERT_TRUE(view2);
  1237. ASSERT_FALSE(view3);
  1238. ASSERT_TRUE(view4);
  1239. auto pack14 = view1 | view4;
  1240. auto pack32 = view3 | view2;
  1241. ASSERT_FALSE(pack14);
  1242. ASSERT_FALSE(pack32);
  1243. ASSERT_EQ(pack14.storage<int>(), nullptr);
  1244. ASSERT_EQ(pack14.storage<const double>(), nullptr);
  1245. ASSERT_NE(pack14.storage<stable_type>(), nullptr);
  1246. ASSERT_EQ(pack32.storage<empty_type>(), nullptr);
  1247. ASSERT_NE(pack32.storage<const char>(), nullptr);
  1248. ASSERT_NE(pack32.storage<float>(), nullptr);
  1249. }