any.cpp 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466
  1. #include <algorithm>
  2. #include <cstdint>
  3. #include <cstring>
  4. #include <iterator>
  5. #include <type_traits>
  6. #include <unordered_map>
  7. #include <utility>
  8. #include <vector>
  9. #include <gtest/gtest.h>
  10. #include <entt/core/any.hpp>
  11. #include <entt/core/type_info.hpp>
  12. #include "../common/config.h"
  13. struct empty {
  14. ~empty() {
  15. ++counter;
  16. }
  17. inline static int counter = 0;
  18. };
  19. struct fat {
  20. fat(double v1, double v2, double v3, double v4)
  21. : value{v1, v2, v3, v4} {}
  22. ~fat() {
  23. ++counter;
  24. }
  25. bool operator==(const fat &other) const {
  26. return std::equal(std::begin(value), std::end(value), std::begin(other.value), std::end(other.value));
  27. }
  28. inline static int counter{0};
  29. double value[4];
  30. };
  31. struct not_comparable {
  32. bool operator==(const not_comparable &) const = delete;
  33. };
  34. struct not_copyable {
  35. not_copyable()
  36. : payload{} {}
  37. not_copyable(const not_copyable &) = delete;
  38. not_copyable(not_copyable &&) = default;
  39. not_copyable &operator=(const not_copyable &) = delete;
  40. not_copyable &operator=(not_copyable &&) = default;
  41. double payload;
  42. };
  43. struct not_movable {
  44. not_movable() = default;
  45. not_movable(const not_movable &) = default;
  46. not_movable(not_movable &&) = delete;
  47. not_movable &operator=(const not_movable &) = default;
  48. not_movable &operator=(not_movable &&) = delete;
  49. double payload{};
  50. };
  51. struct alignas(64u) over_aligned {};
  52. struct Any: ::testing::Test {
  53. void SetUp() override {
  54. fat::counter = 0;
  55. empty::counter = 0;
  56. }
  57. };
  58. using AnyDeathTest = Any;
  59. TEST_F(Any, SBO) {
  60. entt::any any{'c'};
  61. ASSERT_TRUE(any);
  62. ASSERT_TRUE(any.owner());
  63. ASSERT_EQ(any.type(), entt::type_id<char>());
  64. ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
  65. ASSERT_EQ(entt::any_cast<char>(any), 'c');
  66. }
  67. TEST_F(Any, NoSBO) {
  68. fat instance{.1, .2, .3, .4};
  69. entt::any any{instance};
  70. ASSERT_TRUE(any);
  71. ASSERT_TRUE(any.owner());
  72. ASSERT_EQ(any.type(), entt::type_id<fat>());
  73. ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
  74. ASSERT_EQ(entt::any_cast<fat>(any), instance);
  75. }
  76. TEST_F(Any, Empty) {
  77. entt::any any{};
  78. ASSERT_FALSE(any);
  79. ASSERT_TRUE(any.owner());
  80. ASSERT_EQ(any.type(), entt::type_id<void>());
  81. ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
  82. ASSERT_EQ(any.data(), nullptr);
  83. }
  84. TEST_F(Any, SBOInPlaceTypeConstruction) {
  85. entt::any any{std::in_place_type<int>, 42};
  86. ASSERT_TRUE(any);
  87. ASSERT_TRUE(any.owner());
  88. ASSERT_EQ(any.type(), entt::type_id<int>());
  89. ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
  90. ASSERT_EQ(entt::any_cast<int>(any), 42);
  91. auto other = any.as_ref();
  92. ASSERT_TRUE(other);
  93. ASSERT_FALSE(other.owner());
  94. ASSERT_EQ(other.type(), entt::type_id<int>());
  95. ASSERT_EQ(entt::any_cast<int>(other), 42);
  96. ASSERT_EQ(other.data(), any.data());
  97. }
  98. TEST_F(Any, SBOAsRefConstruction) {
  99. int value = 42;
  100. entt::any any{entt::forward_as_any(value)};
  101. ASSERT_TRUE(any);
  102. ASSERT_FALSE(any.owner());
  103. ASSERT_EQ(any.type(), entt::type_id<int>());
  104. ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
  105. ASSERT_EQ(entt::any_cast<const int>(&any), &value);
  106. ASSERT_EQ(entt::any_cast<int>(&any), &value);
  107. ASSERT_EQ(entt::any_cast<const int>(&std::as_const(any)), &value);
  108. ASSERT_EQ(entt::any_cast<int>(&std::as_const(any)), &value);
  109. ASSERT_EQ(entt::any_cast<const int &>(any), 42);
  110. ASSERT_EQ(entt::any_cast<int>(any), 42);
  111. ASSERT_EQ(any.data(), &value);
  112. ASSERT_EQ(std::as_const(any).data(), &value);
  113. any.emplace<int &>(value);
  114. ASSERT_TRUE(any);
  115. ASSERT_FALSE(any.owner());
  116. ASSERT_EQ(any.type(), entt::type_id<int>());
  117. ASSERT_EQ(entt::any_cast<int>(&any), &value);
  118. auto other = any.as_ref();
  119. ASSERT_TRUE(other);
  120. ASSERT_FALSE(other.owner());
  121. ASSERT_EQ(other.type(), entt::type_id<int>());
  122. ASSERT_EQ(entt::any_cast<int>(other), 42);
  123. ASSERT_EQ(other.data(), any.data());
  124. }
  125. TEST_F(Any, SBOAsConstRefConstruction) {
  126. const int value = 42;
  127. entt::any any{entt::forward_as_any(value)};
  128. ASSERT_TRUE(any);
  129. ASSERT_FALSE(any.owner());
  130. ASSERT_EQ(any.type(), entt::type_id<int>());
  131. ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
  132. ASSERT_EQ(entt::any_cast<const int>(&any), &value);
  133. ASSERT_EQ(entt::any_cast<int>(&any), nullptr);
  134. ASSERT_EQ(entt::any_cast<const int>(&std::as_const(any)), &value);
  135. ASSERT_EQ(entt::any_cast<int>(&std::as_const(any)), &value);
  136. ASSERT_EQ(entt::any_cast<const int &>(any), 42);
  137. ASSERT_EQ(entt::any_cast<int>(any), 42);
  138. ASSERT_EQ(any.data(), nullptr);
  139. ASSERT_EQ(std::as_const(any).data(), &value);
  140. any.emplace<const int &>(value);
  141. ASSERT_TRUE(any);
  142. ASSERT_FALSE(any.owner());
  143. ASSERT_EQ(any.type(), entt::type_id<int>());
  144. ASSERT_EQ(entt::any_cast<const int>(&any), &value);
  145. auto other = any.as_ref();
  146. ASSERT_TRUE(other);
  147. ASSERT_FALSE(other.owner());
  148. ASSERT_EQ(other.type(), entt::type_id<int>());
  149. ASSERT_EQ(entt::any_cast<int>(other), 42);
  150. ASSERT_EQ(other.data(), any.data());
  151. }
  152. TEST_F(Any, SBOCopyConstruction) {
  153. entt::any any{42};
  154. entt::any other{any};
  155. ASSERT_TRUE(any);
  156. ASSERT_TRUE(other);
  157. ASSERT_TRUE(any.owner());
  158. ASSERT_TRUE(other.owner());
  159. ASSERT_EQ(any.type(), entt::type_id<int>());
  160. ASSERT_EQ(other.type(), entt::type_id<int>());
  161. ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
  162. ASSERT_EQ(entt::any_cast<int>(other), 42);
  163. }
  164. TEST_F(Any, SBOCopyAssignment) {
  165. entt::any any{42};
  166. entt::any other{3};
  167. other = any;
  168. ASSERT_TRUE(any);
  169. ASSERT_TRUE(other);
  170. ASSERT_TRUE(any.owner());
  171. ASSERT_TRUE(other.owner());
  172. ASSERT_EQ(any.type(), entt::type_id<int>());
  173. ASSERT_EQ(other.type(), entt::type_id<int>());
  174. ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
  175. ASSERT_EQ(entt::any_cast<int>(other), 42);
  176. }
  177. TEST_F(Any, SBOMoveConstruction) {
  178. entt::any any{42};
  179. entt::any other{std::move(any)};
  180. ASSERT_TRUE(any);
  181. ASSERT_TRUE(other);
  182. ASSERT_TRUE(any.owner());
  183. ASSERT_TRUE(other.owner());
  184. ASSERT_NE(any.data(), nullptr);
  185. ASSERT_EQ(any.type(), entt::type_id<int>());
  186. ASSERT_EQ(other.type(), entt::type_id<int>());
  187. ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
  188. ASSERT_EQ(entt::any_cast<int>(other), 42);
  189. }
  190. TEST_F(Any, SBOMoveAssignment) {
  191. entt::any any{42};
  192. entt::any other{3};
  193. other = std::move(any);
  194. ASSERT_TRUE(any);
  195. ASSERT_TRUE(other);
  196. ASSERT_TRUE(any.owner());
  197. ASSERT_TRUE(other.owner());
  198. ASSERT_NE(any.data(), nullptr);
  199. ASSERT_EQ(any.type(), entt::type_id<int>());
  200. ASSERT_EQ(other.type(), entt::type_id<int>());
  201. ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
  202. ASSERT_EQ(entt::any_cast<int>(other), 42);
  203. }
  204. TEST_F(Any, SBODirectAssignment) {
  205. entt::any any{};
  206. any = 42;
  207. ASSERT_TRUE(any);
  208. ASSERT_TRUE(any.owner());
  209. ASSERT_EQ(any.type(), entt::type_id<int>());
  210. ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
  211. ASSERT_EQ(entt::any_cast<int>(any), 42);
  212. }
  213. TEST_F(Any, SBOAssignValue) {
  214. entt::any any{42};
  215. entt::any other{3};
  216. entt::any invalid{'c'};
  217. ASSERT_TRUE(any);
  218. ASSERT_EQ(entt::any_cast<int>(any), 42);
  219. ASSERT_TRUE(any.assign(other));
  220. ASSERT_FALSE(any.assign(invalid));
  221. ASSERT_EQ(entt::any_cast<int>(any), 3);
  222. }
  223. TEST_F(Any, SBOAsRefAssignValue) {
  224. int value = 42;
  225. entt::any any{entt::forward_as_any(value)};
  226. entt::any other{3};
  227. entt::any invalid{'c'};
  228. ASSERT_TRUE(any);
  229. ASSERT_EQ(entt::any_cast<int>(any), 42);
  230. ASSERT_TRUE(any.assign(other));
  231. ASSERT_FALSE(any.assign(invalid));
  232. ASSERT_EQ(entt::any_cast<int>(any), 3);
  233. ASSERT_EQ(value, 3);
  234. }
  235. TEST_F(Any, SBOAsConstRefAssignValue) {
  236. const int value = 42;
  237. entt::any any{entt::forward_as_any(value)};
  238. entt::any other{3};
  239. entt::any invalid{'c'};
  240. ASSERT_TRUE(any);
  241. ASSERT_EQ(entt::any_cast<int>(any), 42);
  242. ASSERT_FALSE(any.assign(other));
  243. ASSERT_FALSE(any.assign(invalid));
  244. ASSERT_EQ(entt::any_cast<int>(any), 42);
  245. ASSERT_EQ(value, 42);
  246. }
  247. TEST_F(Any, SBOTransferValue) {
  248. entt::any any{42};
  249. ASSERT_TRUE(any);
  250. ASSERT_EQ(entt::any_cast<int>(any), 42);
  251. ASSERT_TRUE(any.assign(3));
  252. ASSERT_FALSE(any.assign('c'));
  253. ASSERT_EQ(entt::any_cast<int>(any), 3);
  254. }
  255. TEST_F(Any, SBOTransferConstValue) {
  256. const int value = 3;
  257. entt::any any{42};
  258. ASSERT_TRUE(any);
  259. ASSERT_EQ(entt::any_cast<int>(any), 42);
  260. ASSERT_TRUE(any.assign(entt::forward_as_any(value)));
  261. ASSERT_EQ(entt::any_cast<int>(any), 3);
  262. }
  263. TEST_F(Any, SBOAsRefTransferValue) {
  264. int value = 42;
  265. entt::any any{entt::forward_as_any(value)};
  266. ASSERT_TRUE(any);
  267. ASSERT_EQ(entt::any_cast<int>(any), 42);
  268. ASSERT_TRUE(any.assign(3));
  269. ASSERT_FALSE(any.assign('c'));
  270. ASSERT_EQ(entt::any_cast<int>(any), 3);
  271. ASSERT_EQ(value, 3);
  272. }
  273. TEST_F(Any, SBOAsConstRefTransferValue) {
  274. const int value = 42;
  275. entt::any any{entt::forward_as_any(value)};
  276. ASSERT_TRUE(any);
  277. ASSERT_EQ(entt::any_cast<int>(any), 42);
  278. ASSERT_FALSE(any.assign(3));
  279. ASSERT_FALSE(any.assign('c'));
  280. ASSERT_EQ(entt::any_cast<int>(any), 42);
  281. ASSERT_EQ(value, 42);
  282. }
  283. TEST_F(Any, NoSBOInPlaceTypeConstruction) {
  284. fat instance{.1, .2, .3, .4};
  285. entt::any any{std::in_place_type<fat>, instance};
  286. ASSERT_TRUE(any);
  287. ASSERT_TRUE(any.owner());
  288. ASSERT_EQ(any.type(), entt::type_id<fat>());
  289. ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
  290. ASSERT_EQ(entt::any_cast<fat>(any), instance);
  291. auto other = any.as_ref();
  292. ASSERT_TRUE(other);
  293. ASSERT_FALSE(other.owner());
  294. ASSERT_EQ(other.type(), entt::type_id<fat>());
  295. ASSERT_EQ(entt::any_cast<fat>(other), (fat{.1, .2, .3, .4}));
  296. ASSERT_EQ(other.data(), any.data());
  297. }
  298. TEST_F(Any, NoSBOAsRefConstruction) {
  299. fat instance{.1, .2, .3, .4};
  300. entt::any any{entt::forward_as_any(instance)};
  301. ASSERT_TRUE(any);
  302. ASSERT_FALSE(any.owner());
  303. ASSERT_EQ(any.type(), entt::type_id<fat>());
  304. ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
  305. ASSERT_EQ(entt::any_cast<const fat>(&any), &instance);
  306. ASSERT_EQ(entt::any_cast<fat>(&any), &instance);
  307. ASSERT_EQ(entt::any_cast<const fat>(&std::as_const(any)), &instance);
  308. ASSERT_EQ(entt::any_cast<fat>(&std::as_const(any)), &instance);
  309. ASSERT_EQ(entt::any_cast<const fat &>(any), instance);
  310. ASSERT_EQ(entt::any_cast<fat>(any), instance);
  311. ASSERT_EQ(any.data(), &instance);
  312. ASSERT_EQ(std::as_const(any).data(), &instance);
  313. any.emplace<fat &>(instance);
  314. ASSERT_TRUE(any);
  315. ASSERT_FALSE(any.owner());
  316. ASSERT_EQ(any.type(), entt::type_id<fat>());
  317. ASSERT_EQ(entt::any_cast<fat>(&any), &instance);
  318. auto other = any.as_ref();
  319. ASSERT_TRUE(other);
  320. ASSERT_FALSE(other.owner());
  321. ASSERT_EQ(other.type(), entt::type_id<fat>());
  322. ASSERT_EQ(entt::any_cast<fat>(other), (fat{.1, .2, .3, .4}));
  323. ASSERT_EQ(other.data(), any.data());
  324. }
  325. TEST_F(Any, NoSBOAsConstRefConstruction) {
  326. const fat instance{.1, .2, .3, .4};
  327. entt::any any{entt::forward_as_any(instance)};
  328. ASSERT_TRUE(any);
  329. ASSERT_FALSE(any.owner());
  330. ASSERT_EQ(any.type(), entt::type_id<fat>());
  331. ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
  332. ASSERT_EQ(entt::any_cast<const fat>(&any), &instance);
  333. ASSERT_EQ(entt::any_cast<fat>(&any), nullptr);
  334. ASSERT_EQ(entt::any_cast<const fat>(&std::as_const(any)), &instance);
  335. ASSERT_EQ(entt::any_cast<fat>(&std::as_const(any)), &instance);
  336. ASSERT_EQ(entt::any_cast<const fat &>(any), instance);
  337. ASSERT_EQ(entt::any_cast<fat>(any), instance);
  338. ASSERT_EQ(any.data(), nullptr);
  339. ASSERT_EQ(std::as_const(any).data(), &instance);
  340. any.emplace<const fat &>(instance);
  341. ASSERT_TRUE(any);
  342. ASSERT_FALSE(any.owner());
  343. ASSERT_EQ(any.type(), entt::type_id<fat>());
  344. ASSERT_EQ(entt::any_cast<const fat>(&any), &instance);
  345. auto other = any.as_ref();
  346. ASSERT_TRUE(other);
  347. ASSERT_FALSE(other.owner());
  348. ASSERT_EQ(other.type(), entt::type_id<fat>());
  349. ASSERT_EQ(entt::any_cast<fat>(other), (fat{.1, .2, .3, .4}));
  350. ASSERT_EQ(other.data(), any.data());
  351. }
  352. TEST_F(Any, NoSBOCopyConstruction) {
  353. fat instance{.1, .2, .3, .4};
  354. entt::any any{instance};
  355. entt::any other{any};
  356. ASSERT_TRUE(any);
  357. ASSERT_TRUE(other);
  358. ASSERT_TRUE(any.owner());
  359. ASSERT_TRUE(other.owner());
  360. ASSERT_EQ(any.type(), entt::type_id<fat>());
  361. ASSERT_EQ(other.type(), entt::type_id<fat>());
  362. ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
  363. ASSERT_EQ(entt::any_cast<fat>(other), instance);
  364. }
  365. TEST_F(Any, NoSBOCopyAssignment) {
  366. fat instance{.1, .2, .3, .4};
  367. entt::any any{instance};
  368. entt::any other{3};
  369. other = any;
  370. ASSERT_TRUE(any);
  371. ASSERT_TRUE(other);
  372. ASSERT_TRUE(any.owner());
  373. ASSERT_TRUE(other.owner());
  374. ASSERT_EQ(any.type(), entt::type_id<fat>());
  375. ASSERT_EQ(other.type(), entt::type_id<fat>());
  376. ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
  377. ASSERT_EQ(entt::any_cast<fat>(other), instance);
  378. }
  379. TEST_F(Any, NoSBOMoveConstruction) {
  380. fat instance{.1, .2, .3, .4};
  381. entt::any any{instance};
  382. entt::any other{std::move(any)};
  383. ASSERT_TRUE(any);
  384. ASSERT_TRUE(other);
  385. ASSERT_TRUE(any.owner());
  386. ASSERT_TRUE(other.owner());
  387. ASSERT_EQ(any.data(), nullptr);
  388. ASSERT_EQ(any.type(), entt::type_id<fat>());
  389. ASSERT_EQ(other.type(), entt::type_id<fat>());
  390. ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
  391. ASSERT_EQ(entt::any_cast<fat>(other), instance);
  392. }
  393. TEST_F(Any, NoSBOMoveAssignment) {
  394. fat instance{.1, .2, .3, .4};
  395. entt::any any{instance};
  396. entt::any other{3};
  397. other = std::move(any);
  398. ASSERT_TRUE(any);
  399. ASSERT_TRUE(other);
  400. ASSERT_TRUE(any.owner());
  401. ASSERT_TRUE(other.owner());
  402. ASSERT_EQ(any.data(), nullptr);
  403. ASSERT_EQ(any.type(), entt::type_id<fat>());
  404. ASSERT_EQ(other.type(), entt::type_id<fat>());
  405. ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
  406. ASSERT_EQ(entt::any_cast<fat>(other), instance);
  407. }
  408. TEST_F(Any, NoSBODirectAssignment) {
  409. fat instance{.1, .2, .3, .4};
  410. entt::any any{};
  411. any = instance;
  412. ASSERT_TRUE(any);
  413. ASSERT_TRUE(any.owner());
  414. ASSERT_EQ(any.type(), entt::type_id<fat>());
  415. ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
  416. ASSERT_EQ(entt::any_cast<fat>(any), instance);
  417. }
  418. TEST_F(Any, NoSBOAssignValue) {
  419. entt::any any{fat{.1, .2, .3, .4}};
  420. entt::any other{fat{.0, .1, .2, .3}};
  421. entt::any invalid{'c'};
  422. const void *addr = std::as_const(any).data();
  423. ASSERT_TRUE(any);
  424. ASSERT_EQ(entt::any_cast<const fat &>(any), (fat{.1, .2, .3, .4}));
  425. ASSERT_TRUE(any.assign(other));
  426. ASSERT_FALSE(any.assign(invalid));
  427. ASSERT_EQ(entt::any_cast<const fat &>(any), (fat{.0, .1, .2, .3}));
  428. ASSERT_EQ(addr, std::as_const(any).data());
  429. }
  430. TEST_F(Any, NoSBOAsRefAssignValue) {
  431. fat instance{.1, .2, .3, .4};
  432. entt::any any{entt::forward_as_any(instance)};
  433. entt::any other{fat{.0, .1, .2, .3}};
  434. entt::any invalid{'c'};
  435. ASSERT_TRUE(any);
  436. ASSERT_EQ(entt::any_cast<const fat &>(any), (fat{.1, .2, .3, .4}));
  437. ASSERT_TRUE(any.assign(other));
  438. ASSERT_FALSE(any.assign(invalid));
  439. ASSERT_EQ(entt::any_cast<const fat &>(any), (fat{.0, .1, .2, .3}));
  440. ASSERT_EQ(instance, (fat{.0, .1, .2, .3}));
  441. }
  442. TEST_F(Any, NoSBOAsConstRefAssignValue) {
  443. const fat instance{.1, .2, .3, .4};
  444. entt::any any{entt::forward_as_any(instance)};
  445. entt::any other{fat{.0, .1, .2, .3}};
  446. entt::any invalid{'c'};
  447. ASSERT_TRUE(any);
  448. ASSERT_EQ(entt::any_cast<const fat &>(any), (fat{.1, .2, .3, .4}));
  449. ASSERT_FALSE(any.assign(other));
  450. ASSERT_FALSE(any.assign(invalid));
  451. ASSERT_EQ(entt::any_cast<const fat &>(any), (fat{.1, .2, .3, .4}));
  452. ASSERT_EQ(instance, (fat{.1, .2, .3, .4}));
  453. }
  454. TEST_F(Any, NoSBOTransferValue) {
  455. entt::any any{fat{.1, .2, .3, .4}};
  456. const void *addr = std::as_const(any).data();
  457. ASSERT_TRUE(any);
  458. ASSERT_EQ(entt::any_cast<const fat &>(any), (fat{.1, .2, .3, .4}));
  459. ASSERT_TRUE(any.assign(fat{.0, .1, .2, .3}));
  460. ASSERT_FALSE(any.assign('c'));
  461. ASSERT_EQ(entt::any_cast<const fat &>(any), (fat{.0, .1, .2, .3}));
  462. ASSERT_EQ(addr, std::as_const(any).data());
  463. }
  464. TEST_F(Any, NoSBOTransferConstValue) {
  465. const fat instance{.0, .1, .2, .3};
  466. entt::any any{fat{.1, .2, .3, .4}};
  467. const void *addr = std::as_const(any).data();
  468. ASSERT_TRUE(any);
  469. ASSERT_EQ(entt::any_cast<const fat &>(any), (fat{.1, .2, .3, .4}));
  470. ASSERT_TRUE(any.assign(entt::forward_as_any(instance)));
  471. ASSERT_EQ(entt::any_cast<const fat &>(any), (fat{.0, .1, .2, .3}));
  472. ASSERT_EQ(addr, std::as_const(any).data());
  473. }
  474. TEST_F(Any, NoSBOAsRefTransferValue) {
  475. fat instance{.1, .2, .3, .4};
  476. entt::any any{entt::forward_as_any(instance)};
  477. const void *addr = std::as_const(any).data();
  478. ASSERT_TRUE(any);
  479. ASSERT_EQ(entt::any_cast<const fat &>(any), (fat{.1, .2, .3, .4}));
  480. ASSERT_TRUE(any.assign(fat{.0, .1, .2, .3}));
  481. ASSERT_FALSE(any.assign('c'));
  482. ASSERT_EQ(entt::any_cast<const fat &>(any), (fat{.0, .1, .2, .3}));
  483. ASSERT_EQ(instance, (fat{.0, .1, .2, .3}));
  484. ASSERT_EQ(addr, std::as_const(any).data());
  485. }
  486. TEST_F(Any, NoSBOAsConstRefTransferValue) {
  487. const fat instance{.1, .2, .3, .4};
  488. entt::any any{entt::forward_as_any(instance)};
  489. const void *addr = std::as_const(any).data();
  490. ASSERT_TRUE(any);
  491. ASSERT_EQ(entt::any_cast<const fat &>(any), (fat{.1, .2, .3, .4}));
  492. ASSERT_FALSE(any.assign(fat{.0, .1, .2, .3}));
  493. ASSERT_FALSE(any.assign('c'));
  494. ASSERT_EQ(entt::any_cast<const fat &>(any), (fat{.1, .2, .3, .4}));
  495. ASSERT_EQ(instance, (fat{.1, .2, .3, .4}));
  496. ASSERT_EQ(addr, std::as_const(any).data());
  497. }
  498. TEST_F(Any, VoidInPlaceTypeConstruction) {
  499. entt::any any{std::in_place_type<void>};
  500. ASSERT_FALSE(any);
  501. ASSERT_TRUE(any.owner());
  502. ASSERT_EQ(any.type(), entt::type_id<void>());
  503. ASSERT_EQ(entt::any_cast<int>(&any), nullptr);
  504. }
  505. TEST_F(Any, VoidCopyConstruction) {
  506. entt::any any{std::in_place_type<void>};
  507. entt::any other{any};
  508. ASSERT_FALSE(any);
  509. ASSERT_FALSE(other);
  510. ASSERT_TRUE(any.owner());
  511. ASSERT_TRUE(other.owner());
  512. ASSERT_EQ(any.type(), entt::type_id<void>());
  513. ASSERT_EQ(other.type(), entt::type_id<void>());
  514. ASSERT_EQ(entt::any_cast<int>(&any), nullptr);
  515. ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
  516. }
  517. TEST_F(Any, VoidCopyAssignment) {
  518. entt::any any{std::in_place_type<void>};
  519. entt::any other{42};
  520. other = any;
  521. ASSERT_FALSE(any);
  522. ASSERT_FALSE(other);
  523. ASSERT_TRUE(any.owner());
  524. ASSERT_TRUE(other.owner());
  525. ASSERT_EQ(any.type(), entt::type_id<void>());
  526. ASSERT_EQ(other.type(), entt::type_id<void>());
  527. ASSERT_EQ(entt::any_cast<int>(&any), nullptr);
  528. ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
  529. }
  530. TEST_F(Any, VoidMoveConstruction) {
  531. entt::any any{std::in_place_type<void>};
  532. entt::any other{std::move(any)};
  533. ASSERT_FALSE(any);
  534. ASSERT_FALSE(other);
  535. ASSERT_TRUE(any.owner());
  536. ASSERT_TRUE(other.owner());
  537. ASSERT_EQ(any.type(), entt::type_id<void>());
  538. ASSERT_EQ(other.type(), entt::type_id<void>());
  539. ASSERT_EQ(entt::any_cast<int>(&any), nullptr);
  540. ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
  541. }
  542. TEST_F(Any, VoidMoveAssignment) {
  543. entt::any any{std::in_place_type<void>};
  544. entt::any other{42};
  545. other = std::move(any);
  546. ASSERT_FALSE(any);
  547. ASSERT_FALSE(other);
  548. ASSERT_TRUE(any.owner());
  549. ASSERT_TRUE(other.owner());
  550. ASSERT_EQ(any.type(), entt::type_id<void>());
  551. ASSERT_EQ(other.type(), entt::type_id<void>());
  552. ASSERT_EQ(entt::any_cast<int>(&any), nullptr);
  553. ASSERT_EQ(entt::any_cast<double>(&other), nullptr);
  554. }
  555. TEST_F(Any, SBOMoveValidButUnspecifiedState) {
  556. entt::any any{42};
  557. entt::any other{std::move(any)};
  558. entt::any valid = std::move(other);
  559. ASSERT_TRUE(any);
  560. ASSERT_TRUE(other);
  561. ASSERT_TRUE(valid);
  562. }
  563. TEST_F(Any, NoSBOMoveValidButUnspecifiedState) {
  564. fat instance{.1, .2, .3, .4};
  565. entt::any any{instance};
  566. entt::any other{std::move(any)};
  567. entt::any valid = std::move(other);
  568. ASSERT_TRUE(any);
  569. ASSERT_TRUE(other);
  570. ASSERT_TRUE(valid);
  571. }
  572. TEST_F(Any, VoidMoveValidButUnspecifiedState) {
  573. entt::any any{std::in_place_type<void>};
  574. entt::any other{std::move(any)};
  575. entt::any valid = std::move(other);
  576. ASSERT_FALSE(any);
  577. ASSERT_FALSE(other);
  578. ASSERT_FALSE(valid);
  579. }
  580. TEST_F(Any, SBODestruction) {
  581. {
  582. entt::any any{std::in_place_type<empty>};
  583. any.emplace<empty>();
  584. any = empty{};
  585. entt::any other{std::move(any)};
  586. any = std::move(other);
  587. }
  588. ASSERT_EQ(empty::counter, 6);
  589. }
  590. TEST_F(Any, NoSBODestruction) {
  591. {
  592. entt::any any{std::in_place_type<fat>, 1., 2., 3., 4.};
  593. any.emplace<fat>(1., 2., 3., 4.);
  594. any = fat{1., 2., 3., 4.};
  595. entt::any other{std::move(any)};
  596. any = std::move(other);
  597. }
  598. ASSERT_EQ(fat::counter, 4);
  599. }
  600. TEST_F(Any, VoidDestruction) {
  601. // just let asan tell us if everything is ok here
  602. [[maybe_unused]] entt::any any{std::in_place_type<void>};
  603. }
  604. TEST_F(Any, Emplace) {
  605. entt::any any{};
  606. any.emplace<int>(42);
  607. ASSERT_TRUE(any);
  608. ASSERT_TRUE(any.owner());
  609. ASSERT_EQ(any.type(), entt::type_id<int>());
  610. ASSERT_EQ(entt::any_cast<double>(&any), nullptr);
  611. ASSERT_EQ(entt::any_cast<int>(any), 42);
  612. }
  613. TEST_F(Any, EmplaceVoid) {
  614. entt::any any{};
  615. any.emplace<void>();
  616. ASSERT_FALSE(any);
  617. ASSERT_TRUE(any.owner());
  618. ASSERT_EQ(any.type(), entt::type_id<void>());
  619. }
  620. TEST_F(Any, Reset) {
  621. entt::any any{42};
  622. ASSERT_TRUE(any);
  623. ASSERT_TRUE(any.owner());
  624. ASSERT_EQ(any.type(), entt::type_id<int>());
  625. any.reset();
  626. ASSERT_FALSE(any);
  627. ASSERT_TRUE(any.owner());
  628. ASSERT_EQ(any.type(), entt::type_id<void>());
  629. int value = 42;
  630. any.emplace<int &>(value);
  631. ASSERT_TRUE(any);
  632. ASSERT_FALSE(any.owner());
  633. ASSERT_EQ(any.type(), entt::type_id<int>());
  634. any.reset();
  635. ASSERT_FALSE(any);
  636. ASSERT_TRUE(any.owner());
  637. ASSERT_EQ(any.type(), entt::type_id<void>());
  638. }
  639. TEST_F(Any, SBOSwap) {
  640. entt::any lhs{'c'};
  641. entt::any rhs{42};
  642. std::swap(lhs, rhs);
  643. ASSERT_TRUE(lhs.owner());
  644. ASSERT_TRUE(rhs.owner());
  645. ASSERT_EQ(lhs.type(), entt::type_id<int>());
  646. ASSERT_EQ(rhs.type(), entt::type_id<char>());
  647. ASSERT_EQ(entt::any_cast<char>(&lhs), nullptr);
  648. ASSERT_EQ(entt::any_cast<int>(&rhs), nullptr);
  649. ASSERT_EQ(entt::any_cast<int>(lhs), 42);
  650. ASSERT_EQ(entt::any_cast<char>(rhs), 'c');
  651. }
  652. TEST_F(Any, NoSBOSwap) {
  653. entt::any lhs{fat{.1, .2, .3, .4}};
  654. entt::any rhs{fat{.4, .3, .2, .1}};
  655. std::swap(lhs, rhs);
  656. ASSERT_TRUE(lhs.owner());
  657. ASSERT_TRUE(rhs.owner());
  658. ASSERT_EQ(entt::any_cast<fat>(lhs), (fat{.4, .3, .2, .1}));
  659. ASSERT_EQ(entt::any_cast<fat>(rhs), (fat{.1, .2, .3, .4}));
  660. }
  661. TEST_F(Any, VoidSwap) {
  662. entt::any lhs{std::in_place_type<void>};
  663. entt::any rhs{std::in_place_type<void>};
  664. const auto *pre = lhs.data();
  665. std::swap(lhs, rhs);
  666. ASSERT_TRUE(lhs.owner());
  667. ASSERT_TRUE(rhs.owner());
  668. ASSERT_EQ(pre, lhs.data());
  669. }
  670. TEST_F(Any, SBOWithNoSBOSwap) {
  671. entt::any lhs{fat{.1, .2, .3, .4}};
  672. entt::any rhs{'c'};
  673. std::swap(lhs, rhs);
  674. ASSERT_TRUE(lhs.owner());
  675. ASSERT_TRUE(rhs.owner());
  676. ASSERT_EQ(lhs.type(), entt::type_id<char>());
  677. ASSERT_EQ(rhs.type(), entt::type_id<fat>());
  678. ASSERT_EQ(entt::any_cast<fat>(&lhs), nullptr);
  679. ASSERT_EQ(entt::any_cast<char>(&rhs), nullptr);
  680. ASSERT_EQ(entt::any_cast<char>(lhs), 'c');
  681. ASSERT_EQ(entt::any_cast<fat>(rhs), (fat{.1, .2, .3, .4}));
  682. }
  683. TEST_F(Any, SBOWithRefSwap) {
  684. int value = 3;
  685. entt::any lhs{entt::forward_as_any(value)};
  686. entt::any rhs{'c'};
  687. std::swap(lhs, rhs);
  688. ASSERT_TRUE(lhs.owner());
  689. ASSERT_FALSE(rhs.owner());
  690. ASSERT_EQ(lhs.type(), entt::type_id<char>());
  691. ASSERT_EQ(rhs.type(), entt::type_id<int>());
  692. ASSERT_EQ(entt::any_cast<int>(&lhs), nullptr);
  693. ASSERT_EQ(entt::any_cast<char>(&rhs), nullptr);
  694. ASSERT_EQ(entt::any_cast<char>(lhs), 'c');
  695. ASSERT_EQ(entt::any_cast<int>(rhs), 3);
  696. ASSERT_EQ(rhs.data(), &value);
  697. }
  698. TEST_F(Any, SBOWithConstRefSwap) {
  699. const int value = 3;
  700. entt::any lhs{entt::forward_as_any(value)};
  701. entt::any rhs{'c'};
  702. std::swap(lhs, rhs);
  703. ASSERT_TRUE(lhs.owner());
  704. ASSERT_FALSE(rhs.owner());
  705. ASSERT_EQ(lhs.type(), entt::type_id<char>());
  706. ASSERT_EQ(rhs.type(), entt::type_id<int>());
  707. ASSERT_EQ(entt::any_cast<int>(&lhs), nullptr);
  708. ASSERT_EQ(entt::any_cast<char>(&rhs), nullptr);
  709. ASSERT_EQ(entt::any_cast<char>(lhs), 'c');
  710. ASSERT_EQ(entt::any_cast<int>(rhs), 3);
  711. ASSERT_EQ(rhs.data(), nullptr);
  712. ASSERT_EQ(std::as_const(rhs).data(), &value);
  713. }
  714. TEST_F(Any, SBOWithEmptySwap) {
  715. entt::any lhs{'c'};
  716. entt::any rhs{};
  717. std::swap(lhs, rhs);
  718. ASSERT_FALSE(lhs);
  719. ASSERT_TRUE(lhs.owner());
  720. ASSERT_EQ(rhs.type(), entt::type_id<char>());
  721. ASSERT_EQ(entt::any_cast<char>(&lhs), nullptr);
  722. ASSERT_EQ(entt::any_cast<double>(&rhs), nullptr);
  723. ASSERT_EQ(entt::any_cast<char>(rhs), 'c');
  724. std::swap(lhs, rhs);
  725. ASSERT_FALSE(rhs);
  726. ASSERT_TRUE(rhs.owner());
  727. ASSERT_EQ(lhs.type(), entt::type_id<char>());
  728. ASSERT_EQ(entt::any_cast<double>(&lhs), nullptr);
  729. ASSERT_EQ(entt::any_cast<char>(&rhs), nullptr);
  730. ASSERT_EQ(entt::any_cast<char>(lhs), 'c');
  731. }
  732. TEST_F(Any, SBOWithVoidSwap) {
  733. entt::any lhs{'c'};
  734. entt::any rhs{std::in_place_type<void>};
  735. std::swap(lhs, rhs);
  736. ASSERT_FALSE(lhs);
  737. ASSERT_TRUE(lhs.owner());
  738. ASSERT_EQ(rhs.type(), entt::type_id<char>());
  739. ASSERT_EQ(entt::any_cast<char>(&lhs), nullptr);
  740. ASSERT_EQ(entt::any_cast<double>(&rhs), nullptr);
  741. ASSERT_EQ(entt::any_cast<char>(rhs), 'c');
  742. std::swap(lhs, rhs);
  743. ASSERT_FALSE(rhs);
  744. ASSERT_TRUE(rhs.owner());
  745. ASSERT_EQ(lhs.type(), entt::type_id<char>());
  746. ASSERT_EQ(entt::any_cast<double>(&lhs), nullptr);
  747. ASSERT_EQ(entt::any_cast<char>(&rhs), nullptr);
  748. ASSERT_EQ(entt::any_cast<char>(lhs), 'c');
  749. }
  750. TEST_F(Any, NoSBOWithRefSwap) {
  751. int value = 3;
  752. entt::any lhs{entt::forward_as_any(value)};
  753. entt::any rhs{fat{.1, .2, .3, .4}};
  754. std::swap(lhs, rhs);
  755. ASSERT_TRUE(lhs.owner());
  756. ASSERT_FALSE(rhs.owner());
  757. ASSERT_EQ(lhs.type(), entt::type_id<fat>());
  758. ASSERT_EQ(rhs.type(), entt::type_id<int>());
  759. ASSERT_EQ(entt::any_cast<int>(&lhs), nullptr);
  760. ASSERT_EQ(entt::any_cast<fat>(&rhs), nullptr);
  761. ASSERT_EQ(entt::any_cast<fat>(lhs), (fat{.1, .2, .3, .4}));
  762. ASSERT_EQ(entt::any_cast<int>(rhs), 3);
  763. ASSERT_EQ(rhs.data(), &value);
  764. }
  765. TEST_F(Any, NoSBOWithConstRefSwap) {
  766. const int value = 3;
  767. entt::any lhs{entt::forward_as_any(value)};
  768. entt::any rhs{fat{.1, .2, .3, .4}};
  769. std::swap(lhs, rhs);
  770. ASSERT_TRUE(lhs.owner());
  771. ASSERT_FALSE(rhs.owner());
  772. ASSERT_EQ(lhs.type(), entt::type_id<fat>());
  773. ASSERT_EQ(rhs.type(), entt::type_id<int>());
  774. ASSERT_EQ(entt::any_cast<int>(&lhs), nullptr);
  775. ASSERT_EQ(entt::any_cast<fat>(&rhs), nullptr);
  776. ASSERT_EQ(entt::any_cast<fat>(lhs), (fat{.1, .2, .3, .4}));
  777. ASSERT_EQ(entt::any_cast<int>(rhs), 3);
  778. ASSERT_EQ(rhs.data(), nullptr);
  779. ASSERT_EQ(std::as_const(rhs).data(), &value);
  780. }
  781. TEST_F(Any, NoSBOWithEmptySwap) {
  782. entt::any lhs{fat{.1, .2, .3, .4}};
  783. entt::any rhs{};
  784. std::swap(lhs, rhs);
  785. ASSERT_FALSE(lhs);
  786. ASSERT_TRUE(lhs.owner());
  787. ASSERT_EQ(rhs.type(), entt::type_id<fat>());
  788. ASSERT_EQ(entt::any_cast<fat>(&lhs), nullptr);
  789. ASSERT_EQ(entt::any_cast<double>(&rhs), nullptr);
  790. ASSERT_EQ(entt::any_cast<fat>(rhs), (fat{.1, .2, .3, .4}));
  791. std::swap(lhs, rhs);
  792. ASSERT_FALSE(rhs);
  793. ASSERT_TRUE(rhs.owner());
  794. ASSERT_EQ(lhs.type(), entt::type_id<fat>());
  795. ASSERT_EQ(entt::any_cast<double>(&lhs), nullptr);
  796. ASSERT_EQ(entt::any_cast<fat>(&rhs), nullptr);
  797. ASSERT_EQ(entt::any_cast<fat>(lhs), (fat{.1, .2, .3, .4}));
  798. }
  799. TEST_F(Any, NoSBOWithVoidSwap) {
  800. entt::any lhs{fat{.1, .2, .3, .4}};
  801. entt::any rhs{std::in_place_type<void>};
  802. std::swap(lhs, rhs);
  803. ASSERT_FALSE(lhs);
  804. ASSERT_TRUE(lhs.owner());
  805. ASSERT_EQ(rhs.type(), entt::type_id<fat>());
  806. ASSERT_EQ(entt::any_cast<fat>(&lhs), nullptr);
  807. ASSERT_EQ(entt::any_cast<double>(&rhs), nullptr);
  808. ASSERT_EQ(entt::any_cast<fat>(rhs), (fat{.1, .2, .3, .4}));
  809. std::swap(lhs, rhs);
  810. ASSERT_FALSE(rhs);
  811. ASSERT_TRUE(rhs.owner());
  812. ASSERT_EQ(lhs.type(), entt::type_id<fat>());
  813. ASSERT_EQ(entt::any_cast<double>(&lhs), nullptr);
  814. ASSERT_EQ(entt::any_cast<fat>(&rhs), nullptr);
  815. ASSERT_EQ(entt::any_cast<fat>(lhs), (fat{.1, .2, .3, .4}));
  816. }
  817. TEST_F(Any, AsRef) {
  818. entt::any any{42};
  819. auto ref = any.as_ref();
  820. auto cref = std::as_const(any).as_ref();
  821. ASSERT_FALSE(ref.owner());
  822. ASSERT_FALSE(cref.owner());
  823. ASSERT_EQ(entt::any_cast<int>(&any), any.data());
  824. ASSERT_EQ(entt::any_cast<int>(&ref), any.data());
  825. ASSERT_EQ(entt::any_cast<int>(&cref), nullptr);
  826. ASSERT_EQ(entt::any_cast<const int>(&any), any.data());
  827. ASSERT_EQ(entt::any_cast<const int>(&ref), any.data());
  828. ASSERT_EQ(entt::any_cast<const int>(&cref), any.data());
  829. ASSERT_EQ(entt::any_cast<int>(any), 42);
  830. ASSERT_EQ(entt::any_cast<int>(ref), 42);
  831. ASSERT_EQ(entt::any_cast<int>(cref), 42);
  832. ASSERT_EQ(entt::any_cast<const int>(any), 42);
  833. ASSERT_EQ(entt::any_cast<const int>(ref), 42);
  834. ASSERT_EQ(entt::any_cast<const int>(cref), 42);
  835. ASSERT_EQ(entt::any_cast<int &>(any), 42);
  836. ASSERT_EQ(entt::any_cast<const int &>(any), 42);
  837. ASSERT_EQ(entt::any_cast<int &>(ref), 42);
  838. ASSERT_EQ(entt::any_cast<const int &>(ref), 42);
  839. ASSERT_EQ(entt::any_cast<int>(&cref), nullptr);
  840. ASSERT_EQ(entt::any_cast<const int &>(cref), 42);
  841. entt::any_cast<int &>(any) = 3;
  842. ASSERT_EQ(entt::any_cast<int>(any), 3);
  843. ASSERT_EQ(entt::any_cast<int>(ref), 3);
  844. ASSERT_EQ(entt::any_cast<int>(cref), 3);
  845. std::swap(ref, cref);
  846. ASSERT_FALSE(ref.owner());
  847. ASSERT_FALSE(cref.owner());
  848. ASSERT_EQ(entt::any_cast<int>(&ref), nullptr);
  849. ASSERT_EQ(entt::any_cast<int>(&cref), any.data());
  850. ref = ref.as_ref();
  851. cref = std::as_const(cref).as_ref();
  852. ASSERT_FALSE(ref.owner());
  853. ASSERT_FALSE(cref.owner());
  854. ASSERT_EQ(entt::any_cast<int>(&ref), nullptr);
  855. ASSERT_EQ(entt::any_cast<int>(&cref), nullptr);
  856. ASSERT_EQ(entt::any_cast<const int>(&ref), any.data());
  857. ASSERT_EQ(entt::any_cast<const int>(&cref), any.data());
  858. ASSERT_EQ(entt::any_cast<int>(&ref), nullptr);
  859. ASSERT_EQ(entt::any_cast<int>(&cref), nullptr);
  860. ASSERT_EQ(entt::any_cast<const int &>(ref), 3);
  861. ASSERT_EQ(entt::any_cast<const int &>(cref), 3);
  862. ref = 42;
  863. cref = 42;
  864. ASSERT_TRUE(ref.owner());
  865. ASSERT_TRUE(cref.owner());
  866. ASSERT_NE(entt::any_cast<int>(&ref), nullptr);
  867. ASSERT_NE(entt::any_cast<int>(&cref), nullptr);
  868. ASSERT_EQ(entt::any_cast<int &>(ref), 42);
  869. ASSERT_EQ(entt::any_cast<int &>(cref), 42);
  870. ASSERT_EQ(entt::any_cast<const int &>(ref), 42);
  871. ASSERT_EQ(entt::any_cast<const int &>(cref), 42);
  872. ASSERT_NE(entt::any_cast<int>(&ref), any.data());
  873. ASSERT_NE(entt::any_cast<int>(&cref), any.data());
  874. }
  875. TEST_F(Any, Comparable) {
  876. auto test = [](entt::any any, entt::any other) {
  877. ASSERT_EQ(any, any);
  878. ASSERT_NE(other, any);
  879. ASSERT_NE(any, entt::any{});
  880. ASSERT_TRUE(any == any);
  881. ASSERT_FALSE(other == any);
  882. ASSERT_TRUE(any != other);
  883. ASSERT_TRUE(entt::any{} != any);
  884. };
  885. int value = 42;
  886. test('c', 'a');
  887. test(fat{.1, .2, .3, .4}, fat{.0, .1, .2, .3});
  888. test(entt::forward_as_any(value), 3);
  889. test(3, entt::make_any<const int &>(value));
  890. test('c', value);
  891. }
  892. TEST_F(Any, NotComparable) {
  893. auto test = [](const auto &instance) {
  894. auto any = entt::forward_as_any(instance);
  895. ASSERT_EQ(any, any);
  896. ASSERT_NE(any, entt::any{instance});
  897. ASSERT_NE(entt::any{}, any);
  898. ASSERT_TRUE(any == any);
  899. ASSERT_FALSE(any == entt::any{instance});
  900. ASSERT_TRUE(entt::any{} != any);
  901. };
  902. test(not_comparable{});
  903. test(std::unordered_map<int, not_comparable>{});
  904. test(std::vector<not_comparable>{});
  905. }
  906. TEST_F(Any, CompareVoid) {
  907. entt::any any{std::in_place_type<void>};
  908. ASSERT_EQ(any, any);
  909. ASSERT_EQ(any, entt::any{std::in_place_type<void>});
  910. ASSERT_NE(entt::any{'a'}, any);
  911. ASSERT_EQ(any, entt::any{});
  912. ASSERT_TRUE(any == any);
  913. ASSERT_TRUE(any == entt::any{std::in_place_type<void>});
  914. ASSERT_FALSE(entt::any{'a'} == any);
  915. ASSERT_TRUE(any != entt::any{'a'});
  916. ASSERT_FALSE(entt::any{} != any);
  917. }
  918. TEST_F(Any, AnyCast) {
  919. entt::any any{42};
  920. const auto &cany = any;
  921. ASSERT_EQ(entt::any_cast<char>(&any), nullptr);
  922. ASSERT_EQ(entt::any_cast<char>(&cany), nullptr);
  923. ASSERT_EQ(*entt::any_cast<int>(&any), 42);
  924. ASSERT_EQ(*entt::any_cast<int>(&cany), 42);
  925. ASSERT_EQ(entt::any_cast<int &>(any), 42);
  926. ASSERT_EQ(entt::any_cast<const int &>(cany), 42);
  927. not_copyable instance{};
  928. instance.payload = 42.;
  929. entt::any ref{entt::forward_as_any(instance)};
  930. entt::any cref{entt::forward_as_any(std::as_const(instance).payload)};
  931. ASSERT_EQ(entt::any_cast<not_copyable>(std::move(ref)).payload, 42.);
  932. ASSERT_EQ(entt::any_cast<double>(std::move(cref)), 42.);
  933. ASSERT_EQ(entt::any_cast<int>(entt::any{42}), 42);
  934. }
  935. ENTT_DEBUG_TEST_F(AnyDeathTest, AnyCast) {
  936. entt::any any{42};
  937. const auto &cany = any;
  938. ASSERT_DEATH(entt::any_cast<double &>(any), "");
  939. ASSERT_DEATH(entt::any_cast<const double &>(cany), "");
  940. not_copyable instance{};
  941. instance.payload = 42.;
  942. entt::any ref{entt::forward_as_any(instance)};
  943. entt::any cref{entt::forward_as_any(std::as_const(instance).payload)};
  944. ASSERT_DEATH(entt::any_cast<not_copyable>(std::as_const(ref).as_ref()), "");
  945. ASSERT_DEATH(entt::any_cast<double>(entt::any{42}), "");
  946. }
  947. TEST_F(Any, MakeAny) {
  948. int value = 42;
  949. auto any = entt::make_any<int>(value);
  950. auto ext = entt::make_any<int, sizeof(int), alignof(int)>(value);
  951. auto ref = entt::make_any<int &>(value);
  952. ASSERT_TRUE(any);
  953. ASSERT_TRUE(ext);
  954. ASSERT_TRUE(ref);
  955. ASSERT_TRUE(any.owner());
  956. ASSERT_TRUE(ext.owner());
  957. ASSERT_FALSE(ref.owner());
  958. ASSERT_EQ(entt::any_cast<const int &>(any), 42);
  959. ASSERT_EQ(entt::any_cast<const int &>(ext), 42);
  960. ASSERT_EQ(entt::any_cast<const int &>(ref), 42);
  961. ASSERT_EQ(decltype(any)::length, entt::any::length);
  962. ASSERT_NE(decltype(ext)::length, entt::any::length);
  963. ASSERT_EQ(decltype(ref)::length, entt::any::length);
  964. ASSERT_NE(any.data(), &value);
  965. ASSERT_NE(ext.data(), &value);
  966. ASSERT_EQ(ref.data(), &value);
  967. }
  968. TEST_F(Any, ForwardAsAny) {
  969. int value = 42;
  970. auto any = entt::forward_as_any(std::move(value));
  971. auto ref = entt::forward_as_any(value);
  972. auto cref = entt::forward_as_any(std::as_const(value));
  973. ASSERT_TRUE(any);
  974. ASSERT_TRUE(ref);
  975. ASSERT_TRUE(cref);
  976. ASSERT_TRUE(any.owner());
  977. ASSERT_FALSE(ref.owner());
  978. ASSERT_FALSE(cref.owner());
  979. ASSERT_NE(entt::any_cast<int>(&any), nullptr);
  980. ASSERT_NE(entt::any_cast<int>(&ref), nullptr);
  981. ASSERT_EQ(entt::any_cast<int>(&cref), nullptr);
  982. ASSERT_EQ(entt::any_cast<const int &>(any), 42);
  983. ASSERT_EQ(entt::any_cast<const int &>(ref), 42);
  984. ASSERT_EQ(entt::any_cast<const int &>(cref), 42);
  985. ASSERT_NE(any.data(), &value);
  986. ASSERT_EQ(ref.data(), &value);
  987. }
  988. TEST_F(Any, NotCopyableType) {
  989. const not_copyable value{};
  990. entt::any any{std::in_place_type<not_copyable>};
  991. entt::any other = entt::forward_as_any(value);
  992. ASSERT_TRUE(any);
  993. ASSERT_TRUE(other);
  994. ASSERT_TRUE(any.owner());
  995. ASSERT_FALSE(other.owner());
  996. ASSERT_EQ(any.type(), other.type());
  997. ASSERT_FALSE(any.assign(other));
  998. ASSERT_FALSE(any.assign(std::move(other)));
  999. entt::any copy{any};
  1000. ASSERT_TRUE(any);
  1001. ASSERT_FALSE(copy);
  1002. ASSERT_TRUE(any.owner());
  1003. ASSERT_TRUE(copy.owner());
  1004. copy = any;
  1005. ASSERT_TRUE(any);
  1006. ASSERT_FALSE(copy);
  1007. ASSERT_TRUE(any.owner());
  1008. ASSERT_TRUE(copy.owner());
  1009. }
  1010. TEST_F(Any, NotCopyableValueType) {
  1011. std::vector<entt::any> vec{};
  1012. vec.emplace_back(std::in_place_type<not_copyable>);
  1013. vec.shrink_to_fit();
  1014. ASSERT_EQ(vec.size(), 1u);
  1015. ASSERT_EQ(vec.capacity(), 1u);
  1016. ASSERT_TRUE(vec[0u]);
  1017. // strong exception guarantee due to noexcept move ctor
  1018. vec.emplace_back(std::in_place_type<not_copyable>);
  1019. ASSERT_EQ(vec.size(), 2u);
  1020. ASSERT_TRUE(vec[0u]);
  1021. ASSERT_TRUE(vec[1u]);
  1022. }
  1023. TEST_F(Any, NotMovableType) {
  1024. entt::any any{std::in_place_type<not_movable>};
  1025. entt::any other{std::in_place_type<not_movable>};
  1026. ASSERT_TRUE(any);
  1027. ASSERT_TRUE(other);
  1028. ASSERT_TRUE(any.owner());
  1029. ASSERT_TRUE(other.owner());
  1030. ASSERT_EQ(any.type(), other.type());
  1031. ASSERT_TRUE(any.assign(other));
  1032. ASSERT_TRUE(any.assign(std::move(other)));
  1033. entt::any copy{any};
  1034. ASSERT_TRUE(any);
  1035. ASSERT_TRUE(copy);
  1036. ASSERT_TRUE(any.owner());
  1037. ASSERT_TRUE(copy.owner());
  1038. copy = any;
  1039. ASSERT_TRUE(any);
  1040. ASSERT_TRUE(copy);
  1041. ASSERT_TRUE(any.owner());
  1042. ASSERT_TRUE(copy.owner());
  1043. }
  1044. TEST_F(Any, Array) {
  1045. entt::any any{std::in_place_type<int[1]>};
  1046. entt::any copy{any};
  1047. ASSERT_TRUE(any);
  1048. ASSERT_FALSE(copy);
  1049. ASSERT_EQ(any.type(), entt::type_id<int[1]>());
  1050. ASSERT_NE(entt::any_cast<int[1]>(&any), nullptr);
  1051. ASSERT_EQ(entt::any_cast<int[2]>(&any), nullptr);
  1052. ASSERT_EQ(entt::any_cast<int *>(&any), nullptr);
  1053. entt::any_cast<int(&)[1]>(any)[0] = 42;
  1054. ASSERT_EQ(entt::any_cast<const int(&)[1]>(std::as_const(any))[0], 42);
  1055. }
  1056. TEST_F(Any, CopyMoveReference) {
  1057. int value{};
  1058. auto test = [&](auto &&ref) {
  1059. value = 3;
  1060. auto any = entt::forward_as_any(ref);
  1061. entt::any move = std::move(any);
  1062. entt::any copy = move;
  1063. ASSERT_TRUE(any);
  1064. ASSERT_TRUE(move);
  1065. ASSERT_TRUE(copy);
  1066. ASSERT_FALSE(any.owner());
  1067. ASSERT_FALSE(move.owner());
  1068. ASSERT_TRUE(copy.owner());
  1069. ASSERT_EQ(move.type(), entt::type_id<int>());
  1070. ASSERT_EQ(copy.type(), entt::type_id<int>());
  1071. ASSERT_EQ(std::as_const(move).data(), &value);
  1072. ASSERT_NE(std::as_const(copy).data(), &value);
  1073. ASSERT_EQ(entt::any_cast<int>(move), 3);
  1074. ASSERT_EQ(entt::any_cast<int>(copy), 3);
  1075. value = 42;
  1076. ASSERT_EQ(entt::any_cast<const int &>(move), 42);
  1077. ASSERT_EQ(entt::any_cast<const int &>(copy), 3);
  1078. };
  1079. test(value);
  1080. test(std::as_const(value));
  1081. }
  1082. TEST_F(Any, SBOVsZeroedSBOSize) {
  1083. entt::any sbo{42};
  1084. const auto *broken = sbo.data();
  1085. entt::any other = std::move(sbo);
  1086. ASSERT_NE(broken, other.data());
  1087. entt::basic_any<0u> dyn{42};
  1088. const auto *valid = dyn.data();
  1089. entt::basic_any<0u> same = std::move(dyn);
  1090. ASSERT_EQ(valid, same.data());
  1091. }
  1092. TEST_F(Any, SboAlignment) {
  1093. static constexpr auto alignment = alignof(over_aligned);
  1094. entt::basic_any<alignment, alignment> sbo[2] = {over_aligned{}, over_aligned{}};
  1095. const auto *data = sbo[0].data();
  1096. ASSERT_TRUE((reinterpret_cast<std::uintptr_t>(sbo[0u].data()) % alignment) == 0u);
  1097. ASSERT_TRUE((reinterpret_cast<std::uintptr_t>(sbo[1u].data()) % alignment) == 0u);
  1098. std::swap(sbo[0], sbo[1]);
  1099. ASSERT_TRUE((reinterpret_cast<std::uintptr_t>(sbo[0u].data()) % alignment) == 0u);
  1100. ASSERT_TRUE((reinterpret_cast<std::uintptr_t>(sbo[1u].data()) % alignment) == 0u);
  1101. ASSERT_NE(data, sbo[1].data());
  1102. }
  1103. TEST_F(Any, NoSboAlignment) {
  1104. static constexpr auto alignment = alignof(over_aligned);
  1105. entt::basic_any<alignment> nosbo[2] = {over_aligned{}, over_aligned{}};
  1106. const auto *data = nosbo[0].data();
  1107. ASSERT_TRUE((reinterpret_cast<std::uintptr_t>(nosbo[0u].data()) % alignment) == 0u);
  1108. ASSERT_TRUE((reinterpret_cast<std::uintptr_t>(nosbo[1u].data()) % alignment) == 0u);
  1109. std::swap(nosbo[0], nosbo[1]);
  1110. ASSERT_TRUE((reinterpret_cast<std::uintptr_t>(nosbo[0u].data()) % alignment) == 0u);
  1111. ASSERT_TRUE((reinterpret_cast<std::uintptr_t>(nosbo[1u].data()) % alignment) == 0u);
  1112. ASSERT_EQ(data, nosbo[1].data());
  1113. }
  1114. TEST_F(Any, AggregatesMustWork) {
  1115. struct aggregate_type {
  1116. int value;
  1117. };
  1118. // the goal of this test is to enforce the requirements for aggregate types
  1119. entt::any{std::in_place_type<aggregate_type>, 42}.emplace<aggregate_type>(42);
  1120. }
  1121. TEST_F(Any, DeducedArrayType) {
  1122. entt::any any{"array of char"};
  1123. ASSERT_TRUE(any);
  1124. ASSERT_EQ(any.type(), entt::type_id<const char *>());
  1125. ASSERT_EQ((std::strcmp("array of char", entt::any_cast<const char *>(any))), 0);
  1126. any = "another array of char";
  1127. ASSERT_TRUE(any);
  1128. ASSERT_EQ(any.type(), entt::type_id<const char *>());
  1129. ASSERT_EQ((std::strcmp("another array of char", entt::any_cast<const char *>(any))), 0);
  1130. }