Files
m-aXimilian c00c2c71ac
MegaLinter / MegaLinter (push) Has been cancelled
CI Workflow / build-ubuntu (push) Has been cancelled
CI Workflow / build-windows (push) Has been cancelled
CI Workflow / generate-docs (push) Has been cancelled
build system and module refactoring + simple histogram scratch (#20)
* 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
2026-02-16 20:36:48 +01:00

93 lines
2.6 KiB
C++

#include "resource.hpp"
#include <atomic>
#include <cstddef>
#include <functional>
#include <mutex>
#include "IPixelariumImage.hpp"
using Image = pixelarium::imaging::IPixelariumImageCvMat;
using namespace std;
namespace
{
/// @brief Atomic counter for generating unique IDs.
static std::atomic<size_t> id_;
/// @brief Generates a unique ID.
/// @return A unique ID.
size_t GenerateId() { return id_.fetch_add(1, memory_order_relaxed); }
} // namespace
/// @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::weak_ptr<Image> pixelarium::resources::ImageResourcePool::GetResource(ResourceKey id) const
{
auto search{this->resources_.find(id)};
if (search == this->resources_.end()) return {};
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<Image> res)
{
if (res == nullptr)
{
throw empty_resource_exception();
}
auto key{::GenerateId()};
{
std::lock_guard<std::mutex> guard(this->mut_);
this->resources_.insert({key, std::move(res)});
}
return key;
}
/// @brief Updates a resource in the pool.
/// @param id The ID of the resource to update.
/// @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<Image> res)
{
auto search{this->resources_.find(id)};
if (search == this->resources_.end()) return false;
search->second = std::move(res);
return true;
}
/// @brief Deletes a resource from the pool.
/// @param id The ID of the resource to delete.
/// @return True if the resource was deleted, false otherwise.
bool pixelarium::resources::ImageResourcePool::DeleteResource(ResourceKey id)
{
auto search{this->resources_.find(id)};
if (search == this->resources_.end()) return false;
this->resources_.erase(search);
return true;
}
/// @brief Enumerates all resources in the pool.
/// @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::IPixelariumImage<cv::Mat>&)>& func)
{
size_t idx{0};
for (const auto& e : this->resources_)
{
func(e.first, idx, *e.second);
++idx;
}
}