mirror of
https://codeberg.org/comaps/comaps
synced 2025-12-22 22:23:44 +00:00
Organic Maps sources as of 02.04.2025 (fad26bbf22ac3da75e01e62aa01e5c8e11861005)
To expand with full Organic Maps and Maps.ME commits history run: git remote add om-historic [om-historic.git repo url] git fetch --tags om-historic git replace squashed-history historic-commits
This commit is contained in:
89
drape/object_pool.hpp
Normal file
89
drape/object_pool.hpp
Normal file
@@ -0,0 +1,89 @@
|
||||
#pragma once
|
||||
|
||||
#include "base/assert.hpp"
|
||||
#include "base/logging.hpp"
|
||||
|
||||
#include <list>
|
||||
#include <mutex>
|
||||
#include <set>
|
||||
#include <string>
|
||||
|
||||
#define LOG_OBJECT_POOL
|
||||
|
||||
namespace dp
|
||||
{
|
||||
template<typename T, typename Factory>
|
||||
class ObjectPool final
|
||||
{
|
||||
public:
|
||||
ObjectPool(int count, Factory const & f)
|
||||
: m_factory(f)
|
||||
{
|
||||
for (int i = 0; i < count; ++i)
|
||||
{
|
||||
T *novice = m_factory.GetNew();
|
||||
#if defined(DEBUG) || defined(LOG_OBJECT_POOL)
|
||||
m_checkerSet.insert(novice);
|
||||
#endif
|
||||
m_pool.push_back(novice);
|
||||
}
|
||||
}
|
||||
|
||||
~ObjectPool()
|
||||
{
|
||||
for (auto it = m_pool.begin(); it != m_pool.end(); it++)
|
||||
{
|
||||
T * cur = *it;
|
||||
#if defined(DEBUG) || defined(LOG_OBJECT_POOL)
|
||||
auto its = m_checkerSet.find(cur);
|
||||
static std::string const kMessage = "The same element has been returned twice or more!";
|
||||
ASSERT(its != m_checkerSet.end(), (kMessage));
|
||||
if (its == m_checkerSet.end())
|
||||
LOG(LWARNING, (kMessage));
|
||||
m_checkerSet.erase(its);
|
||||
#endif
|
||||
delete cur;
|
||||
}
|
||||
#if defined(DEBUG) || defined(LOG_OBJECT_POOL)
|
||||
static std::string const kMessage2 = "Not all elements were returned to pool!";
|
||||
ASSERT(m_checkerSet.empty(), (kMessage2));
|
||||
if (!m_checkerSet.empty())
|
||||
LOG(LWARNING, (kMessage2));
|
||||
#endif
|
||||
}
|
||||
|
||||
T * Get()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_lock);
|
||||
if (m_pool.empty())
|
||||
{
|
||||
T * novice = m_factory.GetNew();
|
||||
#if defined(DEBUG) || defined(LOG_OBJECT_POOL)
|
||||
m_checkerSet.insert(novice);
|
||||
#endif
|
||||
return novice;
|
||||
}
|
||||
else
|
||||
{
|
||||
T * pt = m_pool.front();
|
||||
m_pool.pop_front();
|
||||
return pt;
|
||||
}
|
||||
}
|
||||
|
||||
void Return(T * object)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_lock);
|
||||
m_pool.push_back(object);
|
||||
}
|
||||
|
||||
private:
|
||||
#if defined(DEBUG) || defined(LOG_OBJECT_POOL)
|
||||
std::set<T *> m_checkerSet;
|
||||
#endif
|
||||
std::list<T *> m_pool;
|
||||
Factory m_factory;
|
||||
std::mutex m_lock;
|
||||
};
|
||||
} // namespace dp
|
||||
|
||||
Reference in New Issue
Block a user