Gerneric image codec abstraction init (#6)
* remove raw pointer from resource manager
* towards generic images
* 💅 and pin libCZI module
* remove raw pointer from resource manager
* towards generic images
* fix rendering
* fix rendering
* fix unit tests
* fix pipeline
* fix gcc build
* re-enable tests
* add czi impl
* remove resource button
* refactor user code app to being a "default app"
* ui resources
* missing lib?
* init czi render support
* typos
This commit is contained in:
committed by
Maximilian Kueffner
parent
bce12b0bb4
commit
0be064bb8e
@@ -6,7 +6,7 @@
|
||||
#include <mutex>
|
||||
#include <optional>
|
||||
|
||||
using pixelarium::imaging::PixelariumImage;
|
||||
using pixelarium::imaging::IPixelariumImage;
|
||||
using namespace std;
|
||||
|
||||
namespace
|
||||
@@ -22,19 +22,24 @@ size_t GenerateId() { return id_.fetch_add(1, memory_order_relaxed); }
|
||||
/// @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(ResourceKey id) const
|
||||
std::optional<std::weak_ptr<IPixelariumImage>> pixelarium::resources::ImageResourcePool::GetResource(ResourceKey id) const
|
||||
{
|
||||
auto search{this->resources_.find(id)};
|
||||
if (search == this->resources_.end()) return std::nullopt;
|
||||
|
||||
return search->second.get();
|
||||
return search->second;
|
||||
}
|
||||
|
||||
/// @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)
|
||||
size_t pixelarium::resources::ImageResourcePool::SetResource(unique_ptr<IPixelariumImage> res)
|
||||
{
|
||||
if (res == nullptr)
|
||||
{
|
||||
throw empty_resource_exception();
|
||||
}
|
||||
|
||||
auto key{::GenerateId()};
|
||||
{
|
||||
std::lock_guard<std::mutex> guard(this->mut_);
|
||||
@@ -49,7 +54,7 @@ size_t pixelarium::resources::ImageResourcePool::SetResource(unique_ptr<Pixelari
|
||||
/// @param res A unique pointer to the new resource.
|
||||
/// @return True if the resource was updated, false otherwise.
|
||||
bool pixelarium::resources::ImageResourcePool::ModifyResource(ResourceKey id,
|
||||
std::unique_ptr<imaging::PixelariumImage> res)
|
||||
std::unique_ptr<imaging::IPixelariumImage> res)
|
||||
{
|
||||
auto search{this->resources_.find(id)};
|
||||
if (search == this->resources_.end()) return false;
|
||||
@@ -76,7 +81,7 @@ bool pixelarium::resources::ImageResourcePool::DeleteResource(ResourceKey id)
|
||||
/// @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(ResourceKey, size_t, const imaging::PixelariumImage&)>& func)
|
||||
const std::function<void(ResourceKey, size_t, const imaging:: IPixelariumImage&)>& func)
|
||||
{
|
||||
size_t idx{0};
|
||||
for (const auto& e : this->resources_)
|
||||
|
||||
+26
-11
@@ -7,11 +7,22 @@
|
||||
#include <optional>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "imaging/PixelariumImage.hpp"
|
||||
#include "imaging/IPixelariumImage.hpp"
|
||||
|
||||
namespace pixelarium::resources
|
||||
{
|
||||
using ResourceKey = size_t;
|
||||
using ResourceKey = size_t;
|
||||
|
||||
struct empty_resource_exception : public std::exception
|
||||
{
|
||||
empty_resource_exception() {};
|
||||
empty_resource_exception(const char* msg) : message_(msg) {};
|
||||
const char* what() { return message_; }
|
||||
|
||||
private:
|
||||
const char* message_ = "Empty Resource";
|
||||
};
|
||||
|
||||
struct IResource
|
||||
{
|
||||
virtual ~IResource() = default;
|
||||
@@ -25,12 +36,14 @@ class IResourcePool
|
||||
{
|
||||
public:
|
||||
virtual ~IResourcePool() = default;
|
||||
virtual std::optional<const ResT*> GetResource(size_t id) const = 0;
|
||||
virtual std::optional<std::weak_ptr<ResT>> GetResource(size_t id) const = 0;
|
||||
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;
|
||||
virtual void EnumerateResources(
|
||||
const std::function<void(ResourceKey, size_t, const imaging::IPixelariumImage&)>& func) = 0;
|
||||
virtual size_t GetTotalSize() const = 0;
|
||||
virtual void Clear() = 0;
|
||||
};
|
||||
|
||||
// Now with the =GetResource= method, I do not want to transfer ownership to the caller of that method. The ownership
|
||||
@@ -38,7 +51,7 @@ class IResourcePool
|
||||
// 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>
|
||||
class ImageResourcePool : public IResourcePool<imaging::IPixelariumImage>
|
||||
{
|
||||
public:
|
||||
ImageResourcePool() = default;
|
||||
@@ -48,15 +61,17 @@ class ImageResourcePool : public IResourcePool<imaging::PixelariumImage>
|
||||
ImageResourcePool& operator=(ImageResourcePool&) = delete;
|
||||
ImageResourcePool& operator=(ImageResourcePool&&) = delete;
|
||||
|
||||
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;
|
||||
std::optional<std::weak_ptr<imaging::IPixelariumImage>> GetResource(ResourceKey id) const override;
|
||||
ResourceKey SetResource(std::unique_ptr<imaging::IPixelariumImage> res) override;
|
||||
bool ModifyResource(ResourceKey id, std::unique_ptr<imaging::IPixelariumImage> res) override;
|
||||
bool DeleteResource(ResourceKey id) override;
|
||||
void Clear() override { this->resources_.clear(); }
|
||||
|
||||
void EnumerateResources(const std::function<void(ResourceKey, size_t, const imaging::PixelariumImage&)>& func) override;
|
||||
void EnumerateResources(
|
||||
const std::function<void(ResourceKey, size_t, const imaging::IPixelariumImage&)>& func) override;
|
||||
|
||||
template <typename Callable>
|
||||
requires std::invocable<Callable, ResourceKey, size_t, const imaging::PixelariumImage&>
|
||||
requires std::invocable<Callable, ResourceKey, size_t, const imaging::IPixelariumImage&>
|
||||
void Enumerate(Callable&& func) const
|
||||
{
|
||||
size_t idx{0};
|
||||
@@ -69,7 +84,7 @@ class ImageResourcePool : public IResourcePool<imaging::PixelariumImage>
|
||||
size_t GetTotalSize() const override { return resources_.size(); }
|
||||
|
||||
private:
|
||||
std::unordered_map<size_t, std::unique_ptr<imaging::PixelariumImage>> resources_;
|
||||
std::unordered_map<size_t, std::shared_ptr<imaging::IPixelariumImage>> resources_;
|
||||
std::mutex mut_;
|
||||
};
|
||||
} // namespace pixelarium::resources
|
||||
|
||||
Reference in New Issue
Block a user