container.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. #ifndef ENTT_META_CONTAINER_HPP
  2. #define ENTT_META_CONTAINER_HPP
  3. #include <array>
  4. #include <deque>
  5. #include <iterator>
  6. #include <list>
  7. #include <map>
  8. #include <set>
  9. #include <type_traits>
  10. #include <unordered_map>
  11. #include <unordered_set>
  12. #include <vector>
  13. #include "../container/dense_map.hpp"
  14. #include "../container/dense_set.hpp"
  15. #include "context.hpp"
  16. #include "meta.hpp"
  17. #include "type_traits.hpp"
  18. namespace entt {
  19. /**
  20. * @cond TURN_OFF_DOXYGEN
  21. * Internal details not to be documented.
  22. */
  23. namespace internal {
  24. template<typename, typename = void>
  25. struct fixed_size_sequence_container: std::true_type {};
  26. template<typename Type>
  27. struct fixed_size_sequence_container<Type, std::void_t<decltype(&Type::clear)>>: std::false_type {};
  28. template<typename Type>
  29. inline constexpr bool fixed_size_sequence_container_v = fixed_size_sequence_container<Type>::value;
  30. template<typename, typename = void>
  31. struct key_only_associative_container: std::true_type {};
  32. template<typename Type>
  33. struct key_only_associative_container<Type, std::void_t<typename Type::mapped_type>>: std::false_type {};
  34. template<typename Type>
  35. inline constexpr bool key_only_associative_container_v = key_only_associative_container<Type>::value;
  36. template<typename, typename = void>
  37. struct reserve_aware_container: std::false_type {};
  38. template<typename Type>
  39. struct reserve_aware_container<Type, std::void_t<decltype(&Type::reserve)>>: std::true_type {};
  40. template<typename Type>
  41. inline constexpr bool reserve_aware_container_v = reserve_aware_container<Type>::value;
  42. } // namespace internal
  43. /**
  44. * Internal details not to be documented.
  45. * @endcond
  46. */
  47. /**
  48. * @brief General purpose implementation of meta sequence container traits.
  49. * @tparam Type Type of underlying sequence container.
  50. */
  51. template<typename Type>
  52. struct basic_meta_sequence_container_traits {
  53. static_assert(std::is_same_v<Type, std::remove_cv_t<std::remove_reference_t<Type>>>, "Unexpected type");
  54. /*! @brief True in case of key-only containers, false otherwise. */
  55. static constexpr bool fixed_size = internal::fixed_size_sequence_container_v<Type>;
  56. /*! @brief Unsigned integer type. */
  57. using size_type = typename meta_sequence_container::size_type;
  58. /*! @brief Meta iterator type. */
  59. using iterator = typename meta_sequence_container::iterator;
  60. /**
  61. * @brief Returns the number of elements in a container.
  62. * @param container Opaque pointer to a container of the given type.
  63. * @return Number of elements.
  64. */
  65. [[nodiscard]] static size_type size(const void *container) {
  66. return static_cast<const Type *>(container)->size();
  67. }
  68. /**
  69. * @brief Clears a container.
  70. * @param container Opaque pointer to a container of the given type.
  71. * @return True in case of success, false otherwise.
  72. */
  73. [[nodiscard]] static bool clear([[maybe_unused]] void *container) {
  74. if constexpr(fixed_size) {
  75. return false;
  76. } else {
  77. static_cast<Type *>(container)->clear();
  78. return true;
  79. }
  80. }
  81. /**
  82. * @brief Increases the capacity of a container.
  83. * @param container Opaque pointer to a container of the given type.
  84. * @param sz Desired capacity.
  85. * @return True in case of success, false otherwise.
  86. */
  87. [[nodiscard]] static bool reserve([[maybe_unused]] void *container, [[maybe_unused]] const size_type sz) {
  88. if constexpr(internal::reserve_aware_container_v<Type>) {
  89. static_cast<Type *>(container)->reserve(sz);
  90. return true;
  91. } else {
  92. return false;
  93. }
  94. }
  95. /**
  96. * @brief Resizes a container.
  97. * @param container Opaque pointer to a container of the given type.
  98. * @param sz The new number of elements.
  99. * @return True in case of success, false otherwise.
  100. */
  101. [[nodiscard]] static bool resize([[maybe_unused]] void *container, [[maybe_unused]] const size_type sz) {
  102. if constexpr(fixed_size || !std::is_default_constructible_v<typename Type::value_type>) {
  103. return false;
  104. } else {
  105. static_cast<Type *>(container)->resize(sz);
  106. return true;
  107. }
  108. }
  109. /**
  110. * @brief Returns a possibly const iterator to the beginning.
  111. * @param area The context to pass to the newly created iterator.
  112. * @param container Opaque pointer to a container of the given type.
  113. * @param as_const Const opaque pointer fallback.
  114. * @return An iterator to the first element of the container.
  115. */
  116. static iterator begin(const meta_ctx &area, void *container, const void *as_const) {
  117. return container ? iterator{area, static_cast<Type *>(container)->begin()}
  118. : iterator{area, static_cast<const Type *>(as_const)->begin()};
  119. }
  120. /**
  121. * @brief Returns a possibly const iterator to the end.
  122. * @param area The context to pass to the newly created iterator.
  123. * @param container Opaque pointer to a container of the given type.
  124. * @param as_const Const opaque pointer fallback.
  125. * @return An iterator that is past the last element of the container.
  126. */
  127. static iterator end(const meta_ctx &area, void *container, const void *as_const) {
  128. return container ? iterator{area, static_cast<Type *>(container)->end()}
  129. : iterator{area, static_cast<const Type *>(as_const)->end()};
  130. }
  131. /**
  132. * @brief Assigns one element to a container and constructs its object from
  133. * a given opaque instance.
  134. * @param area The context to pass to the newly created iterator.
  135. * @param container Opaque pointer to a container of the given type.
  136. * @param value Optional opaque instance of the object to construct (as
  137. * value type).
  138. * @param cref Optional opaque instance of the object to construct (as
  139. * decayed const reference type).
  140. * @param it Iterator before which the element will be inserted.
  141. * @return A possibly invalid iterator to the inserted element.
  142. */
  143. [[nodiscard]] static iterator insert(const meta_ctx &area, [[maybe_unused]] void *container, [[maybe_unused]] const void *value, [[maybe_unused]] const void *cref, [[maybe_unused]] const iterator &it) {
  144. if constexpr(fixed_size) {
  145. return iterator{area};
  146. } else {
  147. auto *const non_const = any_cast<typename Type::iterator>(&it.base());
  148. return {area, static_cast<Type *>(container)->insert(
  149. non_const ? *non_const : any_cast<const typename Type::const_iterator &>(it.base()),
  150. value ? *static_cast<const typename Type::value_type *>(value) : *static_cast<const std::remove_reference_t<typename Type::const_reference> *>(cref))};
  151. }
  152. }
  153. /**
  154. * @brief Erases an element from a container.
  155. * @param area The context to pass to the newly created iterator.
  156. * @param container Opaque pointer to a container of the given type.
  157. * @param it An opaque iterator to the element to erase.
  158. * @return A possibly invalid iterator following the last removed element.
  159. */
  160. [[nodiscard]] static iterator erase(const meta_ctx &area, [[maybe_unused]] void *container, [[maybe_unused]] const iterator &it) {
  161. if constexpr(fixed_size) {
  162. return iterator{area};
  163. } else {
  164. auto *const non_const = any_cast<typename Type::iterator>(&it.base());
  165. return {area, static_cast<Type *>(container)->erase(non_const ? *non_const : any_cast<const typename Type::const_iterator &>(it.base()))};
  166. }
  167. }
  168. };
  169. /**
  170. * @brief General purpose implementation of meta associative container traits.
  171. * @tparam Type Type of underlying associative container.
  172. */
  173. template<typename Type>
  174. struct basic_meta_associative_container_traits {
  175. static_assert(std::is_same_v<Type, std::remove_cv_t<std::remove_reference_t<Type>>>, "Unexpected type");
  176. /*! @brief True in case of key-only containers, false otherwise. */
  177. static constexpr bool key_only = internal::key_only_associative_container_v<Type>;
  178. /*! @brief Unsigned integer type. */
  179. using size_type = typename meta_associative_container::size_type;
  180. /*! @brief Meta iterator type. */
  181. using iterator = typename meta_associative_container::iterator;
  182. /**
  183. * @brief Returns the number of elements in a container.
  184. * @param container Opaque pointer to a container of the given type.
  185. * @return Number of elements.
  186. */
  187. [[nodiscard]] static size_type size(const void *container) {
  188. return static_cast<const Type *>(container)->size();
  189. }
  190. /**
  191. * @brief Clears a container.
  192. * @param container Opaque pointer to a container of the given type.
  193. * @return True in case of success, false otherwise.
  194. */
  195. [[nodiscard]] static bool clear(void *container) {
  196. static_cast<Type *>(container)->clear();
  197. return true;
  198. }
  199. /**
  200. * @brief Increases the capacity of a container.
  201. * @param container Opaque pointer to a container of the given type.
  202. * @param sz Desired capacity.
  203. * @return True in case of success, false otherwise.
  204. */
  205. [[nodiscard]] static bool reserve([[maybe_unused]] void *container, [[maybe_unused]] const size_type sz) {
  206. if constexpr(internal::reserve_aware_container_v<Type>) {
  207. static_cast<Type *>(container)->reserve(sz);
  208. return true;
  209. } else {
  210. return false;
  211. }
  212. }
  213. /**
  214. * @brief Returns a possibly const iterator to the beginning.
  215. * @param area The context to pass to the newly created iterator.
  216. * @param container Opaque pointer to a container of the given type.
  217. * @param as_const Const opaque pointer fallback.
  218. * @return An iterator to the first element of the container.
  219. */
  220. static iterator begin(const meta_ctx &area, void *container, const void *as_const) {
  221. return container ? iterator{area, std::bool_constant<key_only>{}, static_cast<Type *>(container)->begin()}
  222. : iterator{area, std::bool_constant<key_only>{}, static_cast<const Type *>(as_const)->begin()};
  223. }
  224. /**
  225. * @brief Returns a possibly const iterator to the end.
  226. * @param area The context to pass to the newly created iterator.
  227. * @param container Opaque pointer to a container of the given type.
  228. * @param as_const Const opaque pointer fallback.
  229. * @return An iterator that is past the last element of the container.
  230. */
  231. static iterator end(const meta_ctx &area, void *container, const void *as_const) {
  232. return container ? iterator{area, std::bool_constant<key_only>{}, static_cast<Type *>(container)->end()}
  233. : iterator{area, std::bool_constant<key_only>{}, static_cast<const Type *>(as_const)->end()};
  234. }
  235. /**
  236. * @brief Inserts an element into a container, if the key does not exist.
  237. * @param container Opaque pointer to a container of the given type.
  238. * @param key An opaque key value of an element to insert.
  239. * @param value Optional opaque value to insert (key-value containers).
  240. * @return True if the insertion took place, false otherwise.
  241. */
  242. [[nodiscard]] static bool insert(void *container, const void *key, [[maybe_unused]] const void *value) {
  243. if constexpr(key_only) {
  244. return static_cast<Type *>(container)->insert(*static_cast<const typename Type::key_type *>(key)).second;
  245. } else {
  246. return static_cast<Type *>(container)->emplace(*static_cast<const typename Type::key_type *>(key), *static_cast<const typename Type::mapped_type *>(value)).second;
  247. }
  248. }
  249. /**
  250. * @brief Removes an element from a container.
  251. * @param container Opaque pointer to a container of the given type.
  252. * @param key An opaque key value of an element to remove.
  253. * @return Number of elements removed (either 0 or 1).
  254. */
  255. [[nodiscard]] static size_type erase(void *container, const void *key) {
  256. return static_cast<Type *>(container)->erase(*static_cast<const typename Type::key_type *>(key));
  257. }
  258. /**
  259. * @brief Finds an element with a given key.
  260. * @param area The context to pass to the newly created iterator.
  261. * @param container Opaque pointer to a container of the given type.
  262. * @param as_const Const opaque pointer fallback.
  263. * @param key Opaque key value of an element to search for.
  264. * @return An iterator to the element with the given key, if any.
  265. */
  266. static iterator find(const meta_ctx &area, void *container, const void *as_const, const void *key) {
  267. return container ? iterator{area, std::bool_constant<key_only>{}, static_cast<Type *>(container)->find(*static_cast<const typename Type::key_type *>(key))}
  268. : iterator{area, std::bool_constant<key_only>{}, static_cast<const Type *>(as_const)->find(*static_cast<const typename Type::key_type *>(key))};
  269. }
  270. };
  271. /**
  272. * @brief Meta sequence container traits for `std::vector`s of any type.
  273. * @tparam Args Template arguments for the container.
  274. */
  275. template<typename... Args>
  276. struct meta_sequence_container_traits<std::vector<Args...>>
  277. : basic_meta_sequence_container_traits<std::vector<Args...>> {};
  278. /**
  279. * @brief Meta sequence container traits for `std::array`s of any type.
  280. * @tparam Type Template arguments for the container.
  281. * @tparam N Template arguments for the container.
  282. */
  283. template<typename Type, auto N>
  284. struct meta_sequence_container_traits<std::array<Type, N>>
  285. : basic_meta_sequence_container_traits<std::array<Type, N>> {};
  286. /**
  287. * @brief Meta sequence container traits for `std::list`s of any type.
  288. * @tparam Args Template arguments for the container.
  289. */
  290. template<typename... Args>
  291. struct meta_sequence_container_traits<std::list<Args...>>
  292. : basic_meta_sequence_container_traits<std::list<Args...>> {};
  293. /**
  294. * @brief Meta sequence container traits for `std::deque`s of any type.
  295. * @tparam Args Template arguments for the container.
  296. */
  297. template<typename... Args>
  298. struct meta_sequence_container_traits<std::deque<Args...>>
  299. : basic_meta_sequence_container_traits<std::deque<Args...>> {};
  300. /**
  301. * @brief Meta associative container traits for `std::map`s of any type.
  302. * @tparam Args Template arguments for the container.
  303. */
  304. template<typename... Args>
  305. struct meta_associative_container_traits<std::map<Args...>>
  306. : basic_meta_associative_container_traits<std::map<Args...>> {};
  307. /**
  308. * @brief Meta associative container traits for `std::unordered_map`s of any
  309. * type.
  310. * @tparam Args Template arguments for the container.
  311. */
  312. template<typename... Args>
  313. struct meta_associative_container_traits<std::unordered_map<Args...>>
  314. : basic_meta_associative_container_traits<std::unordered_map<Args...>> {};
  315. /**
  316. * @brief Meta associative container traits for `std::set`s of any type.
  317. * @tparam Args Template arguments for the container.
  318. */
  319. template<typename... Args>
  320. struct meta_associative_container_traits<std::set<Args...>>
  321. : basic_meta_associative_container_traits<std::set<Args...>> {};
  322. /**
  323. * @brief Meta associative container traits for `std::unordered_set`s of any
  324. * type.
  325. * @tparam Args Template arguments for the container.
  326. */
  327. template<typename... Args>
  328. struct meta_associative_container_traits<std::unordered_set<Args...>>
  329. : basic_meta_associative_container_traits<std::unordered_set<Args...>> {};
  330. /**
  331. * @brief Meta associative container traits for `dense_map`s of any type.
  332. * @tparam Args Template arguments for the container.
  333. */
  334. template<typename... Args>
  335. struct meta_associative_container_traits<dense_map<Args...>>
  336. : basic_meta_associative_container_traits<dense_map<Args...>> {};
  337. /**
  338. * @brief Meta associative container traits for `dense_set`s of any type.
  339. * @tparam Args Template arguments for the container.
  340. */
  341. template<typename... Args>
  342. struct meta_associative_container_traits<dense_set<Args...>>
  343. : basic_meta_associative_container_traits<dense_set<Args...>> {};
  344. } // namespace entt
  345. #endif