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