Files
pixelarium/lib/resources/resource.hpp
T

98 lines
3.5 KiB
C++
Raw Normal View History

2025-06-13 22:23:20 +00:00
#pragma once
2025-08-18 22:39:43 +00:00
#include <concepts>
2025-06-13 22:23:20 +00:00
#include <functional>
#include <memory>
2025-09-13 14:49:59 +02:00
#include <mutex>
2025-06-13 22:23:20 +00:00
#include <unordered_map>
2025-09-22 23:13:28 +02:00
#include "imaging/IPixelariumImage.hpp"
2025-06-13 22:23:20 +00:00
namespace pixelarium::resources
{
2025-09-22 23:13:28 +02:00
using ResourceKey = size_t;
2025-09-26 21:09:51 +02:00
/// @brief A dedicated exception to be thrown when a resource of an IResourcePool is empty.
2025-09-22 23:13:28 +02:00
struct empty_resource_exception : public std::exception
{
empty_resource_exception() {};
2025-09-23 21:57:08 +02:00
empty_resource_exception(std::string& msg) : message_(msg) {};
const std::string& what() { return message_; }
2025-09-22 23:13:28 +02:00
private:
2025-09-23 21:57:08 +02:00
std::string message_ = "Empty Resource";
2025-09-22 23:13:28 +02:00
};
2025-09-26 21:09:51 +02:00
/// @brief Abstract representation of a Resource.
/// This is meant to be implemented by arbitrary explicit resource types and thus
/// gives no contract other than the abstract type.
2025-06-13 22:23:20 +00:00
struct IResource
{
2025-07-28 08:46:16 +00:00
virtual ~IResource() = default;
2025-06-13 22:23:20 +00:00
};
2025-09-26 21:09:51 +02:00
/// @brief Defines a concept for a resource type
/// @tparam R The resource template parameter
2025-06-13 22:23:20 +00:00
template <typename R>
concept ResT = requires(R& r) { static_cast<IResource&>(r); };
2025-09-26 21:09:51 +02:00
/// @brief Defines an interface for a resource pool
/// @tparam ResT defines the resource type that is accepted by the pool
2025-08-18 22:39:43 +00:00
template <typename ResT>
2025-06-13 22:23:20 +00:00
class IResourcePool
{
public:
virtual ~IResourcePool() = default;
2025-09-23 21:57:08 +02:00
virtual std::weak_ptr<ResT> GetResource(size_t id) const = 0;
2025-09-13 14:49:59 +02:00
virtual ResourceKey SetResource(std::unique_ptr<ResT> res) = 0;
virtual bool ModifyResource(ResourceKey id, std::unique_ptr<ResT> res) = 0;
virtual bool DeleteResource(ResourceKey id) = 0;
2025-09-22 23:13:28 +02:00
virtual void EnumerateResources(
const std::function<void(ResourceKey, size_t, const imaging::IPixelariumImage&)>& func) = 0;
2025-08-18 22:39:43 +00:00
virtual size_t GetTotalSize() const = 0;
2025-09-22 23:13:28 +02:00
virtual void Clear() = 0;
2025-06-13 22:23:20 +00:00
};
// 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.
2025-09-22 23:13:28 +02:00
class ImageResourcePool : public IResourcePool<imaging::IPixelariumImage>
2025-06-13 22:23:20 +00:00
{
public:
ImageResourcePool() = default;
ImageResourcePool(ImageResourcePool&) = delete;
ImageResourcePool(const ImageResourcePool&) = delete;
ImageResourcePool(ImageResourcePool&&) = delete;
ImageResourcePool& operator=(ImageResourcePool&) = delete;
ImageResourcePool& operator=(ImageResourcePool&&) = delete;
2025-09-23 21:57:08 +02:00
std::weak_ptr<imaging::IPixelariumImage> GetResource(ResourceKey id) const override;
2025-09-22 23:13:28 +02:00
ResourceKey SetResource(std::unique_ptr<imaging::IPixelariumImage> res) override;
bool ModifyResource(ResourceKey id, std::unique_ptr<imaging::IPixelariumImage> res) override;
2025-09-13 14:49:59 +02:00
bool DeleteResource(ResourceKey id) override;
2025-09-22 23:13:28 +02:00
void Clear() override { this->resources_.clear(); }
2025-06-13 22:23:20 +00:00
2025-09-22 23:13:28 +02:00
void EnumerateResources(
const std::function<void(ResourceKey, size_t, const imaging::IPixelariumImage&)>& func) override;
2025-06-13 22:23:20 +00:00
2025-08-18 22:39:43 +00:00
template <typename Callable>
2025-09-22 23:13:28 +02:00
requires std::invocable<Callable, ResourceKey, size_t, const imaging::IPixelariumImage&>
2025-08-18 22:39:43 +00:00
void Enumerate(Callable&& func) const
{
2025-09-13 14:49:59 +02:00
size_t idx{0};
2025-08-18 22:39:43 +00:00
for (const auto& e : this->resources_)
{
2025-09-13 14:49:59 +02:00
func(e.first, idx, *e.second);
2025-08-18 22:39:43 +00:00
}
}
size_t GetTotalSize() const override { return resources_.size(); }
2025-06-13 22:23:20 +00:00
private:
2025-09-22 23:13:28 +02:00
std::unordered_map<size_t, std::shared_ptr<imaging::IPixelariumImage>> resources_;
2025-09-13 14:49:59 +02:00
std::mutex mut_;
2025-06-13 22:23:20 +00:00
};
} // namespace pixelarium::resources