2016-06-26 19:05:09 +00:00
|
|
|
#pragma once
|
|
|
|
#include <memory>
|
|
|
|
#include <functional>
|
|
|
|
#include <stack>
|
|
|
|
#include <mutex>
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
class ObjectPool {
|
|
|
|
public:
|
|
|
|
using ptr_type = std::unique_ptr<T, std::function<void(T*)>>;
|
|
|
|
|
2016-07-27 19:43:09 +00:00
|
|
|
ObjectPool(std::function<std::unique_ptr<T>()> createObject) :
|
2016-06-26 19:05:09 +00:00
|
|
|
createObject(createObject)
|
|
|
|
{}
|
|
|
|
|
|
|
|
virtual ~ObjectPool() {}
|
|
|
|
|
|
|
|
ptr_type acquire() {
|
|
|
|
std::lock_guard<std::mutex> lock(poolMutex);
|
|
|
|
|
|
|
|
if (pool.empty()) {
|
2016-07-27 19:43:09 +00:00
|
|
|
pool.push(createObject());
|
2016-06-26 19:05:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ptr_type tmp(pool.top().release(), [this](T* p) {
|
|
|
|
std::lock_guard<std::mutex> lock(poolMutex);
|
|
|
|
this->pool.push(std::unique_ptr<T>(p));
|
|
|
|
});
|
|
|
|
pool.pop();
|
|
|
|
return std::move(tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool empty() const {
|
|
|
|
std::lock_guard<std::mutex> lock(poolMutex);
|
|
|
|
return pool.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t size() const {
|
|
|
|
std::lock_guard<std::mutex> lock(poolMutex);
|
|
|
|
return pool.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::function<T*()> createObject;
|
|
|
|
std::stack<std::unique_ptr<T>> pool;
|
|
|
|
mutable std::mutex poolMutex;
|
|
|
|
};
|