67 lines
2.2 KiB
C++
67 lines
2.2 KiB
C++
#ifndef CREATURELIB_HISTORYHOLDER_HPP
|
|
#define CREATURELIB_HISTORYHOLDER_HPP
|
|
|
|
#include "../../Library/Exceptions/CreatureException.hpp"
|
|
#include "HistoryElements/HistoryElement.hpp"
|
|
|
|
template <class T> concept HistoryElementType = std::is_base_of<CreatureLib::Battling::HistoryElement, T>::value;
|
|
|
|
namespace CreatureLib::Battling {
|
|
class HistoryHolder {
|
|
size_t _offset;
|
|
size_t _capacity;
|
|
uint8_t* _memory = nullptr;
|
|
HistoryElement* _top = nullptr;
|
|
|
|
static constexpr size_t initialSize = 2048;
|
|
static constexpr size_t stepSize = 1024;
|
|
|
|
public:
|
|
HistoryHolder() : _offset(0), _capacity(2048) {
|
|
auto ptr = malloc(_capacity);
|
|
if (ptr == nullptr) {
|
|
THROW("Out of memory.");
|
|
}
|
|
_memory = static_cast<uint8_t*>(ptr);
|
|
}
|
|
HistoryHolder(const HistoryHolder&) = delete;
|
|
HistoryHolder& operator=(const HistoryHolder&) = delete;
|
|
|
|
~HistoryHolder() {
|
|
if (_top != nullptr)
|
|
_top->Clear();
|
|
free(_memory);
|
|
}
|
|
|
|
template <HistoryElementType T, class... parameters> void Register(parameters... args) {
|
|
if (_offset + sizeof(T) >= _capacity) {
|
|
_capacity += stepSize;
|
|
auto newPtr = realloc(_memory, _capacity);
|
|
if (newPtr == nullptr) {
|
|
THROW("Out of memory.");
|
|
}
|
|
_memory = static_cast<uint8_t*>(newPtr);
|
|
}
|
|
uint8_t* ptr = _memory + _offset;
|
|
T* element = new (ptr) T(args...);
|
|
_offset += sizeof(T);
|
|
element->_previous = _top;
|
|
_top = element;
|
|
}
|
|
|
|
ArbUt::BorrowedPtr<const HistoryElement> GetTopElement() const noexcept { return _top; }
|
|
|
|
ArbUt::BorrowedPtr<const HistoryElement> GetLastUsedAttack() const noexcept {
|
|
const auto* c = _top;
|
|
while (c != nullptr) {
|
|
if (c->GetKind() == HistoryElementKind::AttackUse)
|
|
return c;
|
|
c = c->_previous;
|
|
}
|
|
return nullptr;
|
|
}
|
|
};
|
|
}
|
|
|
|
#endif // CREATURELIB_HISTORYHOLDER_HPP
|