Files
pixelarium/lib/resources/resource.hpp
T

76 lines
2.7 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 <optional>
#include <unordered_map>
#include "imaging/PixelariumImage.hpp"
namespace pixelarium::resources
{
2025-09-13 14:49:59 +02:00
using ResourceKey = size_t;
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
};
template <typename R>
concept ResT = requires(R& r) { static_cast<IResource&>(r); };
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-08-18 22:39:43 +00:00
virtual std::optional<const 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;
virtual void EnumerateResources(const std::function<void(ResourceKey, size_t, const imaging::PixelariumImage&)>& func) = 0;
2025-08-18 22:39:43 +00:00
virtual size_t GetTotalSize() const = 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.
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;
2025-09-13 14:49:59 +02:00
std::optional<const imaging::PixelariumImage*> GetResource(ResourceKey id) const override;
ResourceKey SetResource(std::unique_ptr<imaging::PixelariumImage> res) override;
bool ModifyResource(ResourceKey id, std::unique_ptr<imaging::PixelariumImage> res) override;
bool DeleteResource(ResourceKey id) override;
2025-06-13 22:23:20 +00:00
2025-09-13 14:49:59 +02:00
void EnumerateResources(const std::function<void(ResourceKey, size_t, const imaging::PixelariumImage&)>& func) override;
2025-06-13 22:23:20 +00:00
2025-08-18 22:39:43 +00:00
template <typename Callable>
2025-09-13 14:49:59 +02:00
requires std::invocable<Callable, ResourceKey, size_t, const imaging::PixelariumImage&>
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:
std::unordered_map<size_t, std::unique_ptr<imaging::PixelariumImage>> resources_;
2025-09-13 14:49:59 +02:00
std::mutex mut_;
2025-06-13 22:23:20 +00:00
};
} // namespace pixelarium::resources