view.cpp 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290
  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(cview.storage()), const entt::storage_type_t<char> &>);
  347. static_assert(std::is_same_v<decltype(cview.storage<0u>()), const entt::storage_type_t<char> &>);
  348. static_assert(std::is_same_v<decltype(cview.storage<const char>()), const entt::storage_type_t<char> &>);
  349. ASSERT_EQ(view.size(), 0u);
  350. ASSERT_EQ(cview.size(), 0u);
  351. view.storage().emplace(entity);
  352. registry.emplace<char>(entity);
  353. ASSERT_EQ(view.size(), 1u);
  354. ASSERT_EQ(cview.size(), 1u);
  355. ASSERT_TRUE(view.storage<int>().contains(entity));
  356. ASSERT_TRUE(cview.storage<0u>().contains(entity));
  357. ASSERT_TRUE((registry.all_of<int, char>(entity)));
  358. view.storage().erase(entity);
  359. ASSERT_EQ(view.size(), 0u);
  360. ASSERT_EQ(cview.size(), 1u);
  361. ASSERT_FALSE(view.storage<0u>().contains(entity));
  362. ASSERT_TRUE(cview.storage<const char>().contains(entity));
  363. ASSERT_FALSE((registry.all_of<int, char>(entity)));
  364. }
  365. TEST(MultiComponentView, Functionalities) {
  366. entt::registry registry;
  367. auto view = registry.view<int, char>();
  368. auto cview = std::as_const(registry).view<const int, const char>();
  369. const auto e0 = registry.create();
  370. registry.emplace<char>(e0, '1');
  371. const auto e1 = registry.create();
  372. registry.emplace<int>(e1, 42);
  373. registry.emplace<char>(e1, '2');
  374. ASSERT_EQ(*view.begin(), e1);
  375. ASSERT_EQ(*cview.begin(), e1);
  376. ASSERT_EQ(++view.begin(), (view.end()));
  377. ASSERT_EQ(++cview.begin(), (cview.end()));
  378. ASSERT_NO_FATAL_FAILURE((view.begin()++));
  379. ASSERT_NO_FATAL_FAILURE((++cview.begin()));
  380. ASSERT_NE(view.begin(), view.end());
  381. ASSERT_NE(cview.begin(), cview.end());
  382. ASSERT_EQ(view.size_hint(), 1u);
  383. for(auto entity: view) {
  384. ASSERT_EQ(std::get<0>(cview.get<const int, const char>(entity)), 42);
  385. ASSERT_EQ(std::get<0>(cview.get<0u, 1u>(entity)), 42);
  386. ASSERT_EQ(std::get<1>(view.get<int, char>(entity)), '2');
  387. ASSERT_EQ(std::get<1>(view.get<0u, 1u>(entity)), '2');
  388. ASSERT_EQ(cview.get<const char>(entity), '2');
  389. ASSERT_EQ(cview.get<1u>(entity), '2');
  390. }
  391. decltype(view) invalid{};
  392. ASSERT_TRUE(view);
  393. ASSERT_TRUE(cview);
  394. ASSERT_FALSE(invalid);
  395. }
  396. TEST(MultiComponentView, Constructors) {
  397. entt::storage<int> storage{};
  398. entt::view<entt::get_t<int, int>> invalid{};
  399. entt::basic_view from_storage{storage, storage};
  400. entt::basic_view from_tuple{std::forward_as_tuple(storage, storage)};
  401. ASSERT_FALSE(invalid);
  402. ASSERT_TRUE(from_storage);
  403. ASSERT_TRUE(from_tuple);
  404. ASSERT_EQ(&from_storage.handle(), &from_tuple.handle());
  405. }
  406. TEST(MultiComponentView, Handle) {
  407. entt::registry registry;
  408. const auto entity = registry.create();
  409. auto view = registry.view<int, char>();
  410. auto &&handle = view.handle();
  411. ASSERT_TRUE(handle.empty());
  412. ASSERT_FALSE(handle.contains(entity));
  413. ASSERT_EQ(&handle, &view.handle());
  414. registry.emplace<int>(entity);
  415. ASSERT_FALSE(handle.empty());
  416. ASSERT_TRUE(handle.contains(entity));
  417. ASSERT_EQ(&handle, &view.handle());
  418. view = view.refresh();
  419. auto &&other = view.handle();
  420. ASSERT_TRUE(other.empty());
  421. ASSERT_FALSE(other.contains(entity));
  422. ASSERT_EQ(&other, &view.handle());
  423. ASSERT_NE(&handle, &other);
  424. view = view.use<int>();
  425. ASSERT_NE(&other, &view.handle());
  426. ASSERT_EQ(&handle, &view.handle());
  427. }
  428. TEST(MultiComponentView, LazyTypesFromConstRegistry) {
  429. entt::registry registry{};
  430. auto view = std::as_const(registry).view<const empty_type, const int>();
  431. const auto entity = registry.create();
  432. registry.emplace<empty_type>(entity);
  433. registry.emplace<int>(entity);
  434. ASSERT_TRUE(view);
  435. ASSERT_EQ(view.size_hint(), 0u);
  436. ASSERT_FALSE(view.contains(entity));
  437. ASSERT_EQ(view.begin(), view.end());
  438. ASSERT_EQ(view.find(entity), view.end());
  439. ASSERT_NE(view.front(), entity);
  440. ASSERT_NE(view.back(), entity);
  441. }
  442. TEST(MultiComponentView, LazyExcludedTypeFromConstRegistry) {
  443. entt::registry registry;
  444. auto entity = registry.create();
  445. registry.emplace<int>(entity);
  446. auto view = std::as_const(registry).view<const int>(entt::exclude<char>);
  447. ASSERT_TRUE(view);
  448. ASSERT_EQ(view.size_hint(), 1u);
  449. ASSERT_TRUE(view.contains(entity));
  450. ASSERT_NE(view.begin(), view.end());
  451. ASSERT_NE(view.find(entity), view.end());
  452. ASSERT_EQ(view.front(), entity);
  453. ASSERT_EQ(view.back(), entity);
  454. }
  455. TEST(MultiComponentView, Iterator) {
  456. entt::registry registry;
  457. const entt::entity entity[2]{registry.create(), registry.create()};
  458. registry.insert<int>(std::begin(entity), std::end(entity));
  459. registry.insert<char>(std::begin(entity), std::end(entity));
  460. const auto view = registry.view<int, char>();
  461. using iterator = typename decltype(view)::iterator;
  462. iterator end{view.begin()};
  463. iterator begin{};
  464. begin = view.end();
  465. std::swap(begin, end);
  466. ASSERT_EQ(begin, view.begin());
  467. ASSERT_EQ(end, view.end());
  468. ASSERT_NE(begin, end);
  469. ASSERT_EQ(*begin, entity[1u]);
  470. ASSERT_EQ(*begin.operator->(), entity[1u]);
  471. ASSERT_EQ(begin++, view.begin());
  472. ASSERT_EQ(*begin, entity[0u]);
  473. ASSERT_EQ(*begin.operator->(), entity[0u]);
  474. ASSERT_EQ(++begin, view.end());
  475. }
  476. TEST(MultiComponentView, ElementAccess) {
  477. entt::registry registry;
  478. auto view = registry.view<int, char>();
  479. auto cview = std::as_const(registry).view<const int, const char>();
  480. const auto e0 = registry.create();
  481. registry.emplace<int>(e0, 42);
  482. registry.emplace<char>(e0, '0');
  483. const auto e1 = registry.create();
  484. registry.emplace<int>(e1, 3);
  485. registry.emplace<char>(e1, '1');
  486. ASSERT_EQ(view[e0], std::make_tuple(42, '0'));
  487. ASSERT_EQ(cview[e1], std::make_tuple(3, '1'));
  488. }
  489. TEST(MultiComponentView, Contains) {
  490. entt::registry registry;
  491. const auto e0 = registry.create();
  492. registry.emplace<int>(e0);
  493. registry.emplace<char>(e0);
  494. const auto e1 = registry.create();
  495. registry.emplace<int>(e1);
  496. registry.emplace<char>(e1);
  497. registry.destroy(e0);
  498. auto view = registry.view<int, char>();
  499. ASSERT_FALSE(view.contains(e0));
  500. ASSERT_TRUE(view.contains(e1));
  501. }
  502. TEST(MultiComponentView, SizeHint) {
  503. entt::registry registry;
  504. const auto e0 = registry.create();
  505. registry.emplace<double>(e0);
  506. registry.emplace<int>(e0);
  507. registry.emplace<float>(e0);
  508. const auto e1 = registry.create();
  509. registry.emplace<char>(e1);
  510. registry.emplace<float>(e1);
  511. auto view = registry.view<char, int, float>();
  512. ASSERT_EQ(view.size_hint(), 1u);
  513. ASSERT_EQ(view.begin(), view.end());
  514. }
  515. TEST(MultiComponentView, Each) {
  516. entt::registry registry;
  517. entt::entity entity[2]{registry.create(), registry.create()};
  518. auto view = registry.view<int, char>(entt::exclude<double>);
  519. auto cview = std::as_const(registry).view<const int, const char>();
  520. registry.emplace<int>(entity[0u], 0);
  521. registry.emplace<char>(entity[0u], static_cast<char>(0));
  522. registry.emplace<int>(entity[1u], 1);
  523. registry.emplace<char>(entity[1u], static_cast<char>(1));
  524. auto iterable = view.each();
  525. auto citerable = cview.each();
  526. ASSERT_NE(citerable.begin(), citerable.end());
  527. ASSERT_NO_FATAL_FAILURE(iterable.begin()->operator=(*iterable.begin()));
  528. ASSERT_EQ(decltype(iterable.end()){}, iterable.end());
  529. auto it = iterable.begin();
  530. ASSERT_EQ((it++, ++it), iterable.end());
  531. view.each([expected = 1](auto entt, int &ivalue, char &cvalue) mutable {
  532. ASSERT_EQ(static_cast<int>(entt::to_integral(entt)), expected);
  533. ASSERT_EQ(ivalue, expected);
  534. ASSERT_EQ(cvalue, expected);
  535. --expected;
  536. });
  537. cview.each([expected = 1](const int &ivalue, const char &cvalue) mutable {
  538. ASSERT_EQ(ivalue, expected);
  539. ASSERT_EQ(cvalue, expected);
  540. --expected;
  541. });
  542. ASSERT_EQ(std::get<0>(*iterable.begin()), entity[1u]);
  543. ASSERT_EQ(std::get<0>(*++citerable.begin()), entity[0u]);
  544. static_assert(std::is_same_v<decltype(std::get<1>(*iterable.begin())), int &>);
  545. static_assert(std::is_same_v<decltype(std::get<2>(*citerable.begin())), const char &>);
  546. // do not use iterable, make sure an iterable view works when created from a temporary
  547. for(auto [entt, ivalue, cvalue]: registry.view<int, char>().each()) {
  548. ASSERT_EQ(static_cast<int>(entt::to_integral(entt)), ivalue);
  549. ASSERT_EQ(static_cast<char>(entt::to_integral(entt)), cvalue);
  550. }
  551. }
  552. TEST(MultiComponentView, EachWithSuggestedType) {
  553. entt::registry registry;
  554. for(auto i = 0; i < 3; ++i) {
  555. const auto entity = registry.create();
  556. registry.emplace<int>(entity, i);
  557. registry.emplace<char>(entity);
  558. }
  559. // makes char a better candidate during iterations
  560. const auto entity = registry.create();
  561. registry.emplace<int>(entity, 99);
  562. registry.view<int, char>().use<int>().each([value = 2](const auto curr, const auto) mutable {
  563. ASSERT_EQ(curr, value--);
  564. });
  565. registry.sort<int>([](const auto lhs, const auto rhs) {
  566. return lhs < rhs;
  567. });
  568. registry.view<int, char>().use<0u>().each([value = 0](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. auto value = registry.view<int, char>().size_hint();
  575. for(auto &&curr: registry.view<int, char>().each()) {
  576. ASSERT_EQ(std::get<1>(curr), static_cast<int>(--value));
  577. }
  578. registry.sort<int>([](const auto lhs, const auto rhs) {
  579. return lhs < rhs;
  580. });
  581. value = {};
  582. for(auto &&curr: registry.view<int, char>().use<int>().each()) {
  583. ASSERT_EQ(std::get<1>(curr), static_cast<int>(value++));
  584. }
  585. }
  586. TEST(MultiComponentView, EachWithHoles) {
  587. entt::registry registry;
  588. const auto e0 = registry.create();
  589. const auto e1 = registry.create();
  590. const auto e2 = registry.create();
  591. registry.emplace<char>(e0, '0');
  592. registry.emplace<char>(e1, '1');
  593. registry.emplace<int>(e0, 0);
  594. registry.emplace<int>(e2, 2);
  595. auto view = registry.view<char, int>();
  596. view.each([e0](auto entity, const char &c, const int &i) {
  597. ASSERT_EQ(entity, e0);
  598. ASSERT_EQ(c, '0');
  599. ASSERT_EQ(i, 0);
  600. });
  601. for(auto &&curr: view.each()) {
  602. ASSERT_EQ(std::get<0>(curr), e0);
  603. ASSERT_EQ(std::get<1>(curr), '0');
  604. ASSERT_EQ(std::get<2>(curr), 0);
  605. }
  606. }
  607. TEST(MultiComponentView, ConstNonConstAndAllInBetween) {
  608. entt::registry registry;
  609. auto view = registry.view<int, empty_type, const char>();
  610. ASSERT_EQ(view.size_hint(), 0u);
  611. const auto entity = registry.create();
  612. registry.emplace<int>(entity, 0);
  613. registry.emplace<empty_type>(entity);
  614. registry.emplace<char>(entity, 'c');
  615. ASSERT_EQ(view.size_hint(), 1u);
  616. static_assert(std::is_same_v<decltype(view.get<0u>({})), int &>);
  617. static_assert(std::is_same_v<decltype(view.get<2u>({})), const char &>);
  618. static_assert(std::is_same_v<decltype(view.get<0u, 2u>({})), std::tuple<int &, const char &>>);
  619. static_assert(std::is_same_v<decltype(view.get<int>({})), int &>);
  620. static_assert(std::is_same_v<decltype(view.get<const char>({})), const char &>);
  621. static_assert(std::is_same_v<decltype(view.get<int, const char>({})), std::tuple<int &, const char &>>);
  622. static_assert(std::is_same_v<decltype(view.get({})), std::tuple<int &, const char &>>);
  623. static_assert(std::is_same_v<decltype(std::as_const(registry).view<char, int>()), decltype(std::as_const(registry).view<const char, const int>())>);
  624. 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>())>);
  625. 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>())>);
  626. view.each([](auto &&i, auto &&c) {
  627. static_assert(std::is_same_v<decltype(i), int &>);
  628. static_assert(std::is_same_v<decltype(c), const char &>);
  629. });
  630. for(auto [entt, iv, cv]: view.each()) {
  631. static_assert(std::is_same_v<decltype(entt), entt::entity>);
  632. static_assert(std::is_same_v<decltype(iv), int &>);
  633. static_assert(std::is_same_v<decltype(cv), const char &>);
  634. }
  635. }
  636. TEST(MultiComponentView, Find) {
  637. entt::registry registry;
  638. auto view = registry.view<int, const char>();
  639. const auto e0 = registry.create();
  640. registry.emplace<int>(e0);
  641. registry.emplace<char>(e0);
  642. const auto e1 = registry.create();
  643. registry.emplace<int>(e1);
  644. registry.emplace<char>(e1);
  645. const auto e2 = registry.create();
  646. registry.emplace<int>(e2);
  647. registry.emplace<char>(e2);
  648. const auto e3 = registry.create();
  649. registry.emplace<int>(e3);
  650. registry.emplace<char>(e3);
  651. registry.erase<int>(e1);
  652. ASSERT_NE(view.find(e0), view.end());
  653. ASSERT_EQ(view.find(e1), view.end());
  654. ASSERT_NE(view.find(e2), view.end());
  655. ASSERT_NE(view.find(e3), view.end());
  656. auto it = view.find(e2);
  657. ASSERT_EQ(*it, e2);
  658. ASSERT_EQ(*(++it), e3);
  659. ASSERT_EQ(*(++it), e0);
  660. ASSERT_EQ(++it, view.end());
  661. ASSERT_EQ(++view.find(e0), view.end());
  662. const auto e4 = registry.create();
  663. registry.destroy(e4);
  664. const auto e5 = registry.create();
  665. registry.emplace<int>(e5);
  666. registry.emplace<char>(e5);
  667. ASSERT_NE(view.find(e5), view.end());
  668. ASSERT_EQ(view.find(e4), view.end());
  669. }
  670. TEST(MultiComponentView, ExcludedComponents) {
  671. entt::registry registry;
  672. const auto e0 = registry.create();
  673. registry.emplace<int>(e0, 0);
  674. const auto e1 = registry.create();
  675. registry.emplace<int>(e1, 1);
  676. registry.emplace<char>(e1);
  677. const auto e2 = registry.create();
  678. registry.emplace<int>(e2, 2);
  679. const auto e3 = registry.create();
  680. registry.emplace<int>(e3, 3);
  681. registry.emplace<char>(e3);
  682. const auto view = std::as_const(registry).view<const int>(entt::exclude<char>);
  683. for(const auto entity: view) {
  684. ASSERT_TRUE(entity == e0 || entity == e2);
  685. if(entity == e0) {
  686. ASSERT_EQ(view.get<const int>(e0), 0);
  687. ASSERT_EQ(view.get<0u>(e0), 0);
  688. } else if(entity == e2) {
  689. ASSERT_EQ(std::get<0>(view.get(e2)), 2);
  690. }
  691. }
  692. registry.emplace<char>(e0);
  693. registry.emplace<char>(e2);
  694. registry.erase<char>(e1);
  695. registry.erase<char>(e3);
  696. for(const auto entity: view) {
  697. ASSERT_TRUE(entity == e1 || entity == e3);
  698. if(entity == e1) {
  699. ASSERT_EQ(std::get<0>(view.get(e1)), 1);
  700. } else if(entity == e3) {
  701. ASSERT_EQ(view.get<const int>(e3), 3);
  702. ASSERT_EQ(view.get<0u>(e3), 3);
  703. }
  704. }
  705. }
  706. TEST(MultiComponentView, EmptyTypes) {
  707. entt::registry registry;
  708. const auto entity = registry.create();
  709. registry.emplace<int>(entity);
  710. registry.emplace<char>(entity);
  711. registry.emplace<empty_type>(entity);
  712. const auto other = registry.create();
  713. registry.emplace<int>(other);
  714. registry.emplace<char>(other);
  715. registry.emplace<double>(other);
  716. registry.emplace<empty_type>(other);
  717. const auto ignored = registry.create();
  718. registry.emplace<int>(ignored);
  719. registry.emplace<char>(ignored);
  720. registry.view<int, char, empty_type>(entt::exclude<double>).each([entity](const auto entt, int, char) {
  721. ASSERT_EQ(entity, entt);
  722. });
  723. for(auto [entt, iv, cv]: registry.view<int, char, empty_type>(entt::exclude<double>).each()) {
  724. static_assert(std::is_same_v<decltype(entt), entt::entity>);
  725. static_assert(std::is_same_v<decltype(iv), int &>);
  726. static_assert(std::is_same_v<decltype(cv), char &>);
  727. ASSERT_EQ(entity, entt);
  728. }
  729. registry.view<int, empty_type, char>(entt::exclude<double>).each([check = true](int, char) mutable {
  730. ASSERT_TRUE(check);
  731. check = false;
  732. });
  733. for(auto [entt, iv, cv]: registry.view<int, empty_type, char>(entt::exclude<double>).each()) {
  734. static_assert(std::is_same_v<decltype(entt), entt::entity>);
  735. static_assert(std::is_same_v<decltype(iv), int &>);
  736. static_assert(std::is_same_v<decltype(cv), char &>);
  737. ASSERT_EQ(entity, entt);
  738. }
  739. registry.view<empty_type, int, char>(entt::exclude<double>).each([entity](const auto entt, int, char) {
  740. ASSERT_EQ(entity, entt);
  741. });
  742. for(auto [entt, iv, cv]: registry.view<empty_type, int, char>(entt::exclude<double>).each()) {
  743. static_assert(std::is_same_v<decltype(entt), entt::entity>);
  744. static_assert(std::is_same_v<decltype(iv), int &>);
  745. static_assert(std::is_same_v<decltype(cv), char &>);
  746. ASSERT_EQ(entity, entt);
  747. }
  748. registry.view<empty_type, int, char>(entt::exclude<double>).use<empty_type>().each([entity](const auto entt, int, char) {
  749. ASSERT_EQ(entity, entt);
  750. });
  751. for(auto [entt, iv, cv]: registry.view<empty_type, int, char>(entt::exclude<double>).use<0u>().each()) {
  752. static_assert(std::is_same_v<decltype(entt), entt::entity>);
  753. static_assert(std::is_same_v<decltype(iv), int &>);
  754. static_assert(std::is_same_v<decltype(cv), char &>);
  755. ASSERT_EQ(entity, entt);
  756. }
  757. registry.view<int, empty_type, char>(entt::exclude<double>).use<1u>().each([check = true](int, char) mutable {
  758. ASSERT_TRUE(check);
  759. check = false;
  760. });
  761. for(auto [entt, iv, cv]: registry.view<int, empty_type, char>(entt::exclude<double>).use<empty_type>().each()) {
  762. static_assert(std::is_same_v<decltype(entt), entt::entity>);
  763. static_assert(std::is_same_v<decltype(iv), int &>);
  764. static_assert(std::is_same_v<decltype(cv), char &>);
  765. ASSERT_EQ(entity, entt);
  766. }
  767. }
  768. TEST(MultiComponentView, FrontBack) {
  769. entt::registry registry;
  770. auto view = registry.view<const int, const char>();
  771. ASSERT_EQ(view.front(), static_cast<entt::entity>(entt::null));
  772. ASSERT_EQ(view.back(), static_cast<entt::entity>(entt::null));
  773. const auto e0 = registry.create();
  774. registry.emplace<int>(e0);
  775. registry.emplace<char>(e0);
  776. const auto e1 = registry.create();
  777. registry.emplace<int>(e1);
  778. registry.emplace<char>(e1);
  779. const auto entity = registry.create();
  780. registry.emplace<char>(entity);
  781. ASSERT_EQ(view.front(), e1);
  782. ASSERT_EQ(view.back(), e0);
  783. }
  784. TEST(MultiComponentView, ExtendedGet) {
  785. using type = decltype(std::declval<entt::registry>().view<int, empty_type, char>().get({}));
  786. static_assert(std::tuple_size_v<type> == 2u);
  787. static_assert(std::is_same_v<std::tuple_element_t<0, type>, int &>);
  788. static_assert(std::is_same_v<std::tuple_element_t<1, type>, char &>);
  789. }
  790. TEST(MultiComponentView, DeductionGuide) {
  791. entt::registry registry;
  792. entt::storage_type_t<int> istorage;
  793. entt::storage_type_t<double> dstorage;
  794. entt::storage_type_t<stable_type> sstorage;
  795. 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})>);
  796. 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})>);
  797. 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)})>);
  798. 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)})>);
  799. 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})>);
  800. 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()})>);
  801. 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)})>);
  802. 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))})>);
  803. 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))})>);
  804. 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)})>);
  805. 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)})>);
  806. 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)})>);
  807. 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))})>);
  808. 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))})>);
  809. 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)})>);
  810. }
  811. TEST(MultiComponentView, IterableViewAlgorithmCompatibility) {
  812. entt::registry registry;
  813. const auto entity = registry.create();
  814. registry.emplace<int>(entity);
  815. registry.emplace<char>(entity);
  816. const auto view = registry.view<int, char>();
  817. const auto iterable = view.each();
  818. const auto it = std::find_if(iterable.begin(), iterable.end(), [entity](auto args) { return std::get<0>(args) == entity; });
  819. ASSERT_EQ(std::get<0>(*it), entity);
  820. }
  821. TEST(MultiComponentView, StableType) {
  822. entt::registry registry;
  823. auto view = registry.view<int, stable_type>();
  824. const auto entity = registry.create();
  825. const auto other = registry.create();
  826. registry.emplace<int>(entity);
  827. registry.emplace<int>(other);
  828. registry.emplace<stable_type>(entity);
  829. registry.emplace<stable_type>(other);
  830. registry.destroy(entity);
  831. ASSERT_EQ(view.size_hint(), 1u);
  832. view = view.use<stable_type>();
  833. ASSERT_EQ(view.size_hint(), 2u);
  834. ASSERT_FALSE(view.contains(entity));
  835. ASSERT_TRUE(view.contains(other));
  836. ASSERT_EQ(view.front(), other);
  837. ASSERT_EQ(view.back(), other);
  838. ASSERT_EQ(*view.begin(), other);
  839. ASSERT_EQ(++view.begin(), view.end());
  840. view.each([other](const auto entt, int, stable_type) {
  841. ASSERT_EQ(other, entt);
  842. });
  843. view.each([check = true](int, stable_type) mutable {
  844. ASSERT_TRUE(check);
  845. check = false;
  846. });
  847. for(auto [entt, iv, st]: view.each()) {
  848. static_assert(std::is_same_v<decltype(entt), entt::entity>);
  849. static_assert(std::is_same_v<decltype(iv), int &>);
  850. static_assert(std::is_same_v<decltype(st), stable_type &>);
  851. ASSERT_EQ(other, entt);
  852. }
  853. registry.compact();
  854. ASSERT_EQ(view.size_hint(), 1u);
  855. }
  856. TEST(MultiComponentView, StableTypeWithExcludedComponent) {
  857. entt::registry registry;
  858. auto view = registry.view<stable_type>(entt::exclude<int>).use<stable_type>();
  859. const auto entity = registry.create();
  860. const auto other = registry.create();
  861. registry.emplace<stable_type>(entity, 0);
  862. registry.emplace<stable_type>(other, 42);
  863. registry.emplace<int>(entity);
  864. ASSERT_EQ(view.size_hint(), 2u);
  865. ASSERT_FALSE(view.contains(entity));
  866. ASSERT_TRUE(view.contains(other));
  867. registry.destroy(entity);
  868. ASSERT_EQ(view.size_hint(), 2u);
  869. ASSERT_FALSE(view.contains(entity));
  870. ASSERT_TRUE(view.contains(other));
  871. for(auto entt: view) {
  872. constexpr entt::entity tombstone = entt::tombstone;
  873. ASSERT_NE(entt, tombstone);
  874. ASSERT_EQ(entt, other);
  875. }
  876. for(auto [entt, comp]: view.each()) {
  877. constexpr entt::entity tombstone = entt::tombstone;
  878. ASSERT_NE(entt, tombstone);
  879. ASSERT_EQ(entt, other);
  880. ASSERT_EQ(comp.value, 42);
  881. }
  882. view.each([other](const auto entt, auto &&...) {
  883. constexpr entt::entity tombstone = entt::tombstone;
  884. ASSERT_NE(entt, tombstone);
  885. ASSERT_EQ(entt, other);
  886. });
  887. }
  888. TEST(MultiComponentView, SameComponentTypes) {
  889. entt::registry registry;
  890. entt::storage_type_t<int> storage;
  891. entt::storage_type_t<int> other;
  892. entt::basic_view view{storage, other};
  893. storage.bind(entt::forward_as_any(registry));
  894. other.bind(entt::forward_as_any(registry));
  895. const entt::entity e0{42u};
  896. const entt::entity e1{3u};
  897. storage.emplace(e0, 7);
  898. other.emplace(e0, 9);
  899. other.emplace(e1, 1);
  900. ASSERT_TRUE(view.contains(e0));
  901. ASSERT_FALSE(view.contains(e1));
  902. ASSERT_EQ((view.get<0u, 1u>(e0)), (std::make_tuple(7, 9)));
  903. ASSERT_EQ(view.get<1u>(e0), 9);
  904. for(auto entt: view) {
  905. ASSERT_EQ(entt, e0);
  906. }
  907. view.each([&](auto entt, auto &&first, auto &&second) {
  908. ASSERT_EQ(entt, e0);
  909. ASSERT_EQ(first, 7);
  910. ASSERT_EQ(second, 9);
  911. });
  912. for(auto [entt, first, second]: view.each()) {
  913. ASSERT_EQ(entt, e0);
  914. ASSERT_EQ(first, 7);
  915. ASSERT_EQ(second, 9);
  916. }
  917. ASSERT_EQ(&view.handle(), &storage);
  918. ASSERT_EQ(&view.use<1u>().handle(), &other);
  919. }
  920. TEST(View, Pipe) {
  921. entt::registry registry;
  922. const auto entity = registry.create();
  923. const auto other = registry.create();
  924. registry.emplace<int>(entity);
  925. registry.emplace<char>(entity);
  926. registry.emplace<double>(entity);
  927. registry.emplace<empty_type>(entity);
  928. registry.emplace<int>(other);
  929. registry.emplace<char>(other);
  930. registry.emplace<stable_type>(other);
  931. const auto view1 = registry.view<int>(entt::exclude<const double>);
  932. const auto view2 = registry.view<const char>(entt::exclude<float>);
  933. const auto view3 = registry.view<empty_type>();
  934. const auto view4 = registry.view<stable_type>();
  935. 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)>);
  936. 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)>);
  937. static_assert(std::is_same_v<decltype((view3 | view2) | view1), decltype(view3 | (view2 | view1))>);
  938. ASSERT_FALSE((view1 | view2).contains(entity));
  939. ASSERT_TRUE((view1 | view2).contains(other));
  940. ASSERT_TRUE((view3 | view2).contains(entity));
  941. ASSERT_FALSE((view3 | view2).contains(other));
  942. ASSERT_FALSE((view1 | view2 | view3).contains(entity));
  943. ASSERT_FALSE((view1 | view2 | view3).contains(other));
  944. ASSERT_FALSE((view1 | view4 | view2).contains(entity));
  945. ASSERT_TRUE((view1 | view4 | view2).contains(other));
  946. }
  947. TEST(MultiComponentView, Storage) {
  948. entt::registry registry;
  949. const auto entity = registry.create();
  950. const auto view = registry.view<int, const char>();
  951. static_assert(std::is_same_v<decltype(view.storage<0u>()), entt::storage_type_t<int> &>);
  952. static_assert(std::is_same_v<decltype(view.storage<int>()), entt::storage_type_t<int> &>);
  953. static_assert(std::is_same_v<decltype(view.storage<1u>()), const entt::storage_type_t<char> &>);
  954. static_assert(std::is_same_v<decltype(view.storage<const char>()), const entt::storage_type_t<char> &>);
  955. ASSERT_EQ(view.size_hint(), 0u);
  956. view.storage<int>().emplace(entity);
  957. registry.emplace<char>(entity);
  958. ASSERT_EQ(view.size_hint(), 1u);
  959. ASSERT_TRUE(view.storage<const char>().contains(entity));
  960. ASSERT_TRUE((registry.all_of<int, char>(entity)));
  961. view.storage<0u>().erase(entity);
  962. ASSERT_EQ(view.size_hint(), 0u);
  963. ASSERT_TRUE(view.storage<1u>().contains(entity));
  964. ASSERT_FALSE((registry.all_of<int, char>(entity)));
  965. }