Files
pixelarium/lib/resources/resource.cpp
T

93 lines
2.6 KiB
C++
Raw Normal View History

2025-06-13 22:23:20 +00:00
#include "resource.hpp"
#include <atomic>
2025-09-13 14:49:59 +02:00
#include <cstddef>
2025-08-18 22:39:43 +00:00
#include <functional>
2025-09-13 14:49:59 +02:00
#include <mutex>
2025-06-13 22:23:20 +00:00
2026-01-23 23:00:35 +00:00
#include "imaging/IPixelariumImage.hpp"
using Image = pixelarium::imaging::IPixelariumImageCvMat;
2025-06-13 22:23:20 +00:00
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.
2026-01-23 23:00:35 +00:00
std::weak_ptr<Image> pixelarium::resources::ImageResourcePool::GetResource(ResourceKey id) const
2025-06-13 22:23:20 +00:00
{
auto search{this->resources_.find(id)};
2025-09-23 21:57:08 +02:00
if (search == this->resources_.end()) return {};
2025-06-13 22:23:20 +00:00
2025-09-22 23:13:28 +02:00
return search->second;
2025-06-13 22:23:20 +00:00
}
/// @brief Sets a resource in the pool.
/// @param res A unique pointer to the resource to set.
/// @return The ID of the new resource.
2026-01-23 23:00:35 +00:00
size_t pixelarium::resources::ImageResourcePool::SetResource(unique_ptr<Image> res)
2025-06-13 22:23:20 +00:00
{
2025-09-22 23:13:28 +02:00
if (res == nullptr)
{
throw empty_resource_exception();
}
2025-06-13 22:23:20 +00:00
auto key{::GenerateId()};
2025-09-13 14:49:59 +02:00
{
std::lock_guard<std::mutex> guard(this->mut_);
this->resources_.insert({key, std::move(res)});
}
2025-06-13 22:23:20 +00:00
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.
2026-01-23 23:00:35 +00:00
bool pixelarium::resources::ImageResourcePool::ModifyResource(ResourceKey id, std::unique_ptr<Image> res)
2025-06-13 22:23:20 +00:00
{
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.
2025-09-13 14:49:59 +02:00
bool pixelarium::resources::ImageResourcePool::DeleteResource(ResourceKey id)
2025-06-13 22:23:20 +00:00
{
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(
2026-01-23 23:00:35 +00:00
const std::function<void(ResourceKey, size_t, const imaging::IPixelariumImage<cv::Mat>&)>& func)
2025-06-13 22:23:20 +00:00
{
2025-09-13 14:49:59 +02:00
size_t idx{0};
2025-06-13 22:23:20 +00:00
for (const auto& e : this->resources_)
{
2025-09-13 14:49:59 +02:00
func(e.first, idx, *e.second);
++idx;
2025-06-13 22:23:20 +00:00
}
}