view.cpp 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327
  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<int>(e1);
  22. registry.emplace<char>(e1);
  23. ASSERT_NO_FATAL_FAILURE(view.begin()++);
  24. ASSERT_NO_FATAL_FAILURE(++cview.begin());
  25. ASSERT_NO_FATAL_FAILURE([](auto it) { return it++; }(view.rbegin()));
  26. ASSERT_NO_FATAL_FAILURE([](auto it) { return ++it; }(cview.rbegin()));
  27. ASSERT_NE(view.begin(), view.end());
  28. ASSERT_NE(cview.begin(), cview.end());
  29. ASSERT_NE(view.rbegin(), view.rend());
  30. ASSERT_NE(cview.rbegin(), cview.rend());
  31. ASSERT_EQ(view.size(), 1u);
  32. ASSERT_FALSE(view.empty());
  33. registry.emplace<char>(e0);
  34. ASSERT_EQ(view.size(), 2u);
  35. view.get<char>(e0) = '1';
  36. std::get<0>(view.get(e1)) = '2';
  37. ASSERT_EQ(view.get<0u>(e0), '1');
  38. ASSERT_EQ(cview.get<0u>(e0), view.get<char>(e0));
  39. ASSERT_EQ(view.get<char>(e1), '2');
  40. for(auto entity: view) {
  41. ASSERT_TRUE(entity == e0 || entity == e1);
  42. ASSERT_TRUE(entity != e0 || cview.get<const char>(entity) == '1');
  43. ASSERT_TRUE(entity != e1 || std::get<const char &>(cview.get(entity)) == '2');
  44. }
  45. registry.erase<char>(e0);
  46. registry.erase<char>(e1);
  47. ASSERT_EQ(view.begin(), view.end());
  48. ASSERT_EQ(view.rbegin(), view.rend());
  49. ASSERT_TRUE(view.empty());
  50. decltype(view) invalid{};
  51. ASSERT_TRUE(view);
  52. ASSERT_TRUE(cview);
  53. ASSERT_FALSE(invalid);
  54. }
  55. TEST(SingleComponentView, Constructors) {
  56. entt::storage<int> storage{};
  57. entt::view<entt::get_t<int>> invalid{};
  58. entt::basic_view from_storage{storage};
  59. entt::basic_view from_tuple{std::forward_as_tuple(storage)};
  60. ASSERT_FALSE(invalid);
  61. ASSERT_TRUE(from_storage);
  62. ASSERT_TRUE(from_tuple);
  63. ASSERT_EQ(&from_storage.handle(), &from_tuple.handle());
  64. }
  65. TEST(SingleComponentView, Handle) {
  66. entt::registry registry;
  67. const auto entity = registry.create();
  68. auto view = registry.view<int>();
  69. auto &&handle = view.handle();
  70. ASSERT_TRUE(handle.empty());
  71. ASSERT_FALSE(handle.contains(entity));
  72. ASSERT_EQ(&handle, &view.handle());
  73. registry.emplace<int>(entity);
  74. ASSERT_FALSE(handle.empty());
  75. ASSERT_TRUE(handle.contains(entity));
  76. ASSERT_EQ(&handle, &view.handle());
  77. }
  78. TEST(SingleComponentView, LazyTypeFromConstRegistry) {
  79. entt::registry registry{};
  80. auto eview = std::as_const(registry).view<const empty_type>();
  81. auto cview = std::as_const(registry).view<const int>();
  82. const auto entity = registry.create();
  83. registry.emplace<empty_type>(entity);
  84. registry.emplace<int>(entity);
  85. ASSERT_TRUE(cview);
  86. ASSERT_TRUE(eview);
  87. ASSERT_TRUE(cview.empty());
  88. ASSERT_EQ(eview.size(), 0u);
  89. ASSERT_FALSE(cview.contains(entity));
  90. ASSERT_EQ(cview.begin(), cview.end());
  91. ASSERT_EQ(eview.rbegin(), eview.rend());
  92. ASSERT_EQ(eview.find(entity), eview.end());
  93. ASSERT_NE(cview.front(), entity);
  94. ASSERT_NE(eview.back(), entity);
  95. }
  96. TEST(SingleComponentView, ElementAccess) {
  97. entt::registry registry;
  98. auto view = registry.view<int>();
  99. auto cview = std::as_const(registry).view<const int>();
  100. const auto e0 = registry.create();
  101. registry.emplace<int>(e0, 42);
  102. const auto e1 = registry.create();
  103. registry.emplace<int>(e1, 3);
  104. for(auto i = 0u; i < view.size(); ++i) {
  105. ASSERT_EQ(view[i], i ? e0 : e1);
  106. ASSERT_EQ(cview[i], i ? e0 : e1);
  107. }
  108. ASSERT_EQ(view[e0], 42);
  109. ASSERT_EQ(cview[e1], 3);
  110. }
  111. TEST(SingleComponentView, Contains) {
  112. entt::registry registry;
  113. const auto e0 = registry.create();
  114. registry.emplace<int>(e0);
  115. const auto e1 = registry.create();
  116. registry.emplace<int>(e1);
  117. registry.destroy(e0);
  118. auto view = registry.view<int>();
  119. ASSERT_FALSE(view.contains(e0));
  120. ASSERT_TRUE(view.contains(e1));
  121. }
  122. TEST(SingleComponentView, Empty) {
  123. entt::registry registry;
  124. const auto e0 = registry.create();
  125. registry.emplace<char>(e0);
  126. registry.emplace<double>(e0);
  127. const auto e1 = registry.create();
  128. registry.emplace<char>(e1);
  129. auto view = registry.view<int>();
  130. ASSERT_EQ(view.size(), 0u);
  131. ASSERT_EQ(view.begin(), view.end());
  132. ASSERT_EQ(view.rbegin(), view.rend());
  133. }
  134. TEST(SingleComponentView, Each) {
  135. entt::registry registry;
  136. entt::entity entity[2]{registry.create(), registry.create()};
  137. auto view = registry.view<int>(entt::exclude<double>);
  138. auto cview = std::as_const(registry).view<const int>();
  139. registry.emplace<int>(entity[0u], 0);
  140. registry.emplace<int>(entity[1u], 1);
  141. auto iterable = view.each();
  142. auto citerable = cview.each();
  143. ASSERT_NE(citerable.begin(), citerable.end());
  144. ASSERT_NO_FATAL_FAILURE(iterable.begin()->operator=(*iterable.begin()));
  145. ASSERT_EQ(decltype(iterable.end()){}, iterable.end());
  146. auto it = iterable.begin();
  147. ASSERT_EQ(it.base(), view.begin());
  148. ASSERT_EQ((it++, ++it), iterable.end());
  149. ASSERT_EQ(it.base(), view.end());
  150. view.each([expected = 1](auto entt, int &value) mutable {
  151. ASSERT_EQ(static_cast<int>(entt::to_integral(entt)), expected);
  152. ASSERT_EQ(value, expected);
  153. --expected;
  154. });
  155. cview.each([expected = 1](const int &value) mutable {
  156. ASSERT_EQ(value, expected);
  157. --expected;
  158. });
  159. ASSERT_EQ(std::get<0>(*iterable.begin()), entity[1u]);
  160. ASSERT_EQ(std::get<0>(*++citerable.begin()), entity[0u]);
  161. static_assert(std::is_same_v<decltype(std::get<1>(*iterable.begin())), int &>);
  162. static_assert(std::is_same_v<decltype(std::get<1>(*citerable.begin())), const int &>);
  163. // do not use iterable, make sure an iterable view works when created from a temporary
  164. for(auto [entt, value]: view.each()) {
  165. ASSERT_EQ(static_cast<int>(entt::to_integral(entt)), value);
  166. }
  167. }
  168. TEST(SingleComponentView, ConstNonConstAndAllInBetween) {
  169. entt::registry registry;
  170. auto view = registry.view<int>();
  171. auto cview = std::as_const(registry).view<const int>();
  172. ASSERT_EQ(view.size(), 0u);
  173. ASSERT_EQ(cview.size(), 0u);
  174. registry.emplace<int>(registry.create(), 0);
  175. ASSERT_EQ(view.size(), 1u);
  176. ASSERT_EQ(cview.size(), 1u);
  177. static_assert(std::is_same_v<decltype(view.get<0u>({})), int &>);
  178. static_assert(std::is_same_v<decltype(view.get<int>({})), int &>);
  179. static_assert(std::is_same_v<decltype(view.get({})), std::tuple<int &>>);
  180. static_assert(std::is_same_v<decltype(cview.get<0u>({})), const int &>);
  181. static_assert(std::is_same_v<decltype(cview.get<const int>({})), const int &>);
  182. static_assert(std::is_same_v<decltype(cview.get({})), std::tuple<const int &>>);
  183. static_assert(std::is_same_v<decltype(std::as_const(registry).view<int>()), decltype(cview)>);
  184. view.each([](auto &&i) {
  185. static_assert(std::is_same_v<decltype(i), int &>);
  186. });
  187. cview.each([](auto &&i) {
  188. static_assert(std::is_same_v<decltype(i), const int &>);
  189. });
  190. for(auto [entt, iv]: view.each()) {
  191. static_assert(std::is_same_v<decltype(entt), entt::entity>);
  192. static_assert(std::is_same_v<decltype(iv), int &>);
  193. }
  194. for(auto [entt, iv]: cview.each()) {
  195. static_assert(std::is_same_v<decltype(entt), entt::entity>);
  196. static_assert(std::is_same_v<decltype(iv), const int &>);
  197. }
  198. }
  199. TEST(SingleComponentView, ConstNonConstAndAllInBetweenWithEmptyType) {
  200. entt::registry registry;
  201. auto view = registry.view<empty_type>();
  202. auto cview = std::as_const(registry).view<const empty_type>();
  203. ASSERT_EQ(view.size(), 0u);
  204. ASSERT_EQ(cview.size(), 0u);
  205. registry.emplace<empty_type>(registry.create());
  206. ASSERT_EQ(view.size(), 1u);
  207. ASSERT_EQ(cview.size(), 1u);
  208. static_assert(std::is_same_v<decltype(view.get({})), std::tuple<>>);
  209. static_assert(std::is_same_v<decltype(cview.get({})), std::tuple<>>);
  210. static_assert(std::is_same_v<decltype(std::as_const(registry).view<empty_type>()), decltype(cview)>);
  211. for(auto [entt]: view.each()) {
  212. static_assert(std::is_same_v<decltype(entt), entt::entity>);
  213. }
  214. for(auto [entt]: cview.each()) {
  215. static_assert(std::is_same_v<decltype(entt), entt::entity>);
  216. }
  217. }
  218. TEST(SingleComponentView, Find) {
  219. entt::registry registry;
  220. auto view = registry.view<int>();
  221. const auto e0 = registry.create();
  222. registry.emplace<int>(e0);
  223. const auto e1 = registry.create();
  224. registry.emplace<int>(e1);
  225. const auto e2 = registry.create();
  226. registry.emplace<int>(e2);
  227. const auto e3 = registry.create();
  228. registry.emplace<int>(e3);
  229. registry.erase<int>(e1);
  230. ASSERT_NE(view.find(e0), view.end());
  231. ASSERT_EQ(view.find(e1), view.end());
  232. ASSERT_NE(view.find(e2), view.end());
  233. ASSERT_NE(view.find(e3), view.end());
  234. auto it = view.find(e2);
  235. ASSERT_EQ(*it, e2);
  236. ASSERT_EQ(*(++it), e3);
  237. ASSERT_EQ(*(++it), e0);
  238. ASSERT_EQ(++it, view.end());
  239. ASSERT_EQ(++view.find(e0), view.end());
  240. const auto e4 = registry.create();
  241. registry.destroy(e4);
  242. const auto e5 = registry.create();
  243. registry.emplace<int>(e5);
  244. ASSERT_NE(view.find(e5), view.end());
  245. ASSERT_EQ(view.find(e4), view.end());
  246. }
  247. TEST(SingleComponentView, EmptyTypes) {
  248. entt::registry registry;
  249. entt::entity entities[2u];
  250. registry.create(std::begin(entities), std::end(entities));
  251. registry.emplace<int>(entities[0u], 0);
  252. registry.emplace<empty_type>(entities[0u]);
  253. registry.emplace<char>(entities[1u], 'c');
  254. registry.view<empty_type>().each([&](const auto entt) {
  255. ASSERT_EQ(entities[0u], entt);
  256. });
  257. registry.view<empty_type>().each([check = true]() mutable {
  258. ASSERT_TRUE(check);
  259. check = false;
  260. });
  261. for(auto [entt]: registry.view<empty_type>().each()) {
  262. static_assert(std::is_same_v<decltype(entt), entt::entity>);
  263. ASSERT_EQ(entities[0u], entt);
  264. }
  265. registry.view<int>().each([&](const auto entt, int) {
  266. ASSERT_EQ(entities[0u], entt);
  267. });
  268. registry.view<int>().each([check = true](int) mutable {
  269. ASSERT_TRUE(check);
  270. check = false;
  271. });
  272. for(auto [entt, iv]: registry.view<int>().each()) {
  273. static_assert(std::is_same_v<decltype(entt), entt::entity>);
  274. static_assert(std::is_same_v<decltype(iv), int &>);
  275. ASSERT_EQ(entities[0u], entt);
  276. }
  277. }
  278. TEST(SingleComponentView, FrontBack) {
  279. entt::registry registry;
  280. auto view = registry.view<const int>();
  281. ASSERT_EQ(view.front(), static_cast<entt::entity>(entt::null));
  282. ASSERT_EQ(view.back(), static_cast<entt::entity>(entt::null));
  283. const auto e0 = registry.create();
  284. registry.emplace<int>(e0);
  285. const auto e1 = registry.create();
  286. registry.emplace<int>(e1);
  287. ASSERT_EQ(view.front(), e1);
  288. ASSERT_EQ(view.back(), e0);
  289. }
  290. TEST(SingleComponentView, DeductionGuide) {
  291. entt::registry registry;
  292. entt::storage_type_t<int> istorage;
  293. entt::storage_type_t<stable_type> sstorage;
  294. 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})>);
  295. 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)})>);
  296. 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})>);
  297. 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()})>);
  298. 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))})>);
  299. 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)})>);
  300. }
  301. TEST(SingleComponentView, IterableViewAlgorithmCompatibility) {
  302. entt::registry registry;
  303. const auto entity = registry.create();
  304. registry.emplace<int>(entity);
  305. const auto view = registry.view<int>();
  306. const auto iterable = view.each();
  307. const auto it = std::find_if(iterable.begin(), iterable.end(), [entity](auto args) { return std::get<0>(args) == entity; });
  308. ASSERT_EQ(std::get<0>(*it), entity);
  309. }
  310. TEST(SingleComponentView, StableType) {
  311. entt::registry registry;
  312. auto view = registry.view<stable_type>();
  313. const auto entity = registry.create();
  314. const auto other = registry.create();
  315. registry.emplace<stable_type>(entity);
  316. registry.emplace<stable_type>(other);
  317. registry.destroy(entity);
  318. ASSERT_EQ(view.size_hint(), 2u);
  319. ASSERT_FALSE(view.contains(entity));
  320. ASSERT_TRUE(view.contains(other));
  321. ASSERT_EQ(view.front(), other);
  322. ASSERT_EQ(view.back(), other);
  323. ASSERT_EQ(*view.begin(), other);
  324. ASSERT_EQ(++view.begin(), view.end());
  325. view.each([other](const auto entt, stable_type) {
  326. ASSERT_EQ(other, entt);
  327. });
  328. view.each([check = true](stable_type) mutable {
  329. ASSERT_TRUE(check);
  330. check = false;
  331. });
  332. for(auto [entt, st]: view.each()) {
  333. static_assert(std::is_same_v<decltype(entt), entt::entity>);
  334. static_assert(std::is_same_v<decltype(st), stable_type &>);
  335. ASSERT_EQ(other, entt);
  336. }
  337. registry.compact();
  338. ASSERT_EQ(view.size_hint(), 1u);
  339. }
  340. TEST(SingleComponentView, Storage) {
  341. entt::registry registry;
  342. const auto entity = registry.create();
  343. const auto view = registry.view<int>();
  344. const auto cview = registry.view<const char>();
  345. static_assert(std::is_same_v<decltype(view.storage()), entt::storage_type_t<int> &>);
  346. static_assert(std::is_same_v<decltype(view.storage<0u>()), entt::storage_type_t<int> &>);
  347. static_assert(std::is_same_v<decltype(view.storage<int>()), entt::storage_type_t<int> &>);
  348. static_assert(std::is_same_v<decltype(view.storage<const int>()), entt::storage_type_t<int> &>);
  349. static_assert(std::is_same_v<decltype(cview.storage()), const entt::storage_type_t<char> &>);
  350. static_assert(std::is_same_v<decltype(cview.storage<0u>()), const entt::storage_type_t<char> &>);
  351. static_assert(std::is_same_v<decltype(cview.storage<char>()), const entt::storage_type_t<char> &>);
  352. static_assert(std::is_same_v<decltype(cview.storage<const char>()), const entt::storage_type_t<char> &>);
  353. ASSERT_EQ(view.size(), 0u);
  354. ASSERT_EQ(cview.size(), 0u);
  355. view.storage().emplace(entity);
  356. registry.emplace<char>(entity);
  357. ASSERT_EQ(view.size(), 1u);
  358. ASSERT_EQ(cview.size(), 1u);
  359. ASSERT_TRUE(view.storage<int>().contains(entity));
  360. ASSERT_TRUE(cview.storage<0u>().contains(entity));
  361. ASSERT_TRUE((registry.all_of<int, char>(entity)));
  362. view.storage().erase(entity);
  363. ASSERT_EQ(view.size(), 0u);
  364. ASSERT_EQ(cview.size(), 1u);
  365. ASSERT_FALSE(view.storage<0u>().contains(entity));
  366. ASSERT_TRUE(cview.storage<const char>().contains(entity));
  367. ASSERT_FALSE((registry.all_of<int, char>(entity)));
  368. }
  369. TEST(MultiComponentView, Functionalities) {
  370. entt::registry registry;
  371. auto view = registry.view<int, char>();
  372. auto cview = std::as_const(registry).view<const int, const char>();
  373. const auto e0 = registry.create();
  374. registry.emplace<char>(e0, '1');
  375. const auto e1 = registry.create();
  376. registry.emplace<int>(e1, 42);
  377. registry.emplace<char>(e1, '2');
  378. ASSERT_EQ(*view.begin(), e1);
  379. ASSERT_EQ(*cview.begin(), e1);
  380. ASSERT_EQ(++view.begin(), (view.end()));
  381. ASSERT_EQ(++cview.begin(), (cview.end()));
  382. ASSERT_NO_FATAL_FAILURE((view.begin()++));
  383. ASSERT_NO_FATAL_FAILURE((++cview.begin()));
  384. ASSERT_NE(view.begin(), view.end());
  385. ASSERT_NE(cview.begin(), cview.end());
  386. ASSERT_EQ(view.size_hint(), 1u);
  387. for(auto entity: view) {
  388. ASSERT_EQ(std::get<0>(cview.get<const int, const char>(entity)), 42);
  389. ASSERT_EQ(std::get<0>(cview.get<0u, 1u>(entity)), 42);
  390. ASSERT_EQ(std::get<1>(view.get<int, char>(entity)), '2');
  391. ASSERT_EQ(std::get<1>(view.get<0u, 1u>(entity)), '2');
  392. ASSERT_EQ(cview.get<const char>(entity), '2');
  393. ASSERT_EQ(cview.get<1u>(entity), '2');
  394. }
  395. decltype(view) invalid{};
  396. ASSERT_TRUE(view);
  397. ASSERT_TRUE(cview);
  398. ASSERT_FALSE(invalid);
  399. }
  400. TEST(MultiComponentView, Constructors) {
  401. entt::storage<int> storage{};
  402. entt::view<entt::get_t<int, int>> invalid{};
  403. entt::basic_view from_storage{storage, storage};
  404. entt::basic_view from_tuple{std::forward_as_tuple(storage, storage)};
  405. ASSERT_FALSE(invalid);
  406. ASSERT_TRUE(from_storage);
  407. ASSERT_TRUE(from_tuple);
  408. ASSERT_EQ(&from_storage.handle(), &from_tuple.handle());
  409. }
  410. TEST(MultiComponentView, Handle) {
  411. entt::registry registry;
  412. const auto entity = registry.create();
  413. auto view = registry.view<int, char>();
  414. auto &&handle = view.handle();
  415. ASSERT_TRUE(handle.empty());
  416. ASSERT_FALSE(handle.contains(entity));
  417. ASSERT_EQ(&handle, &view.handle());
  418. registry.emplace<int>(entity);
  419. ASSERT_FALSE(handle.empty());
  420. ASSERT_TRUE(handle.contains(entity));
  421. ASSERT_EQ(&handle, &view.handle());
  422. view = view.refresh();
  423. auto &&other = view.handle();
  424. ASSERT_TRUE(other.empty());
  425. ASSERT_FALSE(other.contains(entity));
  426. ASSERT_EQ(&other, &view.handle());
  427. ASSERT_NE(&handle, &other);
  428. view = view.use<int>();
  429. ASSERT_NE(&other, &view.handle());
  430. ASSERT_EQ(&handle, &view.handle());
  431. }
  432. TEST(MultiComponentView, LazyTypesFromConstRegistry) {
  433. entt::registry registry{};
  434. auto view = std::as_const(registry).view<const empty_type, const int>();
  435. const auto entity = registry.create();
  436. registry.emplace<empty_type>(entity);
  437. registry.emplace<int>(entity);
  438. ASSERT_TRUE(view);
  439. ASSERT_EQ(view.size_hint(), 0u);
  440. ASSERT_FALSE(view.contains(entity));
  441. ASSERT_EQ(view.begin(), view.end());
  442. ASSERT_EQ(view.find(entity), view.end());
  443. ASSERT_NE(view.front(), entity);
  444. ASSERT_NE(view.back(), entity);
  445. }
  446. TEST(MultiComponentView, LazyExcludedTypeFromConstRegistry) {
  447. entt::registry registry;
  448. auto entity = registry.create();
  449. registry.emplace<int>(entity);
  450. auto view = std::as_const(registry).view<const int>(entt::exclude<char>);
  451. ASSERT_TRUE(view);
  452. ASSERT_EQ(view.size_hint(), 1u);
  453. ASSERT_TRUE(view.contains(entity));
  454. ASSERT_NE(view.begin(), view.end());
  455. ASSERT_NE(view.find(entity), view.end());
  456. ASSERT_EQ(view.front(), entity);
  457. ASSERT_EQ(view.back(), entity);
  458. }
  459. TEST(MultiComponentView, Iterator) {
  460. entt::registry registry;
  461. const entt::entity entity[2]{registry.create(), registry.create()};
  462. registry.insert<int>(std::begin(entity), std::end(entity));
  463. registry.insert<char>(std::begin(entity), std::end(entity));
  464. const auto view = registry.view<int, char>();
  465. using iterator = typename decltype(view)::iterator;
  466. iterator end{view.begin()};
  467. iterator begin{};
  468. begin = view.end();
  469. std::swap(begin, end);
  470. ASSERT_EQ(begin, view.begin());
  471. ASSERT_EQ(end, view.end());
  472. ASSERT_NE(begin, end);
  473. ASSERT_EQ(*begin, entity[1u]);
  474. ASSERT_EQ(*begin.operator->(), entity[1u]);
  475. ASSERT_EQ(begin++, view.begin());
  476. ASSERT_EQ(*begin, entity[0u]);
  477. ASSERT_EQ(*begin.operator->(), entity[0u]);
  478. ASSERT_EQ(++begin, view.end());
  479. }
  480. TEST(MultiComponentView, ElementAccess) {
  481. entt::registry registry;
  482. auto view = registry.view<int, char>();
  483. auto cview = std::as_const(registry).view<const int, const char>();
  484. const auto e0 = registry.create();
  485. registry.emplace<int>(e0, 42);
  486. registry.emplace<char>(e0, '0');
  487. const auto e1 = registry.create();
  488. registry.emplace<int>(e1, 3);
  489. registry.emplace<char>(e1, '1');
  490. ASSERT_EQ(view[e0], std::make_tuple(42, '0'));
  491. ASSERT_EQ(cview[e1], std::make_tuple(3, '1'));
  492. }
  493. TEST(MultiComponentView, Contains) {
  494. entt::registry registry;
  495. const auto e0 = registry.create();
  496. registry.emplace<int>(e0);
  497. registry.emplace<char>(e0);
  498. const auto e1 = registry.create();
  499. registry.emplace<int>(e1);
  500. registry.emplace<char>(e1);
  501. registry.destroy(e0);
  502. auto view = registry.view<int, char>();
  503. ASSERT_FALSE(view.contains(e0));
  504. ASSERT_TRUE(view.contains(e1));
  505. }
  506. TEST(MultiComponentView, SizeHint) {
  507. entt::registry registry;
  508. const auto e0 = registry.create();
  509. registry.emplace<double>(e0);
  510. registry.emplace<int>(e0);
  511. registry.emplace<float>(e0);
  512. const auto e1 = registry.create();
  513. registry.emplace<char>(e1);
  514. registry.emplace<float>(e1);
  515. auto view = registry.view<char, int, float>();
  516. ASSERT_EQ(view.size_hint(), 1u);
  517. ASSERT_EQ(view.begin(), view.end());
  518. }
  519. TEST(MultiComponentView, Each) {
  520. entt::registry registry;
  521. entt::entity entity[2]{registry.create(), registry.create()};
  522. auto view = registry.view<int, char>(entt::exclude<double>);
  523. auto cview = std::as_const(registry).view<const int, const char>();
  524. registry.emplace<int>(entity[0u], 0);
  525. registry.emplace<char>(entity[0u], static_cast<char>(0));
  526. registry.emplace<int>(entity[1u], 1);
  527. registry.emplace<char>(entity[1u], static_cast<char>(1));
  528. auto iterable = view.each();
  529. auto citerable = cview.each();
  530. ASSERT_NE(citerable.begin(), citerable.end());
  531. ASSERT_NO_FATAL_FAILURE(iterable.begin()->operator=(*iterable.begin()));
  532. ASSERT_EQ(decltype(iterable.end()){}, iterable.end());
  533. auto it = iterable.begin();
  534. ASSERT_EQ(it.base(), view.begin());
  535. ASSERT_EQ((it++, ++it), iterable.end());
  536. ASSERT_EQ(it.base(), view.end());
  537. view.each([expected = 1](auto entt, int &ivalue, char &cvalue) mutable {
  538. ASSERT_EQ(static_cast<int>(entt::to_integral(entt)), expected);
  539. ASSERT_EQ(ivalue, expected);
  540. ASSERT_EQ(cvalue, expected);
  541. --expected;
  542. });
  543. cview.each([expected = 1](const int &ivalue, const char &cvalue) mutable {
  544. ASSERT_EQ(ivalue, expected);
  545. ASSERT_EQ(cvalue, expected);
  546. --expected;
  547. });
  548. ASSERT_EQ(std::get<0>(*iterable.begin()), entity[1u]);
  549. ASSERT_EQ(std::get<0>(*++citerable.begin()), entity[0u]);
  550. static_assert(std::is_same_v<decltype(std::get<1>(*iterable.begin())), int &>);
  551. static_assert(std::is_same_v<decltype(std::get<2>(*citerable.begin())), const char &>);
  552. // do not use iterable, make sure an iterable view works when created from a temporary
  553. for(auto [entt, ivalue, cvalue]: registry.view<int, char>().each()) {
  554. ASSERT_EQ(static_cast<int>(entt::to_integral(entt)), ivalue);
  555. ASSERT_EQ(static_cast<char>(entt::to_integral(entt)), cvalue);
  556. }
  557. }
  558. TEST(MultiComponentView, EachWithSuggestedType) {
  559. entt::registry registry;
  560. for(auto i = 0; i < 3; ++i) {
  561. const auto entity = registry.create();
  562. registry.emplace<int>(entity, i);
  563. registry.emplace<char>(entity);
  564. }
  565. // makes char a better candidate during iterations
  566. const auto entity = registry.create();
  567. registry.emplace<int>(entity, 99);
  568. registry.view<int, char>().use<int>().each([value = 2](const auto curr, const auto) mutable {
  569. ASSERT_EQ(curr, value--);
  570. });
  571. registry.sort<int>([](const auto lhs, const auto rhs) {
  572. return lhs < rhs;
  573. });
  574. registry.view<int, char>().use<0u>().each([value = 0](const auto curr, const auto) mutable {
  575. ASSERT_EQ(curr, value++);
  576. });
  577. registry.sort<int>([](const auto lhs, const auto rhs) {
  578. return lhs > rhs;
  579. });
  580. auto value = registry.view<int, char>().size_hint();
  581. for(auto &&curr: registry.view<int, char>().each()) {
  582. ASSERT_EQ(std::get<1>(curr), static_cast<int>(--value));
  583. }
  584. registry.sort<int>([](const auto lhs, const auto rhs) {
  585. return lhs < rhs;
  586. });
  587. value = {};
  588. for(auto &&curr: registry.view<int, char>().use<int>().each()) {
  589. ASSERT_EQ(std::get<1>(curr), static_cast<int>(value++));
  590. }
  591. }
  592. TEST(MultiComponentView, EachWithHoles) {
  593. entt::registry registry;
  594. const auto e0 = registry.create();
  595. const auto e1 = registry.create();
  596. const auto e2 = registry.create();
  597. registry.emplace<char>(e0, '0');
  598. registry.emplace<char>(e1, '1');
  599. registry.emplace<int>(e0, 0);
  600. registry.emplace<int>(e2, 2);
  601. auto view = registry.view<char, int>();
  602. view.each([e0](auto entity, const char &c, const int &i) {
  603. ASSERT_EQ(entity, e0);
  604. ASSERT_EQ(c, '0');
  605. ASSERT_EQ(i, 0);
  606. });
  607. for(auto &&curr: view.each()) {
  608. ASSERT_EQ(std::get<0>(curr), e0);
  609. ASSERT_EQ(std::get<1>(curr), '0');
  610. ASSERT_EQ(std::get<2>(curr), 0);
  611. }
  612. }
  613. TEST(MultiComponentView, ConstNonConstAndAllInBetween) {
  614. entt::registry registry;
  615. auto view = registry.view<int, empty_type, const char>();
  616. ASSERT_EQ(view.size_hint(), 0u);
  617. const auto entity = registry.create();
  618. registry.emplace<int>(entity, 0);
  619. registry.emplace<empty_type>(entity);
  620. registry.emplace<char>(entity, 'c');
  621. ASSERT_EQ(view.size_hint(), 1u);
  622. static_assert(std::is_same_v<decltype(view.get<0u>({})), int &>);
  623. static_assert(std::is_same_v<decltype(view.get<2u>({})), const char &>);
  624. static_assert(std::is_same_v<decltype(view.get<0u, 2u>({})), std::tuple<int &, const char &>>);
  625. static_assert(std::is_same_v<decltype(view.get<int>({})), int &>);
  626. static_assert(std::is_same_v<decltype(view.get<const char>({})), const char &>);
  627. static_assert(std::is_same_v<decltype(view.get<int, const char>({})), std::tuple<int &, const char &>>);
  628. static_assert(std::is_same_v<decltype(view.get({})), std::tuple<int &, const char &>>);
  629. static_assert(std::is_same_v<decltype(std::as_const(registry).view<char, int>()), decltype(std::as_const(registry).view<const char, const int>())>);
  630. 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>())>);
  631. 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>())>);
  632. view.each([](auto &&i, auto &&c) {
  633. static_assert(std::is_same_v<decltype(i), int &>);
  634. static_assert(std::is_same_v<decltype(c), const char &>);
  635. });
  636. for(auto [entt, iv, cv]: view.each()) {
  637. static_assert(std::is_same_v<decltype(entt), entt::entity>);
  638. static_assert(std::is_same_v<decltype(iv), int &>);
  639. static_assert(std::is_same_v<decltype(cv), const char &>);
  640. }
  641. }
  642. TEST(MultiComponentView, Find) {
  643. entt::registry registry;
  644. auto view = registry.view<int, const char>();
  645. const auto e0 = registry.create();
  646. registry.emplace<int>(e0);
  647. registry.emplace<char>(e0);
  648. const auto e1 = registry.create();
  649. registry.emplace<int>(e1);
  650. registry.emplace<char>(e1);
  651. const auto e2 = registry.create();
  652. registry.emplace<int>(e2);
  653. registry.emplace<char>(e2);
  654. const auto e3 = registry.create();
  655. registry.emplace<int>(e3);
  656. registry.emplace<char>(e3);
  657. registry.erase<int>(e1);
  658. ASSERT_NE(view.find(e0), view.end());
  659. ASSERT_EQ(view.find(e1), view.end());
  660. ASSERT_NE(view.find(e2), view.end());
  661. ASSERT_NE(view.find(e3), view.end());
  662. auto it = view.find(e2);
  663. ASSERT_EQ(*it, e2);
  664. ASSERT_EQ(*(++it), e3);
  665. ASSERT_EQ(*(++it), e0);
  666. ASSERT_EQ(++it, view.end());
  667. ASSERT_EQ(++view.find(e0), view.end());
  668. const auto e4 = registry.create();
  669. registry.destroy(e4);
  670. const auto e5 = registry.create();
  671. registry.emplace<int>(e5);
  672. registry.emplace<char>(e5);
  673. ASSERT_NE(view.find(e5), view.end());
  674. ASSERT_EQ(view.find(e4), view.end());
  675. }
  676. TEST(MultiComponentView, ExcludedComponents) {
  677. entt::registry registry;
  678. const auto e0 = registry.create();
  679. registry.emplace<int>(e0, 0);
  680. const auto e1 = registry.create();
  681. registry.emplace<int>(e1, 1);
  682. registry.emplace<char>(e1);
  683. const auto e2 = registry.create();
  684. registry.emplace<int>(e2, 2);
  685. const auto e3 = registry.create();
  686. registry.emplace<int>(e3, 3);
  687. registry.emplace<char>(e3);
  688. const auto view = std::as_const(registry).view<const int>(entt::exclude<char>);
  689. for(const auto entity: view) {
  690. ASSERT_TRUE(entity == e0 || entity == e2);
  691. if(entity == e0) {
  692. ASSERT_EQ(view.get<const int>(e0), 0);
  693. ASSERT_EQ(view.get<0u>(e0), 0);
  694. } else if(entity == e2) {
  695. ASSERT_EQ(std::get<0>(view.get(e2)), 2);
  696. }
  697. }
  698. registry.emplace<char>(e0);
  699. registry.emplace<char>(e2);
  700. registry.erase<char>(e1);
  701. registry.erase<char>(e3);
  702. for(const auto entity: view) {
  703. ASSERT_TRUE(entity == e1 || entity == e3);
  704. if(entity == e1) {
  705. ASSERT_EQ(std::get<0>(view.get(e1)), 1);
  706. } else if(entity == e3) {
  707. ASSERT_EQ(view.get<const int>(e3), 3);
  708. ASSERT_EQ(view.get<0u>(e3), 3);
  709. }
  710. }
  711. }
  712. TEST(MultiComponentView, EmptyTypes) {
  713. entt::registry registry;
  714. const auto entity = registry.create();
  715. registry.emplace<int>(entity);
  716. registry.emplace<char>(entity);
  717. registry.emplace<empty_type>(entity);
  718. const auto other = registry.create();
  719. registry.emplace<int>(other);
  720. registry.emplace<char>(other);
  721. registry.emplace<double>(other);
  722. registry.emplace<empty_type>(other);
  723. const auto ignored = registry.create();
  724. registry.emplace<int>(ignored);
  725. registry.emplace<char>(ignored);
  726. registry.view<int, char, empty_type>(entt::exclude<double>).each([entity](const auto entt, int, char) {
  727. ASSERT_EQ(entity, entt);
  728. });
  729. for(auto [entt, iv, cv]: registry.view<int, char, empty_type>(entt::exclude<double>).each()) {
  730. static_assert(std::is_same_v<decltype(entt), entt::entity>);
  731. static_assert(std::is_same_v<decltype(iv), int &>);
  732. static_assert(std::is_same_v<decltype(cv), char &>);
  733. ASSERT_EQ(entity, entt);
  734. }
  735. registry.view<int, empty_type, char>(entt::exclude<double>).each([check = true](int, char) mutable {
  736. ASSERT_TRUE(check);
  737. check = false;
  738. });
  739. for(auto [entt, iv, cv]: registry.view<int, empty_type, char>(entt::exclude<double>).each()) {
  740. static_assert(std::is_same_v<decltype(entt), entt::entity>);
  741. static_assert(std::is_same_v<decltype(iv), int &>);
  742. static_assert(std::is_same_v<decltype(cv), char &>);
  743. ASSERT_EQ(entity, entt);
  744. }
  745. registry.view<empty_type, int, char>(entt::exclude<double>).each([entity](const auto entt, int, char) {
  746. ASSERT_EQ(entity, entt);
  747. });
  748. for(auto [entt, iv, cv]: registry.view<empty_type, int, char>(entt::exclude<double>).each()) {
  749. static_assert(std::is_same_v<decltype(entt), entt::entity>);
  750. static_assert(std::is_same_v<decltype(iv), int &>);
  751. static_assert(std::is_same_v<decltype(cv), char &>);
  752. ASSERT_EQ(entity, entt);
  753. }
  754. registry.view<empty_type, int, char>(entt::exclude<double>).use<empty_type>().each([entity](const auto entt, int, char) {
  755. ASSERT_EQ(entity, entt);
  756. });
  757. for(auto [entt, iv, cv]: registry.view<empty_type, int, char>(entt::exclude<double>).use<0u>().each()) {
  758. static_assert(std::is_same_v<decltype(entt), entt::entity>);
  759. static_assert(std::is_same_v<decltype(iv), int &>);
  760. static_assert(std::is_same_v<decltype(cv), char &>);
  761. ASSERT_EQ(entity, entt);
  762. }
  763. registry.view<int, empty_type, char>(entt::exclude<double>).use<1u>().each([check = true](int, char) mutable {
  764. ASSERT_TRUE(check);
  765. check = false;
  766. });
  767. for(auto [entt, iv, cv]: registry.view<int, empty_type, char>(entt::exclude<double>).use<empty_type>().each()) {
  768. static_assert(std::is_same_v<decltype(entt), entt::entity>);
  769. static_assert(std::is_same_v<decltype(iv), int &>);
  770. static_assert(std::is_same_v<decltype(cv), char &>);
  771. ASSERT_EQ(entity, entt);
  772. }
  773. }
  774. TEST(MultiComponentView, FrontBack) {
  775. entt::registry registry;
  776. auto view = registry.view<const int, const char>();
  777. ASSERT_EQ(view.front(), static_cast<entt::entity>(entt::null));
  778. ASSERT_EQ(view.back(), static_cast<entt::entity>(entt::null));
  779. const auto e0 = registry.create();
  780. registry.emplace<int>(e0);
  781. registry.emplace<char>(e0);
  782. const auto e1 = registry.create();
  783. registry.emplace<int>(e1);
  784. registry.emplace<char>(e1);
  785. const auto entity = registry.create();
  786. registry.emplace<char>(entity);
  787. ASSERT_EQ(view.front(), e1);
  788. ASSERT_EQ(view.back(), e0);
  789. }
  790. TEST(MultiComponentView, ExtendedGet) {
  791. using type = decltype(std::declval<entt::registry>().view<int, empty_type, char>().get({}));
  792. static_assert(std::tuple_size_v<type> == 2u);
  793. static_assert(std::is_same_v<std::tuple_element_t<0, type>, int &>);
  794. static_assert(std::is_same_v<std::tuple_element_t<1, type>, char &>);
  795. }
  796. TEST(MultiComponentView, DeductionGuide) {
  797. entt::registry registry;
  798. entt::storage_type_t<int> istorage;
  799. entt::storage_type_t<double> dstorage;
  800. entt::storage_type_t<stable_type> sstorage;
  801. 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})>);
  802. 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})>);
  803. 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)})>);
  804. 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)})>);
  805. 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})>);
  806. 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()})>);
  807. 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)})>);
  808. 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))})>);
  809. 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))})>);
  810. 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)})>);
  811. 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)})>);
  812. 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)})>);
  813. 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))})>);
  814. 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))})>);
  815. 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)})>);
  816. }
  817. TEST(MultiComponentView, IterableViewAlgorithmCompatibility) {
  818. entt::registry registry;
  819. const auto entity = registry.create();
  820. registry.emplace<int>(entity);
  821. registry.emplace<char>(entity);
  822. const auto view = registry.view<int, char>();
  823. const auto iterable = view.each();
  824. const auto it = std::find_if(iterable.begin(), iterable.end(), [entity](auto args) { return std::get<0>(args) == entity; });
  825. ASSERT_EQ(std::get<0>(*it), entity);
  826. }
  827. TEST(MultiComponentView, StableType) {
  828. entt::registry registry;
  829. auto view = registry.view<int, stable_type>();
  830. const auto entity = registry.create();
  831. const auto other = registry.create();
  832. registry.emplace<int>(entity);
  833. registry.emplace<int>(other);
  834. registry.emplace<stable_type>(entity);
  835. registry.emplace<stable_type>(other);
  836. registry.destroy(entity);
  837. ASSERT_EQ(view.size_hint(), 1u);
  838. view = view.use<stable_type>();
  839. ASSERT_EQ(view.size_hint(), 2u);
  840. ASSERT_FALSE(view.contains(entity));
  841. ASSERT_TRUE(view.contains(other));
  842. ASSERT_EQ(view.front(), other);
  843. ASSERT_EQ(view.back(), other);
  844. ASSERT_EQ(*view.begin(), other);
  845. ASSERT_EQ(++view.begin(), view.end());
  846. view.each([other](const auto entt, int, stable_type) {
  847. ASSERT_EQ(other, entt);
  848. });
  849. view.each([check = true](int, stable_type) mutable {
  850. ASSERT_TRUE(check);
  851. check = false;
  852. });
  853. for(auto [entt, iv, st]: view.each()) {
  854. static_assert(std::is_same_v<decltype(entt), entt::entity>);
  855. static_assert(std::is_same_v<decltype(iv), int &>);
  856. static_assert(std::is_same_v<decltype(st), stable_type &>);
  857. ASSERT_EQ(other, entt);
  858. }
  859. registry.compact();
  860. ASSERT_EQ(view.size_hint(), 1u);
  861. }
  862. TEST(MultiComponentView, StableTypeWithExcludedComponent) {
  863. entt::registry registry;
  864. auto view = registry.view<stable_type>(entt::exclude<int>).use<stable_type>();
  865. const auto entity = registry.create();
  866. const auto other = registry.create();
  867. registry.emplace<stable_type>(entity, 0);
  868. registry.emplace<stable_type>(other, 42);
  869. registry.emplace<int>(entity);
  870. ASSERT_EQ(view.size_hint(), 2u);
  871. ASSERT_FALSE(view.contains(entity));
  872. ASSERT_TRUE(view.contains(other));
  873. registry.destroy(entity);
  874. ASSERT_EQ(view.size_hint(), 2u);
  875. ASSERT_FALSE(view.contains(entity));
  876. ASSERT_TRUE(view.contains(other));
  877. for(auto entt: view) {
  878. constexpr entt::entity tombstone = entt::tombstone;
  879. ASSERT_NE(entt, tombstone);
  880. ASSERT_EQ(entt, other);
  881. }
  882. for(auto [entt, comp]: view.each()) {
  883. constexpr entt::entity tombstone = entt::tombstone;
  884. ASSERT_NE(entt, tombstone);
  885. ASSERT_EQ(entt, other);
  886. ASSERT_EQ(comp.value, 42);
  887. }
  888. view.each([other](const auto entt, auto &&...) {
  889. constexpr entt::entity tombstone = entt::tombstone;
  890. ASSERT_NE(entt, tombstone);
  891. ASSERT_EQ(entt, other);
  892. });
  893. }
  894. TEST(MultiComponentView, SameComponentTypes) {
  895. entt::registry registry;
  896. entt::storage_type_t<int> storage;
  897. entt::storage_type_t<int> other;
  898. entt::basic_view view{storage, other};
  899. storage.bind(entt::forward_as_any(registry));
  900. other.bind(entt::forward_as_any(registry));
  901. const entt::entity e0{42u};
  902. const entt::entity e1{3u};
  903. storage.emplace(e0, 7);
  904. other.emplace(e0, 9);
  905. other.emplace(e1, 1);
  906. ASSERT_TRUE(view.contains(e0));
  907. ASSERT_FALSE(view.contains(e1));
  908. ASSERT_EQ((view.get<0u, 1u>(e0)), (std::make_tuple(7, 9)));
  909. ASSERT_EQ(view.get<1u>(e0), 9);
  910. for(auto entt: view) {
  911. ASSERT_EQ(entt, e0);
  912. }
  913. view.each([&](auto entt, auto &&first, auto &&second) {
  914. ASSERT_EQ(entt, e0);
  915. ASSERT_EQ(first, 7);
  916. ASSERT_EQ(second, 9);
  917. });
  918. for(auto [entt, first, second]: view.each()) {
  919. ASSERT_EQ(entt, e0);
  920. ASSERT_EQ(first, 7);
  921. ASSERT_EQ(second, 9);
  922. }
  923. ASSERT_EQ(&view.handle(), &storage);
  924. ASSERT_EQ(&view.use<1u>().handle(), &other);
  925. }
  926. TEST(MultiComponentView, Storage) {
  927. entt::registry registry;
  928. const auto entity = registry.create();
  929. const auto view = registry.view<int, const char>(entt::exclude<double, const float>);
  930. static_assert(std::is_same_v<decltype(view.storage<0u>()), entt::storage_type_t<int> &>);
  931. static_assert(std::is_same_v<decltype(view.storage<int>()), entt::storage_type_t<int> &>);
  932. static_assert(std::is_same_v<decltype(view.storage<const int>()), entt::storage_type_t<int> &>);
  933. static_assert(std::is_same_v<decltype(view.storage<1u>()), const entt::storage_type_t<char> &>);
  934. static_assert(std::is_same_v<decltype(view.storage<char>()), const entt::storage_type_t<char> &>);
  935. static_assert(std::is_same_v<decltype(view.storage<const char>()), const entt::storage_type_t<char> &>);
  936. static_assert(std::is_same_v<decltype(view.storage<2u>()), entt::storage_type_t<double> &>);
  937. static_assert(std::is_same_v<decltype(view.storage<double>()), entt::storage_type_t<double> &>);
  938. static_assert(std::is_same_v<decltype(view.storage<const double>()), entt::storage_type_t<double> &>);
  939. static_assert(std::is_same_v<decltype(view.storage<3u>()), const entt::storage_type_t<float> &>);
  940. static_assert(std::is_same_v<decltype(view.storage<float>()), const entt::storage_type_t<float> &>);
  941. static_assert(std::is_same_v<decltype(view.storage<const float>()), const entt::storage_type_t<float> &>);
  942. ASSERT_EQ(view.size_hint(), 0u);
  943. view.storage<int>().emplace(entity);
  944. view.storage<double>().emplace(entity);
  945. registry.emplace<char>(entity);
  946. registry.emplace<float>(entity);
  947. ASSERT_EQ(view.size_hint(), 1u);
  948. ASSERT_EQ(view.begin(), view.end());
  949. ASSERT_TRUE(view.storage<int>().contains(entity));
  950. ASSERT_TRUE(view.storage<const char>().contains(entity));
  951. ASSERT_TRUE(view.storage<double>().contains(entity));
  952. ASSERT_TRUE(view.storage<const float>().contains(entity));
  953. ASSERT_TRUE((registry.all_of<int, char, double, float>(entity)));
  954. view.storage<double>().erase(entity);
  955. registry.erase<float>(entity);
  956. ASSERT_EQ(view.size_hint(), 1u);
  957. ASSERT_NE(view.begin(), view.end());
  958. ASSERT_TRUE(view.storage<const int>().contains(entity));
  959. ASSERT_TRUE(view.storage<char>().contains(entity));
  960. ASSERT_FALSE(view.storage<const double>().contains(entity));
  961. ASSERT_FALSE(view.storage<float>().contains(entity));
  962. ASSERT_TRUE((registry.all_of<int, char>(entity)));
  963. ASSERT_FALSE((registry.any_of<double, float>(entity)));
  964. view.storage<0u>().erase(entity);
  965. ASSERT_EQ(view.size_hint(), 0u);
  966. ASSERT_EQ(view.begin(), view.end());
  967. ASSERT_FALSE(view.storage<0u>().contains(entity));
  968. ASSERT_TRUE(view.storage<1u>().contains(entity));
  969. ASSERT_FALSE(view.storage<2u>().contains(entity));
  970. ASSERT_FALSE(view.storage<3u>().contains(entity));
  971. ASSERT_TRUE((registry.all_of<char>(entity)));
  972. ASSERT_FALSE((registry.any_of<int, double, float>(entity)));
  973. }
  974. TEST(View, Pipe) {
  975. entt::registry registry;
  976. const auto entity = registry.create();
  977. const auto other = registry.create();
  978. registry.emplace<int>(entity);
  979. registry.emplace<char>(entity);
  980. registry.emplace<double>(entity);
  981. registry.emplace<empty_type>(entity);
  982. registry.emplace<int>(other);
  983. registry.emplace<char>(other);
  984. registry.emplace<stable_type>(other);
  985. const auto view1 = registry.view<int>(entt::exclude<const double>);
  986. const auto view2 = registry.view<const char>(entt::exclude<float>);
  987. const auto view3 = registry.view<empty_type>();
  988. const auto view4 = registry.view<stable_type>();
  989. 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)>);
  990. 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)>);
  991. static_assert(std::is_same_v<decltype((view3 | view2) | view1), decltype(view3 | (view2 | view1))>);
  992. ASSERT_FALSE((view1 | view2).contains(entity));
  993. ASSERT_TRUE((view1 | view2).contains(other));
  994. ASSERT_TRUE((view3 | view2).contains(entity));
  995. ASSERT_FALSE((view3 | view2).contains(other));
  996. ASSERT_FALSE((view1 | view2 | view3).contains(entity));
  997. ASSERT_FALSE((view1 | view2 | view3).contains(other));
  998. ASSERT_FALSE((view1 | view4 | view2).contains(entity));
  999. ASSERT_TRUE((view1 | view4 | view2).contains(other));
  1000. }