2 * Copyright 2001-2010, Haiku, Inc.
3 * Distributed under the terms of the MIT License.
6 #ifndef __TEXT_VIEW_SUPPORT_BUFFER__H__
7 #define __TEXT_VIEW_SUPPORT_BUFFER__H__
13 #include <SupportDefs.h>
16 // _BTextViewSupportBuffer_ class ----------------------------------------------
18 class _BTextViewSupportBuffer_
{
21 _BTextViewSupportBuffer_(int32 inExtraCount
= 0, int32 inCount
= 0);
22 virtual ~_BTextViewSupportBuffer_();
24 void InsertItemsAt(int32 inNumItems
, int32 inAtIndex
, const T
* inItem
);
25 void RemoveItemsAt(int32 inNumItems
, int32 inAtIndex
);
27 int32
ItemCount() const;
38 _BTextViewSupportBuffer_
<T
>::_BTextViewSupportBuffer_(int32 inExtraCount
,
40 : fExtraCount(inExtraCount
),
42 fBufferCount(fExtraCount
+ fItemCount
),
45 fBuffer
= (T
*)calloc(fExtraCount
+ fItemCount
, sizeof(T
));
50 _BTextViewSupportBuffer_
<T
>::~_BTextViewSupportBuffer_()
58 _BTextViewSupportBuffer_
<T
>::InsertItemsAt(int32 inNumItems
,
65 inAtIndex
= (inAtIndex
> fItemCount
) ? fItemCount
: inAtIndex
;
66 inAtIndex
= (inAtIndex
< 0) ? 0 : inAtIndex
;
68 int32 delta
= inNumItems
* sizeof(T
);
69 int32 logSize
= fItemCount
* sizeof(T
);
70 if ((logSize
+ delta
) >= fBufferCount
) {
71 fBufferCount
= logSize
+ delta
+ (fExtraCount
* sizeof(T
));
72 fBuffer
= (T
*)realloc(fBuffer
, fBufferCount
);
74 debugger("InsertItemsAt(): reallocation failed");
77 T
* loc
= fBuffer
+ inAtIndex
;
78 memmove(loc
+ inNumItems
, loc
, (fItemCount
- inAtIndex
) * sizeof(T
));
79 memcpy(loc
, inItem
, delta
);
81 fItemCount
+= inNumItems
;
87 _BTextViewSupportBuffer_
<T
>::RemoveItemsAt(int32 inNumItems
,
93 inAtIndex
= (inAtIndex
> fItemCount
- 1) ? (fItemCount
- 1) : inAtIndex
;
94 inAtIndex
= (inAtIndex
< 0) ? 0 : inAtIndex
;
96 T
* loc
= fBuffer
+ inAtIndex
;
97 memmove(loc
, loc
+ inNumItems
,
98 (fItemCount
- (inNumItems
+ inAtIndex
)) * sizeof(T
));
100 int32 delta
= inNumItems
* sizeof(T
);
101 int32 logSize
= fItemCount
* sizeof(T
);
102 uint32 extraSize
= fBufferCount
- (logSize
- delta
);
103 if (extraSize
> (fExtraCount
* sizeof(T
))) {
104 fBufferCount
= (logSize
- delta
) + (fExtraCount
* sizeof(T
));
105 fBuffer
= (T
*)realloc(fBuffer
, fBufferCount
);
107 debugger("RemoveItemsAt(): reallocation failed");
110 fItemCount
-= inNumItems
;
116 _BTextViewSupportBuffer_
<T
>::ItemCount() const
122 #endif // __TEXT_VIEW_SUPPORT_BUFFER__H__