mirror of
https://github.com/ddnet/ddnet.git
synced 2024-11-10 01:58:19 +00:00
Add CDynamicRingBuffer
to avoid specifying size as template
This commit is contained in:
parent
626b7ca805
commit
c90a52f51c
|
@ -1,7 +1,5 @@
|
|||
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
|
||||
/* If you are missing that file, acquire a complete release at teeworlds.com. */
|
||||
#include <base/system.h>
|
||||
|
||||
#include "ringbuffer.h"
|
||||
|
||||
CRingBufferBase::CItem *CRingBufferBase::NextBlock(CItem *pItem)
|
||||
|
|
|
@ -3,6 +3,8 @@
|
|||
#ifndef ENGINE_SHARED_RINGBUFFER_H
|
||||
#define ENGINE_SHARED_RINGBUFFER_H
|
||||
|
||||
#include <base/system.h>
|
||||
|
||||
class CRingBufferBase
|
||||
{
|
||||
class CItem
|
||||
|
@ -44,18 +46,13 @@ public:
|
|||
// Will start to destroy items to try to fit the next one
|
||||
FLAG_RECYCLE = 1
|
||||
};
|
||||
static constexpr int ITEM_SIZE = sizeof(CItem);
|
||||
};
|
||||
|
||||
template<typename T, int TSIZE, int TFLAGS = 0>
|
||||
class CStaticRingBuffer : public CRingBufferBase
|
||||
template<typename T>
|
||||
class CTypedRingBuffer : public CRingBufferBase
|
||||
{
|
||||
unsigned char m_aBuffer[TSIZE];
|
||||
|
||||
public:
|
||||
CStaticRingBuffer() { Init(); }
|
||||
|
||||
void Init() { CRingBufferBase::Init(m_aBuffer, TSIZE, TFLAGS); }
|
||||
|
||||
T *Allocate(int Size) { return (T *)CRingBufferBase::Allocate(Size); }
|
||||
int PopFirst() { return CRingBufferBase::PopFirst(); }
|
||||
|
||||
|
@ -65,4 +62,36 @@ public:
|
|||
T *Last() { return (T *)CRingBufferBase::Last(); }
|
||||
};
|
||||
|
||||
template<typename T, int TSIZE, int TFLAGS = 0>
|
||||
class CStaticRingBuffer : public CTypedRingBuffer<T>
|
||||
{
|
||||
unsigned char m_aBuffer[TSIZE];
|
||||
|
||||
public:
|
||||
CStaticRingBuffer() { Init(); }
|
||||
|
||||
void Init() { CRingBufferBase::Init(m_aBuffer, TSIZE, TFLAGS); }
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
class CDynamicRingBuffer : public CTypedRingBuffer<T>
|
||||
{
|
||||
unsigned char *m_pBuffer = nullptr;
|
||||
|
||||
public:
|
||||
CDynamicRingBuffer(int Size, int Flags = 0) { Init(Size, Flags); }
|
||||
|
||||
virtual ~CDynamicRingBuffer()
|
||||
{
|
||||
free(m_pBuffer);
|
||||
}
|
||||
|
||||
void Init(int Size, int Flags)
|
||||
{
|
||||
free(m_pBuffer);
|
||||
m_pBuffer = static_cast<unsigned char *>(malloc(Size));
|
||||
CRingBufferBase::Init(m_pBuffer, Size, Flags);
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in a new issue