Files
ss/include/egg/prim/eggBuffer.h
T
Elijah Thomas 26af4db82d update from dtk-template - clangd :) (#66)
* update from dtk-template and start work towards using clangd

* include <a> -> "a"

* Update build.yml

* remove/add non-trivial class in union warning
2024-10-16 15:36:02 -04:00

73 lines
1.4 KiB
C++

#ifndef EGG_BUFFER_H
#define EGG_BUFFER_H
#include "common.h"
#include "egg/core/eggHeap.h"
namespace EGG {
template <typename T>
class TBuffer {
public:
// vtable 0x00
/* vt 0x08 */ virtual ~TBuffer() {
if (mBuffer != nullptr) {
delete[] mBuffer;
mBuffer = nullptr;
}
}
/* vt 0x0C */ virtual void allocate(int n, int);
/* vt 0x10 */ virtual void allocate(int n, Heap *heap, int);
/* vt 0x14 */ virtual void onAllocate(Heap *) {
return;
}
/* vt 0x18 */ virtual void errRangeOver() {
return;
}
public:
/* 0x08 */ s32 mSize;
/* 0x0C */ T *mBuffer;
public:
inline TBuffer() : mSize(0), mBuffer(nullptr) {}
inline bool isRangeValid(int i) {
return (i >= 0 && i < mSize);
}
inline void checkRange(int i) {
if (!isRangeValid(i)) {
errRangeOver();
}
}
inline T &operator()(int i) {
checkRange(i);
return mBuffer[i];
}
inline s32 getSize() {
return mSize;
}
};
template <typename T>
void TBuffer<T>::allocate(int n, int) {
mSize = n;
mBuffer = new T[n];
onAllocate(nullptr);
}
template <typename T>
void TBuffer<T>::allocate(int n, Heap *heap, int) {
mSize = n;
if (heap == nullptr) {
heap = Heap::sCurrentHeap;
}
mBuffer = new (heap, 4) T[mSize];
onAllocate(heap);
}
} // namespace EGG
#endif