Pool.h 1001 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /*
  2. * 2D Game Engine
  3. * Pool.h:
  4. * Based on pikuma.com 2D game engine in C++ and Lua course
  5. * Copyright (c) 2021 986-Studio. All rights reserved.
  6. *
  7. * Created by Manoël Trapier on 11/02/2021.
  8. */
  9. #ifndef GAMEENGINE_POOL_H
  10. #define GAMEENGINE_POOL_H
  11. #include <vector>
  12. class IPool
  13. {
  14. public:
  15. virtual ~IPool() = default;
  16. };
  17. template <typename T> class Pool:public IPool
  18. {
  19. private:
  20. std::vector<T> data;
  21. public:
  22. Pool(int size = 100) { this->resize(size); }
  23. virtual ~Pool() = default;
  24. bool isEmpty() const { return this->data.empty(); }
  25. uint32_t getSize() { return this->data.size(); }
  26. void resize(uint32_t size) { this->data.resize(size); }
  27. void clear() { this->data.clear(); }
  28. void add(T object) { this->data.push_back(object); }
  29. void set(uint32_t index, T object) { this->data[index] = object; }
  30. T& get(uint32_t index) { return this->data[index]; }
  31. T& operator[](uint32_t index) { return this->data[index]; }
  32. };
  33. #endif /* GAMEENGINE_POOL_H */