view.cpp 45 KB

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