GlobeEngine
Cache.h
Go to the documentation of this file.
1 
9 #ifndef GlobeEngine_Cache_h
10 #define GlobeEngine_Cache_h
11 
12 #include "OpenGL_Includes.h"
13 #include <stdio.h>
14 #include <memory>
15 #include <vector>
16 
17 namespace geSpatial {
18 
19  template <class T> class Cache
20  {
21  public:
23  {
24  capacity = 0;
25  clear();
26  }
27 
29  {
30  deleteFrontbuffer();
31  deleteBackbuffer();
32  capacity = 0;
33  }
34 
35  void clear()
36  {
37  deleteFrontbuffer();
38  deleteBackbuffer();
39  cacheIndex = 0;
40  capacity = 0;
41  }
42 
43  void create(int _capacity)
44  {
45  setCapacity(_capacity);
46  }
47 
48  void swap()
49  {
50  deleteFrontbuffer();
51  frontbuffer = backbuffer;
52  //backbuffer = new T[capacity];
53  deleteBackbuffer();
54  allocateBackbuffer();
55  }
56 
57  std::shared_ptr<T> requestCacheWritePointer()
58  //T* requestCacheWritePointer()
59  {
60  unsigned int res = cacheIndex;
61  cacheIndex++;
62  //return &backbuffer[res];
63  return backbuffer[res];
64  }
65 
66  const std::vector< std::shared_ptr<T> >& getFrontBuffer()
67  //T* getFrontBuffer()
68  {
69  return frontbuffer;
70  }
71 
72  void setCapacity(unsigned int _capacity)
73  {
74  clear();
75  capacity = _capacity;
76  allocateCache();
77  }
78 
80  return cacheIndex;
81  }
82 
83  private:
84  void allocateCache() {
85  for (int i=0;i<capacity;i++) {
86  std::shared_ptr<T> frontTile = std::make_shared<T>();
87  frontbuffer.push_back(frontTile);
88  }
89  allocateBackbuffer();
90  //frontbuffer = new T[capacity];
91  //backbuffer = new T[capacity];
92  }
93 
94  void allocateBackbuffer() {
95  for (int i=0;i<capacity;i++) {
96  std::shared_ptr<T> backTile = std::make_shared<T>();
97  backbuffer.push_back(backTile);
98  }
99  }
100 
101  void deleteFrontbuffer() {
102  /*if (this->frontbuffer != NULL) {
103  delete [] this->frontbuffer;
104  this->frontbuffer = NULL;
105  }*/
106  if (this->frontbuffer.size() != 0) {
107  this->frontbuffer.clear();
108  }
109  }
110 
111  void deleteBackbuffer() {
112 // if (backbuffer != NULL) {
113 // delete [] backbuffer;
114 // backbuffer = NULL;
115 // }
116  if (this->backbuffer.size() != 0) {
117  this->backbuffer.clear();
118  }
119  }
120 
121  std::vector< std::shared_ptr<T> > frontbuffer;
122  std::vector< std::shared_ptr<T> > backbuffer;
123  //T* frontbuffer;
124  //T* backbuffer;
125  unsigned int cacheIndex;
126  unsigned int capacity;
127  };
128 }
129 #endif
void clear()
Definition: Cache.h:35
const std::vector< std::shared_ptr< T > > & getFrontBuffer()
Definition: Cache.h:66
Cache()
Definition: Cache.h:22
int getObjectCount()
Definition: Cache.h:79
Definition: Cache.h:19
void setCapacity(unsigned int _capacity)
Definition: Cache.h:72
~Cache()
Definition: Cache.h:28
void create(int _capacity)
Definition: Cache.h:43
std::shared_ptr< T > requestCacheWritePointer()
Definition: Cache.h:57
void swap()
Definition: Cache.h:48
Definition: Cache.h:17