initial port for Cevo game in C++ (libs)
This commit is contained in:
commit
8436242032
375 files changed
+46282
No files matched your search
@@ -0,0 +1,105 @@
|
||||
#pragma once
|
||||
|
||||
#include <QMutex>
|
||||
#include <QVector>
|
||||
#include <QWaitCondition>
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
namespace Cevo {
|
||||
|
||||
// Equivalent of Pool.pas's TPool: a simple object pool (Acquire/Release).
|
||||
// Override newItemObject() to customize item construction. NOT thread-safe
|
||||
// by itself (matches the original) - see ThreadedPool below for that.
|
||||
template <typename T>
|
||||
class Pool {
|
||||
public:
|
||||
virtual ~Pool() { setTotalCount(0); }
|
||||
|
||||
int totalCount() const { return m_items.size(); }
|
||||
void setTotalCount(int value)
|
||||
{
|
||||
if (value > m_items.size()) {
|
||||
for (int i = m_items.size(); i < value; ++i) {
|
||||
T *item = newItemObject();
|
||||
m_items.append(item);
|
||||
m_freeItems.append(item);
|
||||
}
|
||||
} else {
|
||||
for (int i = m_items.size() - 1; i >= value; --i) {
|
||||
T *item = m_items.at(i);
|
||||
const int freeIndex = m_freeItems.indexOf(item);
|
||||
if (freeIndex < 0)
|
||||
throw std::logic_error("Pool: shrinking below a still-acquired item");
|
||||
m_freeItems.removeAt(freeIndex);
|
||||
m_items.removeAt(i);
|
||||
delete item;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int usedCount() const { return m_items.size() - m_freeItems.size(); }
|
||||
|
||||
// NOTE: unlike the original (which busy-waits forever if empty, a
|
||||
// same-thread deadlock risk since nothing else runs to free an item),
|
||||
// this throws immediately - see ThreadedPool for the genuinely
|
||||
// multi-threaded blocking-wait behavior.
|
||||
virtual T *acquire()
|
||||
{
|
||||
if (m_freeItems.isEmpty())
|
||||
throw std::runtime_error("Object pool is empty");
|
||||
return m_freeItems.takeLast();
|
||||
}
|
||||
|
||||
virtual void release(T *item)
|
||||
{
|
||||
if (!m_items.contains(item))
|
||||
throw std::invalid_argument("Unknown object for release from pool");
|
||||
m_freeItems.append(item);
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual T *newItemObject() { return new T(); }
|
||||
|
||||
QVector<T *> m_items; // owns all
|
||||
QVector<T *> m_freeItems; // non-owning subset
|
||||
};
|
||||
|
||||
// Equivalent of Pool.pas's TThreadedPool: acquire() blocks (instead of
|
||||
// throwing) while the pool has capacity but no free item, waking up once
|
||||
// another thread calls release() - replaces the original's Sleep(1)
|
||||
// busy-polling loop with a QWaitCondition.
|
||||
template <typename T>
|
||||
class ThreadedPool : public Pool<T> {
|
||||
public:
|
||||
int totalCount() const { QMutexLocker locker(&m_lock); return Pool<T>::totalCount(); }
|
||||
void setTotalCount(int value)
|
||||
{
|
||||
QMutexLocker locker(&m_lock);
|
||||
Pool<T>::setTotalCount(value);
|
||||
}
|
||||
int usedCount() const { QMutexLocker locker(&m_lock); return Pool<T>::usedCount(); }
|
||||
|
||||
T *acquire() override
|
||||
{
|
||||
QMutexLocker locker(&m_lock);
|
||||
if (Pool<T>::totalCount() == 0)
|
||||
throw std::runtime_error("Object pool is empty");
|
||||
while (Pool<T>::m_freeItems.isEmpty())
|
||||
m_releaseWait.wait(&m_lock);
|
||||
return Pool<T>::m_freeItems.takeLast();
|
||||
}
|
||||
|
||||
void release(T *item) override
|
||||
{
|
||||
QMutexLocker locker(&m_lock);
|
||||
Pool<T>::release(item);
|
||||
m_releaseWait.wakeOne();
|
||||
}
|
||||
|
||||
private:
|
||||
mutable QMutex m_lock;
|
||||
QWaitCondition m_releaseWait;
|
||||
};
|
||||
|
||||
} // namespace Cevo
|
||||
Reference in new issue
Block a user