xref: /haiku/headers/private/interface/TextViewSupportBuffer.h (revision 9eb55bc1d104b8fda80898f8b25c94d8000c8255)
1 //------------------------------------------------------------------------------
2 //	Copyright (c) 2001-2003, OpenBeOS
3 //
4 //	Permission is hereby granted, free of charge, to any person obtaining a
5 //	copy of this software and associated documentation files (the "Software"),
6 //	to deal in the Software without restriction, including without limitation
7 //	the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 //	and/or sell copies of the Software, and to permit persons to whom the
9 //	Software is furnished to do so, subject to the following conditions:
10 //
11 //	The above copyright notice and this permission notice shall be included in
12 //	all copies or substantial portions of the Software.
13 //
14 //	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 //	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 //	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 //	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 //	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 //	FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 //	DEALINGS IN THE SOFTWARE.
21 //
22 //	File Name:		TextViewSupportBuffer.h
23 //	Author:			Marc Flerackers (mflerackers@androme.be)
24 //	Description:	Template class used to implement the various BTextView
25 //					buffer classes.
26 //------------------------------------------------------------------------------
27 
28 #ifndef __TEXT_VIEW_SUPPORT_BUFFER__H__
29 #define __TEXT_VIEW_SUPPORT_BUFFER__H__
30 
31 // Standard Includes -----------------------------------------------------------
32 #include <cstdlib>
33 #include <cstring>
34 
35 // System Includes -------------------------------------------------------------
36 #include <SupportDefs.h>
37 
38 // Project Includes ------------------------------------------------------------
39 
40 // Local Includes --------------------------------------------------------------
41 
42 // Local Defines ---------------------------------------------------------------
43 
44 // Globals ---------------------------------------------------------------------
45 
46 // _BTextViewSupportBuffer_ class ----------------------------------------------
47 template <class T>
48 class _BTextViewSupportBuffer_ {
49 
50 public:
51 				_BTextViewSupportBuffer_(int32 inExtraCount = 0, int32 inCount = 0);
52 virtual			~_BTextViewSupportBuffer_();
53 
54 		void	InsertItemsAt(int32 inNumItems, int32 inAtIndex, const T *inItem);
55 		void	RemoveItemsAt(int32 inNumItems, int32 inAtIndex);
56 
57 		int32	ItemCount() const;
58 
59 protected:
60 		int32	fExtraCount;
61 		int32	fItemCount;
62 		int32	fBufferCount;
63 		T*		fBuffer;
64 };
65 //------------------------------------------------------------------------------
66 template <class T>
67 _BTextViewSupportBuffer_<T>::_BTextViewSupportBuffer_(int32 inExtraCount,
68 													  int32 inCount)
69 	:	fExtraCount(inExtraCount),
70 		fItemCount(inCount),
71 		fBufferCount(fExtraCount + fItemCount),
72 		fBuffer(NULL)
73 {
74 	fBuffer = (T *)calloc(fExtraCount + fItemCount, sizeof(T));
75 }
76 //------------------------------------------------------------------------------
77 template <class T>
78 _BTextViewSupportBuffer_<T>::~_BTextViewSupportBuffer_()
79 {
80 	free(fBuffer);
81 }
82 //------------------------------------------------------------------------------
83 template <class T>
84 void _BTextViewSupportBuffer_<T>::InsertItemsAt(int32 inNumItems,
85 												int32 inAtIndex,
86 												const T *inItem)
87 {
88 	if (inNumItems < 1)
89 		return;
90 
91 	inAtIndex = (inAtIndex > fItemCount) ? fItemCount : inAtIndex;
92 	inAtIndex = (inAtIndex < 0) ? 0 : inAtIndex;
93 
94 	int32 delta = inNumItems * sizeof(T);
95 	int32 logSize = fItemCount * sizeof(T);
96 	if ((logSize + delta) >= fBufferCount) {
97 		fBufferCount = logSize + delta + (fExtraCount * sizeof(T));
98 		fBuffer = (T *)realloc(fBuffer, fBufferCount);
99 	}
100 
101 	T *loc = fBuffer + inAtIndex;
102 	memmove(loc + inNumItems, loc, (fItemCount - inAtIndex) * sizeof(T));
103 	memcpy(loc, inItem, delta);
104 
105 	fItemCount += inNumItems;
106 }
107 //------------------------------------------------------------------------------
108 template <class T>
109 void
110 _BTextViewSupportBuffer_<T>::RemoveItemsAt(int32 inNumItems,
111 												int32 inAtIndex)
112 {
113 	if (inNumItems < 1)
114 		return;
115 
116 	inAtIndex = (inAtIndex > fItemCount - 1) ? (fItemCount - 1) : inAtIndex;
117 	inAtIndex = (inAtIndex < 0) ? 0 : inAtIndex;
118 
119 	T *loc = fBuffer + inAtIndex;
120 	memmove(loc, loc + inNumItems,
121 			(fItemCount - (inNumItems + inAtIndex)) * sizeof(T));
122 
123 	int32 delta = inNumItems * sizeof(T);
124 	int32 logSize = fItemCount * sizeof(T);
125 	uint32 extraSize = fBufferCount - (logSize - delta);
126 	if (extraSize > (fExtraCount * sizeof(T))) {
127 		fBufferCount = (logSize - delta) + (fExtraCount * sizeof(T));
128 		fBuffer = (T *)realloc(fBuffer, fBufferCount);
129 	}
130 
131 	fItemCount -= inNumItems;
132 }
133 //------------------------------------------------------------------------------
134 template<class T>
135 inline int32
136 _BTextViewSupportBuffer_<T>::ItemCount() const
137 {
138 	return fItemCount;
139 }
140 //------------------------------------------------------------------------------
141 
142 #endif // __TEXT_VIEW_SUPPORT_BUFFER__H__
143 
144 /*
145  * $Log $
146  *
147  * $Id  $
148  *
149  */
150