1#ifndef ENTT_RESOURCE_RESOURCE_CACHE_HPP
2#define ENTT_RESOURCE_RESOURCE_CACHE_HPP
11#include "../container/dense_map.hpp"
12#include "../core/compressed_pair.hpp"
13#include "../core/fwd.hpp"
14#include "../core/iterator.hpp"
15#include "../core/utility.hpp"
18#include "resource.hpp"
29template<
typename Type,
typename It>
30class resource_cache_iterator final {
31 template<
typename,
typename>
32 friend class resource_cache_iterator;
35 using value_type = std::pair<id_type, resource<Type>>;
36 using pointer = input_iterator_pointer<value_type>;
37 using reference = value_type;
38 using difference_type = std::ptrdiff_t;
39 using iterator_category = std::input_iterator_tag;
41 constexpr resource_cache_iterator() noexcept = default;
43 constexpr resource_cache_iterator(const It iter) noexcept
46 template<
typename Other,
typename = std::enable_if_t<!std::is_same_v<It, Other> && std::is_constructible_v<It, Other>>>
47 constexpr resource_cache_iterator(
const resource_cache_iterator<std::remove_const_t<Type>, Other> &other) noexcept
50 constexpr resource_cache_iterator &operator++() noexcept {
54 constexpr resource_cache_iterator operator++(
int)
noexcept {
55 resource_cache_iterator orig = *
this;
56 return ++(*this), orig;
59 constexpr resource_cache_iterator &operator--() noexcept {
63 constexpr resource_cache_iterator operator--(
int)
noexcept {
64 resource_cache_iterator orig = *
this;
65 return operator--(), orig;
68 constexpr resource_cache_iterator &operator+=(
const difference_type value)
noexcept {
73 constexpr resource_cache_iterator
operator+(
const difference_type value)
const noexcept {
74 resource_cache_iterator copy = *
this;
75 return (copy += value);
78 constexpr resource_cache_iterator &operator-=(
const difference_type value)
noexcept {
79 return (*
this += -value);
82 constexpr resource_cache_iterator operator-(
const difference_type value)
const noexcept {
83 return (*
this + -value);
86 [[nodiscard]]
constexpr reference operator[](
const difference_type value)
const noexcept {
87 return {it[value].first, resource<Type>{it[value].second}};
90 [[nodiscard]]
constexpr reference operator*() const noexcept {
94 [[nodiscard]]
constexpr pointer operator->() const noexcept {
98 template<
typename... Lhs,
typename... Rhs>
99 friend constexpr std::ptrdiff_t operator-(
const resource_cache_iterator<Lhs...> &,
const resource_cache_iterator<Rhs...> &)
noexcept;
101 template<
typename... Lhs,
typename... Rhs>
102 friend constexpr bool operator==(
const resource_cache_iterator<Lhs...> &,
const resource_cache_iterator<Rhs...> &)
noexcept;
104 template<
typename... Lhs,
typename... Rhs>
105 friend constexpr bool operator<(
const resource_cache_iterator<Lhs...> &,
const resource_cache_iterator<Rhs...> &)
noexcept;
111template<
typename... Lhs,
typename... Rhs>
112[[nodiscard]]
constexpr std::ptrdiff_t operator-(
const resource_cache_iterator<Lhs...> &lhs,
const resource_cache_iterator<Rhs...> &rhs)
noexcept {
113 return lhs.it - rhs.it;
116template<
typename... Lhs,
typename... Rhs>
117[[nodiscard]]
constexpr bool operator==(
const resource_cache_iterator<Lhs...> &lhs,
const resource_cache_iterator<Rhs...> &rhs)
noexcept {
118 return lhs.it == rhs.it;
121template<
typename... Lhs,
typename... Rhs>
122[[nodiscard]]
constexpr bool operator!=(
const resource_cache_iterator<Lhs...> &lhs,
const resource_cache_iterator<Rhs...> &rhs)
noexcept {
123 return !(lhs == rhs);
126template<
typename... Lhs,
typename... Rhs>
127[[nodiscard]]
constexpr bool operator<(
const resource_cache_iterator<Lhs...> &lhs,
const resource_cache_iterator<Rhs...> &rhs)
noexcept {
128 return lhs.it < rhs.it;
131template<
typename... Lhs,
typename... Rhs>
132[[nodiscard]]
constexpr bool operator>(
const resource_cache_iterator<Lhs...> &lhs,
const resource_cache_iterator<Rhs...> &rhs)
noexcept {
136template<
typename... Lhs,
typename... Rhs>
137[[nodiscard]]
constexpr bool operator<=(
const resource_cache_iterator<Lhs...> &lhs,
const resource_cache_iterator<Rhs...> &rhs)
noexcept {
141template<
typename... Lhs,
typename... Rhs>
142[[nodiscard]]
constexpr bool operator>=(
const resource_cache_iterator<Lhs...> &lhs,
const resource_cache_iterator<Rhs...> &rhs)
noexcept {
159template<
typename Type,
typename Loader,
typename Allocator>
161 using alloc_traits = std::allocator_traits<Allocator>;
162 static_assert(std::is_same_v<typename alloc_traits::value_type, Type>,
"Invalid value type");
163 using container_allocator =
typename alloc_traits::template rebind_alloc<std::pair<const id_type, typename Loader::result_type>>;
176 using iterator = internal::resource_cache_iterator<Type, typename container_type::iterator>;
178 using const_iterator = internal::resource_cache_iterator<const Type, typename container_type::const_iterator>;
197 : pool{container_type{allocator}, callable} {}
208 : pool{std::piecewise_construct, std::forward_as_tuple(other.pool.first(), allocator), std::forward_as_tuple(other.pool.second())} {}
219 : pool{std::piecewise_construct, std::forward_as_tuple(std::move(other.pool.first()), allocator), std::forward_as_tuple(std::move(other.pool.second()))} {}
238 return pool.first().get_allocator();
249 return pool.first().begin();
259 return pool.first().begin();
268 return pool.first().end();
278 return pool.first().end();
285 [[nodiscard]]
bool empty() const noexcept {
286 return pool.first().empty();
294 return pool.first().size();
299 pool.first().clear();
319 template<
typename... Args>
320 std::pair<iterator, bool>
load(
const id_type id, Args &&...args) {
321 if(
auto it = pool.first().find(
id); it != pool.first().end()) {
325 return pool.first().emplace(
id, pool.second()(std::forward<Args>(args)...));
332 template<
typename... Args>
334 return {pool.first().insert_or_assign(
id, pool.second()(std::forward<Args>(args)...)).first,
true};
348 if(
auto it = pool.first().find(
id); it != pool.first().cend()) {
357 if(
auto it = pool.first().find(
id); it != pool.first().end()) {
370 return pool.first().contains(
id);
379 const auto it = pool.first().begin();
390 const auto it = pool.first().begin();
400 return pool.first().erase(
id);
408 return pool.second();
Associative container for key-value pairs with unique keys.
Basic cache for resources of any type.
size_type erase(const id_type id)
Removes the given elements from a cache.
resource_cache()
Default constructor.
iterator begin() noexcept
Returns an iterator to the beginning.
resource_cache(const resource_cache &other, const allocator_type &allocator)
Allocator-extended copy constructor.
loader_type loader() const
Returns the loader used to create resources.
resource< value_type > operator[](const id_type id)
Returns a handle for a given resource identifier.
const_iterator cend() const noexcept
Returns an iterator to the end.
std::pair< iterator, bool > load(const id_type id, Args &&...args)
Loads a resource, if its identifier does not exist.
iterator erase(const_iterator pos)
Removes an element from a given position.
Type value_type
Resource type.
Loader loader_type
Loader type.
resource_cache(resource_cache &&other, const allocator_type &allocator)
Allocator-extended move constructor.
resource_cache(const allocator_type &allocator)
Constructs an empty cache with a given allocator.
const_iterator cbegin() const noexcept
Returns an iterator to the beginning.
resource_cache(const loader_type &callable, const allocator_type &allocator=allocator_type{})
Constructs an empty cache with a given allocator and loader.
internal::resource_cache_iterator< const Type, typename container_type::const_iterator > const_iterator
Constant input iterator type.
resource< const value_type > operator[](const id_type id) const
Returns a handle for a given resource identifier.
resource_cache(resource_cache &&)=default
Default move constructor.
std::pair< iterator, bool > force_load(const id_type id, Args &&...args)
Force loads a resource, if its identifier does not exist.
internal::resource_cache_iterator< Type, typename container_type::iterator > iterator
Input iterator type.
Allocator allocator_type
Allocator type.
resource_cache(const resource_cache &)=default
Default copy constructor.
bool empty() const noexcept
Returns true if a cache contains no resources, false otherwise.
resource_cache & operator=(resource_cache &&)=default
Default move assignment operator.
resource_cache & operator=(const resource_cache &)=default
Default copy assignment operator.
const_iterator begin() const noexcept
Returns an iterator to the beginning.
iterator end() noexcept
Returns an iterator to the end.
constexpr allocator_type get_allocator() const noexcept
Returns the associated allocator.
size_type size() const noexcept
Number of resources managed by a cache.
std::size_t size_type
Unsigned integer type.
const_iterator end() const noexcept
Returns an iterator to the end.
iterator erase(const_iterator first, const_iterator last)
Removes the given elements from a cache.
void clear() noexcept
Clears a cache.
bool contains(const id_type id) const
Checks if a cache contains a given identifier.
std::uint32_t id_type
Alias declaration for type identifiers.
constexpr bool operator<=(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
constexpr bool operator<(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
constexpr type_list< Type..., Other... > operator+(type_list< Type... >, type_list< Other... >)
Concatenates multiple type lists.
constexpr bool operator!=(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
constexpr bool operator>=(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
constexpr bool operator>(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
constexpr bool operator==(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.