mirror of
https://gitlab.com/hashborgir/d2tweaks-rnd2k.git
synced 2025-10-13 16:34:22 -05:00
Initial commit
This commit is contained in:
41
include/fw/pool.h
Normal file
41
include/fw/pool.h
Normal file
@@ -0,0 +1,41 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <queue>
|
||||
|
||||
template<typename T>
|
||||
class growing_object_pool {
|
||||
std::queue<T*> m_objects;
|
||||
uint32_t m_objects_count;
|
||||
T* (*m_factory)();
|
||||
public:
|
||||
explicit growing_object_pool(T* (*factory)(), uint32_t initialSize = 0) {
|
||||
m_factory = factory;
|
||||
|
||||
m_objects_count = initialSize;
|
||||
for (size_t i = 0; i < initialSize; i++) {
|
||||
m_objects.push(m_factory());
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t get_count() const {
|
||||
return m_objects_count;
|
||||
}
|
||||
|
||||
T* get() {
|
||||
if (m_objects.empty()) {
|
||||
m_objects.push(m_factory());
|
||||
m_objects_count++;
|
||||
}
|
||||
|
||||
auto result = m_objects.front();
|
||||
|
||||
m_objects.pop();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void put(T* obj) {
|
||||
m_objects.push(obj);
|
||||
}
|
||||
};
|
21
include/fw/singleton.h
Normal file
21
include/fw/singleton.h
Normal file
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
template<typename T>
|
||||
class singleton {
|
||||
public:
|
||||
static T& instance();
|
||||
|
||||
singleton(const singleton&) = delete;
|
||||
singleton& operator= (singleton) = delete;
|
||||
|
||||
protected:
|
||||
struct token {};
|
||||
singleton() = default;
|
||||
};
|
||||
|
||||
#include <memory>
|
||||
template<typename T>
|
||||
T& singleton<T>::instance() {
|
||||
static const std::unique_ptr<T> instance{ new T{token{}} };
|
||||
return *instance;
|
||||
}
|
Reference in New Issue
Block a user