tesseract  4.00.00dev
tesseract::ObjectCache< T > Class Template Reference

#include <object_cache.h>

Public Member Functions

 ObjectCache ()
 
 ~ObjectCache ()
 
T * Get (STRING id, TessResultCallback< 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 ( )
inline

Definition at line 37 of file object_cache.h.

37 {}

◆ ~ObjectCache()

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

Definition at line 38 of file object_cache.h.

38  {
39  mu_.Lock();
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.string());
46  } else {
47  delete cache_[i].object;
48  cache_[i].object = NULL;
49  }
50  }
51  mu_.Unlock();
52  }
#define tprintf(...)
Definition: tprintf.h:31
int size() const
Definition: genericvector.h:72
int count(LIST var_list)
Definition: oldlist.cpp:103

Member Function Documentation

◆ DeleteUnusedObjects()

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

Definition at line 100 of file object_cache.h.

100  {
101  mu_.Lock();
102  for (int i = cache_.size() - 1; i >= 0; i--) {
103  if (cache_[i].count <= 0) {
104  delete cache_[i].object;
105  cache_.remove(i);
106  }
107  }
108  mu_.Unlock();
109  }
void remove(int index)
int size() const
Definition: genericvector.h:72
int count(LIST var_list)
Definition: oldlist.cpp:103

◆ Free()

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

Definition at line 86 of file object_cache.h.

86  {
87  if (t == NULL) return false;
88  mu_.Lock();
89  for (int i = 0; i < cache_.size(); i++) {
90  if (cache_[i].object == t) {
91  --cache_[i].count;
92  mu_.Unlock();
93  return true;
94  }
95  }
96  mu_.Unlock();
97  return false;
98  }
int size() const
Definition: genericvector.h:72

◆ Get()

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

Definition at line 60 of file object_cache.h.

61  {
62  T *retval = NULL;
63  mu_.Lock();
64  for (int i = 0; i < cache_.size(); i++) {
65  if (id == cache_[i].id) {
66  retval = cache_[i].object;
67  if (cache_[i].object != NULL) {
68  cache_[i].count++;
69  }
70  mu_.Unlock();
71  delete loader;
72  return retval;
73  }
74  }
75  cache_.push_back(ReferenceCount());
76  ReferenceCount &rc = cache_.back();
77  rc.id = id;
78  retval = rc.object = loader->Run();
79  rc.count = (retval != NULL) ? 1 : 0;
80  mu_.Unlock();
81  return retval;
82  }
int push_back(T object)
int size() const
Definition: genericvector.h:72
T & back() const
virtual R Run()=0

The documentation for this class was generated from the following file: