c00c2c71ac
* scratch adding histogram to image views Histograms should come from some sort of histogram service. This is currently just a POC. * custom logger implementation w/o spdlog * missing cmake file * fix tests * use operator<< over direct stream exposure * rm print header * add threading test + refactor towards interface libraries omits the need for =target_include_directories= calls /everywhere/ * rm print header * rm constexpr * templated thread_pool * fix doxyfile * default enable doc building * czi reader refactor * rm erroneous include expression * clang-format * single lib include with PUBLIC visibility * compile imgui stdlib * clang format * documentation update centralize `LogLevelToString` to `ILog.hpp` update docs and examples
98 lines
3.5 KiB
C++
98 lines
3.5 KiB
C++
#pragma once
|
|
|
|
#include <concepts>
|
|
#include <functional>
|
|
#include <memory>
|
|
#include <mutex>
|
|
#include <unordered_map>
|
|
|
|
#include "IPixelariumImage.hpp"
|
|
|
|
namespace pixelarium::resources
|
|
{
|
|
using ResourceKey = size_t;
|
|
|
|
/// @brief A dedicated exception to be thrown when a resource of an IResourcePool is empty.
|
|
struct empty_resource_exception : public std::exception
|
|
{
|
|
empty_resource_exception() {};
|
|
empty_resource_exception(std::string& msg) : message_(msg) {};
|
|
const std::string& what() { return message_; }
|
|
|
|
private:
|
|
std::string message_ = "Empty Resource";
|
|
};
|
|
|
|
/// @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.
|
|
struct IResource
|
|
{
|
|
virtual ~IResource() = default;
|
|
};
|
|
|
|
/// @brief Defines a concept for a resource type
|
|
/// @tparam R The resource template parameter
|
|
template <typename R>
|
|
concept ResT = requires(R& r) { static_cast<IResource&>(r); };
|
|
|
|
/// @brief Defines an interface for a resource pool
|
|
/// @tparam ResT defines the resource type that is accepted by the pool
|
|
template <typename ResT, class Data>
|
|
class IResourcePool
|
|
{
|
|
public:
|
|
virtual ~IResourcePool() = default;
|
|
virtual 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::IPixelariumImage<Data>&)>& 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
|
|
// 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::IPixelariumImageCvMat, cv::Mat>
|
|
{
|
|
public:
|
|
ImageResourcePool() = default;
|
|
ImageResourcePool(ImageResourcePool&) = delete;
|
|
ImageResourcePool(const ImageResourcePool&) = delete;
|
|
ImageResourcePool(ImageResourcePool&&) = delete;
|
|
ImageResourcePool& operator=(ImageResourcePool&) = delete;
|
|
ImageResourcePool& operator=(ImageResourcePool&&) = delete;
|
|
|
|
std::weak_ptr<imaging::IPixelariumImageCvMat> GetResource(ResourceKey id) const override;
|
|
ResourceKey SetResource(std::unique_ptr<imaging::IPixelariumImageCvMat> res) override;
|
|
bool ModifyResource(ResourceKey id, std::unique_ptr<imaging::IPixelariumImageCvMat> res) override;
|
|
bool DeleteResource(ResourceKey id) override;
|
|
void Clear() override { this->resources_.clear(); }
|
|
|
|
void EnumerateResources(
|
|
const std::function<void(ResourceKey, size_t, const imaging::IPixelariumImage<cv::Mat>&)>& func) override;
|
|
|
|
template <typename Callable>
|
|
requires std::invocable<Callable, ResourceKey, size_t, const imaging::IPixelariumImageCvMat&>
|
|
void Enumerate(Callable&& func) const
|
|
{
|
|
size_t idx{0};
|
|
for (const auto& e : this->resources_)
|
|
{
|
|
func(e.first, idx, *e.second);
|
|
}
|
|
}
|
|
|
|
size_t GetTotalSize() const override { return resources_.size(); }
|
|
|
|
private:
|
|
std::unordered_map<size_t, std::shared_ptr<imaging::IPixelariumImageCvMat>> resources_;
|
|
std::mutex mut_;
|
|
};
|
|
} // namespace pixelarium::resources
|