poly.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. #include <cstddef>
  2. #include <cstdint>
  3. #include <utility>
  4. #include <gtest/gtest.h>
  5. #include <entt/core/type_info.hpp>
  6. #include <entt/core/type_traits.hpp>
  7. #include <entt/poly/poly.hpp>
  8. #include "../common/config.h"
  9. #include "../common/linter.hpp"
  10. template<typename Base>
  11. struct common_type: Base {
  12. void incr() {
  13. entt::poly_call<0>(*this);
  14. }
  15. void set(int v) {
  16. entt::poly_call<1>(*this, v);
  17. }
  18. [[nodiscard]] int get() const {
  19. return static_cast<int>(entt::poly_call<2>(*this));
  20. }
  21. void decr() {
  22. entt::poly_call<3>(*this);
  23. }
  24. [[nodiscard]] int mul(int v) const {
  25. return static_cast<int>(entt::poly_call<4>(*this, v));
  26. }
  27. [[nodiscard]] int rand() const {
  28. return static_cast<int>(entt::poly_call<5>(*this)); // NOLINT
  29. }
  30. };
  31. template<typename Type>
  32. struct common_members {
  33. static void decr(Type &self) {
  34. self.set(self.get() - 1);
  35. }
  36. [[nodiscard]] static double mul(const Type &self, double v) {
  37. return v * self.get();
  38. }
  39. };
  40. namespace {
  41. [[nodiscard]] int absolutely_random() {
  42. return 4;
  43. }
  44. } // namespace
  45. template<typename Type>
  46. using common_impl = entt::value_list<
  47. &Type::incr,
  48. &Type::set,
  49. &Type::get,
  50. &common_members<Type>::decr,
  51. &common_members<Type>::mul,
  52. &absolutely_random>;
  53. struct Deduced
  54. : entt::type_list<> {
  55. template<typename Base>
  56. using type = common_type<Base>;
  57. template<typename Type>
  58. using members = common_members<Type>;
  59. template<typename Type>
  60. using impl = common_impl<Type>;
  61. };
  62. struct Defined
  63. : entt::type_list<
  64. void(),
  65. void(int),
  66. int() const,
  67. void(),
  68. int(int) const,
  69. int() const> {
  70. template<typename Base>
  71. using type = common_type<Base>;
  72. template<typename Type>
  73. using members = common_members<Type>;
  74. template<typename Type>
  75. using impl = common_impl<Type>;
  76. };
  77. struct DeducedEmbedded
  78. : entt::type_list<> {
  79. template<typename Base>
  80. struct type: Base {
  81. [[nodiscard]] int get() const {
  82. return entt::poly_call<0>(*this);
  83. }
  84. };
  85. template<typename Type>
  86. using impl = entt::value_list<&Type::get>;
  87. };
  88. struct DefinedEmbedded
  89. : entt::type_list<int()> {
  90. template<typename Base>
  91. struct type: Base {
  92. // non-const get on purpose
  93. [[nodiscard]] int get() {
  94. return entt::poly_call<0>(*this);
  95. }
  96. };
  97. template<typename Type>
  98. using impl = entt::value_list<&Type::get>;
  99. };
  100. struct impl {
  101. impl() = default;
  102. impl(int v)
  103. : value{v} {}
  104. void incr() {
  105. ++value;
  106. }
  107. void set(int v) {
  108. value = v;
  109. }
  110. [[nodiscard]] int get() const {
  111. return value;
  112. }
  113. int value{};
  114. };
  115. struct alignas(64u) over_aligned: impl {};
  116. template<typename Type>
  117. struct Poly: testing::Test {
  118. template<std::size_t... Args>
  119. using type = entt::basic_poly<Type, Args...>;
  120. };
  121. template<typename Type>
  122. using PolyDeathTest = Poly<Type>;
  123. using PolyTypes = ::testing::Types<Deduced, Defined>;
  124. TYPED_TEST_SUITE(Poly, PolyTypes, );
  125. TYPED_TEST_SUITE(PolyDeathTest, PolyTypes, );
  126. template<typename Type>
  127. struct PolyEmbedded: testing::Test {
  128. using type = entt::basic_poly<Type>;
  129. };
  130. using PolyEmbeddedTypes = ::testing::Types<DeducedEmbedded, DefinedEmbedded>;
  131. TYPED_TEST_SUITE(PolyEmbedded, PolyEmbeddedTypes, );
  132. TYPED_TEST(Poly, Functionalities) {
  133. using poly_type = typename TestFixture::template type<>;
  134. impl instance{};
  135. poly_type empty{};
  136. poly_type in_place{std::in_place_type<impl>, 3};
  137. poly_type alias{std::in_place_type<impl &>, instance};
  138. poly_type value{impl{}};
  139. ASSERT_FALSE(empty);
  140. ASSERT_TRUE(in_place);
  141. ASSERT_TRUE(alias);
  142. ASSERT_TRUE(value);
  143. ASSERT_EQ(empty.type(), entt::type_id<void>());
  144. ASSERT_EQ(in_place.type(), entt::type_id<impl>());
  145. ASSERT_EQ(alias.type(), entt::type_id<impl>());
  146. ASSERT_EQ(value.type(), entt::type_id<impl>());
  147. ASSERT_EQ(alias.data(), &instance);
  148. ASSERT_EQ(std::as_const(alias).data(), &instance);
  149. ASSERT_EQ(value->rand(), 4);
  150. empty = impl{};
  151. ASSERT_TRUE(empty);
  152. ASSERT_NE(empty.data(), nullptr);
  153. ASSERT_NE(std::as_const(empty).data(), nullptr);
  154. ASSERT_EQ(empty.type(), entt::type_id<impl>());
  155. ASSERT_EQ(empty->get(), 0);
  156. empty.template emplace<impl>(3);
  157. ASSERT_TRUE(empty);
  158. ASSERT_EQ(std::as_const(empty)->get(), 3);
  159. poly_type ref = in_place.as_ref();
  160. ASSERT_TRUE(ref);
  161. ASSERT_NE(ref.data(), nullptr);
  162. ASSERT_EQ(ref.data(), in_place.data());
  163. ASSERT_EQ(std::as_const(ref).data(), std::as_const(in_place).data());
  164. ASSERT_EQ(ref.type(), entt::type_id<impl>());
  165. ASSERT_EQ(ref->get(), 3);
  166. poly_type null{};
  167. std::swap(empty, null);
  168. ASSERT_FALSE(empty);
  169. poly_type copy = in_place;
  170. ASSERT_TRUE(copy);
  171. ASSERT_EQ(copy->get(), 3);
  172. poly_type move = std::move(copy);
  173. test::is_initialized(copy);
  174. ASSERT_TRUE(move);
  175. ASSERT_TRUE(copy);
  176. ASSERT_EQ(move->get(), 3);
  177. move.reset();
  178. ASSERT_FALSE(move);
  179. ASSERT_EQ(move.type(), entt::type_id<void>());
  180. }
  181. TYPED_TEST(PolyEmbedded, EmbeddedVtable) {
  182. using poly_type = typename TestFixture::type;
  183. poly_type poly{impl{}};
  184. auto *ptr = static_cast<impl *>(poly.data());
  185. ASSERT_TRUE(poly);
  186. ASSERT_NE(poly.data(), nullptr);
  187. ASSERT_NE(std::as_const(poly).data(), nullptr);
  188. ASSERT_EQ(poly->get(), 0);
  189. ptr->value = 2;
  190. ASSERT_EQ(poly->get(), 2);
  191. }
  192. TYPED_TEST(Poly, Owned) {
  193. using poly_type = typename TestFixture::template type<>;
  194. poly_type poly{impl{}};
  195. auto *ptr = static_cast<impl *>(poly.data());
  196. ASSERT_TRUE(poly);
  197. ASSERT_NE(poly.data(), nullptr);
  198. ASSERT_NE(std::as_const(poly).data(), nullptr);
  199. ASSERT_EQ(ptr->value, 0);
  200. ASSERT_EQ(poly->get(), 0);
  201. poly->set(1);
  202. poly->incr();
  203. ASSERT_EQ(ptr->value, 2);
  204. ASSERT_EQ(std::as_const(poly)->get(), 2);
  205. ASSERT_EQ(poly->mul(3), 6);
  206. poly->decr();
  207. ASSERT_EQ(ptr->value, 1);
  208. ASSERT_EQ(poly->get(), 1);
  209. ASSERT_EQ(poly->mul(3), 3);
  210. }
  211. TYPED_TEST(Poly, Reference) {
  212. using poly_type = typename TestFixture::template type<>;
  213. impl instance{};
  214. poly_type poly{std::in_place_type<impl &>, instance};
  215. ASSERT_TRUE(poly);
  216. ASSERT_NE(poly.data(), nullptr);
  217. ASSERT_NE(std::as_const(poly).data(), nullptr);
  218. ASSERT_EQ(instance.value, 0);
  219. ASSERT_EQ(poly->get(), 0);
  220. poly->set(1);
  221. poly->incr();
  222. ASSERT_EQ(instance.value, 2);
  223. ASSERT_EQ(std::as_const(poly)->get(), 2);
  224. ASSERT_EQ(poly->mul(3), 6);
  225. poly->decr();
  226. ASSERT_EQ(instance.value, 1);
  227. ASSERT_EQ(poly->get(), 1);
  228. ASSERT_EQ(poly->mul(3), 3);
  229. }
  230. TYPED_TEST(Poly, ConstReference) {
  231. using poly_type = typename TestFixture::template type<>;
  232. impl instance{};
  233. poly_type poly{std::in_place_type<const impl &>, instance};
  234. ASSERT_TRUE(poly);
  235. ASSERT_EQ(poly.data(), nullptr);
  236. ASSERT_NE(std::as_const(poly).data(), nullptr);
  237. ASSERT_EQ(instance.value, 0);
  238. ASSERT_EQ(poly->get(), 0);
  239. ASSERT_EQ(instance.value, 0);
  240. ASSERT_EQ(std::as_const(poly)->get(), 0);
  241. ASSERT_EQ(poly->mul(3), 0);
  242. ASSERT_EQ(instance.value, 0);
  243. ASSERT_EQ(poly->get(), 0);
  244. ASSERT_EQ(poly->mul(3), 0);
  245. }
  246. ENTT_DEBUG_TYPED_TEST(PolyDeathTest, ConstReference) {
  247. using poly_type = typename TestFixture::template type<>;
  248. impl instance{};
  249. poly_type poly{std::in_place_type<const impl &>, instance};
  250. ASSERT_TRUE(poly);
  251. ASSERT_DEATH(poly->set(1), "");
  252. }
  253. TYPED_TEST(Poly, AsRef) {
  254. using poly_type = typename TestFixture::template type<>;
  255. poly_type poly{impl{}};
  256. auto ref = poly.as_ref();
  257. auto cref = std::as_const(poly).as_ref();
  258. ASSERT_NE(poly.data(), nullptr);
  259. ASSERT_NE(ref.data(), nullptr);
  260. ASSERT_EQ(cref.data(), nullptr);
  261. ASSERT_NE(std::as_const(cref).data(), nullptr);
  262. std::swap(ref, cref);
  263. ASSERT_EQ(ref.data(), nullptr);
  264. ASSERT_NE(std::as_const(ref).data(), nullptr);
  265. ASSERT_NE(cref.data(), nullptr);
  266. ref = ref.as_ref();
  267. cref = std::as_const(cref).as_ref();
  268. ASSERT_EQ(ref.data(), nullptr);
  269. ASSERT_NE(std::as_const(ref).data(), nullptr);
  270. ASSERT_EQ(cref.data(), nullptr);
  271. ASSERT_NE(std::as_const(cref).data(), nullptr);
  272. ref = impl{};
  273. cref = impl{};
  274. ASSERT_NE(ref.data(), nullptr);
  275. ASSERT_NE(cref.data(), nullptr);
  276. }
  277. TYPED_TEST(Poly, SBOVsZeroedSBOSize) {
  278. using poly_type = typename TestFixture::template type<>;
  279. using zeroed_type = typename TestFixture::template type<0u>;
  280. poly_type poly{impl{}};
  281. const auto broken = poly.data();
  282. poly_type other = std::move(poly);
  283. ASSERT_NE(broken, other.data());
  284. zeroed_type dyn{impl{}};
  285. const auto valid = dyn.data();
  286. zeroed_type same = std::move(dyn);
  287. ASSERT_EQ(valid, same.data());
  288. // everything works as expected
  289. same->incr();
  290. ASSERT_EQ(same->get(), 1);
  291. }
  292. TYPED_TEST(Poly, SboAlignment) {
  293. constexpr auto alignment = alignof(over_aligned);
  294. using poly_type = typename TestFixture::template type<alignment, alignment>;
  295. poly_type sbo[2]{over_aligned{}, over_aligned{}}; // NOLINT
  296. const auto *data = sbo[0].data();
  297. // NOLINTBEGIN(*-reinterpret-cast)
  298. ASSERT_TRUE((reinterpret_cast<std::uintptr_t>(sbo[0u].data()) % alignment) == 0u);
  299. ASSERT_TRUE((reinterpret_cast<std::uintptr_t>(sbo[1u].data()) % alignment) == 0u);
  300. // NOLINTEND(*-reinterpret-cast)
  301. std::swap(sbo[0], sbo[1]);
  302. // NOLINTBEGIN(*-reinterpret-cast)
  303. ASSERT_TRUE((reinterpret_cast<std::uintptr_t>(sbo[0u].data()) % alignment) == 0u);
  304. ASSERT_TRUE((reinterpret_cast<std::uintptr_t>(sbo[1u].data()) % alignment) == 0u);
  305. // NOLINTEND(*-reinterpret-cast)
  306. ASSERT_NE(data, sbo[1].data());
  307. }
  308. TYPED_TEST(Poly, NoSboAlignment) {
  309. constexpr auto alignment = alignof(over_aligned);
  310. using poly_type = typename TestFixture::template type<alignment>;
  311. poly_type nosbo[2]{over_aligned{}, over_aligned{}}; // NOLINT
  312. const auto *data = nosbo[0].data();
  313. // NOLINTBEGIN(*-reinterpret-cast)
  314. ASSERT_TRUE((reinterpret_cast<std::uintptr_t>(nosbo[0u].data()) % alignment) == 0u);
  315. ASSERT_TRUE((reinterpret_cast<std::uintptr_t>(nosbo[1u].data()) % alignment) == 0u);
  316. // NOLINTEND(*-reinterpret-cast)
  317. std::swap(nosbo[0], nosbo[1]);
  318. // NOLINTBEGIN(*-reinterpret-cast)
  319. ASSERT_TRUE((reinterpret_cast<std::uintptr_t>(nosbo[0u].data()) % alignment) == 0u);
  320. ASSERT_TRUE((reinterpret_cast<std::uintptr_t>(nosbo[1u].data()) % alignment) == 0u);
  321. // NOLINTEND(*-reinterpret-cast)
  322. ASSERT_EQ(data, nosbo[1].data());
  323. }