Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2020
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.03 KB | None | 0 0
  1. #include <iostream>
  2. #include <memory>
  3. #include <vector>
  4. #include <type_traits>
  5.  
  6.     template<class T, uint Size>
  7.     struct ObjectPool
  8.     {
  9.         using value_type = T;
  10.         using pointer = value_type *;
  11.  
  12.         ObjectPool()
  13.         {
  14.             for (auto i = 1; i < Size; ++i)
  15.                 buffer[i - 1].next = &buffer[i];
  16.  
  17.             nextFreeItem = &buffer[0];
  18.         }
  19.  
  20.         ObjectPool(const ObjectPool&) = delete;
  21.  
  22.         ObjectPool(ObjectPool&& other) noexcept
  23.             : buffer{ std::move(other.buffer) }
  24.             , nextFreeItem{ other.nextFreeItem }
  25.         {
  26.             other.nextFreeItem = nullptr;
  27.         }
  28.  
  29.         ~ObjectPool() = default;
  30.  
  31.         template<typename U>
  32.         struct rebind
  33.         {
  34.             typedef ObjectPool<U, Size> other;
  35.         };
  36.  
  37.         template<typename U, uint other_capacity>
  38.         ObjectPool(const ObjectPool<U, other_capacity>& other) {}
  39.  
  40.         [[nodiscard]] pointer allocate(uint size = 0)
  41.         {
  42.             std::cout << "ObjectPool: allocate " << size << "\n";
  43.             if (nextFreeItem == nullptr)
  44.                 throw std::bad_alloc{};
  45.  
  46.             const auto item = nextFreeItem;
  47.             nextFreeItem = item->next;
  48.  
  49.             return reinterpret_cast<pointer>(&item->storage);
  50.         }
  51.  
  52.         void deallocate(pointer p, uint = 0) noexcept
  53.         {
  54.             std::cout << "ObjectPool: deallocate\n";
  55.             const auto item = reinterpret_cast<Item*>(p);
  56.  
  57.             item->next = nextFreeItem;
  58.             nextFreeItem = item;
  59.         }
  60.  
  61.         template<typename U, typename ...Args>
  62.         void construct(U* mem, Args&& ...args)
  63.         {
  64.             std::cout << "ObjectPool: construct\n";
  65.            new (mem) value_type(std::forward<Args>(args)...);
  66.         }
  67.  
  68.         template<typename U>
  69.         void destroy(U* mem) noexcept
  70.         {
  71.             std::cout << "ObjectPool: destroy\n";
  72.             if (mem == nullptr)
  73.                 return;
  74.  
  75.             mem->~value_type();
  76.             //deallocate(mem, 0);
  77.         }
  78.  
  79.         ObjectPool& operator =(const ObjectPool&) = delete;
  80.  
  81.         ObjectPool& operator =(ObjectPool&& other) noexcept
  82.         {
  83.             if (this == &other)
  84.                 return *this;
  85.  
  86.             buffer = std::move(other.buffer);
  87.             nextFreeItem = other.nextFreeItem;
  88.  
  89.             other.nextFreeItem = nullptr;
  90.  
  91.             return *this;
  92.         }
  93.  
  94.     private:
  95.         union Item
  96.         {
  97.             std::aligned_storage_t<sizeof(value_type), alignof(value_type)> storage;
  98.             Item* next;
  99.         };
  100.  
  101.         std::unique_ptr<Item[]> buffer = std::make_unique<Item[]>(Size);
  102.         Item* nextFreeItem = nullptr;
  103.     };
  104.  
  105.     int main()
  106.     {
  107.         std::vector<int, ObjectPool<int, 5>> pool;
  108.  
  109.         pool.push_back(5);
  110.         pool.push_back(3);
  111.         pool.push_back(523);
  112.  
  113.         for(const auto& p : pool) {
  114.             std::cout << p << std::endl;
  115.         }
  116.  
  117.         pool.pop_back();
  118.  
  119.         for(const auto& p : pool) {
  120.             std::cout << p << std::endl;
  121.         }
  122.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement