bump product version to 5.0.4.1
[LibreOffice.git] / cppu / source / uno / destr.hxx
blob1f0cc3213bb3fe6f2d4c9c8ec97aa45725fc7194
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 INCLUDED_CPPU_SOURCE_UNO_DESTR_HXX
20 #define INCLUDED_CPPU_SOURCE_UNO_DESTR_HXX
22 #include <sal/config.h>
24 #include <cassert>
26 #include "prim.hxx"
29 namespace cppu
33 //#### destruction #################################################################################
37 void destructStruct(
38 void * pValue,
39 typelib_CompoundTypeDescription * pTypeDescr,
40 uno_ReleaseFunc release );
42 inline void _destructStruct(
43 void * pValue,
44 typelib_CompoundTypeDescription * pTypeDescr,
45 uno_ReleaseFunc release )
47 if (pTypeDescr->pBaseTypeDescription)
49 destructStruct( pValue, pTypeDescr->pBaseTypeDescription, release );
52 typelib_TypeDescriptionReference ** ppTypeRefs = pTypeDescr->ppTypeRefs;
53 sal_Int32 * pMemberOffsets = pTypeDescr->pMemberOffsets;
54 sal_Int32 nDescr = pTypeDescr->nMembers;
55 while (nDescr--)
57 ::uno_type_destructData(
58 static_cast<char *>(pValue) + pMemberOffsets[nDescr],
59 ppTypeRefs[nDescr], release );
64 void destructSequence(
65 uno_Sequence * pSequence,
66 typelib_TypeDescriptionReference * pType,
67 typelib_TypeDescription * pTypeDescr,
68 uno_ReleaseFunc release );
71 inline void _destructAny(
72 uno_Any * pAny,
73 uno_ReleaseFunc release )
75 typelib_TypeDescriptionReference * pType = pAny->pType;
77 switch (pType->eTypeClass)
79 case typelib_TypeClass_HYPER:
80 case typelib_TypeClass_UNSIGNED_HYPER:
81 if (sizeof(void *) < sizeof(sal_Int64))
83 ::rtl_freeMemory( pAny->pData );
85 break;
86 case typelib_TypeClass_FLOAT:
87 if (sizeof(void *) < sizeof(float))
89 ::rtl_freeMemory( pAny->pData );
91 break;
92 case typelib_TypeClass_DOUBLE:
93 if (sizeof(void *) < sizeof(double))
95 ::rtl_freeMemory( pAny->pData );
97 break;
98 case typelib_TypeClass_STRING:
99 ::rtl_uString_release( static_cast<rtl_uString *>(pAny->pReserved) );
100 break;
101 case typelib_TypeClass_TYPE:
102 ::typelib_typedescriptionreference_release(
103 static_cast<typelib_TypeDescriptionReference *>(pAny->pReserved) );
104 break;
105 case typelib_TypeClass_ANY:
106 OSL_FAIL( "### unexpected nested any!" );
107 ::uno_any_destruct( static_cast<uno_Any *>(pAny->pData), release );
108 ::rtl_freeMemory( pAny->pData );
109 break;
110 case typelib_TypeClass_TYPEDEF:
111 OSL_FAIL( "### unexpected typedef!" );
112 break;
113 case typelib_TypeClass_STRUCT:
114 case typelib_TypeClass_EXCEPTION:
116 typelib_TypeDescription * pTypeDescr = 0;
117 TYPELIB_DANGER_GET( &pTypeDescr, pType );
118 _destructStruct( pAny->pData, reinterpret_cast<typelib_CompoundTypeDescription *>(pTypeDescr), release );
119 TYPELIB_DANGER_RELEASE( pTypeDescr );
120 ::rtl_freeMemory( pAny->pData );
121 break;
123 case typelib_TypeClass_SEQUENCE:
125 destructSequence(
126 static_cast<uno_Sequence *>(pAny->pReserved), pType, 0, release );
127 break;
129 case typelib_TypeClass_INTERFACE:
130 _release( pAny->pReserved, release );
131 break;
132 default:
133 break;
135 #if OSL_DEBUG_LEVEL > 0
136 pAny->pData = reinterpret_cast<void *>(0xdeadbeef);
137 #endif
139 ::typelib_typedescriptionreference_release( pType );
142 inline sal_Int32 idestructElements(
143 void * pElements, typelib_TypeDescriptionReference * pElementType,
144 sal_Int32 nStartIndex, sal_Int32 nStopIndex,
145 uno_ReleaseFunc release )
147 switch (pElementType->eTypeClass)
149 case typelib_TypeClass_CHAR:
150 return (sal_Int32)(sizeof(sal_Unicode));
151 case typelib_TypeClass_BOOLEAN:
152 return (sal_Int32)(sizeof(sal_Bool));
153 case typelib_TypeClass_BYTE:
154 return (sal_Int32)(sizeof(sal_Int8));
155 case typelib_TypeClass_SHORT:
156 case typelib_TypeClass_UNSIGNED_SHORT:
157 return (sal_Int32)(sizeof(sal_Int16));
158 case typelib_TypeClass_LONG:
159 case typelib_TypeClass_UNSIGNED_LONG:
160 return (sal_Int32)(sizeof(sal_Int32));
161 case typelib_TypeClass_HYPER:
162 case typelib_TypeClass_UNSIGNED_HYPER:
163 return (sal_Int32)(sizeof(sal_Int64));
164 case typelib_TypeClass_FLOAT:
165 return (sal_Int32)(sizeof(float));
166 case typelib_TypeClass_DOUBLE:
167 return (sal_Int32)(sizeof(double));
169 case typelib_TypeClass_STRING:
171 rtl_uString ** pDest = static_cast<rtl_uString **>(pElements);
172 for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
174 ::rtl_uString_release( pDest[nPos] );
176 return (sal_Int32)(sizeof(rtl_uString *));
178 case typelib_TypeClass_TYPE:
180 typelib_TypeDescriptionReference ** pDest = static_cast<typelib_TypeDescriptionReference **>(pElements);
181 for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
183 ::typelib_typedescriptionreference_release( pDest[nPos] );
185 return (sal_Int32)(sizeof(typelib_TypeDescriptionReference *));
187 case typelib_TypeClass_ANY:
189 uno_Any * pDest = static_cast<uno_Any *>(pElements);
190 for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
192 _destructAny( &pDest[nPos], release );
194 return (sal_Int32)(sizeof(uno_Any));
196 case typelib_TypeClass_ENUM:
197 return (sal_Int32)(sizeof(sal_Int32));
198 case typelib_TypeClass_STRUCT:
199 case typelib_TypeClass_EXCEPTION:
201 typelib_TypeDescription * pElementTypeDescr = 0;
202 TYPELIB_DANGER_GET( &pElementTypeDescr, pElementType );
203 sal_Int32 nElementSize = pElementTypeDescr->nSize;
204 for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
206 _destructStruct(
207 static_cast<char *>(pElements) + (nElementSize * nPos),
208 reinterpret_cast<typelib_CompoundTypeDescription *>(pElementTypeDescr),
209 release );
211 sal_Int32 nSize = pElementTypeDescr->nSize;
212 TYPELIB_DANGER_RELEASE( pElementTypeDescr );
213 return nSize;
215 case typelib_TypeClass_SEQUENCE:
217 typelib_TypeDescription * pElementTypeDescr = 0;
218 TYPELIB_DANGER_GET( &pElementTypeDescr, pElementType );
219 uno_Sequence ** pDest = static_cast<uno_Sequence **>(pElements);
220 for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
222 destructSequence(
223 pDest[nPos],
224 pElementTypeDescr->pWeakRef, pElementTypeDescr,
225 release );
227 TYPELIB_DANGER_RELEASE( pElementTypeDescr );
228 return (sal_Int32)(sizeof(uno_Sequence *));
230 case typelib_TypeClass_INTERFACE:
232 if (release)
234 for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
236 void * p = static_cast<void **>(pElements)[nPos];
237 if (p)
239 (*release)( p );
243 else
245 for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
247 uno_Interface * p = static_cast<uno_Interface **>(pElements)[nPos];
248 if (p)
250 (*p->release)( p );
254 return (sal_Int32)(sizeof(void *));
256 default:
257 OSL_ASSERT(false);
258 return 0;
262 inline void idestroySequence(
263 uno_Sequence * pSeq,
264 typelib_TypeDescriptionReference * pType,
265 typelib_TypeDescription * pTypeDescr,
266 uno_ReleaseFunc release )
268 assert(pSeq != nullptr);
269 assert(pSeq->nRefCount == 0);
270 if (pSeq->nElements > 0)
272 if (pTypeDescr)
274 idestructElements(
275 pSeq->elements,
276 reinterpret_cast<typelib_IndirectTypeDescription *>(pTypeDescr)->pType, 0,
277 pSeq->nElements, release );
279 else
281 TYPELIB_DANGER_GET( &pTypeDescr, pType );
282 idestructElements(
283 pSeq->elements,
284 reinterpret_cast<typelib_IndirectTypeDescription *>(pTypeDescr)->pType, 0,
285 pSeq->nElements, release );
286 TYPELIB_DANGER_RELEASE( pTypeDescr );
289 ::rtl_freeMemory( pSeq );
292 inline void idestructSequence(
293 uno_Sequence * pSeq,
294 typelib_TypeDescriptionReference * pType,
295 typelib_TypeDescription * pTypeDescr,
296 uno_ReleaseFunc release )
298 if (osl_atomic_decrement( &pSeq->nRefCount ) == 0)
300 idestroySequence(pSeq, pType, pTypeDescr, release);
304 inline void _destructData(
305 void * pValue,
306 typelib_TypeDescriptionReference * pType,
307 typelib_TypeDescription * pTypeDescr,
308 uno_ReleaseFunc release )
310 switch (pType->eTypeClass)
312 case typelib_TypeClass_STRING:
313 ::rtl_uString_release( *static_cast<rtl_uString **>(pValue) );
314 break;
315 case typelib_TypeClass_TYPE:
316 ::typelib_typedescriptionreference_release( *static_cast<typelib_TypeDescriptionReference **>(pValue) );
317 break;
318 case typelib_TypeClass_ANY:
319 _destructAny( static_cast<uno_Any *>(pValue), release );
320 break;
321 case typelib_TypeClass_TYPEDEF:
322 OSL_FAIL( "### unexpected typedef!" );
323 break;
324 case typelib_TypeClass_STRUCT:
325 case typelib_TypeClass_EXCEPTION:
326 if (pTypeDescr)
328 _destructStruct( pValue, reinterpret_cast<typelib_CompoundTypeDescription *>(pTypeDescr), release );
330 else
332 TYPELIB_DANGER_GET( &pTypeDescr, pType );
333 _destructStruct( pValue, reinterpret_cast<typelib_CompoundTypeDescription *>(pTypeDescr), release );
334 TYPELIB_DANGER_RELEASE( pTypeDescr );
336 break;
337 case typelib_TypeClass_SEQUENCE:
339 idestructSequence(
340 *static_cast<uno_Sequence **>(pValue), pType, pTypeDescr, release );
341 break;
343 case typelib_TypeClass_INTERFACE:
344 _release( *static_cast<void **>(pValue), release );
345 break;
346 default:
347 break;
353 #endif
355 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */