19#ifndef TESSERACT_CCUTIL_OBJECT_CACHE_H_
20#define TESSERACT_CCUTIL_OBJECT_CACHE_H_
40 std::lock_guard<std::mutex> guard(mu_);
41 for (
auto &it : cache_) {
44 "ObjectCache(%p)::~ObjectCache(): WARNING! LEAK! object %p "
45 "still has count %d (id %s)\n",
46 static_cast<void *
>(
this),
static_cast<void *
>(it.object),
47 it.count, it.id.c_str());
61 T *
Get(
const std::string &
id, std::function<T *()> loader) {
63 std::lock_guard<std::mutex> guard(mu_);
64 for (
auto &it : cache_) {
67 if (it.object !=
nullptr) {
73 cache_.push_back(ReferenceCount());
74 ReferenceCount &rc = cache_.back();
76 retval = rc.object = loader();
77 rc.count = (retval !=
nullptr) ? 1 : 0;
87 std::lock_guard<std::mutex> guard(mu_);
88 for (
auto &it : cache_) {
98 std::lock_guard<std::mutex> guard(mu_);
99 cache_.erase(std::remove_if(cache_.begin(), cache_.end(),
100 [](
const ReferenceCount &it) {
112 struct ReferenceCount {
119 std::vector<ReferenceCount> cache_;
void tprintf(const char *format,...)
void DeleteUnusedObjects()
T * Get(const std::string &id, std::function< T *()> loader)