tesseract  5.0.0-alpha-619-ge9db
tesseract::ObjectCache< T > Class Template Reference

#include <object_cache.h>

Public Member Functions

 ObjectCache ()=default
 
 ~ObjectCache ()
 
T * Get (STRING id, std::function< T *()> loader)
 
bool Free (T *t)
 
void DeleteUnusedObjects ()
 

Detailed Description

template<typename T>
class tesseract::ObjectCache< T >

Definition at line 35 of file object_cache.h.

Constructor & Destructor Documentation

◆ ObjectCache()

template<typename T>
tesseract::ObjectCache< T >::ObjectCache ( )
default

◆ ~ObjectCache()

template<typename T>
tesseract::ObjectCache< T >::~ObjectCache ( )
inline

Definition at line 38 of file object_cache.h.

38  {
39  std::lock_guard<std::mutex> guard(mu_);
40  for (int i = 0; i < cache_.size(); i++) {
41  if (cache_[i].count > 0) {
42  tprintf("ObjectCache(%p)::~ObjectCache(): WARNING! LEAK! object %p "
43  "still has count %d (id %s)\n",
44  this, cache_[i].object, cache_[i].count,
45  cache_[i].id.c_str());
46  } else {
47  delete cache_[i].object;
48  cache_[i].object = nullptr;
49  }
50  }
51  }

Member Function Documentation

◆ DeleteUnusedObjects()

template<typename T>
void tesseract::ObjectCache< T >::DeleteUnusedObjects ( )
inline

Definition at line 93 of file object_cache.h.

93  {
94  std::lock_guard<std::mutex> guard(mu_);
95  for (int i = cache_.size() - 1; i >= 0; i--) {
96  if (cache_[i].count <= 0) {
97  delete cache_[i].object;
98  cache_.remove(i);
99  }
100  }
101  }

◆ Free()

template<typename T>
bool tesseract::ObjectCache< T >::Free ( T *  t)
inline

Definition at line 81 of file object_cache.h.

81  {
82  if (t == nullptr) return false;
83  std::lock_guard<std::mutex> guard(mu_);
84  for (int i = 0; i < cache_.size(); i++) {
85  if (cache_[i].object == t) {
86  --cache_[i].count;
87  return true;
88  }
89  }
90  return false;
91  }

◆ Get()

template<typename T>
T* tesseract::ObjectCache< T >::Get ( STRING  id,
std::function< T *()>  loader 
)
inline

Definition at line 59 of file object_cache.h.

59  {
60  T *retval = nullptr;
61  std::lock_guard<std::mutex> guard(mu_);
62  for (int i = 0; i < cache_.size(); i++) {
63  if (id == cache_[i].id) {
64  retval = cache_[i].object;
65  if (cache_[i].object != nullptr) {
66  cache_[i].count++;
67  }
68  return retval;
69  }
70  }
71  cache_.push_back(ReferenceCount());
72  ReferenceCount &rc = cache_.back();
73  rc.id = id;
74  retval = rc.object = loader();
75  rc.count = (retval != nullptr) ? 1 : 0;
76  return retval;
77  }

The documentation for this class was generated from the following file:
GenericVector::remove
void remove(int index)
Definition: genericvector.h:765
GenericVector::back
T & back() const
Definition: genericvector.h:728
GenericVector::push_back
int push_back(T object)
Definition: genericvector.h:799
count
int count(LIST var_list)
Definition: oldlist.cpp:79
tprintf
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:34
GenericVector::size
int size() const
Definition: genericvector.h:71