7 #ifndef _DISK_RING_BUFFER_H
8 #define _DISK_RING_BUFFER_H
27 template <
typename ADDITIONAL_DATA = tUInt8, tUInt8 ALIGNMENT = 1>
96 m_nStartOffset(nStartOffset),
99 m_bBookkeeping(
tTrue),
100 m_pCallback(pDropCallback)
103 m_nCurrentPos = nStartOffset;
107 m_oAlignmentBuffer.
Alloc(ALIGNMENT - 1);
120 return m_nCurrentSize;
134 m_nMaxSize = m_nCurrentSize;
158 if (m_sRearItem.
nFilePos == -1 && !m_oItems.empty())
161 m_sRearItem = m_oItems.back();
166 *pRearDataItem = m_sRearItem;
171 if (!m_oItems.empty())
173 *pLastDataItem = m_oItems.back();
177 *pLastDataItem =
tItem();
193 const ADDITIONAL_DATA& xAdditional,
tFilePos* pFilePos =
nullptr)
198 return AppendItem(&sItem, 1, xAdditional, pFilePos);
210 const ADDITIONAL_DATA& xAdditional,
tFilePos* pFilePos =
nullptr)
213 for (
tSize nPiece = 0; nPiece < nCount; ++nPiece)
218 if (m_nMaxSize && !m_oItems.empty())
220 if (m_nCurrentPos + nDataSize > m_nMaxSize)
224 m_nCurrentSize = m_nCurrentPos;
226 m_sRearItem = m_oItems.back();
229 while (m_oItems.front().nFilePos >= m_nCurrentPos)
234 m_nCurrentPos = m_oItems.front().nFilePos;
244 *pFilePos = m_nCurrentPos;
249 for (
tSize nPiece = 0; nPiece < nCount; ++nPiece)
254 m_nCurrentPos += nDataSize;
258 if (m_nCurrentPos > m_nCurrentSize)
260 m_nCurrentSize = m_nCurrentPos;
267 sItem.
nSize = nDataSize;
270 m_oItems.push_back(sItem);
275 while (m_oItems.size() > 1 &&
276 m_oItems.front().nFilePos >= nStart &&
277 m_oItems.front().nFilePos < nEnd)
291 m_sRearItem = m_oItems.back();
294 m_nCurrentSize = m_nCurrentPos;
308 return m_oItems.begin();
317 return m_oItems.end();
328 tFilePos nMod = m_nCurrentPos % ALIGNMENT;
333 static_cast<tInt>(nFill)));
334 m_nCurrentPos += nFill;
348 static tItem s_sDummy;
350 tItem& sItem = m_oItems.front();
351 tItem* pNext = &s_sDummy;
352 if (m_oItems.size() > 1)
354 pNext = &m_oItems[1];
358 m_oItems.pop_front();
tInt64 tFileSize
type definition for a file or stream size value (platform and compiler independent type).
tInt64 tFilePos
type definition for a file or stream position value (platform and compiler independent type).
void tVoid
The tVoid is always the definition for the void (non-type).
int tInt
type definition for signed integer value (platform and compiler dependent type).
bool tBool
The tBool defines the type for the Values tTrue and tFalse (platform and compiler dependent).
size_t tSize
type definition for a array size values, map size values etc.
#define RETURN_IF_FAILED(s)
Return if expression is failed, which requires the calling function's return type to be tResult.
#define RETURN_NOERROR
Return status ERR_NOERROR, which requires the calling function's return type to be tResult.
#define RETURN_ERROR(code)
Return specific error code, which requires the calling function's return type to be tResult.
tFilePos SetFilePos(tFilePos nOffset, tFilePosRef MoveMode)
This function moves the file pointer.
@ FP_Begin
offsets are measured from the beginning of the file
@ FP_End
offsets are measured from the end of the file
tResult Truncate(tFilePos nSize)
This method truncates the file at the given size.
tResult WriteAll(const tVoid *pvBuffer, tInt nBufferSize)
This function writes data from a buffer to the file associated with the cFile object.
tFilePos GetFilePos() const
This function retrieves the file pointer offset.
Callback interface for dropped items.
virtual tResult OnDrop(const tItem &sDroppedItem, const tItem &sNextItem)=0
Called whenever an item is removed from the head of the buffer.
tResult AppendItem(const tItemPiece *pPieces, tSize nCount, const ADDITIONAL_DATA &xAdditional, tFilePos *pFilePos=nullptr)
Adds a new item that is made up by multiple data buffers at the end of the buffer.
const_iterator begin() const
Retunrs an iterator to the beginning of the buffer.
tResult StartAppending(tItem *pRearDataItem=NULL, tItem *pLastDataItem=NULL)
After a call to this method, data will be appended after the ring buffer.
cFileRingBuffer(cFile *pFile, tFilePos nStartOffset=0, tFileSize nMaxSize=0, IDropCallback *pDropCallback=nullptr)
Constructor.
tResult StartWrappingAround()
Limits the size of the buffer by the current size.
tResult AppendItem(const tVoid *pData, tSize nDataSize, const ADDITIONAL_DATA &xAdditional, tFilePos *pFilePos=nullptr)
Adds a new item at the end of the buffer.
std::deque< tItem > tItems
Typedef for convienient access.
tItems::const_iterator const_iterator
An const iterator in the buffer.
const tFileSize & GetCurrentSize()
Returns the current size of the buffer.
const_iterator end() const
Retuns an iterator to the end of the buffer.
tResult FillForAlignment()
Writes data to the file until a field position is reached that satisfies the alignment requirement.
tResult PopFront()
Removes the first item in the buffer.
tResult Alloc(tSize nSize)
(Re)allocates memory
static tVoid MemZero(tVoid *pDest, tSize nCount)
Sets the data of a buffer to zero.
tVoid * GetPtr() const
Get a pointer to the allocated memory.
tSize GetSize() const
Get the amount of allocated memory in bytes.
A common result class usable as return value throughout.
#define tFalse
Value for tBool.
#define tTrue
Value for tBool.
ADTF A_UTIL Namespace - Within adtf this is used as adtf::util or adtf_util.
This struct store information about an item in the buffer.
ADDITIONAL_DATA xAdditional
additional bookkeeping data
tSize nSize
The size of the items data.
tFilePos nFilePos
The file position of the items data.
Helper struct to store an item that is made up by multiple data buffers.
const tVoid * pData
The data of the piece.
tSize nDataSize
The size of the piece.