generic cache system

This commit is contained in:
arthur.jamet
2021-06-01 17:00:00 +02:00
parent 812a4da727
commit 382b830b27
12 changed files with 115 additions and 74 deletions
+1
View File
@@ -56,6 +56,7 @@ set(HEADERS
sources/Model/ModelAnimations.hpp
sources/Vector/Vector2.hpp
sources/Vector/Vector3.hpp
sources/Utils/Cache.hpp
)
set(SRC
+2 -13
View File
@@ -10,11 +10,11 @@
#include "Drawables/2D/Rectangle.hpp"
namespace RAY {
std::unordered_map<std::string, std::shared_ptr<::Image>> Image::_ImageCache;
Cache<::Image> Image::_imagesCache(LoadImage, UnloadImage);
Image::Image(const std::string &filename):
Rectangle(Vector2(0, 0), Vector2(0, 0), WHITE),
_image(fetchImageInCache(filename))
_image(_imagesCache.fetch(filename))
{
this->_dimensions = Vector2(this->_image->width, this->_image->height);
}
@@ -35,17 +35,6 @@ namespace RAY {
return this->_image.get();
}
std::shared_ptr<::Image> Image::fetchImageInCache(const std::string &path)
{
if (Image::_ImageCache.find(path) == Image::_ImageCache.end())
Image::_ImageCache.emplace(path, std::shared_ptr<::Image>(
new ::Image(LoadImage(path.c_str())), [](::Image *p) {
UnloadImage(*p);
delete p;
}));
return _ImageCache[path];
}
void Image::draw(Drawables::ADrawable2D &drawable)
{
drawable.drawOn(*this);
+2 -5
View File
@@ -11,8 +11,7 @@
#include <raylib.h>
#include <string>
#include "Texture.hpp"
#include <memory>
#include <unordered_map>
#include "Utils/Cache.hpp"
#include "Drawables/2D/Rectangle.hpp"
namespace RAY
@@ -50,10 +49,8 @@ namespace RAY
private:
//! @brief Image, really, that's just it...
std::shared_ptr<::Image> _image;
//! @brief, look through cache to see if a model using same file
std::shared_ptr<::Image>fetchImageInCache(const std::string &path);
static std::unordered_map<std::string, std::shared_ptr<::Image>> _ImageCache;
static Cache<::Image> _imagesCache;
INTERNAL:
+2 -13
View File
@@ -9,10 +9,10 @@
namespace RAY {
std::unordered_map<std::string, std::shared_ptr<::Texture>> Texture::_textureCache;
Cache<::Texture> Texture::_texturesCache(LoadTexture, UnloadTexture);
Texture::Texture(const std::string &filename):
_texture(fetchTextureInCache(filename)),
_texture(_texturesCache.fetch(filename)),
_resourcePath(filename)
{
}
@@ -27,15 +27,4 @@ namespace RAY {
{
return *this->_texture;
}
std::shared_ptr<::Texture> Texture::fetchTextureInCache(const std::string &path)
{
if (Texture::_textureCache.find(path) == Texture::_textureCache.end())
Texture::_textureCache.emplace(path, std::shared_ptr<::Texture>(
new ::Texture(LoadTexture(path.c_str())), [](::Texture *p) {
UnloadTexture(*p);
delete p;
}));
return _textureCache[path];
}
}
+2 -6
View File
@@ -10,8 +10,7 @@
#include <raylib.h>
#include <string>
#include <unordered_map>
#include <memory>
#include "Utils/Cache.hpp"
namespace RAY
{
@@ -42,10 +41,7 @@ namespace RAY
//! @brief path to the file the texture is loaded from
std::string _resourcePath;
//! @param path path of the file to load
//! @return a newly loaded texture if it hasn't be previously loaded, or one from cache
std::shared_ptr<::Texture> fetchTextureInCache(const std::string &path);
static std::unordered_map<std::string, std::shared_ptr<::Texture>> _textureCache;
static Cache<::Texture> _texturesCache;
INTERNAL:
//! @return libray Texture struct
+5 -7
View File
@@ -7,16 +7,14 @@
#include "Font.hpp"
RAY::Cache<::Font> RAY::Font::_fontsCache(LoadFont, UnloadFont);
RAY::Font::Font(const std::string &filename):
_font(LoadFont(filename.c_str()))
_font(_fontsCache.fetch(filename))
{
}
RAY::Font::Font()
RAY::Font::Font():
_font(std::make_shared<::Font>())
{
}
RAY::Font::~Font()
{
UnloadFont(this->_font);
}
+6 -3
View File
@@ -10,6 +10,7 @@
#include <raylib.h>
#include <string>
#include "Utils/Cache.hpp"
namespace RAY
{
@@ -29,13 +30,15 @@ namespace RAY
//! @brief An font is assignable
Font &operator=(const Font &) = default;
//! @brief Unload font at destruction
~Font();
//! @brief Default destructor
~Font() = default;
protected:
private:
//! @brief Font, really, that's just it...
::Font _font;
std::shared_ptr<::Font> _font;
static Cache<::Font> _fontsCache;
};
}
+3 -13
View File
@@ -12,7 +12,8 @@
namespace RAY::Drawables::Drawables3D {
std::unordered_map<std::string, std::shared_ptr<::Model>> Model::_modelsCache;
RAY::Cache<::Model> Model::_modelsCache(LoadModel, UnloadModel);
Model::Model(const std::string &filename,
std::optional<std::pair<MaterialType, std::string>> texture,
const RAY::Vector3 &position,
@@ -20,7 +21,7 @@ namespace RAY::Drawables::Drawables3D {
float rotationAngle,
const RAY::Vector3 &scale)
: ADrawable3D(position, WHITE),
_model(fetchModelInCache(filename)),
_model(_modelsCache.fetch(filename)),
_rotationAxis(rotationAxis),
_rotationAngle(rotationAngle),
_scale(scale)
@@ -105,15 +106,4 @@ namespace RAY::Drawables::Drawables3D {
{
DrawModelEx(*this->_model, this->_position, this->_rotationAxis, this->_rotationAngle, this->_scale, this->_color);
}
std::shared_ptr<::Model> Model::fetchModelInCache(const std::string &path)
{
if (Model::_modelsCache.find(path) == Model::_modelsCache.end())
Model::_modelsCache.emplace(path, std::shared_ptr<::Model>(
new ::Model(LoadModel(path.c_str())), [](::Model *p) {
UnloadModel(*p);
delete p;
}));
return _modelsCache[path];
}
}
+2 -5
View File
@@ -14,8 +14,7 @@
#include <raylib.h>
#include <vector>
#include <optional>
#include <unordered_map>
#include <memory>
#include "Utils/Cache.hpp"
namespace RAY::Drawables::Drawables3D {
//! @brief Basic 3D Model type
@@ -91,10 +90,8 @@ namespace RAY::Drawables::Drawables3D {
float _rotationAngle;
//! @brief Scale of the shape
RAY::Vector3 _scale;
//! @brief, look through cache to see if a model using same file
std::shared_ptr<::Model>fetchModelInCache(const std::string &path);
static std::unordered_map<std::string, std::shared_ptr<::Model>> _modelsCache;
static RAY::Cache<::Model> _modelsCache;
INTERNAL:
//! @brief A RAY Model is cast-able in libray's model
+3 -6
View File
@@ -7,8 +7,10 @@
#include "Model/ModelAnimations.hpp"
RAY::Cache<::ModelAnimation> RAY::ModelAnimations::_animationsCache(LoadModelAnimations, UnloadModelAnimations);
RAY::ModelAnimations::ModelAnimations(const std::string &filePath):
_animationsPtr(LoadModelAnimations(filePath.c_str(), &this->_animationCount))
_animationsPtr(_animationsCache.fetch(filePath, &this->_animationCount))
{
::ModelAnimation *ptr = this->_animationsPtr.get();
@@ -16,11 +18,6 @@ RAY::ModelAnimations::ModelAnimations(const std::string &filePath):
this->_animations.push_back(RAY::ModelAnimation(ptr[i]));
}
RAY::ModelAnimations::~ModelAnimations()
{
UnloadModelAnimations(this->_animationsPtr.release(), this->_animationCount);
}
RAY::ModelAnimation &RAY::ModelAnimations::operator[](int index)
{
return this->_animations[index];
+6 -3
View File
@@ -11,6 +11,7 @@
#include "Model/ModelAnimation.hpp"
#include <vector>
#include <string>
#include "Utils/Cache.hpp"
namespace RAY {
//! @brief A Holder for Model Animations
@@ -23,8 +24,8 @@ namespace RAY {
//! @brief Only single entity can hold these animations pointers
ModelAnimations(const ModelAnimations &) = delete;
//! @brief Unloads all animations
~ModelAnimations();
//! @brief Default constructor
~ModelAnimations() = default;
//! @brief Only single entity can hold these animations pointers
ModelAnimations &operator=(const ModelAnimations &) = delete;
@@ -37,13 +38,15 @@ namespace RAY {
private:
//! @brief Holds the pointer returned by the loading function
std::unique_ptr<::ModelAnimation> _animationsPtr;
std::shared_ptr<::ModelAnimation> _animationsPtr;
//! @brief A holder for animations
std::vector<ModelAnimation> _animations;
//! @brief the number of loaded animations
int _animationCount;
static Cache<::ModelAnimation> _animationsCache;
};
}
+81
View File
@@ -0,0 +1,81 @@
/*
** EPITECH PROJECT, 2021
** Bomberman
** File description:
** Cache
*/
#pragma once
#include <memory>
#include <unordered_map>
#include <functional>
namespace RAY {
//! @brief A templated class used to cache ressources, indexed with a string
template<typename T>
class Cache {
public:
//! @brief
Cache(std::function<T(const char *)> dataLoader, std::function<void(T)> dataUnloader):
_dataLoader(dataLoader), _dataUnloader(dataUnloader)
{};
//! @brief Default destructor, will destroy ray's data
~Cache() = default;
//! @brief default copy constructor
Cache(const Cache &) = default;
//! @brief a cache is assignable
Cache &operator=(const Cache &) = default;
//! @param path path of the file
//! @return a newly loaded ressource if it hasn't be previously loaded, or one from cache
std::shared_ptr<T>fetch(const std::string &path)
{
if (this->_cache.find(path) == this->_cache.end())
this->_cache.emplace(path, std::shared_ptr<T>(
new T(this->_dataLoader(path.c_str())), [this](T *p) {
this->_dataUnloader(*p);
delete p;
}));
return _cache[path];
};
private:
//! @brief function to call to load data
std::function<T(const char *)> _dataLoader;
//! @brief function to call when the ray data will be unloaded
std::function<void(T)> _dataUnloader;
//! @brief map storing shared ptr of caches
std::unordered_map<std::string, std::shared_ptr<T>> _cache;
};
template<>
class Cache<::ModelAnimation> {
public:
Cache(std::function<::ModelAnimation *(const char *, int *)> dataLoader, std::function<void(::ModelAnimation *, unsigned int)>dataUnloader):
_dataLoader(dataLoader), _dataUnloader(dataUnloader)
{};
std::shared_ptr<::ModelAnimation> fetch(const std::string &path, int *counter)
{
if (this->_cache.find(path) == this->_cache.end())
this->_cache.emplace(path, std::shared_ptr<::ModelAnimation>(
this->_dataLoader(path.c_str(), counter), [this, counter](::ModelAnimation *p) {
this->_dataUnloader(p, *counter);
delete p;
}));
return _cache[path];
};
private:
//! @brief function to call to load data
std::function<::ModelAnimation *(const char *, int *)> _dataLoader;
//! @brief function to call when the ray data will be unloaded
std::function<void(::ModelAnimation *, unsigned int)> _dataUnloader;
//! @brief map storing shared ptr of caches
std::unordered_map<std::string, std::shared_ptr<::ModelAnimation>> _cache;
};
}