Skip to content

Commit

Permalink
Reusable item pool template class
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Mendenhall committed May 2, 2016
1 parent 45ee8e8 commit 5df08d3
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions GeneralUtils/RefPool.hh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@ using std::vector;
#include <set>
using std::set;

template<class T>
/// Base for "reusable" item pointer pool to avoid over-use of "new"
class ReusePool {
public:
/// Destructor
virtual ~ReusePool() { for(auto i: myPool) delete i; }
/// Get item
virtual T* getItem() { if(!myPool.size()) myPool.push_back(new T); T* i = myPool.back(); myPool.pop_back(); return i; }
/// Return item
virtual void returnItem(T* i) { myPool.push_back(i); }
protected:
vector<T*> myPool;
};

class RefPool;

/// Reference-counted item which returns itself to a pool for re-use
Expand Down

0 comments on commit 5df08d3

Please sign in to comment.