merge the formfield patch from ooo-build
[ooovba.git] / cppu / source / uno / destr.hxx
blob6b19216e960026eaee43ab83c95835825149e9af
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: destr.hxx,v $
10 * $Revision: 1.15 $
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 ************************************************************************/
30 #ifndef DESTR_HXX
31 #define DESTR_HXX
33 #include "prim.hxx"
36 namespace cppu
39 //##################################################################################################
40 //#### destruction #################################################################################
41 //##################################################################################################
43 //--------------------------------------------------------------------------------------------------
44 inline void _destructUnion(
45 void * pValue,
46 typelib_TypeDescription * pTypeDescr,
47 uno_ReleaseFunc release )
48 SAL_THROW( () )
50 typelib_TypeDescriptionReference * pType = _unionGetSetType( pValue, pTypeDescr );
51 ::uno_type_destructData(
52 (char *)pValue + ((typelib_UnionTypeDescription *)pTypeDescr)->nValueOffset,
53 pType, release );
54 ::typelib_typedescriptionreference_release( pType );
56 //==================================================================================================
57 void destructStruct(
58 void * pValue,
59 typelib_CompoundTypeDescription * pTypeDescr,
60 uno_ReleaseFunc release )
61 SAL_THROW( () );
62 //--------------------------------------------------------------------------------------------------
63 inline void _destructStruct(
64 void * pValue,
65 typelib_CompoundTypeDescription * pTypeDescr,
66 uno_ReleaseFunc release )
67 SAL_THROW( () )
69 if (pTypeDescr->pBaseTypeDescription)
71 destructStruct( pValue, pTypeDescr->pBaseTypeDescription, release );
74 typelib_TypeDescriptionReference ** ppTypeRefs = pTypeDescr->ppTypeRefs;
75 sal_Int32 * pMemberOffsets = pTypeDescr->pMemberOffsets;
76 sal_Int32 nDescr = pTypeDescr->nMembers;
77 while (nDescr--)
79 ::uno_type_destructData(
80 (char *)pValue + pMemberOffsets[nDescr],
81 ppTypeRefs[nDescr], release );
85 //--------------------------------------------------------------------------------------------------
86 inline void _destructArray(
87 void * pValue,
88 typelib_ArrayTypeDescription * pTypeDescr,
89 uno_ReleaseFunc release )
90 throw ()
92 typelib_TypeDescription * pElementType = NULL;
93 TYPELIB_DANGER_GET( &pElementType, ((typelib_IndirectTypeDescription *)pTypeDescr)->pType );
94 sal_Int32 nElementSize = pElementType->nSize;
95 TYPELIB_DANGER_RELEASE( pElementType );
97 sal_Int32 nTotalElements = pTypeDescr->nTotalElements;
98 for(sal_Int32 i=0; i < nTotalElements; i++)
100 ::uno_type_destructData(
101 (sal_Char *)pValue + i * nElementSize,
102 ((typelib_IndirectTypeDescription *)pTypeDescr)->pType, release );
105 typelib_typedescriptionreference_release(((typelib_IndirectTypeDescription *)pTypeDescr)->pType);
108 //==============================================================================
109 void destructSequence(
110 uno_Sequence * pSequence,
111 typelib_TypeDescriptionReference * pType,
112 typelib_TypeDescription * pTypeDescr,
113 uno_ReleaseFunc release );
115 //--------------------------------------------------------------------------------------------------
116 inline void _destructAny(
117 uno_Any * pAny,
118 uno_ReleaseFunc release )
119 SAL_THROW( () )
121 typelib_TypeDescriptionReference * pType = pAny->pType;
123 switch (pType->eTypeClass)
125 case typelib_TypeClass_HYPER:
126 case typelib_TypeClass_UNSIGNED_HYPER:
127 if (sizeof(void *) < sizeof(sal_Int64))
129 ::rtl_freeMemory( pAny->pData );
131 break;
132 case typelib_TypeClass_FLOAT:
133 if (sizeof(void *) < sizeof(float))
135 ::rtl_freeMemory( pAny->pData );
137 break;
138 case typelib_TypeClass_DOUBLE:
139 if (sizeof(void *) < sizeof(double))
141 ::rtl_freeMemory( pAny->pData );
143 break;
144 case typelib_TypeClass_STRING:
145 ::rtl_uString_release( (rtl_uString *)pAny->pReserved );
146 break;
147 case typelib_TypeClass_TYPE:
148 ::typelib_typedescriptionreference_release(
149 (typelib_TypeDescriptionReference *)pAny->pReserved );
150 break;
151 case typelib_TypeClass_ANY:
152 OSL_ENSURE( sal_False, "### unexpected nested any!" );
153 ::uno_any_destruct( (uno_Any *)pAny->pData, release );
154 ::rtl_freeMemory( pAny->pData );
155 break;
156 case typelib_TypeClass_TYPEDEF:
157 OSL_ENSURE( 0, "### unexpected typedef!" );
158 break;
159 case typelib_TypeClass_STRUCT:
160 case typelib_TypeClass_EXCEPTION:
162 typelib_TypeDescription * pTypeDescr = 0;
163 TYPELIB_DANGER_GET( &pTypeDescr, pType );
164 _destructStruct( pAny->pData, (typelib_CompoundTypeDescription *)pTypeDescr, release );
165 TYPELIB_DANGER_RELEASE( pTypeDescr );
166 ::rtl_freeMemory( pAny->pData );
167 break;
169 case typelib_TypeClass_UNION:
171 typelib_TypeDescription * pTypeDescr = 0;
172 TYPELIB_DANGER_GET( &pTypeDescr, pType );
173 _destructUnion( pAny->pData, pTypeDescr, release );
174 TYPELIB_DANGER_RELEASE( pTypeDescr );
175 ::rtl_freeMemory( pAny->pData );
176 break;
178 case typelib_TypeClass_SEQUENCE:
180 destructSequence(
181 *(uno_Sequence **) &pAny->pReserved, pType, 0, release );
182 break;
184 case typelib_TypeClass_INTERFACE:
185 _release( pAny->pReserved, release );
186 break;
187 default:
188 break;
190 #if OSL_DEBUG_LEVEL > 0
191 pAny->pData = (void *)0xdeadbeef;
192 #endif
194 ::typelib_typedescriptionreference_release( pType );
196 //--------------------------------------------------------------------------------------------------
197 inline sal_Int32 idestructElements(
198 void * pElements, typelib_TypeDescriptionReference * pElementType,
199 sal_Int32 nStartIndex, sal_Int32 nStopIndex,
200 uno_ReleaseFunc release )
201 SAL_THROW( () )
203 switch (pElementType->eTypeClass)
205 case typelib_TypeClass_CHAR:
206 return (sal_Int32)(sizeof(sal_Unicode));
207 case typelib_TypeClass_BOOLEAN:
208 return (sal_Int32)(sizeof(sal_Bool));
209 case typelib_TypeClass_BYTE:
210 return (sal_Int32)(sizeof(sal_Int8));
211 case typelib_TypeClass_SHORT:
212 case typelib_TypeClass_UNSIGNED_SHORT:
213 return (sal_Int32)(sizeof(sal_Int16));
214 case typelib_TypeClass_LONG:
215 case typelib_TypeClass_UNSIGNED_LONG:
216 return (sal_Int32)(sizeof(sal_Int32));
217 case typelib_TypeClass_HYPER:
218 case typelib_TypeClass_UNSIGNED_HYPER:
219 return (sal_Int32)(sizeof(sal_Int64));
220 case typelib_TypeClass_FLOAT:
221 return (sal_Int32)(sizeof(float));
222 case typelib_TypeClass_DOUBLE:
223 return (sal_Int32)(sizeof(double));
225 case typelib_TypeClass_STRING:
227 rtl_uString ** pDest = (rtl_uString **)pElements;
228 for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
230 ::rtl_uString_release( pDest[nPos] );
232 return (sal_Int32)(sizeof(rtl_uString *));
234 case typelib_TypeClass_TYPE:
236 typelib_TypeDescriptionReference ** pDest = (typelib_TypeDescriptionReference **)pElements;
237 for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
239 ::typelib_typedescriptionreference_release( pDest[nPos] );
241 return (sal_Int32)(sizeof(typelib_TypeDescriptionReference *));
243 case typelib_TypeClass_ANY:
245 uno_Any * pDest = (uno_Any *)pElements;
246 for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
248 _destructAny( &pDest[nPos], release );
250 return (sal_Int32)(sizeof(uno_Any));
252 case typelib_TypeClass_ENUM:
253 return (sal_Int32)(sizeof(sal_Int32));
254 case typelib_TypeClass_STRUCT:
255 case typelib_TypeClass_EXCEPTION:
257 typelib_TypeDescription * pElementTypeDescr = 0;
258 TYPELIB_DANGER_GET( &pElementTypeDescr, pElementType );
259 sal_Int32 nElementSize = pElementTypeDescr->nSize;
260 for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
262 _destructStruct(
263 (char *)pElements + (nElementSize * nPos),
264 (typelib_CompoundTypeDescription *)pElementTypeDescr,
265 release );
267 sal_Int32 nSize = pElementTypeDescr->nSize;
268 TYPELIB_DANGER_RELEASE( pElementTypeDescr );
269 return nSize;
271 case typelib_TypeClass_UNION:
273 typelib_TypeDescription * pElementTypeDescr = 0;
274 TYPELIB_DANGER_GET( &pElementTypeDescr, pElementType );
275 sal_Int32 nElementSize = pElementTypeDescr->nSize;
276 for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
278 _destructUnion(
279 (char *)pElements + (nElementSize * nPos),
280 pElementTypeDescr,
281 release );
283 sal_Int32 nSize = pElementTypeDescr->nSize;
284 TYPELIB_DANGER_RELEASE( pElementTypeDescr );
285 return nSize;
287 case typelib_TypeClass_SEQUENCE:
289 typelib_TypeDescription * pElementTypeDescr = 0;
290 TYPELIB_DANGER_GET( &pElementTypeDescr, pElementType );
291 uno_Sequence ** pDest = (uno_Sequence **)pElements;
292 for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
294 destructSequence(
295 pDest[nPos],
296 pElementTypeDescr->pWeakRef, pElementTypeDescr,
297 release );
299 TYPELIB_DANGER_RELEASE( pElementTypeDescr );
300 return (sal_Int32)(sizeof(uno_Sequence *));
302 case typelib_TypeClass_INTERFACE:
304 if (release)
306 for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
308 void * p = ((void **)pElements)[nPos];
309 if (p)
311 (*release)( p );
315 else
317 for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
319 uno_Interface * p = ((uno_Interface **)pElements)[nPos];
320 if (p)
322 (*p->release)( p );
326 return (sal_Int32)(sizeof(void *));
328 default:
329 OSL_ASSERT(false);
330 return 0;
334 //------------------------------------------------------------------------------
335 inline void idestructSequence(
336 uno_Sequence * pSeq,
337 typelib_TypeDescriptionReference * pType,
338 typelib_TypeDescription * pTypeDescr,
339 uno_ReleaseFunc release )
341 if (::osl_decrementInterlockedCount( &pSeq->nRefCount ) == 0)
343 if (pSeq->nElements > 0)
345 if (pTypeDescr)
347 idestructElements(
348 pSeq->elements,
349 ((typelib_IndirectTypeDescription *) pTypeDescr)->pType, 0,
350 pSeq->nElements, release );
352 else
354 TYPELIB_DANGER_GET( &pTypeDescr, pType );
355 idestructElements(
356 pSeq->elements,
357 ((typelib_IndirectTypeDescription *) pTypeDescr)->pType, 0,
358 pSeq->nElements, release );
359 TYPELIB_DANGER_RELEASE( pTypeDescr );
362 ::rtl_freeMemory( pSeq );
366 //--------------------------------------------------------------------------------------------------
367 inline void _destructData(
368 void * pValue,
369 typelib_TypeDescriptionReference * pType,
370 typelib_TypeDescription * pTypeDescr,
371 uno_ReleaseFunc release )
372 SAL_THROW( () )
374 switch (pType->eTypeClass)
376 case typelib_TypeClass_STRING:
377 ::rtl_uString_release( *(rtl_uString **)pValue );
378 break;
379 case typelib_TypeClass_TYPE:
380 ::typelib_typedescriptionreference_release( *(typelib_TypeDescriptionReference **)pValue );
381 break;
382 case typelib_TypeClass_ANY:
383 _destructAny( (uno_Any *)pValue, release );
384 break;
385 case typelib_TypeClass_TYPEDEF:
386 OSL_ENSURE( 0, "### unexpected typedef!" );
387 break;
388 case typelib_TypeClass_STRUCT:
389 case typelib_TypeClass_EXCEPTION:
390 if (pTypeDescr)
392 _destructStruct( pValue, (typelib_CompoundTypeDescription *)pTypeDescr, release );
394 else
396 TYPELIB_DANGER_GET( &pTypeDescr, pType );
397 _destructStruct( pValue, (typelib_CompoundTypeDescription *)pTypeDescr, release );
398 TYPELIB_DANGER_RELEASE( pTypeDescr );
400 break;
401 case typelib_TypeClass_ARRAY:
402 if (pTypeDescr)
404 _destructArray( pValue, (typelib_ArrayTypeDescription *)pTypeDescr, release );
406 else
408 TYPELIB_DANGER_GET( &pTypeDescr, pType );
409 _destructArray( pValue, (typelib_ArrayTypeDescription *)pTypeDescr, release );
410 TYPELIB_DANGER_RELEASE( pTypeDescr );
412 break;
413 case typelib_TypeClass_UNION:
414 if (pTypeDescr)
416 _destructUnion( pValue, pTypeDescr, release );
418 else
420 TYPELIB_DANGER_GET( &pTypeDescr, pType );
421 _destructUnion( pValue, pTypeDescr, release );
422 TYPELIB_DANGER_RELEASE( pTypeDescr );
424 break;
425 case typelib_TypeClass_SEQUENCE:
427 idestructSequence(
428 *(uno_Sequence **)pValue, pType, pTypeDescr, release );
429 break;
431 case typelib_TypeClass_INTERFACE:
432 _release( *(void **)pValue, release );
433 break;
434 default:
435 break;
441 #endif