Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 : #ifndef DESTR_HXX
20 : #define DESTR_HXX
21 :
22 : #include "prim.hxx"
23 :
24 :
25 : namespace cppu
26 : {
27 :
28 :
29 : //#### destruction #################################################################################
30 :
31 :
32 :
33 : void destructStruct(
34 : void * pValue,
35 : typelib_CompoundTypeDescription * pTypeDescr,
36 : uno_ReleaseFunc release )
37 : SAL_THROW(());
38 :
39 3 : inline void _destructStruct(
40 : void * pValue,
41 : typelib_CompoundTypeDescription * pTypeDescr,
42 : uno_ReleaseFunc release )
43 : SAL_THROW(())
44 : {
45 3 : if (pTypeDescr->pBaseTypeDescription)
46 : {
47 0 : destructStruct( pValue, pTypeDescr->pBaseTypeDescription, release );
48 : }
49 :
50 3 : typelib_TypeDescriptionReference ** ppTypeRefs = pTypeDescr->ppTypeRefs;
51 3 : sal_Int32 * pMemberOffsets = pTypeDescr->pMemberOffsets;
52 3 : sal_Int32 nDescr = pTypeDescr->nMembers;
53 12 : while (nDescr--)
54 : {
55 : ::uno_type_destructData(
56 12 : (char *)pValue + pMemberOffsets[nDescr],
57 18 : ppTypeRefs[nDescr], release );
58 : }
59 3 : }
60 :
61 :
62 : void destructSequence(
63 : uno_Sequence * pSequence,
64 : typelib_TypeDescriptionReference * pType,
65 : typelib_TypeDescription * pTypeDescr,
66 : uno_ReleaseFunc release );
67 :
68 :
69 103239 : inline void _destructAny(
70 : uno_Any * pAny,
71 : uno_ReleaseFunc release )
72 : SAL_THROW(())
73 : {
74 103239 : typelib_TypeDescriptionReference * pType = pAny->pType;
75 :
76 103239 : switch (pType->eTypeClass)
77 : {
78 : case typelib_TypeClass_HYPER:
79 : case typelib_TypeClass_UNSIGNED_HYPER:
80 : if (sizeof(void *) < sizeof(sal_Int64))
81 : {
82 87 : ::rtl_freeMemory( pAny->pData );
83 : }
84 87 : break;
85 : case typelib_TypeClass_FLOAT:
86 : if (sizeof(void *) < sizeof(float))
87 : {
88 : ::rtl_freeMemory( pAny->pData );
89 : }
90 0 : break;
91 : case typelib_TypeClass_DOUBLE:
92 : if (sizeof(void *) < sizeof(double))
93 : {
94 237 : ::rtl_freeMemory( pAny->pData );
95 : }
96 237 : break;
97 : case typelib_TypeClass_STRING:
98 38599 : ::rtl_uString_release( (rtl_uString *)pAny->pReserved );
99 38599 : break;
100 : case typelib_TypeClass_TYPE:
101 : ::typelib_typedescriptionreference_release(
102 0 : (typelib_TypeDescriptionReference *)pAny->pReserved );
103 0 : break;
104 : case typelib_TypeClass_ANY:
105 : OSL_FAIL( "### unexpected nested any!" );
106 0 : ::uno_any_destruct( (uno_Any *)pAny->pData, release );
107 0 : ::rtl_freeMemory( pAny->pData );
108 0 : break;
109 : case typelib_TypeClass_TYPEDEF:
110 : OSL_FAIL( "### unexpected typedef!" );
111 0 : break;
112 : case typelib_TypeClass_STRUCT:
113 : case typelib_TypeClass_EXCEPTION:
114 : {
115 3 : typelib_TypeDescription * pTypeDescr = 0;
116 3 : TYPELIB_DANGER_GET( &pTypeDescr, pType );
117 3 : _destructStruct( pAny->pData, (typelib_CompoundTypeDescription *)pTypeDescr, release );
118 3 : TYPELIB_DANGER_RELEASE( pTypeDescr );
119 3 : ::rtl_freeMemory( pAny->pData );
120 3 : break;
121 : }
122 : case typelib_TypeClass_SEQUENCE:
123 : {
124 : destructSequence(
125 3553 : static_cast<uno_Sequence *>(pAny->pReserved), pType, 0, release );
126 3553 : break;
127 : }
128 : case typelib_TypeClass_INTERFACE:
129 279 : _release( pAny->pReserved, release );
130 279 : break;
131 : default:
132 60481 : break;
133 : }
134 : #if OSL_DEBUG_LEVEL > 0
135 : pAny->pData = (void *)0xdeadbeef;
136 : #endif
137 :
138 103239 : ::typelib_typedescriptionreference_release( pType );
139 103239 : }
140 :
141 793 : inline sal_Int32 idestructElements(
142 : void * pElements, typelib_TypeDescriptionReference * pElementType,
143 : sal_Int32 nStartIndex, sal_Int32 nStopIndex,
144 : uno_ReleaseFunc release )
145 : SAL_THROW(())
146 : {
147 793 : switch (pElementType->eTypeClass)
148 : {
149 : case typelib_TypeClass_CHAR:
150 0 : return (sal_Int32)(sizeof(sal_Unicode));
151 : case typelib_TypeClass_BOOLEAN:
152 0 : return (sal_Int32)(sizeof(sal_Bool));
153 : case typelib_TypeClass_BYTE:
154 33 : return (sal_Int32)(sizeof(sal_Int8));
155 : case typelib_TypeClass_SHORT:
156 : case typelib_TypeClass_UNSIGNED_SHORT:
157 0 : return (sal_Int32)(sizeof(sal_Int16));
158 : case typelib_TypeClass_LONG:
159 : case typelib_TypeClass_UNSIGNED_LONG:
160 3 : return (sal_Int32)(sizeof(sal_Int32));
161 : case typelib_TypeClass_HYPER:
162 : case typelib_TypeClass_UNSIGNED_HYPER:
163 1 : return (sal_Int32)(sizeof(sal_Int64));
164 : case typelib_TypeClass_FLOAT:
165 0 : return (sal_Int32)(sizeof(float));
166 : case typelib_TypeClass_DOUBLE:
167 0 : return (sal_Int32)(sizeof(double));
168 :
169 : case typelib_TypeClass_STRING:
170 : {
171 736 : rtl_uString ** pDest = (rtl_uString **)pElements;
172 2990 : for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
173 : {
174 2254 : ::rtl_uString_release( pDest[nPos] );
175 : }
176 736 : return (sal_Int32)(sizeof(rtl_uString *));
177 : }
178 : case typelib_TypeClass_TYPE:
179 : {
180 0 : typelib_TypeDescriptionReference ** pDest = (typelib_TypeDescriptionReference **)pElements;
181 0 : for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
182 : {
183 0 : ::typelib_typedescriptionreference_release( pDest[nPos] );
184 : }
185 0 : return (sal_Int32)(sizeof(typelib_TypeDescriptionReference *));
186 : }
187 : case typelib_TypeClass_ANY:
188 : {
189 3 : uno_Any * pDest = (uno_Any *)pElements;
190 6 : for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
191 : {
192 3 : _destructAny( &pDest[nPos], release );
193 : }
194 3 : return (sal_Int32)(sizeof(uno_Any));
195 : }
196 : case typelib_TypeClass_ENUM:
197 0 : return (sal_Int32)(sizeof(sal_Int32));
198 : case typelib_TypeClass_STRUCT:
199 : case typelib_TypeClass_EXCEPTION:
200 : {
201 0 : typelib_TypeDescription * pElementTypeDescr = 0;
202 0 : TYPELIB_DANGER_GET( &pElementTypeDescr, pElementType );
203 0 : sal_Int32 nElementSize = pElementTypeDescr->nSize;
204 0 : for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
205 : {
206 : _destructStruct(
207 0 : (char *)pElements + (nElementSize * nPos),
208 : (typelib_CompoundTypeDescription *)pElementTypeDescr,
209 0 : release );
210 : }
211 0 : sal_Int32 nSize = pElementTypeDescr->nSize;
212 0 : TYPELIB_DANGER_RELEASE( pElementTypeDescr );
213 0 : return nSize;
214 : }
215 : case typelib_TypeClass_SEQUENCE:
216 : {
217 0 : typelib_TypeDescription * pElementTypeDescr = 0;
218 0 : TYPELIB_DANGER_GET( &pElementTypeDescr, pElementType );
219 0 : uno_Sequence ** pDest = (uno_Sequence **)pElements;
220 0 : for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
221 : {
222 : destructSequence(
223 0 : pDest[nPos],
224 : pElementTypeDescr->pWeakRef, pElementTypeDescr,
225 0 : release );
226 : }
227 0 : TYPELIB_DANGER_RELEASE( pElementTypeDescr );
228 0 : return (sal_Int32)(sizeof(uno_Sequence *));
229 : }
230 : case typelib_TypeClass_INTERFACE:
231 : {
232 17 : if (release)
233 : {
234 53 : for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
235 : {
236 36 : void * p = ((void **)pElements)[nPos];
237 36 : if (p)
238 : {
239 36 : (*release)( p );
240 : }
241 : }
242 : }
243 : else
244 : {
245 0 : for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
246 : {
247 0 : uno_Interface * p = ((uno_Interface **)pElements)[nPos];
248 0 : if (p)
249 : {
250 0 : (*p->release)( p );
251 : }
252 : }
253 : }
254 17 : return (sal_Int32)(sizeof(void *));
255 : }
256 : default:
257 : OSL_ASSERT(false);
258 0 : return 0;
259 : }
260 : }
261 :
262 :
263 4569 : inline void idestructSequence(
264 : uno_Sequence * pSeq,
265 : typelib_TypeDescriptionReference * pType,
266 : typelib_TypeDescription * pTypeDescr,
267 : uno_ReleaseFunc release )
268 : {
269 4569 : if (osl_atomic_decrement( &pSeq->nRefCount ) == 0)
270 : {
271 924 : if (pSeq->nElements > 0)
272 : {
273 793 : if (pTypeDescr)
274 : {
275 : idestructElements(
276 : pSeq->elements,
277 : ((typelib_IndirectTypeDescription *) pTypeDescr)->pType, 0,
278 0 : pSeq->nElements, release );
279 : }
280 : else
281 : {
282 793 : TYPELIB_DANGER_GET( &pTypeDescr, pType );
283 : idestructElements(
284 : pSeq->elements,
285 : ((typelib_IndirectTypeDescription *) pTypeDescr)->pType, 0,
286 793 : pSeq->nElements, release );
287 793 : TYPELIB_DANGER_RELEASE( pTypeDescr );
288 : }
289 : }
290 924 : ::rtl_freeMemory( pSeq );
291 : }
292 4569 : }
293 :
294 :
295 984 : inline void _destructData(
296 : void * pValue,
297 : typelib_TypeDescriptionReference * pType,
298 : typelib_TypeDescription * pTypeDescr,
299 : uno_ReleaseFunc release )
300 : SAL_THROW(())
301 : {
302 984 : switch (pType->eTypeClass)
303 : {
304 : case typelib_TypeClass_STRING:
305 0 : ::rtl_uString_release( *(rtl_uString **)pValue );
306 0 : break;
307 : case typelib_TypeClass_TYPE:
308 0 : ::typelib_typedescriptionreference_release( *(typelib_TypeDescriptionReference **)pValue );
309 0 : break;
310 : case typelib_TypeClass_ANY:
311 3 : _destructAny( (uno_Any *)pValue, release );
312 3 : break;
313 : case typelib_TypeClass_TYPEDEF:
314 : OSL_FAIL( "### unexpected typedef!" );
315 0 : break;
316 : case typelib_TypeClass_STRUCT:
317 : case typelib_TypeClass_EXCEPTION:
318 0 : if (pTypeDescr)
319 : {
320 0 : _destructStruct( pValue, (typelib_CompoundTypeDescription *)pTypeDescr, release );
321 : }
322 : else
323 : {
324 0 : TYPELIB_DANGER_GET( &pTypeDescr, pType );
325 0 : _destructStruct( pValue, (typelib_CompoundTypeDescription *)pTypeDescr, release );
326 0 : TYPELIB_DANGER_RELEASE( pTypeDescr );
327 : }
328 0 : break;
329 : case typelib_TypeClass_SEQUENCE:
330 : {
331 : idestructSequence(
332 978 : *(uno_Sequence **)pValue, pType, pTypeDescr, release );
333 978 : break;
334 : }
335 : case typelib_TypeClass_INTERFACE:
336 0 : _release( *(void **)pValue, release );
337 0 : break;
338 : default:
339 3 : break;
340 : }
341 984 : }
342 :
343 : }
344 :
345 : #endif
346 :
347 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|