2010-07-03 13:34:59 +04:00
|
|
|
/*
|
|
|
|
* Copyright 2001-2010, Haiku, Inc.
|
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*/
|
2004-01-15 10:15:37 +03:00
|
|
|
|
|
|
|
#ifndef __TEXT_VIEW_SUPPORT_BUFFER__H__
|
|
|
|
#define __TEXT_VIEW_SUPPORT_BUFFER__H__
|
|
|
|
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
|
2004-11-04 11:53:46 +03:00
|
|
|
#include <OS.h>
|
2004-01-15 10:15:37 +03:00
|
|
|
#include <SupportDefs.h>
|
|
|
|
|
|
|
|
|
|
|
|
// _BTextViewSupportBuffer_ class ----------------------------------------------
|
|
|
|
template <class T>
|
|
|
|
class _BTextViewSupportBuffer_ {
|
|
|
|
|
|
|
|
public:
|
|
|
|
_BTextViewSupportBuffer_(int32 inExtraCount = 0, int32 inCount = 0);
|
|
|
|
virtual ~_BTextViewSupportBuffer_();
|
|
|
|
|
2010-07-03 13:34:59 +04:00
|
|
|
void InsertItemsAt(int32 inNumItems, int32 inAtIndex, const T* inItem);
|
2004-01-15 10:15:37 +03:00
|
|
|
void RemoveItemsAt(int32 inNumItems, int32 inAtIndex);
|
|
|
|
|
|
|
|
int32 ItemCount() const;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
int32 fExtraCount;
|
|
|
|
int32 fItemCount;
|
|
|
|
int32 fBufferCount;
|
|
|
|
T* fBuffer;
|
|
|
|
};
|
2004-11-04 11:53:46 +03:00
|
|
|
|
|
|
|
|
2004-01-15 10:15:37 +03:00
|
|
|
template <class T>
|
|
|
|
_BTextViewSupportBuffer_<T>::_BTextViewSupportBuffer_(int32 inExtraCount,
|
|
|
|
int32 inCount)
|
|
|
|
: fExtraCount(inExtraCount),
|
|
|
|
fItemCount(inCount),
|
|
|
|
fBufferCount(fExtraCount + fItemCount),
|
|
|
|
fBuffer(NULL)
|
|
|
|
{
|
2010-07-03 13:34:59 +04:00
|
|
|
fBuffer = (T*)calloc(fExtraCount + fItemCount, sizeof(T));
|
2004-01-15 10:15:37 +03:00
|
|
|
}
|
2004-11-04 11:53:46 +03:00
|
|
|
|
|
|
|
|
2004-01-15 10:15:37 +03:00
|
|
|
template <class T>
|
|
|
|
_BTextViewSupportBuffer_<T>::~_BTextViewSupportBuffer_()
|
|
|
|
{
|
|
|
|
free(fBuffer);
|
|
|
|
}
|
2004-11-04 11:53:46 +03:00
|
|
|
|
|
|
|
|
2004-01-15 10:15:37 +03:00
|
|
|
template <class T>
|
2004-11-04 11:53:46 +03:00
|
|
|
void
|
|
|
|
_BTextViewSupportBuffer_<T>::InsertItemsAt(int32 inNumItems,
|
2004-01-15 10:15:37 +03:00
|
|
|
int32 inAtIndex,
|
2010-07-03 13:34:59 +04:00
|
|
|
const T* inItem)
|
2004-01-15 10:15:37 +03:00
|
|
|
{
|
|
|
|
if (inNumItems < 1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
inAtIndex = (inAtIndex > fItemCount) ? fItemCount : inAtIndex;
|
|
|
|
inAtIndex = (inAtIndex < 0) ? 0 : inAtIndex;
|
|
|
|
|
|
|
|
int32 delta = inNumItems * sizeof(T);
|
|
|
|
int32 logSize = fItemCount * sizeof(T);
|
|
|
|
if ((logSize + delta) >= fBufferCount) {
|
|
|
|
fBufferCount = logSize + delta + (fExtraCount * sizeof(T));
|
2010-07-03 13:34:59 +04:00
|
|
|
fBuffer = (T*)realloc(fBuffer, fBufferCount);
|
2004-11-04 11:53:46 +03:00
|
|
|
if (fBuffer == NULL)
|
|
|
|
debugger("InsertItemsAt(): reallocation failed");
|
2004-01-15 10:15:37 +03:00
|
|
|
}
|
|
|
|
|
2010-07-03 13:34:59 +04:00
|
|
|
T* loc = fBuffer + inAtIndex;
|
2004-01-15 10:15:37 +03:00
|
|
|
memmove(loc + inNumItems, loc, (fItemCount - inAtIndex) * sizeof(T));
|
|
|
|
memcpy(loc, inItem, delta);
|
|
|
|
|
|
|
|
fItemCount += inNumItems;
|
|
|
|
}
|
2004-11-04 11:53:46 +03:00
|
|
|
|
|
|
|
|
2004-01-15 10:15:37 +03:00
|
|
|
template <class T>
|
|
|
|
void
|
|
|
|
_BTextViewSupportBuffer_<T>::RemoveItemsAt(int32 inNumItems,
|
|
|
|
int32 inAtIndex)
|
|
|
|
{
|
|
|
|
if (inNumItems < 1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
inAtIndex = (inAtIndex > fItemCount - 1) ? (fItemCount - 1) : inAtIndex;
|
|
|
|
inAtIndex = (inAtIndex < 0) ? 0 : inAtIndex;
|
|
|
|
|
2010-07-03 13:34:59 +04:00
|
|
|
T* loc = fBuffer + inAtIndex;
|
2004-01-15 10:15:37 +03:00
|
|
|
memmove(loc, loc + inNumItems,
|
|
|
|
(fItemCount - (inNumItems + inAtIndex)) * sizeof(T));
|
|
|
|
|
|
|
|
int32 delta = inNumItems * sizeof(T);
|
|
|
|
int32 logSize = fItemCount * sizeof(T);
|
|
|
|
uint32 extraSize = fBufferCount - (logSize - delta);
|
|
|
|
if (extraSize > (fExtraCount * sizeof(T))) {
|
|
|
|
fBufferCount = (logSize - delta) + (fExtraCount * sizeof(T));
|
2010-07-03 13:34:59 +04:00
|
|
|
fBuffer = (T*)realloc(fBuffer, fBufferCount);
|
2004-11-04 11:53:46 +03:00
|
|
|
if (fBuffer == NULL)
|
|
|
|
debugger("RemoveItemsAt(): reallocation failed");
|
2004-01-15 10:15:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fItemCount -= inNumItems;
|
|
|
|
}
|
2004-11-04 11:53:46 +03:00
|
|
|
|
|
|
|
|
2004-01-15 10:15:37 +03:00
|
|
|
template<class T>
|
|
|
|
inline int32
|
|
|
|
_BTextViewSupportBuffer_<T>::ItemCount() const
|
|
|
|
{
|
|
|
|
return fItemCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-11-04 11:53:46 +03:00
|
|
|
#endif // __TEXT_VIEW_SUPPORT_BUFFER__H__
|