Libftpp
A modern C++ library
pool.hpp
Go to the documentation of this file.
1 #ifndef POOL_HPP
2 #define POOL_HPP
3 
4 #include <cstddef>
5 #include <stack>
6 #include <stdexcept>
7 #include <vector>
8 
35 template <typename TType>
36 class Pool
37 {
38 public:
44  class Object
45  {
46  friend class Pool<TType>;
47 
48  private:
49  Pool<TType>* _pool_ptr;
50  int _idx;
51  std::aligned_storage_t<sizeof(TType), alignof(TType)> _myself;
52 
53  template <typename... TArgs>
54  Object(Pool<TType>* pool, int index, TArgs&&... p_args);
55 
56  public:
57  Object();
58  virtual ~Object();
59 
60  TType* operator->();
61  TType* get();
62  };
63 
64  Pool(size_t size = 0);
65  ~Pool();
66 
71  void resize(size_t numberOfObjectStored);
72 
86  template <typename... TArgs>
87  Object& acquire(TArgs&&... p_args);
88 
93  void release(Object& obj);
94 
95 private:
96  std::stack<int> _available;
97  std::vector<Object> _objects;
98 };
99 
100 #include "pool.tpp"
101 
102 #endif
Wrapper around a TType object managed by the Pool. Provides access to the underlying TType object.
Definition: pool.hpp:45
TType * get()
virtual ~Object()
TType * operator->()
Pool of reusable memory.
Definition: pool.hpp:37
Pool(size_t size=0)
void resize(size_t numberOfObjectStored)
Pre-allocates memory for a given number of objects.
Object & acquire(TArgs &&... p_args)
Acquires an object from the pool.
void release(Object &obj)
Releases an object back into the pool. Destroys the contained TType object and return its slot to the...