1 //------------------------------------------------------------------------------ 2 // Copyright (c) 2001-2004, Haiku, Inc. 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 // Authors Marc Flerackers (mflerackers@androme.be) 24 // Stefano Ceccherini (burton666@libero.it) 25 // Description: Template class used to implement the various BTextView 26 // buffer classes. 27 //------------------------------------------------------------------------------ 28 29 #ifndef __TEXT_VIEW_SUPPORT_BUFFER__H__ 30 #define __TEXT_VIEW_SUPPORT_BUFFER__H__ 31 32 #include <cstdlib> 33 #include <cstring> 34 35 #include <OS.h> 36 #include <SupportDefs.h> 37 38 39 // _BTextViewSupportBuffer_ class ---------------------------------------------- 40 template <class T> 41 class _BTextViewSupportBuffer_ { 42 43 public: 44 _BTextViewSupportBuffer_(int32 inExtraCount = 0, int32 inCount = 0); 45 virtual ~_BTextViewSupportBuffer_(); 46 47 void InsertItemsAt(int32 inNumItems, int32 inAtIndex, const T *inItem); 48 void RemoveItemsAt(int32 inNumItems, int32 inAtIndex); 49 50 int32 ItemCount() const; 51 52 protected: 53 int32 fExtraCount; 54 int32 fItemCount; 55 int32 fBufferCount; 56 T* fBuffer; 57 }; 58 59 60 template <class T> 61 _BTextViewSupportBuffer_<T>::_BTextViewSupportBuffer_(int32 inExtraCount, 62 int32 inCount) 63 : fExtraCount(inExtraCount), 64 fItemCount(inCount), 65 fBufferCount(fExtraCount + fItemCount), 66 fBuffer(NULL) 67 { 68 fBuffer = (T *)calloc(fExtraCount + fItemCount, sizeof(T)); 69 } 70 71 72 template <class T> 73 _BTextViewSupportBuffer_<T>::~_BTextViewSupportBuffer_() 74 { 75 free(fBuffer); 76 } 77 78 79 template <class T> 80 void 81 _BTextViewSupportBuffer_<T>::InsertItemsAt(int32 inNumItems, 82 int32 inAtIndex, 83 const T *inItem) 84 { 85 if (inNumItems < 1) 86 return; 87 88 inAtIndex = (inAtIndex > fItemCount) ? fItemCount : inAtIndex; 89 inAtIndex = (inAtIndex < 0) ? 0 : inAtIndex; 90 91 int32 delta = inNumItems * sizeof(T); 92 int32 logSize = fItemCount * sizeof(T); 93 if ((logSize + delta) >= fBufferCount) { 94 fBufferCount = logSize + delta + (fExtraCount * sizeof(T)); 95 fBuffer = (T *)realloc(fBuffer, fBufferCount); 96 if (fBuffer == NULL) 97 debugger("InsertItemsAt(): reallocation failed"); 98 } 99 100 T *loc = fBuffer + inAtIndex; 101 memmove(loc + inNumItems, loc, (fItemCount - inAtIndex) * sizeof(T)); 102 memcpy(loc, inItem, delta); 103 104 fItemCount += inNumItems; 105 } 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 if (fBuffer == NULL) 130 debugger("RemoveItemsAt(): reallocation failed"); 131 } 132 133 fItemCount -= inNumItems; 134 } 135 136 137 template<class T> 138 inline int32 139 _BTextViewSupportBuffer_<T>::ItemCount() const 140 { 141 return fItemCount; 142 } 143 144 145 #endif // __TEXT_VIEW_SUPPORT_BUFFER__H__ 146