Version 4.0.2.1, tag libreoffice-4.0.2.1
[LibreOffice.git] / cppu / source / uno / destr.hxx
blobec73ff0a717286f0549a56a77099c958352e08a9
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 #ifndef DESTR_HXX
20 #define DESTR_HXX
22 #include "prim.hxx"
25 namespace cppu
28 //##################################################################################################
29 //#### destruction #################################################################################
30 //##################################################################################################
32 //--------------------------------------------------------------------------------------------------
33 inline void _destructUnion(
34 void * pValue,
35 typelib_TypeDescription * pTypeDescr,
36 uno_ReleaseFunc release )
37 SAL_THROW(())
39 typelib_TypeDescriptionReference * pType = _unionGetSetType( pValue, pTypeDescr );
40 ::uno_type_destructData(
41 (char *)pValue + ((typelib_UnionTypeDescription *)pTypeDescr)->nValueOffset,
42 pType, release );
43 ::typelib_typedescriptionreference_release( pType );
45 //==================================================================================================
46 void destructStruct(
47 void * pValue,
48 typelib_CompoundTypeDescription * pTypeDescr,
49 uno_ReleaseFunc release )
50 SAL_THROW(());
51 //--------------------------------------------------------------------------------------------------
52 inline void _destructStruct(
53 void * pValue,
54 typelib_CompoundTypeDescription * pTypeDescr,
55 uno_ReleaseFunc release )
56 SAL_THROW(())
58 if (pTypeDescr->pBaseTypeDescription)
60 destructStruct( pValue, pTypeDescr->pBaseTypeDescription, release );
63 typelib_TypeDescriptionReference ** ppTypeRefs = pTypeDescr->ppTypeRefs;
64 sal_Int32 * pMemberOffsets = pTypeDescr->pMemberOffsets;
65 sal_Int32 nDescr = pTypeDescr->nMembers;
66 while (nDescr--)
68 ::uno_type_destructData(
69 (char *)pValue + pMemberOffsets[nDescr],
70 ppTypeRefs[nDescr], release );
74 //--------------------------------------------------------------------------------------------------
75 inline void _destructArray(
76 void * pValue,
77 typelib_ArrayTypeDescription * pTypeDescr,
78 uno_ReleaseFunc release )
79 throw ()
81 typelib_TypeDescription * pElementType = NULL;
82 TYPELIB_DANGER_GET( &pElementType, ((typelib_IndirectTypeDescription *)pTypeDescr)->pType );
83 sal_Int32 nElementSize = pElementType->nSize;
84 TYPELIB_DANGER_RELEASE( pElementType );
86 sal_Int32 nTotalElements = pTypeDescr->nTotalElements;
87 for(sal_Int32 i=0; i < nTotalElements; i++)
89 ::uno_type_destructData(
90 (sal_Char *)pValue + i * nElementSize,
91 ((typelib_IndirectTypeDescription *)pTypeDescr)->pType, release );
94 typelib_typedescriptionreference_release(((typelib_IndirectTypeDescription *)pTypeDescr)->pType);
97 //==============================================================================
98 void destructSequence(
99 uno_Sequence * pSequence,
100 typelib_TypeDescriptionReference * pType,
101 typelib_TypeDescription * pTypeDescr,
102 uno_ReleaseFunc release );
104 //--------------------------------------------------------------------------------------------------
105 inline void _destructAny(
106 uno_Any * pAny,
107 uno_ReleaseFunc release )
108 SAL_THROW(())
110 typelib_TypeDescriptionReference * pType = pAny->pType;
112 switch (pType->eTypeClass)
114 case typelib_TypeClass_HYPER:
115 case typelib_TypeClass_UNSIGNED_HYPER:
116 if (sizeof(void *) < sizeof(sal_Int64))
118 ::rtl_freeMemory( pAny->pData );
120 break;
121 case typelib_TypeClass_FLOAT:
122 if (sizeof(void *) < sizeof(float))
124 ::rtl_freeMemory( pAny->pData );
126 break;
127 case typelib_TypeClass_DOUBLE:
128 if (sizeof(void *) < sizeof(double))
130 ::rtl_freeMemory( pAny->pData );
132 break;
133 case typelib_TypeClass_STRING:
134 ::rtl_uString_release( (rtl_uString *)pAny->pReserved );
135 break;
136 case typelib_TypeClass_TYPE:
137 ::typelib_typedescriptionreference_release(
138 (typelib_TypeDescriptionReference *)pAny->pReserved );
139 break;
140 case typelib_TypeClass_ANY:
141 OSL_FAIL( "### unexpected nested any!" );
142 ::uno_any_destruct( (uno_Any *)pAny->pData, release );
143 ::rtl_freeMemory( pAny->pData );
144 break;
145 case typelib_TypeClass_TYPEDEF:
146 OSL_FAIL( "### unexpected typedef!" );
147 break;
148 case typelib_TypeClass_STRUCT:
149 case typelib_TypeClass_EXCEPTION:
151 typelib_TypeDescription * pTypeDescr = 0;
152 TYPELIB_DANGER_GET( &pTypeDescr, pType );
153 _destructStruct( pAny->pData, (typelib_CompoundTypeDescription *)pTypeDescr, release );
154 TYPELIB_DANGER_RELEASE( pTypeDescr );
155 ::rtl_freeMemory( pAny->pData );
156 break;
158 case typelib_TypeClass_UNION:
160 typelib_TypeDescription * pTypeDescr = 0;
161 TYPELIB_DANGER_GET( &pTypeDescr, pType );
162 _destructUnion( pAny->pData, pTypeDescr, release );
163 TYPELIB_DANGER_RELEASE( pTypeDescr );
164 ::rtl_freeMemory( pAny->pData );
165 break;
167 case typelib_TypeClass_SEQUENCE:
169 destructSequence(
170 *(uno_Sequence **) &pAny->pReserved, pType, 0, release );
171 break;
173 case typelib_TypeClass_INTERFACE:
174 _release( pAny->pReserved, release );
175 break;
176 default:
177 break;
179 #if OSL_DEBUG_LEVEL > 0
180 pAny->pData = (void *)0xdeadbeef;
181 #endif
183 ::typelib_typedescriptionreference_release( pType );
185 //--------------------------------------------------------------------------------------------------
186 inline sal_Int32 idestructElements(
187 void * pElements, typelib_TypeDescriptionReference * pElementType,
188 sal_Int32 nStartIndex, sal_Int32 nStopIndex,
189 uno_ReleaseFunc release )
190 SAL_THROW(())
192 switch (pElementType->eTypeClass)
194 case typelib_TypeClass_CHAR:
195 return (sal_Int32)(sizeof(sal_Unicode));
196 case typelib_TypeClass_BOOLEAN:
197 return (sal_Int32)(sizeof(sal_Bool));
198 case typelib_TypeClass_BYTE:
199 return (sal_Int32)(sizeof(sal_Int8));
200 case typelib_TypeClass_SHORT:
201 case typelib_TypeClass_UNSIGNED_SHORT:
202 return (sal_Int32)(sizeof(sal_Int16));
203 case typelib_TypeClass_LONG:
204 case typelib_TypeClass_UNSIGNED_LONG:
205 return (sal_Int32)(sizeof(sal_Int32));
206 case typelib_TypeClass_HYPER:
207 case typelib_TypeClass_UNSIGNED_HYPER:
208 return (sal_Int32)(sizeof(sal_Int64));
209 case typelib_TypeClass_FLOAT:
210 return (sal_Int32)(sizeof(float));
211 case typelib_TypeClass_DOUBLE:
212 return (sal_Int32)(sizeof(double));
214 case typelib_TypeClass_STRING:
216 rtl_uString ** pDest = (rtl_uString **)pElements;
217 for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
219 ::rtl_uString_release( pDest[nPos] );
221 return (sal_Int32)(sizeof(rtl_uString *));
223 case typelib_TypeClass_TYPE:
225 typelib_TypeDescriptionReference ** pDest = (typelib_TypeDescriptionReference **)pElements;
226 for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
228 ::typelib_typedescriptionreference_release( pDest[nPos] );
230 return (sal_Int32)(sizeof(typelib_TypeDescriptionReference *));
232 case typelib_TypeClass_ANY:
234 uno_Any * pDest = (uno_Any *)pElements;
235 for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
237 _destructAny( &pDest[nPos], release );
239 return (sal_Int32)(sizeof(uno_Any));
241 case typelib_TypeClass_ENUM:
242 return (sal_Int32)(sizeof(sal_Int32));
243 case typelib_TypeClass_STRUCT:
244 case typelib_TypeClass_EXCEPTION:
246 typelib_TypeDescription * pElementTypeDescr = 0;
247 TYPELIB_DANGER_GET( &pElementTypeDescr, pElementType );
248 sal_Int32 nElementSize = pElementTypeDescr->nSize;
249 for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
251 _destructStruct(
252 (char *)pElements + (nElementSize * nPos),
253 (typelib_CompoundTypeDescription *)pElementTypeDescr,
254 release );
256 sal_Int32 nSize = pElementTypeDescr->nSize;
257 TYPELIB_DANGER_RELEASE( pElementTypeDescr );
258 return nSize;
260 case typelib_TypeClass_UNION:
262 typelib_TypeDescription * pElementTypeDescr = 0;
263 TYPELIB_DANGER_GET( &pElementTypeDescr, pElementType );
264 sal_Int32 nElementSize = pElementTypeDescr->nSize;
265 for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
267 _destructUnion(
268 (char *)pElements + (nElementSize * nPos),
269 pElementTypeDescr,
270 release );
272 sal_Int32 nSize = pElementTypeDescr->nSize;
273 TYPELIB_DANGER_RELEASE( pElementTypeDescr );
274 return nSize;
276 case typelib_TypeClass_SEQUENCE:
278 typelib_TypeDescription * pElementTypeDescr = 0;
279 TYPELIB_DANGER_GET( &pElementTypeDescr, pElementType );
280 uno_Sequence ** pDest = (uno_Sequence **)pElements;
281 for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
283 destructSequence(
284 pDest[nPos],
285 pElementTypeDescr->pWeakRef, pElementTypeDescr,
286 release );
288 TYPELIB_DANGER_RELEASE( pElementTypeDescr );
289 return (sal_Int32)(sizeof(uno_Sequence *));
291 case typelib_TypeClass_INTERFACE:
293 if (release)
295 for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
297 void * p = ((void **)pElements)[nPos];
298 if (p)
300 (*release)( p );
304 else
306 for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
308 uno_Interface * p = ((uno_Interface **)pElements)[nPos];
309 if (p)
311 (*p->release)( p );
315 return (sal_Int32)(sizeof(void *));
317 default:
318 OSL_ASSERT(false);
319 return 0;
323 //------------------------------------------------------------------------------
324 inline void idestructSequence(
325 uno_Sequence * pSeq,
326 typelib_TypeDescriptionReference * pType,
327 typelib_TypeDescription * pTypeDescr,
328 uno_ReleaseFunc release )
330 if (osl_atomic_decrement( &pSeq->nRefCount ) == 0)
332 if (pSeq->nElements > 0)
334 if (pTypeDescr)
336 idestructElements(
337 pSeq->elements,
338 ((typelib_IndirectTypeDescription *) pTypeDescr)->pType, 0,
339 pSeq->nElements, release );
341 else
343 TYPELIB_DANGER_GET( &pTypeDescr, pType );
344 idestructElements(
345 pSeq->elements,
346 ((typelib_IndirectTypeDescription *) pTypeDescr)->pType, 0,
347 pSeq->nElements, release );
348 TYPELIB_DANGER_RELEASE( pTypeDescr );
351 ::rtl_freeMemory( pSeq );
355 //--------------------------------------------------------------------------------------------------
356 inline void _destructData(
357 void * pValue,
358 typelib_TypeDescriptionReference * pType,
359 typelib_TypeDescription * pTypeDescr,
360 uno_ReleaseFunc release )
361 SAL_THROW(())
363 switch (pType->eTypeClass)
365 case typelib_TypeClass_STRING:
366 ::rtl_uString_release( *(rtl_uString **)pValue );
367 break;
368 case typelib_TypeClass_TYPE:
369 ::typelib_typedescriptionreference_release( *(typelib_TypeDescriptionReference **)pValue );
370 break;
371 case typelib_TypeClass_ANY:
372 _destructAny( (uno_Any *)pValue, release );
373 break;
374 case typelib_TypeClass_TYPEDEF:
375 OSL_FAIL( "### unexpected typedef!" );
376 break;
377 case typelib_TypeClass_STRUCT:
378 case typelib_TypeClass_EXCEPTION:
379 if (pTypeDescr)
381 _destructStruct( pValue, (typelib_CompoundTypeDescription *)pTypeDescr, release );
383 else
385 TYPELIB_DANGER_GET( &pTypeDescr, pType );
386 _destructStruct( pValue, (typelib_CompoundTypeDescription *)pTypeDescr, release );
387 TYPELIB_DANGER_RELEASE( pTypeDescr );
389 break;
390 case typelib_TypeClass_ARRAY:
391 if (pTypeDescr)
393 _destructArray( pValue, (typelib_ArrayTypeDescription *)pTypeDescr, release );
395 else
397 TYPELIB_DANGER_GET( &pTypeDescr, pType );
398 _destructArray( pValue, (typelib_ArrayTypeDescription *)pTypeDescr, release );
399 TYPELIB_DANGER_RELEASE( pTypeDescr );
401 break;
402 case typelib_TypeClass_UNION:
403 if (pTypeDescr)
405 _destructUnion( pValue, pTypeDescr, release );
407 else
409 TYPELIB_DANGER_GET( &pTypeDescr, pType );
410 _destructUnion( pValue, pTypeDescr, release );
411 TYPELIB_DANGER_RELEASE( pTypeDescr );
413 break;
414 case typelib_TypeClass_SEQUENCE:
416 idestructSequence(
417 *(uno_Sequence **)pValue, pType, pTypeDescr, release );
418 break;
420 case typelib_TypeClass_INTERFACE:
421 _release( *(void **)pValue, release );
422 break;
423 default:
424 break;
430 #endif
432 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */