cache.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. #ifndef ENTT_RESOURCE_RESOURCE_CACHE_HPP
  2. #define ENTT_RESOURCE_RESOURCE_CACHE_HPP
  3. #include <cstddef>
  4. #include <functional>
  5. #include <iterator>
  6. #include <memory>
  7. #include <tuple>
  8. #include <type_traits>
  9. #include <utility>
  10. #include "../container/dense_map.hpp"
  11. #include "../core/compressed_pair.hpp"
  12. #include "../core/fwd.hpp"
  13. #include "../core/iterator.hpp"
  14. #include "../core/utility.hpp"
  15. #include "fwd.hpp"
  16. #include "loader.hpp"
  17. #include "resource.hpp"
  18. namespace entt {
  19. /**
  20. * @cond TURN_OFF_DOXYGEN
  21. * Internal details not to be documented.
  22. */
  23. namespace internal {
  24. template<typename Type, typename It>
  25. class resource_cache_iterator final {
  26. template<typename, typename>
  27. friend class resource_cache_iterator;
  28. public:
  29. using value_type = std::pair<id_type, resource<Type>>;
  30. using pointer = input_iterator_pointer<value_type>;
  31. using reference = value_type;
  32. using difference_type = std::ptrdiff_t;
  33. using iterator_category = std::input_iterator_tag;
  34. using iterator_concept = std::random_access_iterator_tag;
  35. constexpr resource_cache_iterator() noexcept = default;
  36. constexpr resource_cache_iterator(const It iter) noexcept
  37. : it{iter} {}
  38. template<typename Other, typename = std::enable_if_t<!std::is_same_v<It, Other> && std::is_constructible_v<It, Other>>>
  39. constexpr resource_cache_iterator(const resource_cache_iterator<std::remove_const_t<Type>, Other> &other) noexcept
  40. : it{other.it} {}
  41. constexpr resource_cache_iterator &operator++() noexcept {
  42. return ++it, *this;
  43. }
  44. constexpr resource_cache_iterator operator++(int) noexcept {
  45. resource_cache_iterator orig = *this;
  46. return ++(*this), orig;
  47. }
  48. constexpr resource_cache_iterator &operator--() noexcept {
  49. return --it, *this;
  50. }
  51. constexpr resource_cache_iterator operator--(int) noexcept {
  52. resource_cache_iterator orig = *this;
  53. return operator--(), orig;
  54. }
  55. constexpr resource_cache_iterator &operator+=(const difference_type value) noexcept {
  56. it += value;
  57. return *this;
  58. }
  59. constexpr resource_cache_iterator operator+(const difference_type value) const noexcept {
  60. resource_cache_iterator copy = *this;
  61. return (copy += value);
  62. }
  63. constexpr resource_cache_iterator &operator-=(const difference_type value) noexcept {
  64. return (*this += -value);
  65. }
  66. constexpr resource_cache_iterator operator-(const difference_type value) const noexcept {
  67. return (*this + -value);
  68. }
  69. [[nodiscard]] constexpr reference operator[](const difference_type value) const noexcept {
  70. return {it[value].first, resource<Type>{it[value].second}};
  71. }
  72. [[nodiscard]] constexpr reference operator*() const noexcept {
  73. return (*this)[0];
  74. }
  75. [[nodiscard]] constexpr pointer operator->() const noexcept {
  76. return operator*();
  77. }
  78. template<typename... Lhs, typename... Rhs>
  79. friend constexpr std::ptrdiff_t operator-(const resource_cache_iterator<Lhs...> &, const resource_cache_iterator<Rhs...> &) noexcept;
  80. template<typename... Lhs, typename... Rhs>
  81. friend constexpr bool operator==(const resource_cache_iterator<Lhs...> &, const resource_cache_iterator<Rhs...> &) noexcept;
  82. template<typename... Lhs, typename... Rhs>
  83. friend constexpr bool operator<(const resource_cache_iterator<Lhs...> &, const resource_cache_iterator<Rhs...> &) noexcept;
  84. private:
  85. It it;
  86. };
  87. template<typename... Lhs, typename... Rhs>
  88. [[nodiscard]] constexpr std::ptrdiff_t operator-(const resource_cache_iterator<Lhs...> &lhs, const resource_cache_iterator<Rhs...> &rhs) noexcept {
  89. return lhs.it - rhs.it;
  90. }
  91. template<typename... Lhs, typename... Rhs>
  92. [[nodiscard]] constexpr bool operator==(const resource_cache_iterator<Lhs...> &lhs, const resource_cache_iterator<Rhs...> &rhs) noexcept {
  93. return lhs.it == rhs.it;
  94. }
  95. template<typename... Lhs, typename... Rhs>
  96. [[nodiscard]] constexpr bool operator!=(const resource_cache_iterator<Lhs...> &lhs, const resource_cache_iterator<Rhs...> &rhs) noexcept {
  97. return !(lhs == rhs);
  98. }
  99. template<typename... Lhs, typename... Rhs>
  100. [[nodiscard]] constexpr bool operator<(const resource_cache_iterator<Lhs...> &lhs, const resource_cache_iterator<Rhs...> &rhs) noexcept {
  101. return lhs.it < rhs.it;
  102. }
  103. template<typename... Lhs, typename... Rhs>
  104. [[nodiscard]] constexpr bool operator>(const resource_cache_iterator<Lhs...> &lhs, const resource_cache_iterator<Rhs...> &rhs) noexcept {
  105. return rhs < lhs;
  106. }
  107. template<typename... Lhs, typename... Rhs>
  108. [[nodiscard]] constexpr bool operator<=(const resource_cache_iterator<Lhs...> &lhs, const resource_cache_iterator<Rhs...> &rhs) noexcept {
  109. return !(lhs > rhs);
  110. }
  111. template<typename... Lhs, typename... Rhs>
  112. [[nodiscard]] constexpr bool operator>=(const resource_cache_iterator<Lhs...> &lhs, const resource_cache_iterator<Rhs...> &rhs) noexcept {
  113. return !(lhs < rhs);
  114. }
  115. } // namespace internal
  116. /**
  117. * Internal details not to be documented.
  118. * @endcond
  119. */
  120. /**
  121. * @brief Basic cache for resources of any type.
  122. * @tparam Type Type of resources managed by a cache.
  123. * @tparam Loader Type of loader used to create the resources.
  124. * @tparam Allocator Type of allocator used to manage memory and elements.
  125. */
  126. template<typename Type, typename Loader, typename Allocator>
  127. class resource_cache {
  128. using alloc_traits = std::allocator_traits<Allocator>;
  129. static_assert(std::is_same_v<typename alloc_traits::value_type, Type>, "Invalid value type");
  130. using container_allocator = typename alloc_traits::template rebind_alloc<std::pair<const id_type, typename Loader::result_type>>;
  131. using container_type = dense_map<id_type, typename Loader::result_type, identity, std::equal_to<id_type>, container_allocator>;
  132. public:
  133. /*! @brief Resource type. */
  134. using value_type = Type;
  135. /*! @brief Unsigned integer type. */
  136. using size_type = std::size_t;
  137. /*! @brief Loader type. */
  138. using loader_type = Loader;
  139. /*! @brief Allocator type. */
  140. using allocator_type = Allocator;
  141. /*! @brief Input iterator type. */
  142. using iterator = internal::resource_cache_iterator<Type, typename container_type::iterator>;
  143. /*! @brief Constant input iterator type. */
  144. using const_iterator = internal::resource_cache_iterator<const Type, typename container_type::const_iterator>;
  145. /*! @brief Default constructor. */
  146. resource_cache()
  147. : resource_cache{loader_type{}} {}
  148. /**
  149. * @brief Constructs an empty cache with a given allocator.
  150. * @param allocator The allocator to use.
  151. */
  152. explicit resource_cache(const allocator_type &allocator)
  153. : resource_cache{loader_type{}, allocator} {}
  154. /**
  155. * @brief Constructs an empty cache with a given allocator and loader.
  156. * @param callable The loader to use.
  157. * @param allocator The allocator to use.
  158. */
  159. explicit resource_cache(const loader_type &callable, const allocator_type &allocator = allocator_type{})
  160. : pool{container_type{allocator}, callable} {}
  161. /*! @brief Default copy constructor. */
  162. resource_cache(const resource_cache &) = default;
  163. /**
  164. * @brief Allocator-extended copy constructor.
  165. * @param other The instance to copy from.
  166. * @param allocator The allocator to use.
  167. */
  168. resource_cache(const resource_cache &other, const allocator_type &allocator)
  169. : pool{std::piecewise_construct, std::forward_as_tuple(other.pool.first(), allocator), std::forward_as_tuple(other.pool.second())} {}
  170. /*! @brief Default move constructor. */
  171. resource_cache(resource_cache &&) = default;
  172. /**
  173. * @brief Allocator-extended move constructor.
  174. * @param other The instance to move from.
  175. * @param allocator The allocator to use.
  176. */
  177. resource_cache(resource_cache &&other, const allocator_type &allocator)
  178. : pool{std::piecewise_construct, std::forward_as_tuple(std::move(other.pool.first()), allocator), std::forward_as_tuple(std::move(other.pool.second()))} {}
  179. /**
  180. * @brief Default copy assignment operator.
  181. * @return This cache.
  182. */
  183. resource_cache &operator=(const resource_cache &) = default;
  184. /**
  185. * @brief Default move assignment operator.
  186. * @return This cache.
  187. */
  188. resource_cache &operator=(resource_cache &&) = default;
  189. /**
  190. * @brief Returns the associated allocator.
  191. * @return The associated allocator.
  192. */
  193. [[nodiscard]] constexpr allocator_type get_allocator() const noexcept {
  194. return pool.first().get_allocator();
  195. }
  196. /**
  197. * @brief Returns an iterator to the beginning.
  198. *
  199. * If the cache is empty, the returned iterator will be equal to `end()`.
  200. *
  201. * @return An iterator to the first instance of the internal cache.
  202. */
  203. [[nodiscard]] const_iterator cbegin() const noexcept {
  204. return pool.first().begin();
  205. }
  206. /*! @copydoc cbegin */
  207. [[nodiscard]] const_iterator begin() const noexcept {
  208. return cbegin();
  209. }
  210. /*! @copydoc begin */
  211. [[nodiscard]] iterator begin() noexcept {
  212. return pool.first().begin();
  213. }
  214. /**
  215. * @brief Returns an iterator to the end.
  216. * @return An iterator to the element following the last instance of the
  217. * internal cache.
  218. */
  219. [[nodiscard]] const_iterator cend() const noexcept {
  220. return pool.first().end();
  221. }
  222. /*! @copydoc cend */
  223. [[nodiscard]] const_iterator end() const noexcept {
  224. return cend();
  225. }
  226. /*! @copydoc end */
  227. [[nodiscard]] iterator end() noexcept {
  228. return pool.first().end();
  229. }
  230. /**
  231. * @brief Returns true if a cache contains no resources, false otherwise.
  232. * @return True if the cache contains no resources, false otherwise.
  233. */
  234. [[nodiscard]] bool empty() const noexcept {
  235. return pool.first().empty();
  236. }
  237. /**
  238. * @brief Number of resources managed by a cache.
  239. * @return Number of resources currently stored.
  240. */
  241. [[nodiscard]] size_type size() const noexcept {
  242. return pool.first().size();
  243. }
  244. /*! @brief Clears a cache. */
  245. void clear() noexcept {
  246. pool.first().clear();
  247. }
  248. /**
  249. * @brief Loads a resource, if its identifier does not exist.
  250. *
  251. * Arguments are forwarded directly to the loader and _consumed_ only if the
  252. * resource doesn't already exist.
  253. *
  254. * @warning
  255. * If the resource isn't loaded correctly, the returned handle could be
  256. * invalid and any use of it will result in undefined behavior.
  257. *
  258. * @tparam Args Types of arguments to use to load the resource if required.
  259. * @param id Unique resource identifier.
  260. * @param args Arguments to use to load the resource if required.
  261. * @return A pair consisting of an iterator to the inserted element (or to
  262. * the element that prevented the insertion) and a bool denoting whether the
  263. * insertion took place.
  264. */
  265. template<typename... Args>
  266. std::pair<iterator, bool> load(const id_type id, Args &&...args) {
  267. if(auto it = pool.first().find(id); it != pool.first().end()) {
  268. return {it, false};
  269. }
  270. return pool.first().emplace(id, pool.second()(std::forward<Args>(args)...));
  271. }
  272. /**
  273. * @brief Force loads a resource, if its identifier does not exist.
  274. * @copydetails load
  275. */
  276. template<typename... Args>
  277. std::pair<iterator, bool> force_load(const id_type id, Args &&...args) {
  278. return {pool.first().insert_or_assign(id, pool.second()(std::forward<Args>(args)...)).first, true};
  279. }
  280. /**
  281. * @brief Returns a handle for a given resource identifier.
  282. *
  283. * @warning
  284. * There is no guarantee that the returned handle is valid.<br/>
  285. * If it is not, any use will result in indefinite behavior.
  286. *
  287. * @param id Unique resource identifier.
  288. * @return A handle for the given resource.
  289. */
  290. [[nodiscard]] resource<const value_type> operator[](const id_type id) const {
  291. if(auto it = pool.first().find(id); it != pool.first().cend()) {
  292. return resource<const value_type>{it->second};
  293. }
  294. return {};
  295. }
  296. /*! @copydoc operator[] */
  297. [[nodiscard]] resource<value_type> operator[](const id_type id) {
  298. if(auto it = pool.first().find(id); it != pool.first().end()) {
  299. return resource<value_type>{it->second};
  300. }
  301. return {};
  302. }
  303. /**
  304. * @brief Checks if a cache contains a given identifier.
  305. * @param id Unique resource identifier.
  306. * @return True if the cache contains the resource, false otherwise.
  307. */
  308. [[nodiscard]] bool contains(const id_type id) const {
  309. return pool.first().contains(id);
  310. }
  311. /**
  312. * @brief Removes an element from a given position.
  313. * @param pos An iterator to the element to remove.
  314. * @return An iterator following the removed element.
  315. */
  316. iterator erase(const_iterator pos) {
  317. const auto it = pool.first().begin();
  318. return pool.first().erase(it + (pos - const_iterator{it}));
  319. }
  320. /**
  321. * @brief Removes the given elements from a cache.
  322. * @param first An iterator to the first element of the range of elements.
  323. * @param last An iterator past the last element of the range of elements.
  324. * @return An iterator following the last removed element.
  325. */
  326. iterator erase(const_iterator first, const_iterator last) {
  327. const auto it = pool.first().begin();
  328. return pool.first().erase(it + (first - const_iterator{it}), it + (last - const_iterator{it}));
  329. }
  330. /**
  331. * @brief Removes the given elements from a cache.
  332. * @param id Unique resource identifier.
  333. * @return Number of resources erased (either 0 or 1).
  334. */
  335. size_type erase(const id_type id) {
  336. return pool.first().erase(id);
  337. }
  338. /**
  339. * @brief Returns the loader used to create resources.
  340. * @return The loader used to create resources.
  341. */
  342. [[nodiscard]] loader_type loader() const {
  343. return pool.second();
  344. }
  345. private:
  346. compressed_pair<container_type, loader_type> pool;
  347. };
  348. } // namespace entt
  349. #endif