HOG2
vectorCache.h
Go to the documentation of this file.
1 /*
2  * vectorCache.h
3  * games
4  *
5  * Created by Nathan Sturtevant on 8/25/10.
6  * Copyright 2010 NS Software. All rights reserved.
7  *
8  */
9 
10 #ifndef VECTORCACHE_H
11 #define VECTORCACHE_H
12 
13 template<class storage>
14 class vectorCache {
15 public:
16  vectorCache() { count = 0; }
17  ~vectorCache();
18  vectorCache(const vectorCache<storage> &v) { count = 0; freeList.resize(0); }
20  std::vector<storage> *getItem();
21  void returnItem(std::vector<storage> *);
22 private:
23  std::vector<std::vector<storage> *> freeList;
24  int count;
25 };
26 
27 
28 template<class storage>
30 {
31 // printf("Cached storage destroyed\n");
32  for (unsigned int x = 0; x < freeList.size(); x++)
33  delete freeList[x];
34  freeList.resize(0);
35  count = 0;
36 }
37 
38 template<class storage>
39 std::vector<storage> *vectorCache<storage>::getItem()
40 {
41  if (freeList.size() > 0)
42  {
43  std::vector<storage> *theItem = freeList.back();
44  freeList.pop_back();
45 // printf("CACHE: REALLOC: %p\n", theItem);
46  return theItem;
47  }
48  else {
49 // printf("%d items allocated\n", ++count);
50  std::vector<storage> *newItem = new std::vector<storage>();
51 // printf("CACHE: ALLOC: %p\n", newItem);
52  return newItem;
53 // theCache.resize(theCache.size()+1);
54 // printf("CACHE: ALLOC: %p\n", &theCache[theCache.size()-1]);
55 // return &theCache[theCache.size()-1];
56  }
57 // printf("CACHE: PTR: %p\n", (void*)0);
58  return 0;
59 }
60 
61 template<class storage>
62 void vectorCache<storage>::returnItem(std::vector<storage> *item)
63 {
64 // printf("CACHE: FREE: %p\n", item);
65  item->clear();
66  freeList.push_back(item);
67 }
68 
69 #endif
vectorCache::vectorCache
vectorCache()
Definition: vectorCache.h:16
vectorCache::~vectorCache
~vectorCache()
Definition: vectorCache.h:29
vectorCache::returnItem
void returnItem(std::vector< storage > *)
Definition: vectorCache.h:62
vectorCache::getItem
std::vector< storage > * getItem()
Definition: vectorCache.h:39
vectorCache::count
int count
Definition: vectorCache.h:24
vectorCache
Definition: vectorCache.h:14
vectorCache::operator=
vectorCache< storage > & operator=(const vectorCache< storage > &v)=delete
vectorCache::freeList
std::vector< std::vector< storage > * > freeList
Definition: vectorCache.h:23
vectorCache::vectorCache
vectorCache(const vectorCache< storage > &v)
Definition: vectorCache.h:18