Resources (#1)

* start some resource fiddling

* start some resource fiddling

* initiate a real resource manager

* fix color flicker in rendering

* delete unintended constructors

and add a convenience function to reset the render-image (from the
original image, aka. clone again)

* [OpenGL deprecation warning]

The compiler said that these functions are "deprecated". They seem to
be useless anyway...

* various improvements

* add resource enumerator and documentation

* fix constness stuff

* use existing iterator for insertion

* init unit tests

* rm bogus file

---------

Co-authored-by: m-aXimilian <keuffnermax@gmail.com>
This commit is contained in:
m-aXimilian
2025-06-13 22:23:20 +00:00
committed by GitHub
parent a175b79c96
commit a8489292b6
21 changed files with 574 additions and 169 deletions
+13
View File
@@ -0,0 +1,13 @@
set(RESOURCELIBNAME pixelariumresourcelib)
set(RESOURCELIBSRC
resource.cpp)
add_library(${RESOURCELIBNAME} STATIC
${RESOURCELIBSRC})
target_link_libraries(${RESOURCELIBNAME}
PRIVATE pixelariumimagelib)
target_include_directories(${RESOURCELIBNAME}
PRIVATE ${CMAKE_SOURCE_DIR}/lib)
+78
View File
@@ -0,0 +1,78 @@
#include "resource.hpp"
#include <atomic>
#include <optional>
using pixelarium::imaging::PixelariumImage;
using namespace std;
namespace
{
/// @brief Atomic counter for generating unique IDs.
static std::atomic<size_t> id_;
/// @brief Generates a unique ID.
/// @return A unique ID.
size_t GenerateId() { return id_.fetch_add(1, memory_order_relaxed); }
} // namespace
/// @brief Retrieves a resource from the pool.
/// @param id The ID of the resource to retrieve.
/// @return A pointer to the resource if found, otherwise an empty optional.
std::optional<const PixelariumImage*> pixelarium::resources::ImageResourcePool::GetResource(size_t id) const
{
auto search{this->resources_.find(id)};
if (search == this->resources_.end()) return std::nullopt;
return search->second.get();
}
/// @brief Sets a resource in the pool.
/// @param res A unique pointer to the resource to set.
/// @return The ID of the new resource.
size_t pixelarium::resources::ImageResourcePool::SetResource(unique_ptr<PixelariumImage> res)
{
auto key{::GenerateId()};
this->resources_.insert({key, std::move(res)});
return key;
}
/// @brief Updates a resource in the pool.
/// @param id The ID of the resource to update.
/// @param res A unique pointer to the new resource.
/// @return True if the resource was updated, false otherwise.
bool pixelarium::resources::ImageResourcePool::UpdateResource(size_t id, std::unique_ptr<imaging::PixelariumImage> res)
{
auto search{this->resources_.find(id)};
if (search == this->resources_.end()) return false;
search->second = std::move(res);
return true;
}
/// @brief Deletes a resource from the pool.
/// @param id The ID of the resource to delete.
/// @return True if the resource was deleted, false otherwise.
bool pixelarium::resources::ImageResourcePool::DeleteResource(size_t id)
{
auto search{this->resources_.find(id)};
if (search == this->resources_.end()) return false;
this->resources_.erase(search);
return true;
}
/// @brief Enumerates all resources in the pool.
/// @param func A function to call for each resource. The function should accept the resource ID and a const reference
/// to a PixelariumImage.
void pixelarium::resources::ImageResourcePool::EnumerateResources(
const std::function<void(size_t, const imaging::PixelariumImage&)>& func)
{
for (const auto& e : this->resources_)
{
func(e.first, *e.second);
}
}
+58
View File
@@ -0,0 +1,58 @@
#pragma once
#include <functional>
#include <memory>
#include <optional>
#include <unordered_map>
#include "imaging/PixelariumImage.hpp"
namespace pixelarium::resources
{
struct IResource
{
virtual ~IResource() = 0;
};
template <typename R>
concept ResT = requires(R& r) { static_cast<IResource&>(r); };
// template <ResT R>
template <typename R>
class IResourcePool
{
public:
virtual ~IResourcePool() = default;
virtual std::optional<const R*> GetResource(size_t id) const = 0;
virtual size_t SetResource(std::unique_ptr<R> res) = 0;
virtual bool UpdateResource(size_t id, std::unique_ptr<R> res) = 0;
virtual bool DeleteResource(size_t id) = 0;
virtual void EnumerateResources(const std::function<void(size_t, const R&)>& func) = 0;
};
// Now with the =GetResource= method, I do not want to transfer ownership to the caller of that method. The ownership
// should still
// reside with the =ResourcePool=!
// In fact, the intention is, that there is no way back once the =ResourcePool= took ownership of an object.
// Callers can get references, but no ownership. A caller might delete a resource though.
class ImageResourcePool : public IResourcePool<imaging::PixelariumImage>
{
public:
ImageResourcePool() = default;
ImageResourcePool(ImageResourcePool&) = delete;
ImageResourcePool(const ImageResourcePool&) = delete;
ImageResourcePool(ImageResourcePool&&) = delete;
ImageResourcePool& operator=(ImageResourcePool&) = delete;
ImageResourcePool& operator=(ImageResourcePool&&) = delete;
std::optional<const imaging::PixelariumImage*> GetResource(size_t id) const override;
size_t SetResource(std::unique_ptr<imaging::PixelariumImage> res) override;
bool UpdateResource(size_t id, std::unique_ptr<imaging::PixelariumImage> res) override;
bool DeleteResource(size_t id) override;
void EnumerateResources(const std::function<void(size_t, const imaging::PixelariumImage&)>& func) override;
private:
std::unordered_map<size_t, std::unique_ptr<imaging::PixelariumImage>> resources_;
};
} // namespace pixelarium::resources