Files
pixelarium/lib/resources/resource.hpp
T
m-aXimilian d71f4168fb some decouplings (#2)
* logger business

* code review

* rm c-style array

* extract image rendering to view

* missing view files

* init view abstractions

* leverage concepts and provide a templateized Enumerate function

* RVO

* get rid of some warnings

* logger business

* code review

* rm c-style array

* extract image rendering to view

* missing view files

* init view abstractions

* leverage concepts and provide a templateized Enumerate function

* RVO

* get rid of some warnings

* logger business

* code review

* rm c-style array

* init view abstractions

* leverage concepts and provide a templateized Enumerate function

* RVO

* get rid of some warnings

* logger business

* code review

* rm c-style array

* init view abstractions

* leverage concepts and provide a templateized Enumerate function

* RVO

* get rid of some warnings

* Factor out AppGLFW base class

The intention here is to get rid of scaffolding in the consumer
application class and allow to focus on the "important bits".

* Some cleanup

* dump unnecessary dependency

* link stuff where needed & use only what's needed

* remove deprecation warnings

* add gallery toggle

* make some includes private

* add presets

* remove dir locals, use presets instead

`projectile-configure-project' in conjunction with CMakePresets.json
will allow to configure the project.
`projectile-compile-project' does sth similar for the compile command.

This is equivalent to something like
~cmake --preset clang-debug && cmake --build build --preset clang-debug~

* use presets in pipeline

---------

Co-authored-by: m-aXimilian <keuffnermax@gmail.com>
2025-08-18 22:39:43 +00:00

72 lines
2.5 KiB
C++

#pragma once
#include <concepts>
#include <functional>
#include <memory>
#include <optional>
#include <unordered_map>
#include "imaging/PixelariumImage.hpp"
namespace pixelarium::resources
{
struct IResource
{
virtual ~IResource() = default;
};
template <typename R>
concept ResT = requires(R& r) { static_cast<IResource&>(r); };
template <typename ResT>
class IResourcePool
{
public:
virtual ~IResourcePool() = default;
virtual std::optional<const ResT*> GetResource(size_t id) const = 0;
virtual size_t SetResource(std::unique_ptr<ResT> res) = 0;
virtual bool ModifyResource(size_t id, std::unique_ptr<ResT> res) = 0;
virtual bool DeleteResource(size_t id) = 0;
virtual void EnumerateResources(const std::function<void(size_t, const ResT&)>& func) = 0;
virtual size_t GetTotalSize() const = 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 ModifyResource(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;
template <typename Callable>
requires std::invocable<Callable, size_t, const imaging::PixelariumImage&>
void Enumerate(Callable&& func) const
{
for (const auto& e : this->resources_)
{
func(e.first, *e.second);
}
}
size_t GetTotalSize() const override { return resources_.size(); }
private:
std::unordered_map<size_t, std::unique_ptr<imaging::PixelariumImage>> resources_;
};
} // namespace pixelarium::resources