8#ifndef GALAXY_RESOURCE_CACHE_HPP_
9#define GALAXY_RESOURCE_CACHE_HPP_
11#include <ankerl/unordered_dense.h>
13#include "galaxy/core/ServiceLocator.hpp"
28 template<meta::not_memory Resource, meta::not_memory Loader>
32 using CacheType = ankerl::unordered_dense::map<std::uint64_t, std::unique_ptr<Resource>>;
55 inline void load(
const std::string& file)
55 inline void load(
const std::string& file) {
…}
70 for (
const auto& file : core::ServiceLocator<fs::VirtualFileSystem>::ref().list(dir))
82 inline void insert(std::string_view
id, std::unique_ptr<Resource>& resource)
84 const auto hashed =
hash(
id);
87 m_cache[hashed] = std::move(resource);
82 inline void insert(std::string_view
id, std::unique_ptr<Resource>& resource) {
…}
103 inline Resource*
get(std::string_view
id)
105 const auto hashed =
hash(
id);
103 inline Resource*
get(std::string_view
id) {
…}
131 inline bool has(std::string_view
id)
131 inline bool has(std::string_view
id) {
…}
151 inline std::size_t
size()
const
173 inline const meta::vector<std::string>&
keys()
173 inline const meta::vector<std::string>&
keys() {
…}
202 inline std::uint64_t
hash(std::string_view str)
204 const auto hash = math::fnv1a_64(str.data());
202 inline std::uint64_t
hash(std::string_view str) {
…}
#define GALAXY_LOG(level, msg,...)
Cache for resources such as audio, fonts, etc.
Cache & operator=(const Cache &)=delete
Copy assignment operator.
void clear()
Destroy resources.
Cache & operator=(Cache &&)=delete
Move assignment operator.
meta::vector< std::string > m_keys
A list of keys currently in the cache.
ankerl::unordered_dense::map< std::uint64_t, std::unique_ptr< Resource > > CacheType
Loader m_loader
Used to load a resource into the cache. Allows for flexiblity.
Cache(const Cache &)=delete
Copy constructor.
void load(const std::string &file)
Load a resource.
std::size_t size() const
Get amount of resources cached.
CacheType m_cache
The actual data store.
void insert(std::string_view id, std::unique_ptr< Resource > &resource)
Insert a resource directly into the cache.
void load_folder(const std::string &dir)
Load resources of a specific type.
std::uint64_t hash(std::string_view str)
Hashes the key into an integer for faster map lookup time.
Cache() noexcept
Constructor.
Cache(Cache &&)=delete
Move constructor.
const CacheType & cache() const
Get entire resource cache.
bool has(std::string_view id)
Check if a resource exists.
Resource * get(std::string_view id)
Retrieve a resource.
const meta::vector< std::string > & keys()
Get a list of keys in the cache.
bool empty() const
Does the cache have any resources.
Loads resources for a resource cache.