registry.cpp 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475
  1. #include <array>
  2. #include <cstddef>
  3. #include <cstdint>
  4. #include <functional>
  5. #include <iterator>
  6. #include <memory>
  7. #include <tuple>
  8. #include <unordered_set>
  9. #include <utility>
  10. #include <vector>
  11. #include <gtest/gtest.h>
  12. #include <entt/config/config.h>
  13. #include <entt/core/hashed_string.hpp>
  14. #include <entt/core/type_info.hpp>
  15. #include <entt/entity/entity.hpp>
  16. #include <entt/entity/registry.hpp>
  17. #include <entt/entity/view.hpp>
  18. #include "../../common/aggregate.h"
  19. #include "../../common/config.h"
  20. #include "../../common/empty.h"
  21. #include "../../common/entity.h"
  22. #include "../../common/mixin.hpp"
  23. #include "../../common/non_default_constructible.h"
  24. #include "../../common/pointer_stable.h"
  25. struct no_eto_type {
  26. static constexpr std::size_t page_size = 1024u;
  27. };
  28. bool operator==(const no_eto_type &lhs, const no_eto_type &rhs) {
  29. return &lhs == &rhs;
  30. }
  31. struct listener {
  32. template<typename Type>
  33. static void sort(entt::registry &registry) {
  34. registry.sort<Type>([](auto lhs, auto rhs) { return lhs < rhs; });
  35. }
  36. void incr(const entt::registry &, entt::entity entity) {
  37. last = entity;
  38. ++counter;
  39. }
  40. void decr(const entt::registry &, entt::entity entity) {
  41. last = entity;
  42. --counter;
  43. }
  44. entt::entity last{entt::null};
  45. int counter{0};
  46. };
  47. struct owner {
  48. void receive(const entt::registry &ref) {
  49. parent = &ref;
  50. }
  51. const entt::registry *parent{nullptr};
  52. };
  53. struct destruction_order {
  54. using ctx_check_type = int;
  55. destruction_order(const entt::registry &ref, bool &ctx)
  56. : registry{&ref},
  57. ctx_check{&ctx} {
  58. *ctx_check = (registry->ctx().find<ctx_check_type>() != nullptr);
  59. }
  60. destruction_order(const destruction_order &) = delete;
  61. destruction_order &operator=(const destruction_order &) = delete;
  62. ~destruction_order() noexcept {
  63. *ctx_check = *ctx_check && (registry->ctx().find<ctx_check_type>() != nullptr);
  64. }
  65. private:
  66. const entt::registry *registry;
  67. bool *ctx_check{};
  68. };
  69. struct entity_traits {
  70. using value_type = test::entity;
  71. using entity_type = uint32_t;
  72. using version_type = uint16_t;
  73. static constexpr entity_type entity_mask = 0xFF;
  74. static constexpr entity_type version_mask = 0x00;
  75. };
  76. template<>
  77. struct entt::entt_traits<test::entity>: entt::basic_entt_traits<entity_traits> {
  78. static constexpr auto page_size = ENTT_SPARSE_PAGE;
  79. };
  80. TEST(Registry, Functionalities) {
  81. using traits_type = entt::entt_traits<entt::entity>;
  82. entt::registry registry{};
  83. ASSERT_NO_THROW([[maybe_unused]] auto alloc = registry.get_allocator());
  84. ASSERT_EQ(registry.storage<entt::entity>().size(), 0u);
  85. ASSERT_EQ(registry.storage<entt::entity>().free_list(), 0u);
  86. ASSERT_NO_THROW(registry.storage<entt::entity>().reserve(4));
  87. ASSERT_EQ(registry.storage<entt::entity>().capacity(), 4u);
  88. ASSERT_TRUE(registry.storage<entt::entity>().empty());
  89. ASSERT_EQ(registry.storage<int>().size(), 0u);
  90. ASSERT_EQ(registry.storage<char>().size(), 0u);
  91. ASSERT_TRUE(registry.storage<int>().empty());
  92. ASSERT_TRUE(registry.storage<char>().empty());
  93. const auto e0 = registry.create();
  94. const auto e1 = registry.create();
  95. registry.emplace<int>(e1);
  96. registry.emplace<char>(e1);
  97. ASSERT_TRUE(registry.all_of<>(e0));
  98. ASSERT_FALSE(registry.any_of<>(e1));
  99. ASSERT_EQ(registry.storage<int>().size(), 1u);
  100. ASSERT_EQ(registry.storage<char>().size(), 1u);
  101. ASSERT_FALSE(registry.storage<int>().empty());
  102. ASSERT_FALSE(registry.storage<char>().empty());
  103. ASSERT_NE(e0, e1);
  104. ASSERT_FALSE((registry.all_of<int, const char>(e0)));
  105. ASSERT_TRUE((registry.all_of<const int, char>(e1)));
  106. ASSERT_FALSE((registry.any_of<int, const double>(e0)));
  107. ASSERT_TRUE((registry.any_of<const int, double>(e1)));
  108. ASSERT_EQ(registry.try_get<int>(e0), nullptr);
  109. ASSERT_NE(registry.try_get<int>(e1), nullptr);
  110. ASSERT_EQ(registry.try_get<char>(e0), nullptr);
  111. ASSERT_NE(registry.try_get<char>(e1), nullptr);
  112. ASSERT_EQ(registry.try_get<double>(e0), nullptr);
  113. ASSERT_EQ(registry.try_get<double>(e1), nullptr);
  114. ASSERT_EQ(registry.emplace<int>(e0, 4), 4);
  115. ASSERT_EQ(registry.emplace<char>(e0, 'c'), 'c');
  116. ASSERT_NO_THROW(registry.erase<int>(e1));
  117. ASSERT_NO_THROW(registry.erase<char>(e1));
  118. ASSERT_TRUE((registry.all_of<const int, char>(e0)));
  119. ASSERT_FALSE((registry.all_of<int, const char>(e1)));
  120. ASSERT_TRUE((registry.any_of<const int, double>(e0)));
  121. ASSERT_FALSE((registry.any_of<int, const double>(e1)));
  122. const auto e2 = registry.create();
  123. registry.emplace_or_replace<int>(e2, registry.get<int>(e0));
  124. registry.emplace_or_replace<char>(e2, registry.get<char>(e0));
  125. ASSERT_TRUE((registry.all_of<int, char>(e2)));
  126. ASSERT_EQ(registry.get<int>(e0), 4);
  127. ASSERT_EQ(registry.get<char>(e0), 'c');
  128. ASSERT_NE(registry.try_get<int>(e0), nullptr);
  129. ASSERT_NE(registry.try_get<char>(e0), nullptr);
  130. ASSERT_EQ(registry.try_get<double>(e0), nullptr);
  131. ASSERT_EQ(*registry.try_get<int>(e0), 4);
  132. ASSERT_EQ(*registry.try_get<char>(e0), 'c');
  133. ASSERT_EQ(std::get<0>(registry.get<int, char>(e0)), 4);
  134. ASSERT_EQ(*std::get<0>(registry.try_get<int, char, double>(e0)), 4);
  135. ASSERT_EQ(std::get<1>(static_cast<const entt::registry &>(registry).get<int, char>(e0)), 'c');
  136. ASSERT_EQ(*std::get<1>(static_cast<const entt::registry &>(registry).try_get<int, char, double>(e0)), 'c');
  137. ASSERT_EQ(registry.get<int>(e0), registry.get<int>(e2));
  138. ASSERT_EQ(registry.get<char>(e0), registry.get<char>(e2));
  139. ASSERT_NE(&registry.get<int>(e0), &registry.get<int>(e2));
  140. ASSERT_NE(&registry.get<char>(e0), &registry.get<char>(e2));
  141. ASSERT_EQ(registry.patch<int>(e0, [](auto &instance) { instance = 2; }), 2);
  142. ASSERT_EQ(registry.replace<int>(e0, 3), 3);
  143. ASSERT_NO_THROW(registry.emplace_or_replace<int>(e0, 1));
  144. ASSERT_NO_THROW(registry.emplace_or_replace<int>(e1, 1));
  145. ASSERT_EQ(static_cast<const entt::registry &>(registry).get<int>(e0), 1);
  146. ASSERT_EQ(static_cast<const entt::registry &>(registry).get<int>(e1), 1);
  147. ASSERT_EQ(registry.storage<entt::entity>().size(), 3u);
  148. ASSERT_EQ(registry.storage<entt::entity>().free_list(), 3u);
  149. ASSERT_EQ(traits_type::to_version(e2), 0u);
  150. ASSERT_EQ(registry.current(e2), 0u);
  151. ASSERT_NO_THROW(registry.destroy(e2));
  152. ASSERT_EQ(traits_type::to_version(e2), 0u);
  153. ASSERT_EQ(registry.current(e2), 1u);
  154. ASSERT_TRUE(registry.valid(e0));
  155. ASSERT_TRUE(registry.valid(e1));
  156. ASSERT_FALSE(registry.valid(e2));
  157. ASSERT_EQ(registry.storage<entt::entity>().size(), 3u);
  158. ASSERT_EQ(registry.storage<entt::entity>().free_list(), 2u);
  159. ASSERT_NO_THROW(registry.clear());
  160. ASSERT_EQ(registry.storage<entt::entity>().size(), 3u);
  161. ASSERT_EQ(registry.storage<entt::entity>().free_list(), 0u);
  162. ASSERT_FALSE(registry.storage<entt::entity>().empty());
  163. const auto e3 = registry.create();
  164. ASSERT_EQ(registry.get_or_emplace<int>(e3, 3), 3);
  165. ASSERT_EQ(registry.get_or_emplace<char>(e3, 'c'), 'c');
  166. ASSERT_EQ(registry.storage<int>().size(), 1u);
  167. ASSERT_EQ(registry.storage<char>().size(), 1u);
  168. ASSERT_FALSE(registry.storage<int>().empty());
  169. ASSERT_FALSE(registry.storage<char>().empty());
  170. ASSERT_TRUE((registry.all_of<int, char>(e3)));
  171. ASSERT_EQ(registry.get<int>(e3), 3);
  172. ASSERT_EQ(registry.get<char>(e3), 'c');
  173. ASSERT_NO_THROW(registry.clear<int>());
  174. ASSERT_EQ(registry.storage<int>().size(), 0u);
  175. ASSERT_EQ(registry.storage<char>().size(), 1u);
  176. ASSERT_TRUE(registry.storage<int>().empty());
  177. ASSERT_FALSE(registry.storage<char>().empty());
  178. ASSERT_NO_THROW(registry.clear());
  179. ASSERT_EQ(registry.storage<int>().size(), 0u);
  180. ASSERT_EQ(registry.storage<char>().size(), 0u);
  181. ASSERT_TRUE(registry.storage<int>().empty());
  182. ASSERT_TRUE(registry.storage<char>().empty());
  183. const auto e4 = registry.create();
  184. const auto e5 = registry.create();
  185. registry.emplace<int>(e4);
  186. ASSERT_EQ(registry.remove<int>(e4), 1u);
  187. ASSERT_EQ(registry.remove<int>(e5), 0u);
  188. ASSERT_EQ(registry.storage<int>().size(), 0u);
  189. ASSERT_EQ(registry.storage<char>().size(), 0u);
  190. ASSERT_TRUE(registry.storage<int>().empty());
  191. }
  192. TEST(Registry, Constructors) {
  193. entt::registry registry{};
  194. entt::registry other{64u};
  195. ASSERT_TRUE(registry.storage<entt::entity>().empty());
  196. ASSERT_NO_THROW([[maybe_unused]] auto alloc = registry.get_allocator());
  197. ASSERT_EQ(registry.storage().begin(), registry.storage().end());
  198. ASSERT_TRUE(other.storage<entt::entity>().empty());
  199. ASSERT_NO_THROW([[maybe_unused]] auto alloc = other.get_allocator());
  200. ASSERT_EQ(other.storage().begin(), other.storage().end());
  201. }
  202. TEST(Registry, Move) {
  203. entt::registry registry{};
  204. const auto entity = registry.create();
  205. owner test{};
  206. registry.on_construct<int>().connect<&owner::receive>(test);
  207. registry.on_destroy<int>().connect<&owner::receive>(test);
  208. ASSERT_EQ(test.parent, nullptr);
  209. registry.emplace<int>(entity);
  210. ASSERT_EQ(test.parent, &registry);
  211. entt::registry other{std::move(registry)};
  212. other.erase<int>(entity);
  213. registry = {};
  214. registry.emplace<int>(registry.create(entity));
  215. ASSERT_EQ(test.parent, &other);
  216. registry = std::move(other);
  217. registry.emplace<int>(entity);
  218. registry.emplace<int>(registry.create(entity));
  219. ASSERT_EQ(test.parent, &registry);
  220. }
  221. TEST(Registry, Swap) {
  222. entt::registry registry{};
  223. const auto entity = registry.create();
  224. owner test{};
  225. registry.on_construct<int>().connect<&owner::receive>(test);
  226. registry.on_destroy<int>().connect<&owner::receive>(test);
  227. ASSERT_EQ(test.parent, nullptr);
  228. registry.emplace<int>(entity);
  229. ASSERT_EQ(test.parent, &registry);
  230. entt::registry other{};
  231. other.swap(registry);
  232. other.erase<int>(entity);
  233. registry = {};
  234. registry.emplace<int>(registry.create(entity));
  235. ASSERT_EQ(test.parent, &other);
  236. registry.swap(other);
  237. registry.emplace<int>(entity);
  238. registry.emplace<int>(registry.create(entity));
  239. ASSERT_EQ(test.parent, &registry);
  240. }
  241. TEST(Registry, StorageIterable) {
  242. using namespace entt::literals;
  243. entt::registry registry{};
  244. const auto entity = registry.create();
  245. auto &storage = registry.storage<int>("other"_hs);
  246. storage.emplace(entity);
  247. for(auto [id, pool]: registry.storage()) {
  248. testing::StaticAssertTypeEq<decltype(pool), entt::sparse_set &>();
  249. testing::StaticAssertTypeEq<decltype(id), entt::id_type>();
  250. ASSERT_TRUE(pool.contains(entity));
  251. ASSERT_EQ(&storage, &pool);
  252. ASSERT_EQ(id, "other"_hs);
  253. }
  254. }
  255. TEST(Registry, ConstStorageIterable) {
  256. using namespace entt::literals;
  257. entt::registry registry{};
  258. const auto entity = registry.create();
  259. auto &storage = registry.storage<int>("other"_hs);
  260. storage.emplace(entity);
  261. for(auto &&curr: std::as_const(registry).storage()) {
  262. testing::StaticAssertTypeEq<decltype(curr.second), const entt::sparse_set &>();
  263. testing::StaticAssertTypeEq<decltype(curr.first), entt::id_type>();
  264. ASSERT_TRUE(curr.second.contains(entity));
  265. ASSERT_EQ(&storage, &curr.second);
  266. ASSERT_EQ(curr.first, "other"_hs);
  267. }
  268. }
  269. TEST(Registry, RegistryStorageIterator) {
  270. entt::registry registry{};
  271. const auto entity = registry.create();
  272. registry.emplace<int>(entity);
  273. auto iterable = registry.storage();
  274. auto end{iterable.begin()};
  275. decltype(end) begin{};
  276. begin = iterable.end();
  277. std::swap(begin, end);
  278. ASSERT_EQ(begin, iterable.cbegin());
  279. ASSERT_EQ(end, iterable.cend());
  280. ASSERT_NE(begin, end);
  281. ASSERT_EQ(begin++, iterable.begin());
  282. ASSERT_EQ(begin--, iterable.end());
  283. ASSERT_EQ(begin + 1, iterable.end());
  284. ASSERT_EQ(end - 1, iterable.begin());
  285. ASSERT_EQ(++begin, iterable.end());
  286. ASSERT_EQ(--begin, iterable.begin());
  287. ASSERT_EQ(begin += 1, iterable.end());
  288. ASSERT_EQ(begin -= 1, iterable.begin());
  289. ASSERT_EQ(begin + (end - begin), iterable.end());
  290. ASSERT_EQ(begin - (begin - end), iterable.end());
  291. ASSERT_EQ(end - (end - begin), iterable.begin());
  292. ASSERT_EQ(end + (begin - end), iterable.begin());
  293. ASSERT_EQ(begin[0u].first, iterable.begin()->first);
  294. ASSERT_EQ(&begin[0u].second, &(*iterable.begin()).second);
  295. ASSERT_LT(begin, end);
  296. ASSERT_LE(begin, iterable.begin());
  297. ASSERT_GT(end, begin);
  298. ASSERT_GE(end, iterable.end());
  299. ASSERT_EQ(begin[0u].first, entt::type_id<int>().hash());
  300. ASSERT_TRUE(begin[0u].second.contains(entity));
  301. }
  302. TEST(Registry, RegistryConstStorageIterator) {
  303. entt::registry registry{};
  304. const auto entity = registry.create();
  305. registry.emplace<int>(entity);
  306. auto iterable = std::as_const(registry).storage();
  307. auto end{iterable.begin()};
  308. decltype(end) begin{};
  309. begin = iterable.end();
  310. std::swap(begin, end);
  311. ASSERT_EQ(begin, iterable.cbegin());
  312. ASSERT_EQ(end, iterable.cend());
  313. ASSERT_NE(begin, end);
  314. ASSERT_EQ(begin++, iterable.begin());
  315. ASSERT_EQ(begin--, iterable.end());
  316. ASSERT_EQ(begin + 1, iterable.end());
  317. ASSERT_EQ(end - 1, iterable.begin());
  318. ASSERT_EQ(++begin, iterable.end());
  319. ASSERT_EQ(--begin, iterable.begin());
  320. ASSERT_EQ(begin += 1, iterable.end());
  321. ASSERT_EQ(begin -= 1, iterable.begin());
  322. ASSERT_EQ(begin + (end - begin), iterable.end());
  323. ASSERT_EQ(begin - (begin - end), iterable.end());
  324. ASSERT_EQ(end - (end - begin), iterable.begin());
  325. ASSERT_EQ(end + (begin - end), iterable.begin());
  326. ASSERT_EQ(begin[0u].first, iterable.begin()->first);
  327. ASSERT_EQ(&begin[0u].second, &(*iterable.begin()).second);
  328. ASSERT_LT(begin, end);
  329. ASSERT_LE(begin, iterable.begin());
  330. ASSERT_GT(end, begin);
  331. ASSERT_GE(end, iterable.end());
  332. ASSERT_EQ(begin[0u].first, entt::type_id<int>().hash());
  333. ASSERT_TRUE(begin[0u].second.contains(entity));
  334. }
  335. TEST(Registry, RegistryStorageIteratorConversion) {
  336. entt::registry registry{};
  337. registry.storage<int>();
  338. auto proxy = registry.storage();
  339. [[maybe_unused]] auto cproxy = std::as_const(registry).storage();
  340. const typename decltype(proxy)::iterator it = proxy.begin();
  341. typename decltype(cproxy)::iterator cit = it;
  342. testing::StaticAssertTypeEq<decltype(*it), std::pair<entt::id_type, entt::sparse_set &>>();
  343. testing::StaticAssertTypeEq<decltype(*cit), std::pair<entt::id_type, const entt::sparse_set &>>();
  344. ASSERT_EQ(it->first, entt::type_id<int>().hash());
  345. ASSERT_EQ((*it).second.type(), entt::type_id<int>());
  346. ASSERT_EQ(it->first, cit->first);
  347. ASSERT_EQ((*it).second.type(), (*cit).second.type());
  348. ASSERT_EQ(it - cit, 0);
  349. ASSERT_EQ(cit - it, 0);
  350. ASSERT_LE(it, cit);
  351. ASSERT_LE(cit, it);
  352. ASSERT_GE(it, cit);
  353. ASSERT_GE(cit, it);
  354. ASSERT_EQ(it, cit);
  355. ASSERT_NE(++cit, it);
  356. }
  357. TEST(Registry, Storage) {
  358. using namespace entt::literals;
  359. entt::registry registry{};
  360. auto &storage = registry.storage<test::empty>("other"_hs);
  361. const auto entity = registry.create();
  362. testing::StaticAssertTypeEq<decltype(registry.storage<test::empty>()), entt::storage_type_t<test::empty> &>();
  363. testing::StaticAssertTypeEq<decltype(std::as_const(registry).storage<test::empty>()), const entt::storage_type_t<test::empty> *>();
  364. testing::StaticAssertTypeEq<decltype(registry.storage("other"_hs)), entt::storage_type_t<test::empty>::base_type *>();
  365. testing::StaticAssertTypeEq<decltype(std::as_const(registry).storage("other"_hs)), const entt::storage_type_t<test::empty>::base_type *>();
  366. ASSERT_NE(registry.storage("other"_hs), nullptr);
  367. ASSERT_EQ(std::as_const(registry).storage("rehto"_hs), nullptr);
  368. ASSERT_EQ(&registry.storage<test::empty>("other"_hs), &storage);
  369. ASSERT_NE(std::as_const(registry).storage<test::empty>(), &storage);
  370. ASSERT_FALSE(registry.any_of<test::empty>(entity));
  371. ASSERT_FALSE(storage.contains(entity));
  372. registry.emplace<test::empty>(entity);
  373. ASSERT_FALSE(storage.contains(entity));
  374. ASSERT_TRUE(registry.any_of<test::empty>(entity));
  375. ASSERT_EQ((entt::basic_view{registry.storage<test::empty>(), storage}.size_hint()), 0u);
  376. storage.emplace(entity);
  377. ASSERT_TRUE(storage.contains(entity));
  378. ASSERT_TRUE(registry.any_of<test::empty>(entity));
  379. ASSERT_EQ((entt::basic_view{registry.storage<test::empty>(), storage}.size_hint()), 1u);
  380. registry.destroy(entity);
  381. ASSERT_EQ(registry.create(entity), entity);
  382. ASSERT_FALSE(storage.contains(entity));
  383. ASSERT_FALSE(registry.any_of<test::empty>(entity));
  384. }
  385. ENTT_DEBUG_TEST(RegistryDeathTest, Storage) {
  386. using namespace entt::literals;
  387. entt::registry registry{};
  388. registry.storage<test::empty>("other"_hs);
  389. ASSERT_DEATH(registry.storage<int>("other"_hs), "");
  390. ASSERT_DEATH([[maybe_unused]] const auto *storage = std::as_const(registry).storage<int>("other"_hs), "");
  391. ASSERT_DEATH(registry.storage<entt::entity>("other"_hs), "");
  392. ASSERT_DEATH([[maybe_unused]] const auto *storage = std::as_const(registry).storage<entt::entity>("other"_hs), "");
  393. }
  394. TEST(Registry, Identifiers) {
  395. using traits_type = entt::entt_traits<entt::entity>;
  396. entt::registry registry{};
  397. const auto pre = registry.create();
  398. ASSERT_EQ(traits_type::to_integral(pre), traits_type::to_entity(pre));
  399. registry.destroy(pre);
  400. const auto post = registry.create();
  401. ASSERT_NE(pre, post);
  402. ASSERT_EQ(traits_type::to_entity(pre), traits_type::to_entity(post));
  403. ASSERT_NE(traits_type::to_version(pre), traits_type::to_version(post));
  404. ASSERT_NE(traits_type::to_version(pre), registry.current(pre));
  405. ASSERT_EQ(traits_type::to_version(post), registry.current(post));
  406. const auto invalid = traits_type::combine(traits_type::to_entity(post) + 1u, {});
  407. ASSERT_EQ(traits_type::to_version(invalid), typename traits_type::version_type{});
  408. ASSERT_EQ(registry.current(invalid), traits_type::to_version(entt::tombstone));
  409. }
  410. TEST(Registry, MoreOnIdentifiers) {
  411. using traits_type = entt::entt_traits<entt::entity>;
  412. entt::registry registry{};
  413. const std::array entity{registry.create(), registry.create()};
  414. registry.destroy(entity[0]);
  415. ASSERT_FALSE(registry.valid(entity[0]));
  416. ASSERT_TRUE(registry.valid(entity[1]));
  417. ASSERT_EQ(registry.current(entt::null), traits_type::to_version(entt::tombstone));
  418. ASSERT_EQ(registry.current(entity[0]), traits_type::to_version(entity[0]) + 1u);
  419. ASSERT_EQ(registry.current(entity[1]), traits_type::to_version(entity[1]));
  420. registry.destroy(entity[1]);
  421. ASSERT_FALSE(registry.valid(entity[1]));
  422. ASSERT_EQ(registry.current(entity[1]), traits_type::to_version(entity[1]) + 1u);
  423. }
  424. TEST(Registry, VersionOverflow) {
  425. using traits_type = entt::entt_traits<entt::entity>;
  426. entt::registry registry{};
  427. const auto entity = registry.create();
  428. registry.destroy(entity);
  429. ASSERT_NE(registry.current(entity), traits_type::to_version(entity));
  430. ASSERT_NE(registry.current(entity), typename traits_type::version_type{});
  431. registry.destroy(registry.create(), traits_type::to_version(entt::tombstone) - 1u);
  432. registry.destroy(registry.create());
  433. ASSERT_EQ(registry.current(entity), traits_type::to_version(entity));
  434. ASSERT_EQ(registry.current(entity), typename traits_type::version_type{});
  435. }
  436. TEST(Registry, NullEntity) {
  437. entt::registry registry{};
  438. const entt::entity entity = entt::null;
  439. ASSERT_FALSE(registry.valid(entity));
  440. ASSERT_NE(registry.create(entity), entity);
  441. }
  442. TEST(Registry, TombstoneVersion) {
  443. using traits_type = entt::entt_traits<entt::entity>;
  444. entt::registry registry{};
  445. const entt::entity entity = entt::tombstone;
  446. ASSERT_FALSE(registry.valid(entity));
  447. const auto other = registry.create();
  448. const auto vers = traits_type::to_version(entity);
  449. const auto required = traits_type::construct(traits_type::to_entity(other), vers);
  450. ASSERT_NE(registry.destroy(other, vers), vers);
  451. ASSERT_NE(registry.create(required), required);
  452. }
  453. TEST(Registry, CreateManyEntitiesAtOnce) {
  454. using traits_type = entt::entt_traits<entt::entity>;
  455. entt::registry registry{};
  456. std::array<entt::entity, 3u> entity{};
  457. const auto entt = registry.create();
  458. registry.destroy(registry.create());
  459. registry.destroy(entt);
  460. registry.destroy(registry.create());
  461. registry.create(entity.begin(), entity.end());
  462. ASSERT_TRUE(registry.valid(entity[0]));
  463. ASSERT_TRUE(registry.valid(entity[1]));
  464. ASSERT_TRUE(registry.valid(entity[2]));
  465. ASSERT_EQ(traits_type::to_entity(entity[0]), 0u);
  466. ASSERT_EQ(traits_type::to_version(entity[0]), 2u);
  467. ASSERT_EQ(traits_type::to_entity(entity[1]), 1u);
  468. ASSERT_EQ(traits_type::to_version(entity[1]), 1u);
  469. ASSERT_EQ(traits_type::to_entity(entity[2]), 2u);
  470. ASSERT_EQ(traits_type::to_version(entity[2]), 0u);
  471. }
  472. TEST(Registry, CreateManyEntitiesAtOnceWithListener) {
  473. entt::registry registry{};
  474. std::array<entt::entity, 3u> entity{};
  475. listener listener;
  476. registry.on_construct<int>().connect<&listener::incr>(listener);
  477. registry.create(entity.begin(), entity.end());
  478. registry.insert(entity.begin(), entity.end(), 1);
  479. registry.insert(entity.begin(), entity.end(), 'c');
  480. ASSERT_EQ(registry.get<int>(entity[0]), 1);
  481. ASSERT_EQ(registry.get<char>(entity[1]), 'c');
  482. ASSERT_EQ(listener.counter, 3);
  483. registry.on_construct<int>().disconnect<&listener::incr>(listener);
  484. registry.on_construct<test::empty>().connect<&listener::incr>(listener);
  485. registry.create(entity.begin(), entity.end());
  486. registry.insert(entity.begin(), entity.end(), 'a');
  487. registry.insert<test::empty>(entity.begin(), entity.end());
  488. ASSERT_TRUE(registry.all_of<test::empty>(entity[0]));
  489. ASSERT_EQ(registry.get<char>(entity[2]), 'a');
  490. ASSERT_EQ(listener.counter, 6);
  491. }
  492. TEST(Registry, CreateWithHint) {
  493. using traits_type = entt::entt_traits<entt::entity>;
  494. entt::registry registry{};
  495. auto e3 = registry.create(entt::entity{3});
  496. auto e2 = registry.create(entt::entity{3});
  497. ASSERT_EQ(e2, entt::entity{0});
  498. ASSERT_FALSE(registry.valid(entt::entity{1}));
  499. ASSERT_FALSE(registry.valid(entt::entity{2}));
  500. ASSERT_EQ(e3, entt::entity{3});
  501. registry.destroy(e2);
  502. ASSERT_EQ(traits_type::to_version(e2), 0u);
  503. ASSERT_EQ(registry.current(e2), 1u);
  504. e2 = registry.create();
  505. auto e1 = registry.create(entt::entity{2});
  506. ASSERT_EQ(traits_type::to_entity(e2), 0u);
  507. ASSERT_EQ(traits_type::to_version(e2), 1u);
  508. ASSERT_EQ(traits_type::to_entity(e1), 2u);
  509. ASSERT_EQ(traits_type::to_version(e1), 0u);
  510. registry.destroy(e1);
  511. registry.destroy(e2);
  512. auto e0 = registry.create(entt::entity{0});
  513. ASSERT_EQ(e0, entt::entity{0});
  514. ASSERT_EQ(traits_type::to_version(e0), 0u);
  515. }
  516. TEST(Registry, CreateClearCycle) {
  517. using traits_type = entt::entt_traits<entt::entity>;
  518. entt::registry registry{};
  519. entt::entity pre{}, post{};
  520. const std::size_t first_iteration = 10u;
  521. const std::size_t second_iteration = 7u;
  522. const std::size_t third_iteration = 5u;
  523. for(std::size_t i{}; i < first_iteration; ++i) {
  524. const auto entity = registry.create();
  525. registry.emplace<double>(entity);
  526. }
  527. registry.clear();
  528. for(std::size_t i{}; i < second_iteration; ++i) {
  529. const auto entity = registry.create();
  530. registry.emplace<int>(entity);
  531. if(i == 3u) {
  532. pre = entity;
  533. }
  534. }
  535. registry.clear();
  536. for(std::size_t i{}; i < third_iteration; ++i) {
  537. const auto entity = registry.create();
  538. if(i == 3u) {
  539. post = entity;
  540. }
  541. }
  542. ASSERT_FALSE(registry.valid(pre));
  543. ASSERT_TRUE(registry.valid(post));
  544. ASSERT_NE(traits_type::to_version(pre), traits_type::to_version(post));
  545. ASSERT_EQ(traits_type::to_version(pre) + 1, traits_type::to_version(post));
  546. ASSERT_EQ(registry.current(pre), registry.current(post));
  547. }
  548. TEST(Registry, CreateDestroyReleaseCornerCase) {
  549. entt::registry registry{};
  550. const auto e0 = registry.create();
  551. const auto e1 = registry.create();
  552. registry.destroy(e0);
  553. registry.storage<entt::entity>().erase(e1);
  554. ASSERT_EQ(registry.storage<entt::entity>().free_list(), 0u);
  555. ASSERT_EQ(registry.current(e0), 1u);
  556. ASSERT_EQ(registry.current(e1), 1u);
  557. }
  558. ENTT_DEBUG_TEST(RegistryDeathTest, CreateTooManyEntities) {
  559. entt::basic_registry<test::entity> registry{};
  560. std::vector<test::entity> entity(entt::entt_traits<test::entity>::to_entity(entt::null));
  561. registry.create(entity.begin(), entity.end());
  562. ASSERT_DEATH([[maybe_unused]] const auto entt = registry.create(), "");
  563. }
  564. TEST(Registry, DestroyVersion) {
  565. entt::registry registry{};
  566. const auto e0 = registry.create();
  567. const auto e1 = registry.create();
  568. ASSERT_EQ(registry.current(e0), 0u);
  569. ASSERT_EQ(registry.current(e1), 0u);
  570. registry.destroy(e0);
  571. registry.destroy(e1, 3);
  572. ASSERT_EQ(registry.current(e0), 1u);
  573. ASSERT_EQ(registry.current(e1), 3u);
  574. }
  575. ENTT_DEBUG_TEST(RegistryDeathTest, DestroyVersion) {
  576. entt::registry registry{};
  577. const auto entity = registry.create();
  578. registry.destroy(entity);
  579. ASSERT_DEATH(registry.destroy(entity), "");
  580. ASSERT_DEATH(registry.destroy(entity, 3), "");
  581. }
  582. TEST(Registry, DestroyRange) {
  583. entt::registry registry{};
  584. const auto iview = registry.view<int>();
  585. const auto icview = registry.view<int, char>();
  586. std::array<entt::entity, 3u> entity{};
  587. registry.create(entity.begin(), entity.end());
  588. registry.emplace<int>(entity[0u]);
  589. registry.emplace<char>(entity[0u]);
  590. registry.emplace<double>(entity[0u]);
  591. registry.emplace<int>(entity[1u]);
  592. registry.emplace<char>(entity[1u]);
  593. registry.emplace<int>(entity[2u]);
  594. ASSERT_TRUE(registry.valid(entity[0u]));
  595. ASSERT_TRUE(registry.valid(entity[1u]));
  596. ASSERT_TRUE(registry.valid(entity[2u]));
  597. registry.destroy(icview.begin(), icview.end());
  598. ASSERT_FALSE(registry.valid(entity[0u]));
  599. ASSERT_FALSE(registry.valid(entity[1u]));
  600. ASSERT_TRUE(registry.valid(entity[2u]));
  601. ASSERT_EQ(registry.storage<int>().size(), 1u);
  602. ASSERT_EQ(registry.storage<char>().size(), 0u);
  603. ASSERT_EQ(registry.storage<double>().size(), 0u);
  604. registry.destroy(iview.begin(), iview.end());
  605. ASSERT_FALSE(registry.valid(entity[2u]));
  606. ASSERT_NO_THROW(registry.destroy(iview.rbegin(), iview.rend()));
  607. ASSERT_EQ(iview.size(), 0u);
  608. ASSERT_EQ(icview.size_hint(), 0u);
  609. ASSERT_EQ(registry.storage<int>().size(), 0u);
  610. ASSERT_EQ(registry.storage<char>().size(), 0u);
  611. ASSERT_EQ(registry.storage<double>().size(), 0u);
  612. registry.create(entity.begin(), entity.end());
  613. registry.insert<int>(entity.begin(), entity.end());
  614. ASSERT_TRUE(registry.valid(entity[0u]));
  615. ASSERT_TRUE(registry.valid(entity[1u]));
  616. ASSERT_TRUE(registry.valid(entity[2u]));
  617. ASSERT_EQ(registry.storage<int>().size(), 3u);
  618. registry.destroy(entity.begin(), entity.end());
  619. ASSERT_FALSE(registry.valid(entity[0u]));
  620. ASSERT_FALSE(registry.valid(entity[1u]));
  621. ASSERT_FALSE(registry.valid(entity[2u]));
  622. ASSERT_EQ(registry.storage<int>().size(), 0u);
  623. entt::sparse_set managed{};
  624. registry.create(entity.begin(), entity.end());
  625. managed.push(entity.begin(), entity.end());
  626. registry.insert<int>(managed.begin(), managed.end());
  627. ASSERT_TRUE(registry.valid(managed[0u]));
  628. ASSERT_TRUE(registry.valid(managed[1u]));
  629. ASSERT_TRUE(registry.valid(managed[2u]));
  630. ASSERT_EQ(registry.storage<int>().size(), 3u);
  631. registry.destroy(managed.begin(), managed.end());
  632. ASSERT_FALSE(registry.valid(managed[0u]));
  633. ASSERT_FALSE(registry.valid(managed[1u]));
  634. ASSERT_FALSE(registry.valid(managed[2u]));
  635. ASSERT_EQ(registry.storage<int>().size(), 0u);
  636. }
  637. TEST(Registry, StableDestroy) {
  638. entt::registry registry{};
  639. const auto iview = registry.view<int>();
  640. const auto icview = registry.view<int, test::pointer_stable>();
  641. std::array<entt::entity, 3u> entity{};
  642. registry.create(entity.begin(), entity.end());
  643. registry.emplace<int>(entity[0u]);
  644. registry.emplace<test::pointer_stable>(entity[0u]);
  645. registry.emplace<double>(entity[0u]);
  646. registry.emplace<int>(entity[1u]);
  647. registry.emplace<test::pointer_stable>(entity[1u]);
  648. registry.emplace<int>(entity[2u]);
  649. ASSERT_TRUE(registry.valid(entity[0u]));
  650. ASSERT_TRUE(registry.valid(entity[1u]));
  651. ASSERT_TRUE(registry.valid(entity[2u]));
  652. registry.destroy(icview.begin(), icview.end());
  653. ASSERT_FALSE(registry.valid(entity[0u]));
  654. ASSERT_FALSE(registry.valid(entity[1u]));
  655. ASSERT_TRUE(registry.valid(entity[2u]));
  656. ASSERT_EQ(registry.storage<int>().size(), 1u);
  657. ASSERT_EQ(registry.storage<test::pointer_stable>().size(), 2u);
  658. ASSERT_EQ(registry.storage<double>().size(), 0u);
  659. registry.destroy(iview.begin(), iview.end());
  660. ASSERT_FALSE(registry.valid(entity[2u]));
  661. ASSERT_EQ(iview.size(), 0u);
  662. ASSERT_EQ(icview.size_hint(), 0u);
  663. ASSERT_EQ(registry.storage<int>().size(), 0u);
  664. ASSERT_EQ(registry.storage<test::pointer_stable>().size(), 2u);
  665. ASSERT_EQ(registry.storage<double>().size(), 0u);
  666. }
  667. TEST(Registry, Emplace) {
  668. entt::registry registry{};
  669. const auto entity = registry.create();
  670. ASSERT_FALSE(registry.all_of<int>(entity));
  671. const auto &ref = registry.emplace<int>(entity, 4);
  672. ASSERT_TRUE(registry.all_of<int>(entity));
  673. ASSERT_EQ(registry.get<int>(entity), ref);
  674. ASSERT_EQ(ref, 4);
  675. }
  676. TEST(Registry, EmplaceTypesFromStandardTemplateLibrary) {
  677. // see #37 - the test shouldn't crash, that's all
  678. entt::registry registry{};
  679. const auto entity = registry.create();
  680. registry.emplace<std::unordered_set<int>>(entity).insert(1);
  681. registry.destroy(entity);
  682. }
  683. TEST(Registry, EmplaceWithComponents) {
  684. // it should compile, that's all
  685. entt::registry registry{};
  686. const auto value = 0;
  687. registry.emplace<int>(registry.create(), value);
  688. }
  689. TEST(Registry, EmplaceMoveOnlyComponent) {
  690. entt::registry registry{};
  691. // the purpose is to ensure that move only types are always accepted
  692. registry.emplace<std::unique_ptr<int>>(registry.create());
  693. }
  694. TEST(Registry, EmplaceNonDefaultConstructibleComponent) {
  695. entt::registry registry{};
  696. // the purpose is to ensure that non default constructible type are always accepted
  697. registry.emplace<test::non_default_constructible>(registry.create(), 1);
  698. }
  699. TEST(Registry, StableEmplace) {
  700. entt::registry registry{};
  701. registry.on_construct<int>().connect<&listener::sort<int>>();
  702. registry.emplace<int>(registry.create(), 0);
  703. ASSERT_EQ(registry.emplace<int>(registry.create(), 1), 1);
  704. }
  705. ENTT_DEBUG_TEST(RegistryDeathTest, Emplace) {
  706. entt::registry registry{};
  707. const auto entity = registry.create();
  708. registry.destroy(entity);
  709. ASSERT_DEATH(registry.emplace<int>(entity), "");
  710. }
  711. TEST(Registry, Insert) {
  712. entt::registry registry{};
  713. std::array<entt::entity, 3u> entity{};
  714. registry.create(entity.begin(), entity.end());
  715. registry.emplace<int>(entity[0u]);
  716. registry.emplace<char>(entity[0u]);
  717. registry.emplace<double>(entity[0u]);
  718. registry.emplace<int>(entity[1u]);
  719. registry.emplace<char>(entity[1u]);
  720. registry.emplace<int>(entity[2u]);
  721. ASSERT_FALSE(registry.all_of<float>(entity[0u]));
  722. ASSERT_FALSE(registry.all_of<float>(entity[1u]));
  723. ASSERT_FALSE(registry.all_of<float>(entity[2u]));
  724. const auto icview = registry.view<int, char>();
  725. registry.insert(icview.begin(), icview.end(), 3.f);
  726. ASSERT_EQ(registry.get<float>(entity[0u]), 3.f);
  727. ASSERT_EQ(registry.get<float>(entity[1u]), 3.f);
  728. ASSERT_FALSE(registry.all_of<float>(entity[2u]));
  729. registry.clear<float>();
  730. std::array value{0.f, 1.f, 2.f};
  731. const auto iview = registry.view<int>();
  732. registry.insert<float>(iview.rbegin(), iview.rend(), value.begin());
  733. ASSERT_EQ(registry.get<float>(entity[0u]), 0.f);
  734. ASSERT_EQ(registry.get<float>(entity[1u]), 1.f);
  735. ASSERT_EQ(registry.get<float>(entity[2u]), 2.f);
  736. }
  737. ENTT_DEBUG_TEST(RegistryDeathTest, Insert) {
  738. entt::registry registry{};
  739. const std::array entity{registry.create()};
  740. const std::array value{0};
  741. registry.destroy(entity[0]);
  742. ASSERT_DEATH(registry.insert<int>(entity.begin(), entity.end(), value[0]), "");
  743. ASSERT_DEATH(registry.insert<int>(entity.begin(), entity.end(), value.begin()), "");
  744. }
  745. TEST(Registry, EmplaceOrReplace) {
  746. entt::registry registry{};
  747. const auto entity = registry.create();
  748. ASSERT_FALSE(registry.all_of<int>(entity));
  749. const auto &ref = registry.emplace_or_replace<int>(entity, 4);
  750. ASSERT_TRUE(registry.all_of<int>(entity));
  751. ASSERT_EQ(registry.get<int>(entity), ref);
  752. ASSERT_EQ(ref, 4);
  753. registry.emplace_or_replace<int>(entity, 0);
  754. ASSERT_EQ(ref, 0);
  755. }
  756. TEST(Registry, EmplaceOrReplaceAggregate) {
  757. entt::registry registry{};
  758. const auto entity = registry.create();
  759. auto &instance = registry.emplace_or_replace<test::aggregate>(entity, 1);
  760. ASSERT_EQ(instance.value, 1);
  761. }
  762. ENTT_DEBUG_TEST(RegistryDeathTest, EmplaceOrReplace) {
  763. entt::registry registry{};
  764. const auto entity = registry.create();
  765. registry.destroy(entity);
  766. ASSERT_DEATH(registry.emplace_or_replace<int>(entity), "");
  767. }
  768. TEST(Registry, Patch) {
  769. entt::registry registry{};
  770. const auto entity = registry.create();
  771. registry.emplace<int>(entity, 3);
  772. ASSERT_EQ(registry.get<int>(entity), 3);
  773. registry.patch<int>(entity);
  774. ASSERT_EQ(registry.get<int>(entity), 3);
  775. registry.patch<int>(entity, [](auto &value) { value = 1; });
  776. ASSERT_EQ(registry.get<int>(entity), 1);
  777. }
  778. TEST(Registry, Replace) {
  779. entt::registry registry{};
  780. const auto entity = registry.create();
  781. registry.emplace<int>(entity, 3);
  782. ASSERT_EQ(registry.get<int>(entity), 3);
  783. registry.replace<int>(entity);
  784. ASSERT_EQ(registry.get<int>(entity), 0);
  785. registry.replace<int>(entity, 1);
  786. ASSERT_EQ(registry.get<int>(entity), 1);
  787. }
  788. TEST(Registry, ReplaceAggregate) {
  789. entt::registry registry{};
  790. const auto entity = registry.create();
  791. registry.emplace<test::aggregate>(entity, 0);
  792. auto &instance = registry.replace<test::aggregate>(entity, 1);
  793. ASSERT_EQ(instance.value, 1);
  794. }
  795. TEST(Registry, Remove) {
  796. entt::registry registry{};
  797. const auto iview = registry.view<int>();
  798. const auto icview = registry.view<int, char>();
  799. std::array<entt::entity, 3u> entity{};
  800. registry.create(entity.begin(), entity.end());
  801. registry.emplace<int>(entity[0u]);
  802. registry.emplace<char>(entity[0u]);
  803. registry.emplace<double>(entity[0u]);
  804. registry.emplace<int>(entity[1u]);
  805. registry.emplace<char>(entity[1u]);
  806. registry.emplace<int>(entity[2u]);
  807. ASSERT_TRUE(registry.any_of<int>(entity[0u]));
  808. ASSERT_TRUE(registry.all_of<int>(entity[1u]));
  809. ASSERT_TRUE(registry.any_of<int>(entity[2u]));
  810. registry.remove<int, char>(entity[0u]);
  811. ASSERT_EQ((registry.remove<int, char>(icview.begin(), icview.end())), 2u);
  812. ASSERT_EQ((registry.remove<int, char>(icview.begin(), icview.end())), 0u);
  813. ASSERT_FALSE(registry.any_of<int>(entity[0u]));
  814. ASSERT_FALSE(registry.all_of<int>(entity[1u]));
  815. ASSERT_TRUE(registry.any_of<int>(entity[2u]));
  816. ASSERT_EQ(registry.storage<int>().size(), 1u);
  817. ASSERT_EQ(registry.storage<char>().size(), 0u);
  818. ASSERT_EQ(registry.storage<double>().size(), 1u);
  819. ASSERT_EQ((registry.remove<int>(iview.begin(), iview.end())), 1u);
  820. ASSERT_EQ(registry.remove<int>(entity[0u]), 0u);
  821. ASSERT_EQ(registry.remove<int>(entity[1u]), 0u);
  822. ASSERT_FALSE(registry.any_of<int>(entity[2u]));
  823. ASSERT_EQ(registry.remove<int>(iview.begin(), iview.end()), 0u);
  824. ASSERT_EQ(registry.storage<int>().size(), 0u);
  825. ASSERT_EQ(registry.storage<char>().size(), 0u);
  826. ASSERT_EQ(registry.storage<double>().size(), 1u);
  827. registry.insert<int>(entity.begin() + 1u, entity.end() - 1u);
  828. registry.insert<char>(entity.begin() + 1u, entity.end() - 1u);
  829. ASSERT_EQ(registry.storage<int>().size(), 1u);
  830. ASSERT_EQ(registry.storage<char>().size(), 1u);
  831. registry.remove<int, char>(iview.begin(), iview.end());
  832. registry.remove<int, char>(iview.begin(), iview.end());
  833. ASSERT_EQ(registry.storage<int>().size(), 0u);
  834. ASSERT_EQ(registry.storage<char>().size(), 0u);
  835. registry.insert<int>(entity.begin(), entity.end());
  836. registry.insert<char>(entity.begin(), entity.end());
  837. ASSERT_EQ(registry.storage<int>().size(), 3u);
  838. ASSERT_EQ(registry.storage<char>().size(), 3u);
  839. registry.remove<int, char>(entity.begin(), entity.end());
  840. registry.remove<int, char>(entity.begin(), entity.end());
  841. ASSERT_EQ(registry.storage<int>().size(), 0u);
  842. ASSERT_EQ(registry.storage<char>().size(), 0u);
  843. ASSERT_FALSE(registry.orphan(entity[0u]));
  844. ASSERT_TRUE(registry.orphan(entity[1u]));
  845. ASSERT_TRUE(registry.orphan(entity[2u]));
  846. }
  847. TEST(Registry, StableRemove) {
  848. entt::registry registry{};
  849. const auto iview = registry.view<int>();
  850. const auto icview = registry.view<int, test::pointer_stable>();
  851. std::array<entt::entity, 3u> entity{};
  852. registry.create(entity.begin(), entity.end());
  853. registry.emplace<int>(entity[0u]);
  854. registry.emplace<test::pointer_stable>(entity[0u]);
  855. registry.emplace<double>(entity[0u]);
  856. registry.emplace<int>(entity[1u]);
  857. registry.emplace<test::pointer_stable>(entity[1u]);
  858. registry.emplace<int>(entity[2u]);
  859. ASSERT_TRUE(registry.any_of<int>(entity[0u]));
  860. ASSERT_TRUE(registry.all_of<int>(entity[1u]));
  861. ASSERT_TRUE(registry.any_of<int>(entity[2u]));
  862. registry.remove<int, test::pointer_stable>(entity[0u]);
  863. ASSERT_EQ((registry.remove<int, test::pointer_stable>(icview.begin(), icview.end())), 2u);
  864. ASSERT_EQ((registry.remove<int, test::pointer_stable>(icview.begin(), icview.end())), 0u);
  865. ASSERT_FALSE(registry.any_of<int>(entity[0u]));
  866. ASSERT_FALSE(registry.all_of<int>(entity[1u]));
  867. ASSERT_TRUE(registry.any_of<int>(entity[2u]));
  868. ASSERT_EQ(registry.storage<int>().size(), 1u);
  869. ASSERT_EQ(registry.storage<test::pointer_stable>().size(), 2u);
  870. ASSERT_EQ(registry.storage<double>().size(), 1u);
  871. ASSERT_EQ((registry.remove<int>(iview.begin(), iview.end())), 1u);
  872. ASSERT_EQ(registry.remove<int>(entity[0u]), 0u);
  873. ASSERT_EQ(registry.remove<int>(entity[1u]), 0u);
  874. ASSERT_FALSE(registry.any_of<int>(entity[2u]));
  875. ASSERT_EQ(registry.storage<int>().size(), 0u);
  876. ASSERT_EQ(registry.storage<test::pointer_stable>().size(), 2u);
  877. ASSERT_EQ(registry.storage<double>().size(), 1u);
  878. }
  879. TEST(Registry, Erase) {
  880. entt::registry registry{};
  881. const auto iview = registry.view<int>();
  882. const auto icview = registry.view<int, char>();
  883. std::array<entt::entity, 3u> entity{};
  884. registry.create(entity.begin(), entity.end());
  885. registry.emplace<int>(entity[0u]);
  886. registry.emplace<char>(entity[0u]);
  887. registry.emplace<double>(entity[0u]);
  888. registry.emplace<int>(entity[1u]);
  889. registry.emplace<char>(entity[1u]);
  890. registry.emplace<int>(entity[2u]);
  891. ASSERT_TRUE(registry.any_of<int>(entity[0u]));
  892. ASSERT_TRUE(registry.all_of<int>(entity[1u]));
  893. ASSERT_TRUE(registry.any_of<int>(entity[2u]));
  894. registry.erase<int, char>(entity[0u]);
  895. registry.erase<int, char>(icview.begin(), icview.end());
  896. ASSERT_FALSE(registry.any_of<int>(entity[0u]));
  897. ASSERT_FALSE(registry.all_of<int>(entity[1u]));
  898. ASSERT_TRUE(registry.any_of<int>(entity[2u]));
  899. ASSERT_EQ(registry.storage<int>().size(), 1u);
  900. ASSERT_EQ(registry.storage<char>().size(), 0u);
  901. ASSERT_EQ(registry.storage<double>().size(), 1u);
  902. registry.erase<int>(iview.begin(), iview.end());
  903. ASSERT_FALSE(registry.any_of<int>(entity[2u]));
  904. ASSERT_NO_THROW(registry.erase<int>(iview.rbegin(), iview.rend()));
  905. ASSERT_EQ(registry.storage<int>().size(), 0u);
  906. ASSERT_EQ(registry.storage<char>().size(), 0u);
  907. ASSERT_EQ(registry.storage<double>().size(), 1u);
  908. registry.insert<int>(entity.begin() + 1u, entity.end() - 1u);
  909. registry.insert<char>(entity.begin() + 1u, entity.end() - 1u);
  910. ASSERT_EQ(registry.storage<int>().size(), 1u);
  911. ASSERT_EQ(registry.storage<char>().size(), 1u);
  912. registry.erase<int, char>(iview.begin(), iview.end());
  913. ASSERT_EQ(registry.storage<int>().size(), 0u);
  914. ASSERT_EQ(registry.storage<char>().size(), 0u);
  915. registry.insert<int>(entity.begin(), entity.end());
  916. registry.insert<char>(entity.begin(), entity.end());
  917. ASSERT_EQ(registry.storage<int>().size(), 3u);
  918. ASSERT_EQ(registry.storage<char>().size(), 3u);
  919. registry.erase<int, char>(entity.begin(), entity.end());
  920. ASSERT_EQ(registry.storage<int>().size(), 0u);
  921. ASSERT_EQ(registry.storage<char>().size(), 0u);
  922. ASSERT_FALSE(registry.orphan(entity[0u]));
  923. ASSERT_TRUE(registry.orphan(entity[1u]));
  924. ASSERT_TRUE(registry.orphan(entity[2u]));
  925. }
  926. ENTT_DEBUG_TEST(RegistryDeathTest, Erase) {
  927. entt::registry registry{};
  928. const std::array entity{registry.create()};
  929. ASSERT_FALSE((registry.any_of<int>(entity[0u])));
  930. ASSERT_DEATH((registry.erase<int>(entity.begin(), entity.end())), "");
  931. ASSERT_DEATH(registry.erase<int>(entity[0u]), "");
  932. }
  933. TEST(Registry, StableErase) {
  934. entt::registry registry{};
  935. const auto iview = registry.view<int>();
  936. const auto icview = registry.view<int, test::pointer_stable>();
  937. std::array<entt::entity, 3u> entity{};
  938. registry.create(entity.begin(), entity.end());
  939. registry.emplace<int>(entity[0u]);
  940. registry.emplace<test::pointer_stable>(entity[0u]);
  941. registry.emplace<double>(entity[0u]);
  942. registry.emplace<int>(entity[1u]);
  943. registry.emplace<test::pointer_stable>(entity[1u]);
  944. registry.emplace<int>(entity[2u]);
  945. ASSERT_TRUE(registry.any_of<int>(entity[0u]));
  946. ASSERT_TRUE(registry.all_of<int>(entity[1u]));
  947. ASSERT_TRUE(registry.any_of<int>(entity[2u]));
  948. registry.erase<int, test::pointer_stable>(entity[0u]);
  949. registry.erase<int, test::pointer_stable>(icview.begin(), icview.end());
  950. registry.erase<int, test::pointer_stable>(icview.begin(), icview.end());
  951. ASSERT_FALSE(registry.any_of<int>(entity[0u]));
  952. ASSERT_FALSE(registry.all_of<int>(entity[1u]));
  953. ASSERT_TRUE(registry.any_of<int>(entity[2u]));
  954. ASSERT_EQ(registry.storage<int>().size(), 1u);
  955. ASSERT_EQ(registry.storage<test::pointer_stable>().size(), 2u);
  956. ASSERT_EQ(registry.storage<double>().size(), 1u);
  957. registry.erase<int>(iview.begin(), iview.end());
  958. ASSERT_FALSE(registry.any_of<int>(entity[2u]));
  959. ASSERT_EQ(registry.storage<int>().size(), 0u);
  960. ASSERT_EQ(registry.storage<test::pointer_stable>().size(), 2u);
  961. ASSERT_EQ(registry.storage<double>().size(), 1u);
  962. }
  963. TEST(Registry, EraseIf) {
  964. using namespace entt::literals;
  965. entt::registry registry{};
  966. const auto entity = registry.create();
  967. registry.emplace<int>(entity);
  968. registry.storage<int>("other"_hs).emplace(entity);
  969. registry.emplace<char>(entity);
  970. ASSERT_TRUE(registry.storage<int>().contains(entity));
  971. ASSERT_TRUE(registry.storage<int>("other"_hs).contains(entity));
  972. ASSERT_TRUE(registry.storage<char>().contains(entity));
  973. registry.erase_if(entity, [](auto &&...) { return false; });
  974. ASSERT_TRUE(registry.storage<int>().contains(entity));
  975. ASSERT_TRUE(registry.storage<int>("other"_hs).contains(entity));
  976. ASSERT_TRUE(registry.storage<char>().contains(entity));
  977. registry.erase_if(entity, [](entt::id_type id, auto &&...) { return id == "other"_hs; });
  978. ASSERT_TRUE(registry.storage<int>().contains(entity));
  979. ASSERT_FALSE(registry.storage<int>("other"_hs).contains(entity));
  980. ASSERT_TRUE(registry.storage<char>().contains(entity));
  981. registry.erase_if(entity, [](auto, const auto &storage) { return storage.type() == entt::type_id<char>(); });
  982. ASSERT_TRUE(registry.storage<int>().contains(entity));
  983. ASSERT_FALSE(registry.storage<int>("other"_hs).contains(entity));
  984. ASSERT_FALSE(registry.storage<char>().contains(entity));
  985. }
  986. TEST(Registry, Compact) {
  987. entt::registry registry{};
  988. std::array<entt::entity, 2u> entity{};
  989. registry.create(entity.begin(), entity.end());
  990. registry.emplace<int>(entity[0u]);
  991. registry.emplace<test::pointer_stable>(entity[0u]);
  992. registry.emplace<int>(entity[1u]);
  993. registry.emplace<test::pointer_stable>(entity[1u]);
  994. ASSERT_EQ(registry.storage<int>().size(), 2u);
  995. ASSERT_EQ(registry.storage<test::pointer_stable>().size(), 2u);
  996. registry.destroy(entity.begin(), entity.end());
  997. ASSERT_EQ(registry.storage<int>().size(), 0u);
  998. ASSERT_EQ(registry.storage<test::pointer_stable>().size(), 2u);
  999. registry.compact<int>();
  1000. ASSERT_EQ(registry.storage<int>().size(), 0u);
  1001. ASSERT_EQ(registry.storage<test::pointer_stable>().size(), 2u);
  1002. registry.compact();
  1003. ASSERT_EQ(registry.storage<int>().size(), 0u);
  1004. ASSERT_EQ(registry.storage<test::pointer_stable>().size(), 0u);
  1005. }
  1006. TEST(Registry, AllAnyOf) {
  1007. entt::registry registry{};
  1008. const auto entity = registry.create();
  1009. ASSERT_FALSE(registry.all_of<int>(entity));
  1010. ASSERT_FALSE((registry.all_of<int, char>(entity)));
  1011. ASSERT_FALSE((registry.any_of<int, char>(entity)));
  1012. registry.emplace<int>(entity);
  1013. ASSERT_TRUE(registry.all_of<int>(entity));
  1014. ASSERT_FALSE((registry.all_of<int, char>(entity)));
  1015. ASSERT_TRUE((registry.any_of<int, char>(entity)));
  1016. registry.emplace<char>(entity);
  1017. ASSERT_TRUE(registry.all_of<int>(entity));
  1018. ASSERT_TRUE((registry.all_of<int, char>(entity)));
  1019. ASSERT_TRUE((registry.any_of<int, char>(entity)));
  1020. }
  1021. TEST(Registry, Get) {
  1022. entt::registry registry{};
  1023. const auto entity = registry.create();
  1024. registry.emplace<int>(entity, 1);
  1025. registry.emplace<char>(entity, 'c');
  1026. ASSERT_EQ(std::as_const(registry).get<int>(entity), 1);
  1027. ASSERT_EQ((std::as_const(registry).get<int, char>(entity)), std::make_tuple(1, 'c'));
  1028. registry.get<int>(entity) = 3;
  1029. std::get<1>(registry.get<int, char>(entity)) = 'a';
  1030. ASSERT_EQ(registry.get<int>(entity), 3);
  1031. ASSERT_EQ((registry.get<int, char>(entity)), std::make_tuple(3, 'a'));
  1032. }
  1033. TEST(Registry, GetOrEmplace) {
  1034. entt::registry registry{};
  1035. const auto entity = registry.create();
  1036. const auto value = registry.get_or_emplace<int>(entity, 3);
  1037. // get_or_emplace must work for empty types
  1038. static_cast<void>(registry.get_or_emplace<test::empty>(entity));
  1039. ASSERT_TRUE((registry.all_of<int, test::empty>(entity)));
  1040. ASSERT_EQ(registry.get<int>(entity), value);
  1041. ASSERT_EQ(registry.get<int>(entity), 3);
  1042. }
  1043. ENTT_DEBUG_TEST(RegistryDeathTest, GetOrEmplace) {
  1044. entt::registry registry{};
  1045. const auto entity = registry.create();
  1046. registry.destroy(entity);
  1047. ASSERT_DEATH([[maybe_unused]] const auto value = registry.get_or_emplace<int>(entity), "");
  1048. }
  1049. TEST(Registry, TryGet) {
  1050. entt::registry registry{};
  1051. const auto entity = registry.create();
  1052. ASSERT_EQ(registry.try_get<int>(entity), nullptr);
  1053. ASSERT_EQ(std::as_const(registry).try_get<int>(entity), nullptr);
  1054. ASSERT_EQ(std::as_const(registry).storage<int>(), nullptr);
  1055. const int &elem = registry.emplace<int>(entity);
  1056. ASSERT_NE(std::as_const(registry).storage<int>(), nullptr);
  1057. ASSERT_EQ(registry.try_get<int>(entity), &elem);
  1058. ASSERT_EQ(std::as_const(registry).try_get<int>(entity), &elem);
  1059. }
  1060. TEST(Registry, Clear) {
  1061. entt::registry registry{};
  1062. const std::array entity{registry.create(), registry.create()};
  1063. registry.insert<int>(entity.begin(), entity.end());
  1064. registry.insert<char>(entity.begin(), entity.end());
  1065. ASSERT_TRUE((registry.all_of<int, char>(entity[0])));
  1066. ASSERT_TRUE((registry.all_of<int, char>(entity[1])));
  1067. registry.clear<int>();
  1068. ASSERT_TRUE(registry.valid(entity[0]));
  1069. ASSERT_TRUE(registry.valid(entity[1]));
  1070. ASSERT_FALSE(registry.all_of<int>(entity[0]));
  1071. ASSERT_FALSE(registry.all_of<int>(entity[1]));
  1072. ASSERT_TRUE(registry.all_of<char>(entity[0]));
  1073. ASSERT_TRUE(registry.all_of<char>(entity[1]));
  1074. registry.clear();
  1075. ASSERT_FALSE(registry.valid(entity[0]));
  1076. ASSERT_FALSE(registry.valid(entity[1]));
  1077. ASSERT_TRUE(registry.storage<int>().empty());
  1078. ASSERT_TRUE(registry.storage<char>().empty());
  1079. }
  1080. TEST(Registry, Orphan) {
  1081. entt::registry registry{};
  1082. std::array<entt::entity, 3u> entity{};
  1083. registry.create(entity.begin(), entity.end());
  1084. registry.emplace<int>(entity[0u]);
  1085. registry.emplace<int>(entity[2u]);
  1086. for(auto [entt]: registry.storage<entt::entity>().each()) {
  1087. ASSERT_TRUE(entt != entity[1u] || registry.orphan(entt));
  1088. }
  1089. registry.erase<int>(entity[0u]);
  1090. registry.erase<int>(entity[2u]);
  1091. for(auto [entt]: registry.storage<entt::entity>().each()) {
  1092. ASSERT_TRUE(registry.orphan(entt));
  1093. }
  1094. }
  1095. TEST(Registry, Signals) {
  1096. entt::registry registry{};
  1097. std::array<entt::entity, 2u> entity{};
  1098. listener listener;
  1099. registry.on_construct<test::empty>().connect<&listener::incr>(listener);
  1100. registry.on_destroy<test::empty>().connect<&listener::decr>(listener);
  1101. registry.on_construct<int>().connect<&listener::incr>(listener);
  1102. registry.on_destroy<int>().connect<&listener::decr>(listener);
  1103. registry.create(entity.begin(), entity.end());
  1104. registry.insert<test::empty>(entity.begin(), entity.end());
  1105. ASSERT_EQ(listener.counter, 2);
  1106. ASSERT_EQ(listener.last, entity[1u]);
  1107. registry.insert<int>(entity.rbegin(), entity.rend());
  1108. ASSERT_EQ(listener.counter, 4);
  1109. ASSERT_EQ(listener.last, entity[0u]);
  1110. registry.erase<test::empty, int>(entity[0u]);
  1111. ASSERT_EQ(listener.counter, 2);
  1112. ASSERT_EQ(listener.last, entity[0u]);
  1113. registry.on_destroy<test::empty>().disconnect<&listener::decr>(listener);
  1114. registry.on_destroy<int>().disconnect<&listener::decr>(listener);
  1115. registry.erase<test::empty, int>(entity[1u]);
  1116. ASSERT_EQ(listener.counter, 2);
  1117. ASSERT_EQ(listener.last, entity[0u]);
  1118. registry.on_construct<test::empty>().disconnect<&listener::incr>(listener);
  1119. registry.on_construct<int>().disconnect<&listener::incr>(listener);
  1120. registry.emplace<test::empty>(entity[1u]);
  1121. registry.emplace<int>(entity[1u]);
  1122. ASSERT_EQ(listener.counter, 2);
  1123. ASSERT_EQ(listener.last, entity[0u]);
  1124. registry.on_construct<int>().connect<&listener::incr>(listener);
  1125. registry.on_destroy<int>().connect<&listener::decr>(listener);
  1126. registry.emplace<int>(entity[0u]);
  1127. registry.erase<int>(entity[1u]);
  1128. ASSERT_EQ(listener.counter, 2);
  1129. ASSERT_EQ(listener.last, entity[1u]);
  1130. registry.on_construct<test::empty>().connect<&listener::incr>(listener);
  1131. registry.on_destroy<test::empty>().connect<&listener::decr>(listener);
  1132. registry.erase<test::empty>(entity[1u]);
  1133. registry.emplace<test::empty>(entity[0u]);
  1134. ASSERT_EQ(listener.counter, 2);
  1135. ASSERT_EQ(listener.last, entity[0u]);
  1136. registry.clear<test::empty, int>();
  1137. ASSERT_EQ(listener.counter, 0);
  1138. ASSERT_EQ(listener.last, entity[0u]);
  1139. registry.insert<test::empty>(entity.begin(), entity.end());
  1140. registry.insert<int>(entity.begin(), entity.end());
  1141. registry.destroy(entity[1u]);
  1142. ASSERT_EQ(listener.counter, 2);
  1143. ASSERT_EQ(listener.last, entity[1u]);
  1144. registry.erase<int, test::empty>(entity[0u]);
  1145. registry.emplace_or_replace<int>(entity[0u]);
  1146. registry.emplace_or_replace<test::empty>(entity[0u]);
  1147. ASSERT_EQ(listener.counter, 2);
  1148. ASSERT_EQ(listener.last, entity[0u]);
  1149. registry.on_destroy<test::empty>().disconnect<&listener::decr>(listener);
  1150. registry.on_destroy<int>().disconnect<&listener::decr>(listener);
  1151. registry.emplace_or_replace<test::empty>(entity[0u]);
  1152. registry.emplace_or_replace<int>(entity[0u]);
  1153. ASSERT_EQ(listener.counter, 2);
  1154. ASSERT_EQ(listener.last, entity[0u]);
  1155. registry.on_update<test::empty>().connect<&listener::incr>(listener);
  1156. registry.on_update<int>().connect<&listener::incr>(listener);
  1157. registry.emplace_or_replace<test::empty>(entity[0u]);
  1158. registry.emplace_or_replace<int>(entity[0u]);
  1159. ASSERT_EQ(listener.counter, 4);
  1160. ASSERT_EQ(listener.last, entity[0u]);
  1161. registry.replace<test::empty>(entity[0u]);
  1162. registry.replace<int>(entity[0u]);
  1163. ASSERT_EQ(listener.counter, 6);
  1164. ASSERT_EQ(listener.last, entity[0u]);
  1165. }
  1166. TEST(Registry, SignalsOnRuntimePool) {
  1167. using namespace entt::literals;
  1168. entt::registry registry{};
  1169. const auto entity = registry.create();
  1170. listener listener;
  1171. registry.on_construct<int>("custom"_hs).connect<&listener::incr>(listener);
  1172. registry.on_update<int>("custom"_hs).connect<&listener::incr>(listener);
  1173. registry.on_destroy<int>("custom"_hs).connect<&listener::incr>(listener);
  1174. ASSERT_EQ(listener.counter, 0);
  1175. registry.emplace<int>(entity);
  1176. registry.patch<int>(entity);
  1177. registry.erase<int>(entity);
  1178. ASSERT_EQ(listener.counter, 0);
  1179. registry.storage<int>("custom"_hs).emplace(entity);
  1180. registry.storage<int>("custom"_hs).patch(entity);
  1181. registry.storage<int>("custom"_hs).erase(entity);
  1182. ASSERT_EQ(listener.counter, 3);
  1183. }
  1184. TEST(Registry, SignalsOnEntity) {
  1185. entt::registry registry{};
  1186. listener listener;
  1187. registry.on_construct<entt::entity>().connect<&listener::incr>(listener);
  1188. entt::entity entity = registry.create();
  1189. entt::entity other = registry.create();
  1190. ASSERT_EQ(listener.counter, 2);
  1191. ASSERT_EQ(listener.last, other);
  1192. registry.destroy(other);
  1193. registry.destroy(entity);
  1194. ASSERT_EQ(listener.counter, 2);
  1195. ASSERT_EQ(listener.last, other);
  1196. registry.on_construct<entt::entity>().disconnect(&listener);
  1197. other = registry.create();
  1198. entity = registry.create();
  1199. ASSERT_EQ(listener.counter, 2);
  1200. ASSERT_NE(listener.last, entity);
  1201. ASSERT_NE(listener.last, other);
  1202. registry.on_update<entt::entity>().connect<&listener::decr>(listener);
  1203. registry.patch<entt::entity>(entity);
  1204. ASSERT_EQ(listener.counter, 1);
  1205. ASSERT_EQ(listener.last, entity);
  1206. registry.on_update<entt::entity>().disconnect(&listener);
  1207. registry.patch<entt::entity>(other);
  1208. ASSERT_EQ(listener.counter, 1);
  1209. ASSERT_NE(listener.last, other);
  1210. registry.on_destroy<entt::entity>().connect<&listener::decr>(listener);
  1211. registry.destroy(entity);
  1212. ASSERT_EQ(listener.counter, 0);
  1213. ASSERT_EQ(listener.last, entity);
  1214. registry.on_destroy<entt::entity>().disconnect(&listener);
  1215. registry.destroy(other);
  1216. ASSERT_EQ(listener.counter, 0);
  1217. ASSERT_NE(listener.last, other);
  1218. }
  1219. TEST(Registry, SignalWhenDestroying) {
  1220. entt::registry registry{};
  1221. const auto entity = registry.create();
  1222. registry.on_destroy<double>().connect<&entt::registry::remove<char>>();
  1223. registry.emplace<double>(entity);
  1224. registry.emplace<int>(entity);
  1225. ASSERT_NE(registry.storage(entt::type_id<double>().hash()), nullptr);
  1226. ASSERT_NE(registry.storage(entt::type_id<int>().hash()), nullptr);
  1227. ASSERT_EQ(registry.storage(entt::type_id<char>().hash()), nullptr);
  1228. ASSERT_TRUE(registry.valid(entity));
  1229. registry.destroy(entity);
  1230. ASSERT_NE(registry.storage(entt::type_id<char>().hash()), nullptr);
  1231. ASSERT_FALSE(registry.valid(entity));
  1232. }
  1233. TEST(Registry, SelfSignal) {
  1234. entt::registry registry{};
  1235. const auto entity = registry.create();
  1236. // required because of an issue of VS2019
  1237. constexpr auto emplace_or_replace = &entt::registry::emplace_or_replace<double>;
  1238. constexpr auto remove = &entt::registry::remove<double>;
  1239. registry.on_construct<int>().connect<emplace_or_replace>();
  1240. registry.on_destroy<int>().connect<remove>();
  1241. registry.emplace<double>(entity, .3);
  1242. ASSERT_FALSE(registry.all_of<int>(entity));
  1243. ASSERT_EQ(registry.get<double>(entity), .3);
  1244. registry.emplace<int>(entity);
  1245. ASSERT_TRUE(registry.all_of<int>(entity));
  1246. ASSERT_EQ(registry.get<double>(entity), .0);
  1247. registry.erase<int>(entity);
  1248. ASSERT_FALSE((registry.any_of<int, double>(entity)));
  1249. registry.on_construct<int>().disconnect<emplace_or_replace>();
  1250. registry.on_destroy<int>().disconnect<remove>();
  1251. registry.emplace<int>(entity);
  1252. ASSERT_TRUE((registry.any_of<int, double>(entity)));
  1253. ASSERT_FALSE(registry.all_of<double>(entity));
  1254. }
  1255. TEST(Registry, View) {
  1256. entt::registry registry{};
  1257. std::array<entt::entity, 3u> entity{};
  1258. auto iview = std::as_const(registry).view<int>();
  1259. auto cview = std::as_const(registry).view<char>();
  1260. ASSERT_FALSE(iview);
  1261. ASSERT_FALSE(cview);
  1262. ASSERT_EQ(std::as_const(registry).storage<int>(), nullptr);
  1263. ASSERT_EQ(std::as_const(registry).storage<char>(), nullptr);
  1264. auto mview = registry.view<int, char>();
  1265. auto fview = registry.view<int>(entt::exclude<char>);
  1266. ASSERT_TRUE(mview);
  1267. ASSERT_TRUE(fview);
  1268. ASSERT_NE(std::as_const(registry).storage<int>(), nullptr);
  1269. ASSERT_NE(std::as_const(registry).storage<char>(), nullptr);
  1270. iview = std::as_const(registry).view<int>();
  1271. cview = std::as_const(registry).view<char>();
  1272. ASSERT_TRUE(iview);
  1273. ASSERT_TRUE(cview);
  1274. registry.create(entity.begin(), entity.end());
  1275. registry.emplace<int>(entity[0u], 0);
  1276. registry.emplace<char>(entity[0u], 'c');
  1277. registry.emplace<int>(entity[1u], 0);
  1278. registry.emplace<int>(entity[2u], 0);
  1279. registry.emplace<char>(entity[2u], 'c');
  1280. ASSERT_EQ(iview.size(), 3u);
  1281. ASSERT_EQ(cview.size(), 2u);
  1282. ASSERT_EQ(mview.size_hint(), 3u);
  1283. ASSERT_EQ(fview.size_hint(), 3u);
  1284. mview.refresh();
  1285. ASSERT_EQ(mview.size_hint(), 2u);
  1286. ASSERT_EQ(fview.size_hint(), 3u);
  1287. ASSERT_NE(mview.begin(), mview.end());
  1288. ASSERT_NE(fview.begin(), fview.end());
  1289. ASSERT_EQ(std::distance(mview.begin(), mview.end()), 2);
  1290. ASSERT_EQ(std::distance(fview.begin(), fview.end()), 1);
  1291. mview.each([&entity, first = true](auto entt, auto &&...) mutable {
  1292. ASSERT_EQ(entt, first ? entity[2u] : entity[0u]);
  1293. first = false;
  1294. });
  1295. fview.each([&entity](auto entt, auto &&...) {
  1296. ASSERT_EQ(entt, entity[1u]);
  1297. });
  1298. }
  1299. TEST(Registry, ExcludeOnlyView) {
  1300. entt::registry registry{};
  1301. std::array<entt::entity, 4u> entity{};
  1302. auto view = registry.view<entt::entity>(entt::exclude<int>);
  1303. registry.create(entity.begin(), entity.end());
  1304. registry.emplace<int>(entity[0u], 0);
  1305. registry.emplace<int>(entity[2u], 0);
  1306. registry.emplace<int>(entity[3u], 0);
  1307. registry.destroy(entity[3u]);
  1308. ASSERT_EQ(view.size_hint(), 4u);
  1309. ASSERT_NE(view.begin(), view.end());
  1310. ASSERT_EQ(std::distance(view.begin(), view.end()), 1);
  1311. ASSERT_EQ(*view.begin(), entity[1u]);
  1312. for(auto [entt]: view.each()) {
  1313. ASSERT_EQ(entt, entity[1u]);
  1314. }
  1315. view.each([&entity](auto entt) {
  1316. ASSERT_EQ(entt, entity[1u]);
  1317. });
  1318. }
  1319. TEST(Registry, CleanViewAfterRemoveAndClear) {
  1320. entt::registry registry{};
  1321. auto view = registry.view<int, char>();
  1322. const auto entity = registry.create();
  1323. registry.emplace<int>(entity);
  1324. registry.emplace<char>(entity);
  1325. ASSERT_EQ(view.size_hint(), 1u);
  1326. registry.erase<char>(entity);
  1327. ASSERT_EQ(view.size_hint(), 1u);
  1328. registry.emplace<char>(entity);
  1329. ASSERT_EQ(view.size_hint(), 1u);
  1330. registry.clear<int>();
  1331. ASSERT_EQ(view.size_hint(), 0u);
  1332. registry.emplace<int>(entity);
  1333. ASSERT_EQ(view.size_hint(), 1u);
  1334. registry.clear();
  1335. ASSERT_EQ(view.size_hint(), 0u);
  1336. }
  1337. TEST(Registry, NonOwningGroupInitOnFirstUse) {
  1338. entt::registry registry{};
  1339. std::array<entt::entity, 3u> entity{};
  1340. registry.create(entity.begin(), entity.end());
  1341. registry.insert<int>(entity.begin(), entity.end(), 0);
  1342. registry.emplace<char>(entity[0u], 'c');
  1343. registry.emplace<char>(entity[2u], 'c');
  1344. std::size_t cnt{};
  1345. auto group = registry.group(entt::get<int, char>);
  1346. group.each([&cnt](auto...) { ++cnt; });
  1347. ASSERT_FALSE((registry.owned<int, char>()));
  1348. ASSERT_EQ(cnt, 2u);
  1349. }
  1350. TEST(Registry, NonOwningGroupInitOnEmplace) {
  1351. entt::registry registry{};
  1352. std::array<entt::entity, 3u> entity{};
  1353. auto group = registry.group(entt::get<int, char>);
  1354. registry.create(entity.begin(), entity.end());
  1355. registry.insert<int>(entity.begin(), entity.end(), 0);
  1356. registry.emplace<char>(entity[0u], 'c');
  1357. registry.emplace<char>(entity[2u], 'c');
  1358. std::size_t cnt{};
  1359. group.each([&cnt](auto...) { ++cnt; });
  1360. ASSERT_FALSE((registry.owned<int, char>()));
  1361. ASSERT_EQ(cnt, 2u);
  1362. }
  1363. TEST(Registry, FullOwningGroupInitOnFirstUse) {
  1364. entt::registry registry{};
  1365. std::array<entt::entity, 3u> entity{};
  1366. registry.create(entity.begin(), entity.end());
  1367. registry.insert<int>(entity.begin(), entity.end(), 0);
  1368. registry.emplace<char>(entity[0u], 'c');
  1369. registry.emplace<char>(entity[2u], 'c');
  1370. std::size_t cnt{};
  1371. auto group = registry.group<int, char>();
  1372. group.each([&cnt](auto...) { ++cnt; });
  1373. ASSERT_TRUE(registry.owned<int>());
  1374. ASSERT_TRUE(registry.owned<char>());
  1375. ASSERT_FALSE(registry.owned<double>());
  1376. ASSERT_EQ(cnt, 2u);
  1377. }
  1378. TEST(Registry, FullOwningGroupInitOnEmplace) {
  1379. entt::registry registry{};
  1380. std::array<entt::entity, 3u> entity{};
  1381. auto group = registry.group<int, char>();
  1382. registry.create(entity.begin(), entity.end());
  1383. registry.insert<int>(entity.begin(), entity.end(), 0);
  1384. registry.emplace<char>(entity[0u], 'c');
  1385. registry.emplace<char>(entity[2u], 'c');
  1386. std::size_t cnt{};
  1387. group.each([&cnt](auto...) { ++cnt; });
  1388. ASSERT_TRUE(registry.owned<int>());
  1389. ASSERT_TRUE(registry.owned<char>());
  1390. ASSERT_FALSE(registry.owned<double>());
  1391. ASSERT_EQ(cnt, 2u);
  1392. }
  1393. TEST(Registry, PartialOwningGroupInitOnFirstUse) {
  1394. entt::registry registry{};
  1395. std::array<entt::entity, 3u> entity{};
  1396. registry.create(entity.begin(), entity.end());
  1397. registry.insert<int>(entity.begin(), entity.end(), 0);
  1398. registry.emplace<char>(entity[0u], 'c');
  1399. registry.emplace<char>(entity[2u], 'c');
  1400. std::size_t cnt{};
  1401. auto group = registry.group<int>(entt::get<char>);
  1402. group.each([&cnt](auto...) { ++cnt; });
  1403. ASSERT_TRUE((registry.owned<int, char>()));
  1404. ASSERT_TRUE(registry.owned<int>());
  1405. ASSERT_FALSE(registry.owned<char>());
  1406. ASSERT_EQ(cnt, 2u);
  1407. }
  1408. TEST(Registry, PartialOwningGroupInitOnEmplace) {
  1409. entt::registry registry{};
  1410. std::array<entt::entity, 3u> entity{};
  1411. auto group = registry.group<int>(entt::get<char>);
  1412. registry.create(entity.begin(), entity.end());
  1413. registry.insert<int>(entity.begin(), entity.end(), 0);
  1414. registry.emplace<char>(entity[0u], 'c');
  1415. registry.emplace<char>(entity[2u], 'c');
  1416. std::size_t cnt{};
  1417. group.each([&cnt](auto...) { ++cnt; });
  1418. ASSERT_TRUE((registry.owned<int, char>()));
  1419. ASSERT_TRUE(registry.owned<int>());
  1420. ASSERT_FALSE(registry.owned<char>());
  1421. ASSERT_EQ(cnt, 2u);
  1422. }
  1423. TEST(Registry, CleanNonOwningGroupViewAfterRemoveAndClear) {
  1424. entt::registry registry{};
  1425. auto group = registry.group(entt::get<int, char>);
  1426. const auto entity = registry.create();
  1427. registry.emplace<int>(entity, 0);
  1428. registry.emplace<char>(entity, 'c');
  1429. ASSERT_EQ(group.size(), 1u);
  1430. registry.erase<char>(entity);
  1431. ASSERT_EQ(group.size(), 0u);
  1432. registry.emplace<char>(entity, 'c');
  1433. ASSERT_EQ(group.size(), 1u);
  1434. registry.clear<int>();
  1435. ASSERT_EQ(group.size(), 0u);
  1436. registry.emplace<int>(entity, 0);
  1437. ASSERT_EQ(group.size(), 1u);
  1438. registry.clear();
  1439. ASSERT_EQ(group.size(), 0u);
  1440. }
  1441. TEST(Registry, CleanFullOwningGroupViewAfterRemoveAndClear) {
  1442. entt::registry registry{};
  1443. auto group = registry.group<int, char>();
  1444. const auto entity = registry.create();
  1445. registry.emplace<int>(entity, 0);
  1446. registry.emplace<char>(entity, 'c');
  1447. ASSERT_EQ(group.size(), 1u);
  1448. registry.erase<char>(entity);
  1449. ASSERT_EQ(group.size(), 0u);
  1450. registry.emplace<char>(entity, 'c');
  1451. ASSERT_EQ(group.size(), 1u);
  1452. registry.clear<int>();
  1453. ASSERT_EQ(group.size(), 0u);
  1454. registry.emplace<int>(entity, 0);
  1455. ASSERT_EQ(group.size(), 1u);
  1456. registry.clear();
  1457. ASSERT_EQ(group.size(), 0u);
  1458. }
  1459. TEST(Registry, CleanPartialOwningGroupViewAfterRemoveAndClear) {
  1460. entt::registry registry{};
  1461. auto group = registry.group<int>(entt::get<char>);
  1462. const auto entity = registry.create();
  1463. registry.emplace<int>(entity, 0);
  1464. registry.emplace<char>(entity, 'c');
  1465. ASSERT_EQ(group.size(), 1u);
  1466. registry.erase<char>(entity);
  1467. ASSERT_EQ(group.size(), 0u);
  1468. registry.emplace<char>(entity, 'c');
  1469. ASSERT_EQ(group.size(), 1u);
  1470. registry.clear<int>();
  1471. ASSERT_EQ(group.size(), 0u);
  1472. registry.emplace<int>(entity, 0);
  1473. ASSERT_EQ(group.size(), 1u);
  1474. registry.clear();
  1475. ASSERT_EQ(group.size(), 0u);
  1476. }
  1477. ENTT_DEBUG_TEST(RegistryDeathTest, NestedGroups) {
  1478. entt::registry registry{};
  1479. registry.group<int, double>(entt::get<char>);
  1480. ASSERT_DEATH(registry.group<int>(entt::get<char>), "");
  1481. ASSERT_DEATH(registry.group<int>(entt::get<char, double>), "");
  1482. ASSERT_DEATH(registry.group<int>(entt::get<char>, entt::exclude<double>), "");
  1483. ASSERT_DEATH((registry.group<int, double>()), "");
  1484. }
  1485. ENTT_DEBUG_TEST(RegistryDeathTest, ConflictingGroups) {
  1486. entt::registry registry{};
  1487. registry.group<char>(entt::get<int>, entt::exclude<double>);
  1488. ASSERT_DEATH(registry.group<char>(entt::get<float>, entt::exclude<double>), "");
  1489. }
  1490. TEST(Registry, GroupIfExists) {
  1491. entt::registry registry{};
  1492. const auto entity = registry.create();
  1493. auto group = std::as_const(registry).group_if_exists<int>(entt::get<char>, entt::exclude<double>);
  1494. ASSERT_FALSE(group);
  1495. ASSERT_EQ(std::as_const(registry).storage<int>(), nullptr);
  1496. ASSERT_EQ(std::as_const(registry).storage<char>(), nullptr);
  1497. ASSERT_EQ(std::as_const(registry).storage<double>(), nullptr);
  1498. registry.emplace<int>(entity);
  1499. group = std::as_const(registry).group_if_exists<int>(entt::get<char>, entt::exclude<double>);
  1500. ASSERT_FALSE(group);
  1501. registry.emplace<char>(entity);
  1502. group = std::as_const(registry).group_if_exists<int>(entt::get<char>, entt::exclude<double>);
  1503. ASSERT_FALSE(group);
  1504. registry.emplace<double>(entity);
  1505. group = std::as_const(registry).group_if_exists<int>(entt::get<char>, entt::exclude<double>);
  1506. ASSERT_FALSE(group);
  1507. registry.group<int>(entt::get<char>, entt::exclude<double>);
  1508. group = std::as_const(registry).group_if_exists<int>(entt::get<char>, entt::exclude<double>);
  1509. ASSERT_TRUE(group);
  1510. }
  1511. TEST(Registry, SortSingle) {
  1512. entt::registry registry{};
  1513. int val = 0;
  1514. registry.emplace<int>(registry.create(), val++);
  1515. registry.emplace<int>(registry.create(), val++);
  1516. registry.emplace<int>(registry.create(), val++);
  1517. for(auto entity: registry.view<int>()) {
  1518. ASSERT_EQ(registry.get<int>(entity), --val);
  1519. }
  1520. registry.sort<int>(std::less<>{});
  1521. for(auto entity: registry.view<int>()) {
  1522. ASSERT_EQ(registry.get<int>(entity), val++);
  1523. }
  1524. }
  1525. TEST(Registry, SortMulti) {
  1526. entt::registry registry{};
  1527. unsigned int uval = 0u;
  1528. int ival = 0;
  1529. for(auto i = 0; i < 3; ++i) {
  1530. const auto entity = registry.create();
  1531. registry.emplace<unsigned int>(entity, uval++);
  1532. registry.emplace<int>(entity, ival++);
  1533. }
  1534. for(auto entity: registry.view<unsigned int>()) {
  1535. ASSERT_EQ(registry.get<unsigned int>(entity), --uval);
  1536. }
  1537. for(auto entity: registry.view<int>()) {
  1538. ASSERT_EQ(registry.get<int>(entity), --ival);
  1539. }
  1540. registry.sort<unsigned int>(std::less<>{});
  1541. registry.sort<int, unsigned int>();
  1542. for(auto entity: registry.view<unsigned int>()) {
  1543. ASSERT_EQ(registry.get<unsigned int>(entity), uval++);
  1544. }
  1545. for(auto entity: registry.view<int>()) {
  1546. ASSERT_EQ(registry.get<int>(entity), ival++);
  1547. }
  1548. }
  1549. TEST(Registry, SortEmpty) {
  1550. entt::registry registry{};
  1551. registry.emplace<test::empty>(registry.create());
  1552. registry.emplace<test::empty>(registry.create());
  1553. registry.emplace<test::empty>(registry.create());
  1554. ASSERT_LT(registry.storage<test::empty>().data()[0], registry.storage<test::empty>().data()[1]);
  1555. ASSERT_LT(registry.storage<test::empty>().data()[1], registry.storage<test::empty>().data()[2]);
  1556. registry.sort<test::empty>(std::less<>{});
  1557. ASSERT_GT(registry.storage<test::empty>().data()[0], registry.storage<test::empty>().data()[1]);
  1558. ASSERT_GT(registry.storage<test::empty>().data()[1], registry.storage<test::empty>().data()[2]);
  1559. }
  1560. TEST(Registry, Context) {
  1561. entt::registry registry{};
  1562. auto &ctx = registry.ctx();
  1563. const auto &cctx = std::as_const(registry).ctx();
  1564. ASSERT_FALSE(ctx.contains<char>());
  1565. ASSERT_FALSE(cctx.contains<const int>());
  1566. ASSERT_EQ(ctx.find<char>(), nullptr);
  1567. ASSERT_EQ(cctx.find<const int>(), nullptr);
  1568. ctx.emplace<char>();
  1569. ctx.emplace<int>();
  1570. ASSERT_TRUE(ctx.contains<char>());
  1571. ASSERT_TRUE(cctx.contains<int>());
  1572. ASSERT_NE(ctx.find<const char>(), nullptr);
  1573. ASSERT_NE(cctx.find<const int>(), nullptr);
  1574. ASSERT_FALSE(ctx.erase<double>());
  1575. ASSERT_TRUE(ctx.erase<int>());
  1576. ASSERT_TRUE(ctx.contains<const char>());
  1577. ASSERT_FALSE(cctx.contains<const int>());
  1578. ASSERT_NE(ctx.find<char>(), nullptr);
  1579. ASSERT_EQ(cctx.find<int>(), nullptr);
  1580. ASSERT_FALSE(ctx.erase<int>());
  1581. ASSERT_TRUE(ctx.erase<char>());
  1582. ctx.emplace<char>('c');
  1583. ctx.emplace<int>(1);
  1584. ASSERT_EQ(ctx.emplace<char>('a'), 'c');
  1585. ASSERT_EQ(ctx.find<const char>(), cctx.find<char>());
  1586. ASSERT_EQ(ctx.get<char>(), cctx.get<const char>());
  1587. ASSERT_EQ(ctx.get<char>(), 'c');
  1588. ASSERT_EQ(ctx.emplace<const int>(0), 1);
  1589. ASSERT_EQ(ctx.find<const int>(), cctx.find<int>());
  1590. ASSERT_EQ(ctx.get<int>(), cctx.get<const int>());
  1591. ASSERT_EQ(ctx.get<int>(), 1);
  1592. ASSERT_EQ(ctx.find<double>(), nullptr);
  1593. ASSERT_EQ(cctx.find<double>(), nullptr);
  1594. ASSERT_EQ(ctx.insert_or_assign<char>('a'), 'a');
  1595. ASSERT_EQ(ctx.find<const char>(), cctx.find<char>());
  1596. ASSERT_EQ(ctx.get<char>(), cctx.get<const char>());
  1597. ASSERT_EQ(ctx.get<const char>(), 'a');
  1598. ASSERT_EQ(ctx.insert_or_assign<const int>(0), 0);
  1599. ASSERT_EQ(ctx.find<const int>(), cctx.find<int>());
  1600. ASSERT_EQ(ctx.get<int>(), cctx.get<const int>());
  1601. ASSERT_EQ(ctx.get<int>(), 0);
  1602. }
  1603. TEST(Registry, ContextHint) {
  1604. using namespace entt::literals;
  1605. entt::registry registry{};
  1606. auto &ctx = registry.ctx();
  1607. const auto &cctx = std::as_const(registry).ctx();
  1608. ctx.emplace<int>(1);
  1609. ctx.emplace_as<int>("other"_hs, 3);
  1610. ASSERT_TRUE(ctx.contains<int>());
  1611. ASSERT_TRUE(cctx.contains<const int>("other"_hs));
  1612. ASSERT_FALSE(ctx.contains<char>("other"_hs));
  1613. ASSERT_NE(cctx.find<const int>(), nullptr);
  1614. ASSERT_NE(ctx.find<int>("other"_hs), nullptr);
  1615. ASSERT_EQ(cctx.find<const char>("other"_hs), nullptr);
  1616. ASSERT_EQ(ctx.get<int>(), 1);
  1617. ASSERT_EQ(cctx.get<const int>("other"_hs), 3);
  1618. ctx.insert_or_assign(3);
  1619. ctx.insert_or_assign("other"_hs, 1);
  1620. ASSERT_EQ(ctx.get<const int>(), 3);
  1621. ASSERT_EQ(cctx.get<int>("other"_hs), 1);
  1622. ASSERT_FALSE(ctx.erase<char>("other"_hs));
  1623. ASSERT_TRUE(ctx.erase<int>());
  1624. ASSERT_TRUE(cctx.contains<int>("other"_hs));
  1625. ASSERT_EQ(ctx.get<int>("other"_hs), 1);
  1626. ASSERT_TRUE(ctx.erase<int>("other"_hs));
  1627. ASSERT_FALSE(cctx.contains<int>("other"_hs));
  1628. ASSERT_EQ(ctx.find<int>("other"_hs), nullptr);
  1629. }
  1630. TEST(Registry, ContextAsRef) {
  1631. entt::registry registry{};
  1632. int value{3};
  1633. registry.ctx().emplace<int &>(value);
  1634. ASSERT_NE(registry.ctx().find<int>(), nullptr);
  1635. ASSERT_NE(registry.ctx().find<const int>(), nullptr);
  1636. ASSERT_NE(std::as_const(registry).ctx().find<const int>(), nullptr);
  1637. ASSERT_EQ(registry.ctx().get<const int>(), value);
  1638. ASSERT_EQ(registry.ctx().get<int>(), value);
  1639. registry.ctx().get<int>() = 2;
  1640. ASSERT_EQ(value, 2);
  1641. ASSERT_EQ(registry.ctx().get<int>(), value);
  1642. value = 1;
  1643. ASSERT_EQ(std::as_const(registry).ctx().get<const int>(), value);
  1644. }
  1645. TEST(Registry, ContextAsConstRef) {
  1646. entt::registry registry{};
  1647. int value{3};
  1648. registry.ctx().emplace<const int &>(value);
  1649. ASSERT_EQ(registry.ctx().find<int>(), nullptr);
  1650. ASSERT_NE(registry.ctx().find<const int>(), nullptr);
  1651. ASSERT_NE(std::as_const(registry).ctx().find<const int>(), nullptr);
  1652. ASSERT_EQ(registry.ctx().get<const int>(), value);
  1653. value = 1;
  1654. ASSERT_EQ(std::as_const(registry).ctx().get<const int>(), value);
  1655. }
  1656. TEST(Registry, ContextPoolMemberDestructionOrder) {
  1657. auto registry = std::make_unique<entt::registry>();
  1658. const auto entity = registry->create();
  1659. bool ctx_check = false;
  1660. registry->ctx().emplace<typename destruction_order::ctx_check_type>();
  1661. registry->emplace<destruction_order>(entity, *registry, ctx_check);
  1662. registry.reset();
  1663. ASSERT_TRUE(ctx_check);
  1664. }
  1665. TEST(Registry, Constness) {
  1666. testing::StaticAssertTypeEq<decltype(std::declval<entt::registry>().emplace<int>({})), int &>();
  1667. testing::StaticAssertTypeEq<decltype(std::declval<entt::registry>().emplace<test::empty>({})), void>();
  1668. testing::StaticAssertTypeEq<decltype(std::declval<entt::registry>().get<>({})), std::tuple<>>();
  1669. testing::StaticAssertTypeEq<decltype(std::declval<entt::registry>().get<int>({})), int &>();
  1670. testing::StaticAssertTypeEq<decltype(std::declval<entt::registry>().get<int, const char>({})), std::tuple<int &, const char &>>();
  1671. testing::StaticAssertTypeEq<decltype(std::declval<entt::registry>().try_get<>({})), std::tuple<>>();
  1672. testing::StaticAssertTypeEq<decltype(std::declval<entt::registry>().try_get<int>({})), int *>();
  1673. testing::StaticAssertTypeEq<decltype(std::declval<entt::registry>().try_get<int, const char>({})), std::tuple<int *, const char *>>();
  1674. testing::StaticAssertTypeEq<decltype(std::declval<entt::registry>().ctx().get<int>()), int &>();
  1675. testing::StaticAssertTypeEq<decltype(std::declval<entt::registry>().ctx().get<const char>()), const char &>();
  1676. testing::StaticAssertTypeEq<decltype(std::declval<entt::registry>().ctx().find<int>()), int *>();
  1677. testing::StaticAssertTypeEq<decltype(std::declval<entt::registry>().ctx().find<const char>()), const char *>();
  1678. testing::StaticAssertTypeEq<decltype(std::declval<const entt::registry>().get<>({})), std::tuple<>>();
  1679. testing::StaticAssertTypeEq<decltype(std::declval<const entt::registry>().get<int>({})), const int &>();
  1680. testing::StaticAssertTypeEq<decltype(std::declval<const entt::registry>().get<int, const char>({})), std::tuple<const int &, const char &>>();
  1681. testing::StaticAssertTypeEq<decltype(std::declval<const entt::registry>().try_get<>({})), std::tuple<>>();
  1682. testing::StaticAssertTypeEq<decltype(std::declval<const entt::registry>().try_get<int>({})), const int *>();
  1683. testing::StaticAssertTypeEq<decltype(std::declval<const entt::registry>().try_get<int, const char>({})), std::tuple<const int *, const char *>>();
  1684. testing::StaticAssertTypeEq<decltype(std::declval<const entt::registry>().ctx().get<int>()), const int &>();
  1685. testing::StaticAssertTypeEq<decltype(std::declval<const entt::registry>().ctx().get<const char>()), const char &>();
  1686. testing::StaticAssertTypeEq<decltype(std::declval<const entt::registry>().ctx().find<int>()), const int *>();
  1687. testing::StaticAssertTypeEq<decltype(std::declval<const entt::registry>().ctx().find<const char>()), const char *>();
  1688. }
  1689. TEST(Registry, AssignEntities) {
  1690. using traits_type = entt::entt_traits<entt::entity>;
  1691. entt::registry registry{};
  1692. std::array<entt::entity, 3u> entity{};
  1693. registry.create(entity.begin(), entity.end());
  1694. registry.destroy(entity[1]);
  1695. registry.destroy(entity[2]);
  1696. entt::registry other{};
  1697. auto &src = registry.storage<entt::entity>();
  1698. auto &dst = other.storage<entt::entity>();
  1699. dst.push(src.rbegin(), src.rend());
  1700. dst.free_list(src.free_list());
  1701. ASSERT_EQ(registry.storage<entt::entity>().size(), other.storage<entt::entity>().size());
  1702. ASSERT_TRUE(other.valid(entity[0]));
  1703. ASSERT_FALSE(other.valid(entity[1]));
  1704. ASSERT_FALSE(other.valid(entity[2]));
  1705. ASSERT_EQ(registry.create(), other.create());
  1706. ASSERT_EQ(traits_type::to_entity(other.create()), traits_type::to_integral(entity[1]));
  1707. }
  1708. TEST(Registry, ScramblingPoolsIsAllowed) {
  1709. entt::registry registry{};
  1710. registry.on_destroy<int>().connect<&listener::sort<int>>();
  1711. for(std::size_t i{}; i < 2u; ++i) {
  1712. const auto entity = registry.create();
  1713. registry.emplace<int>(entity, static_cast<int>(i));
  1714. }
  1715. registry.destroy(registry.view<int>().back());
  1716. // thanks to @andranik3949 for pointing out this missing test
  1717. registry.view<const int>().each([](const auto entity, const auto &value) {
  1718. ASSERT_EQ(static_cast<int>(entt::to_integral(entity)), value);
  1719. });
  1720. }
  1721. TEST(Registry, AssureMixinLoop) {
  1722. entt::registry registry{};
  1723. const auto entity = registry.create();
  1724. ASSERT_EQ(std::as_const(registry).storage<test::assure_loop>(), nullptr);
  1725. ASSERT_EQ(std::as_const(registry).storage<int>(), nullptr);
  1726. registry.emplace<test::assure_loop>(entity);
  1727. ASSERT_NE(std::as_const(registry).storage<test::assure_loop>(), nullptr);
  1728. ASSERT_NE(std::as_const(registry).storage<int>(), nullptr);
  1729. ASSERT_TRUE(registry.all_of<test::assure_loop>(entity));
  1730. ASSERT_FALSE(registry.all_of<int>(entity));
  1731. }
  1732. TEST(Registry, VoidType) {
  1733. using namespace entt::literals;
  1734. entt::registry registry{};
  1735. const auto entity = registry.create();
  1736. auto &storage = registry.storage<void>("custom"_hs);
  1737. storage.emplace(entity);
  1738. ASSERT_TRUE(registry.storage<void>().empty());
  1739. ASSERT_FALSE(registry.storage<void>("custom"_hs).empty());
  1740. ASSERT_TRUE(registry.storage<void>("custom"_hs).contains(entity));
  1741. ASSERT_EQ(registry.storage<void>().type(), entt::type_id<void>());
  1742. ASSERT_EQ(registry.storage<void>("custom"_hs).type(), entt::type_id<void>());
  1743. }
  1744. TEST(Registry, NoEtoType) {
  1745. entt::registry registry{};
  1746. const auto entity = registry.create();
  1747. registry.emplace<no_eto_type>(entity);
  1748. registry.emplace<int>(entity, 1);
  1749. ASSERT_NE(registry.storage<no_eto_type>().raw(), nullptr);
  1750. ASSERT_NE(registry.try_get<no_eto_type>(entity), nullptr);
  1751. ASSERT_EQ(registry.view<no_eto_type>().get(entity), std::as_const(registry).view<const no_eto_type>().get(entity));
  1752. auto view = registry.view<no_eto_type, int>();
  1753. auto cview = std::as_const(registry).view<const no_eto_type, const int>();
  1754. ASSERT_EQ((std::get<0>(view.get<no_eto_type, int>(entity))), (std::get<0>(cview.get<const no_eto_type, const int>(entity))));
  1755. }