merge the formfield patch from ooo-build
[ooovba.git] / binfilter / inc / bf_goodies / bucket.hxx
bloba640388a4e40102d67acf6e1984028276cc7f7af
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: bucket.hxx,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef _B3D_BUCKET_HXX
32 #define _B3D_BUCKET_HXX
34 #ifndef _SVARRAY_HXX
35 #include <bf_svtools/svarray.hxx>
36 #endif
38 namespace binfilter {
40 /*************************************************************************
42 |* Bucket deklarator
44 \************************************************************************/
46 #define BASE3D_DECL_BUCKET(TheClassName,TheExtension) \
47 SV_DECL_VARARR(TheClassName##TheExtension##MemArr, char*, 32, 32) \
48 class TheClassName##TheExtension { \
49 private: \
50 TheClassName##TheExtension##MemArr aMemArray; \
51 UINT32 nMask; \
52 UINT32 nCount; \
53 INT16 nFreeMemArray; \
54 INT16 nActMemArray; \
55 UINT16 nFreeEntry; \
56 UINT16 nShift; \
57 UINT16 nBlockShift; \
58 UINT16 nEntriesPerArray; \
59 UINT16 nSlotSize; \
60 UINT16 nNext; \
61 UINT16 nMemArray; \
62 public: \
63 TheClassName##TheExtension(UINT16 TheSize); \
64 /* Zu verwendende Groesse der Speicherarrays setzen */ \
65 /* Bitte NUR verwenden, falls sich der Leerkonstruktor */ \
66 /* nicht vermeiden laesst! Nicht nachtraeglich anwenden! */ \
67 void InitializeSize(UINT16 TheSize); \
68 /* Destruktor */ \
69 ~TheClassName##TheExtension(); \
70 /* Anhaengen und kopieren */ \
71 BOOL Append(TheClassName& rVec) \
72 { if(CareForSpace()) return ImplAppend(rVec); return FALSE; } \
73 /* nur neuen Eintrag anhaengen, ohne ausfuellen */ \
74 BOOL Append() \
75 { if(CareForSpace()) return ImplAppend(); return FALSE; } \
76 /* Letzten Eintrag entfernen */ \
77 BOOL Remove() { if(nCount) return ImplRemove(); return FALSE; } \
78 /* leeren und Speicher freigeben */ \
79 void Empty(); \
80 /* leeren aber Speicher behalten */ \
81 void Erase(); \
82 TheClassName& operator[] (UINT32 nPos); \
83 UINT32 Count() { return nCount; } \
84 UINT32 GetNumAllocated() { return aMemArray.Count() * nEntriesPerArray; } \
85 void operator=(const TheClassName##TheExtension&); \
86 UINT16 GetBlockShift() { return nBlockShift; } \
87 UINT16 GetSlotSize() { return nSlotSize; } \
88 private: \
89 BOOL CareForSpace() \
90 { if(nFreeEntry == nEntriesPerArray) \
91 return ImplCareForSpace(); return TRUE; } \
92 BOOL ImplCareForSpace(); \
93 /* Anhaengen und kopieren */ \
94 BOOL ImplAppend(TheClassName& rVec); \
95 /* nur neuen Eintrag anhaengen, ohne ausfuellen */ \
96 BOOL ImplAppend(); \
97 BOOL ImplRemove(); \
100 /*************************************************************************
102 |* Bucket implementor
104 \************************************************************************/
106 #define BASE3D_IMPL_BUCKET(TheClassName,TheExtension) \
107 SV_IMPL_VARARR(TheClassName##TheExtension##MemArr, char*) \
108 TheClassName##TheExtension::TheClassName##TheExtension(UINT16 TheSize) { \
109 InitializeSize(TheSize); \
111 void TheClassName##TheExtension::InitializeSize(UINT16 TheSize) { \
112 UINT16 nSiz; \
113 for(nShift=0,nSiz=1;nSiz<sizeof(TheClassName);nSiz<<=1,nShift++); \
114 nBlockShift = TheSize - nShift; \
115 nMask = (1L << nBlockShift)-1L; \
116 nSlotSize = 1<<nShift; \
117 nEntriesPerArray = (UINT16)((1L << TheSize) >> nShift); \
118 Empty(); \
120 void TheClassName##TheExtension::operator=(const TheClassName##TheExtension& rObj) { \
121 Erase(); \
122 TheClassName##TheExtension& rSrc = (TheClassName##TheExtension&)rObj; \
123 for(UINT32 a=0;a<rSrc.Count();a++) \
124 Append(rSrc[a]); \
126 void TheClassName##TheExtension::Empty() { \
127 for(UINT16 i=0;i<aMemArray.Count();i++) \
128 /*#90353#*/ delete [] aMemArray[i]; \
129 if(aMemArray.Count()) \
130 aMemArray.Remove(0, aMemArray.Count()); \
131 nFreeMemArray = 0; \
132 nActMemArray = -1; \
133 Erase(); \
135 void TheClassName##TheExtension::Erase() { \
136 nFreeEntry = nEntriesPerArray; \
137 nCount = 0; \
138 nActMemArray = -1; \
140 TheClassName##TheExtension::~TheClassName##TheExtension() { \
141 Empty(); \
143 BOOL TheClassName##TheExtension::ImplAppend(TheClassName& rVec) { \
144 *((TheClassName*)(aMemArray[nActMemArray] + (nFreeEntry++ << nShift))) = rVec; \
145 nCount++; \
146 return TRUE; \
148 BOOL TheClassName##TheExtension::ImplAppend() { \
149 nFreeEntry++; \
150 nCount++; \
151 return TRUE; \
153 BOOL TheClassName##TheExtension::ImplRemove() { \
154 if(nFreeEntry == 1) { \
155 nFreeEntry = nEntriesPerArray + 1; \
156 if(nActMemArray == -1) \
157 return FALSE; \
158 nActMemArray--; \
160 nFreeEntry--; \
161 nCount--; \
162 return TRUE; \
164 BOOL TheClassName##TheExtension::ImplCareForSpace() { \
165 /* neues array bestimmem */ \
166 if(nActMemArray + 1 < nFreeMemArray) { \
167 /* ist scon allokiert, gehe auf naechstes */ \
168 nActMemArray++; \
169 } else { \
170 /* neues muss allokiert werden */ \
171 char* pNew = new char[nEntriesPerArray << nShift]; \
172 if(!pNew) \
173 return FALSE; \
174 aMemArray.Insert((const char*&) pNew, aMemArray.Count()); \
175 nActMemArray = nFreeMemArray++; \
177 nFreeEntry = 0; \
178 return TRUE; \
180 TheClassName& TheClassName##TheExtension::operator[] (UINT32 nPos) { \
181 if(nPos >= nCount) { \
182 DBG_ERROR("Access to Bucket out of range!"); \
183 return *((TheClassName*)aMemArray[0]); \
185 return *((TheClassName*)(aMemArray[(UINT16)(nPos >> nBlockShift)] + ((nPos & nMask) << nShift))); \
189 }//end of namespace binfilter
191 #endif // _B3D_BUCKET_HXX