LibreOffice Module cppu (master) 1
assign.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 "prim.hxx"
22#include "destr.hxx"
23#include "constr.hxx"
24#include "copy.hxx"
25
26
27namespace cppu
28{
29
30
31//#### assignment ##################################################################################
32
33
34inline void _assignInterface(
35 void ** ppDest, void * pSource,
36 uno_AcquireFunc acquire, uno_ReleaseFunc release )
37
38{
39 _acquire( pSource, acquire );
40 void * const pToBeReleased = *ppDest;
41 *ppDest = pSource;
42 _release( pToBeReleased, release );
43}
44
45inline void * _queryInterface(
46 void * pSource,
47 typelib_TypeDescriptionReference * pDestType,
48 uno_QueryInterfaceFunc queryInterface )
49{
50 if (pSource)
51 {
52 if (nullptr == queryInterface)
54 pSource = (*queryInterface)( pSource, pDestType );
55 }
56 return pSource;
57}
58
59bool assignStruct(
60 void * pDest, void * pSource,
61 typelib_CompoundTypeDescription * pTypeDescr,
62 uno_QueryInterfaceFunc queryInterface, uno_AcquireFunc acquire, uno_ReleaseFunc release );
63
64inline bool _assignStruct(
65 void * pDest, void * pSource,
66 typelib_CompoundTypeDescription * pTypeDescr,
67 uno_QueryInterfaceFunc queryInterface, uno_AcquireFunc acquire, uno_ReleaseFunc release )
68{
69 if (pTypeDescr->pBaseTypeDescription)
70 {
71 // copy base value
72 if (! assignStruct( pDest, pSource, pTypeDescr->pBaseTypeDescription,
73 queryInterface, acquire, release ))
74 {
75 return false;
76 }
77 }
78 // then copy members
79 typelib_TypeDescriptionReference ** ppTypeRefs = pTypeDescr->ppTypeRefs;
80 sal_Int32 * pMemberOffsets = pTypeDescr->pMemberOffsets;
81 sal_Int32 nDescr = pTypeDescr->nMembers;
82 while (nDescr--)
83 {
84 if (! ::uno_type_assignData( static_cast<char *>(pDest) + pMemberOffsets[nDescr],
85 ppTypeRefs[nDescr],
86 static_cast<char *>(pSource) + pMemberOffsets[nDescr],
87 ppTypeRefs[nDescr],
88 queryInterface, acquire, release ))
89 {
90 return false;
91 }
92 }
93 return true;
94}
95
96inline bool _assignData(
97 void * pDest,
98 typelib_TypeDescriptionReference * pDestType, typelib_TypeDescription * pDestTypeDescr,
99 void * pSource,
100 typelib_TypeDescriptionReference * pSourceType, typelib_TypeDescription * pSourceTypeDescr,
101 uno_QueryInterfaceFunc queryInterface, uno_AcquireFunc acquire, uno_ReleaseFunc release )
102{
103 if (pDest == pSource)
104 return _type_equals( pDestType, pSourceType );
105
106 if (! pSource)
107 {
108 _destructData( pDest, pDestType, pDestTypeDescr, release );
109 _defaultConstructData( pDest, pDestType, pDestTypeDescr );
110 return true;
111 }
112 while (typelib_TypeClass_ANY == pSourceType->eTypeClass)
113 {
114 pSourceTypeDescr = nullptr;
115 pSourceType = static_cast<uno_Any *>(pSource)->pType;
116 pSource = static_cast<uno_Any *>(pSource)->pData;
117 if (pDest == pSource)
118 return true;
119 }
120
121 switch (pDestType->eTypeClass)
122 {
123 case typelib_TypeClass_VOID:
124 return pSourceType->eTypeClass == typelib_TypeClass_VOID;
125 case typelib_TypeClass_CHAR:
126 switch (pSourceType->eTypeClass)
127 {
128 case typelib_TypeClass_CHAR:
129 *static_cast<sal_Unicode *>(pDest) = *static_cast<sal_Unicode *>(pSource);
130 return true;
131 default:
132 return false;
133 }
134 case typelib_TypeClass_BOOLEAN:
135 switch (pSourceType->eTypeClass)
136 {
137 case typelib_TypeClass_BOOLEAN:
138 *static_cast<sal_Bool *>(pDest) = bool(*static_cast<sal_Bool *>(pSource));
139 return true;
140 default:
141 return false;
142 }
143 case typelib_TypeClass_BYTE:
144 switch (pSourceType->eTypeClass)
145 {
146 case typelib_TypeClass_BYTE:
147 *static_cast<sal_Int8 *>(pDest) = *static_cast<sal_Int8 *>(pSource);
148 return true;
149 default:
150 return false;
151 }
152 case typelib_TypeClass_SHORT:
153 switch (pSourceType->eTypeClass)
154 {
155 case typelib_TypeClass_BYTE:
156 *static_cast<sal_Int16 *>(pDest) = *static_cast<sal_Int8 *>(pSource);
157 return true;
158 case typelib_TypeClass_SHORT:
159 case typelib_TypeClass_UNSIGNED_SHORT:
160 *static_cast<sal_Int16 *>(pDest) = *static_cast<sal_Int16 *>(pSource);
161 return true;
162 default:
163 return false;
164 }
165 case typelib_TypeClass_UNSIGNED_SHORT:
166 switch (pSourceType->eTypeClass)
167 {
168 case typelib_TypeClass_BYTE:
169 *static_cast<sal_uInt16 *>(pDest) = *static_cast<sal_Int8 *>(pSource);
170 return true;
171 case typelib_TypeClass_SHORT:
172 case typelib_TypeClass_UNSIGNED_SHORT:
173 *static_cast<sal_uInt16 *>(pDest) = *static_cast<sal_uInt16 *>(pSource);
174 return true;
175 default:
176 return false;
177 }
178 case typelib_TypeClass_LONG:
179 switch (pSourceType->eTypeClass)
180 {
181 case typelib_TypeClass_BYTE:
182 *static_cast<sal_Int32 *>(pDest) = *static_cast<sal_Int8 *>(pSource);
183 return true;
184 case typelib_TypeClass_SHORT:
185 *static_cast<sal_Int32 *>(pDest) = *static_cast<sal_Int16 *>(pSource);
186 return true;
187 case typelib_TypeClass_UNSIGNED_SHORT:
188 *static_cast<sal_Int32 *>(pDest) = *static_cast<sal_uInt16 *>(pSource);
189 return true;
190 case typelib_TypeClass_LONG:
191 case typelib_TypeClass_UNSIGNED_LONG:
192 *static_cast<sal_Int32 *>(pDest) = *static_cast<sal_Int32 *>(pSource);
193 return true;
194 default:
195 return false;
196 }
197 case typelib_TypeClass_UNSIGNED_LONG:
198 switch (pSourceType->eTypeClass)
199 {
200 case typelib_TypeClass_BYTE:
201 *static_cast<sal_uInt32 *>(pDest) = *static_cast<sal_Int8 *>(pSource);
202 return true;
203 case typelib_TypeClass_SHORT:
204 *static_cast<sal_uInt32 *>(pDest) = *static_cast<sal_Int16 *>(pSource);
205 return true;
206 case typelib_TypeClass_UNSIGNED_SHORT:
207 *static_cast<sal_uInt32 *>(pDest) = *static_cast<sal_uInt16 *>(pSource);
208 return true;
209 case typelib_TypeClass_LONG:
210 case typelib_TypeClass_UNSIGNED_LONG:
211 *static_cast<sal_uInt32 *>(pDest) = *static_cast<sal_uInt32 *>(pSource);
212 return true;
213 default:
214 return false;
215 }
216 case typelib_TypeClass_HYPER:
217 switch (pSourceType->eTypeClass)
218 {
219 case typelib_TypeClass_BYTE:
220 *static_cast<sal_Int64 *>(pDest) = *static_cast<sal_Int8 *>(pSource);
221 return true;
222 case typelib_TypeClass_SHORT:
223 *static_cast<sal_Int64 *>(pDest) = *static_cast<sal_Int16 *>(pSource);
224 return true;
225 case typelib_TypeClass_UNSIGNED_SHORT:
226 *static_cast<sal_Int64 *>(pDest) = *static_cast<sal_uInt16 *>(pSource);
227 return true;
228 case typelib_TypeClass_LONG:
229 *static_cast<sal_Int64 *>(pDest) = *static_cast<sal_Int32 *>(pSource);
230 return true;
231 case typelib_TypeClass_UNSIGNED_LONG:
232 *static_cast<sal_Int64 *>(pDest) = *static_cast<sal_uInt32 *>(pSource);
233 return true;
234 case typelib_TypeClass_HYPER:
235 case typelib_TypeClass_UNSIGNED_HYPER:
236 *static_cast<sal_Int64 *>(pDest) = *static_cast<sal_Int64 *>(pSource);
237 return true;
238 default:
239 return false;
240 }
241 case typelib_TypeClass_UNSIGNED_HYPER:
242 switch (pSourceType->eTypeClass)
243 {
244 case typelib_TypeClass_BYTE:
245 *static_cast<sal_uInt64 *>(pDest) = *static_cast<sal_Int8 *>(pSource);
246 return true;
247 case typelib_TypeClass_SHORT:
248 *static_cast<sal_uInt64 *>(pDest) = *static_cast<sal_Int16 *>(pSource);
249 return true;
250 case typelib_TypeClass_UNSIGNED_SHORT:
251 *static_cast<sal_uInt64 *>(pDest) = *static_cast<sal_uInt16 *>(pSource);
252 return true;
253 case typelib_TypeClass_LONG:
254 *static_cast<sal_uInt64 *>(pDest) = *static_cast<sal_Int32 *>(pSource);
255 return true;
256 case typelib_TypeClass_UNSIGNED_LONG:
257 *static_cast<sal_uInt64 *>(pDest) = *static_cast<sal_uInt32 *>(pSource);
258 return true;
259 case typelib_TypeClass_HYPER:
260 case typelib_TypeClass_UNSIGNED_HYPER:
261 *static_cast<sal_uInt64 *>(pDest) = *static_cast<sal_uInt64 *>(pSource);
262 return true;
263 default:
264 return false;
265 }
266 case typelib_TypeClass_FLOAT:
267 switch (pSourceType->eTypeClass)
268 {
269 case typelib_TypeClass_BYTE:
270 *static_cast<float *>(pDest) = *static_cast<sal_Int8 *>(pSource);
271 return true;
272 case typelib_TypeClass_SHORT:
273 *static_cast<float *>(pDest) = *static_cast<sal_Int16 *>(pSource);
274 return true;
275 case typelib_TypeClass_UNSIGNED_SHORT:
276 *static_cast<float *>(pDest) = *static_cast<sal_uInt16 *>(pSource);
277 return true;
278 case typelib_TypeClass_FLOAT:
279 *static_cast<float *>(pDest) = *static_cast<float *>(pSource);
280 return true;
281 default:
282 return false;
283 }
284 case typelib_TypeClass_DOUBLE:
285 switch (pSourceType->eTypeClass)
286 {
287 case typelib_TypeClass_BYTE:
288 *static_cast<double *>(pDest) = *static_cast<sal_Int8 *>(pSource);
289 return true;
290 case typelib_TypeClass_SHORT:
291 *static_cast<double *>(pDest) = *static_cast<sal_Int16 *>(pSource);
292 return true;
293 case typelib_TypeClass_UNSIGNED_SHORT:
294 *static_cast<double *>(pDest) = *static_cast<sal_uInt16 *>(pSource);
295 return true;
296 case typelib_TypeClass_LONG:
297 *static_cast<double *>(pDest) = *static_cast<sal_Int32 *>(pSource);
298 return true;
299 case typelib_TypeClass_UNSIGNED_LONG:
300 *static_cast<double *>(pDest) = *static_cast<sal_uInt32 *>(pSource);
301 return true;
302 case typelib_TypeClass_FLOAT:
303 *static_cast<double *>(pDest) = *static_cast<float *>(pSource);
304 return true;
305 case typelib_TypeClass_DOUBLE:
306 *static_cast<double *>(pDest) = *static_cast<double *>(pSource);
307 return true;
308 default:
309 return false;
310 }
311 case typelib_TypeClass_STRING:
312 switch (pSourceType->eTypeClass)
313 {
314 case typelib_TypeClass_STRING:
315 ::rtl_uString_assign( static_cast<rtl_uString **>(pDest), *static_cast<rtl_uString **>(pSource) );
316 return true;
317 default:
318 return false;
319 }
320 case typelib_TypeClass_TYPE:
321 switch (pSourceType->eTypeClass)
322 {
323 case typelib_TypeClass_TYPE:
324 {
325 typelib_TypeDescriptionReference ** pp = static_cast<typelib_TypeDescriptionReference **>(pDest);
327 *pp = *static_cast<typelib_TypeDescriptionReference **>(pSource);
328 TYPE_ACQUIRE( *pp );
329 return true;
330 }
331 default:
332 return false;
333 }
334 case typelib_TypeClass_ANY:
335 _destructAny( static_cast<uno_Any *>(pDest), release );
336 _copyConstructAny( static_cast<uno_Any *>(pDest), pSource, pSourceType, pSourceTypeDescr, acquire, nullptr );
337 return true;
338 case typelib_TypeClass_ENUM:
339 if (_type_equals( pDestType, pSourceType ))
340 {
341 *static_cast<sal_Int32 *>(pDest) = *static_cast<sal_Int32 *>(pSource);
342 return true;
343 }
344 return false;
345 case typelib_TypeClass_STRUCT:
346 case typelib_TypeClass_EXCEPTION:
347 if (typelib_TypeClass_STRUCT == pSourceType->eTypeClass ||
348 typelib_TypeClass_EXCEPTION == pSourceType->eTypeClass)
349 {
350 bool bRet = false;
351 if (pSourceTypeDescr)
352 {
353 typelib_CompoundTypeDescription * pTypeDescr =
354 reinterpret_cast<typelib_CompoundTypeDescription *>(pSourceTypeDescr);
355 while (pTypeDescr &&
356 !_type_equals(pTypeDescr->aBase.pWeakRef, pDestType))
357 {
358 pTypeDescr = pTypeDescr->pBaseTypeDescription;
359 }
360 if (pTypeDescr)
361 {
362 bRet = _assignStruct(
363 pDest, pSource, pTypeDescr, queryInterface, acquire, release );
364 }
365 }
366 else
367 {
368 TYPELIB_DANGER_GET( &pSourceTypeDescr, pSourceType );
369 typelib_CompoundTypeDescription * pTypeDescr =
370 reinterpret_cast<typelib_CompoundTypeDescription *>(pSourceTypeDescr);
371 while (pTypeDescr &&
372 !_type_equals(pTypeDescr->aBase.pWeakRef, pDestType))
373 {
374 pTypeDescr = pTypeDescr->pBaseTypeDescription;
375 }
376 if (pTypeDescr)
377 {
378 bRet = _assignStruct(
379 pDest, pSource, pTypeDescr, queryInterface, acquire, release );
380 }
381 TYPELIB_DANGER_RELEASE( pSourceTypeDescr );
382 }
383 return bRet;
384 }
385 return false;
386 case typelib_TypeClass_SEQUENCE:
387 if (!_type_equals( pDestType, pSourceType ))
388 return false;
389 // check self assignment (only after _type_equals, to account for shared static empty):
390 if (*static_cast<uno_Sequence **>(pSource) != *static_cast<uno_Sequence **>(pDest))
391 {
392 osl_atomic_increment( &(*static_cast<uno_Sequence **>(pSource))->nRefCount );
394 *static_cast<uno_Sequence **>(pDest), pDestType, pDestTypeDescr, release );
395 *static_cast<uno_Sequence **>(pDest) = *static_cast<uno_Sequence **>(pSource);
396 }
397 return true;
398 case typelib_TypeClass_INTERFACE:
399 if (typelib_TypeClass_INTERFACE != pSourceType->eTypeClass)
400 return false;
401 if (_type_equals( pDestType, pSourceType ))
402 {
403 _assignInterface( static_cast<void **>(pDest), *static_cast<void **>(pSource), acquire, release );
404 return true;
405 }
406 else if (*static_cast< void ** >(pSource) == nullptr)
407 {
408 // A null reference of any interface type can be converted to a null
409 // reference of any other interface type:
410 void * const pToBeReleased = *static_cast< void ** >(pDest);
411 *static_cast< void ** >(pDest) = nullptr;
412 _release( pToBeReleased, release );
413 return true;
414 }
415 else
416 {
417 if (pSourceTypeDescr)
418 {
419 typelib_TypeDescription * pTD = pSourceTypeDescr;
420 while (pTD && !_type_equals( pTD->pWeakRef, pDestType ))
421 {
422 pTD = &reinterpret_cast<typelib_InterfaceTypeDescription *>(pTD)->pBaseTypeDescription->aBase;
423 }
424 if (pTD) // is base of dest
425 {
426 _assignInterface( static_cast<void **>(pDest), *static_cast<void **>(pSource), acquire, release );
427 return true;
428 }
429 }
430
431 // query for interface:
432 void * pQueried = _queryInterface( *static_cast<void **>(pSource),
433 pDestType, queryInterface );
434 if (pQueried != nullptr) {
435 void * const pToBeReleased = *static_cast<void **>(pDest);
436 *static_cast<void **>(pDest) = pQueried;
437 _release( pToBeReleased, release );
438 }
439 return (pQueried != nullptr);
440 }
441 default:
442 OSL_ASSERT(false);
443 return false;
444 }
445}
446
447}
448
449/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Int32 nRefCount
Definition: copy.hxx:38
sal_Bool SAL_CALL uno_type_assignData(void *pDest, typelib_TypeDescriptionReference *pDestType, void *pSource, typelib_TypeDescriptionReference *pSourceType, uno_QueryInterfaceFunc queryInterface, uno_AcquireFunc acquire, uno_ReleaseFunc release) SAL_THROW_EXTERN_C()
Definition: data.cxx:262
std::unique_ptr< sal_Int32[]> pData
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
css::uno::Any SAL_CALL queryInterface(const css::uno::Type &rType, Interface1 *p1)
void _acquire(void *p, uno_AcquireFunc acquire)
Definition: prim.hxx:64
void _assignInterface(void **ppDest, void *pSource, uno_AcquireFunc acquire, uno_ReleaseFunc release)
Definition: assign.hxx:34
bool assignStruct(void *pDest, void *pSource, typelib_CompoundTypeDescription *pTypeDescr, uno_QueryInterfaceFunc queryInterface, uno_AcquireFunc acquire, uno_ReleaseFunc release)
Definition: data.cxx:130
void _defaultConstructData(void *pMem, typelib_TypeDescriptionReference *pType, typelib_TypeDescription *pTypeDescr)
Definition: constr.hxx:55
void _copyConstructAny(uno_Any *pDestAny, void *pSource, typelib_TypeDescriptionReference *pType, typelib_TypeDescription *pTypeDescr, uno_AcquireFunc acquire, uno_Mapping *mapping)
Definition: copy.hxx:256
bool _assignData(void *pDest, typelib_TypeDescriptionReference *pDestType, typelib_TypeDescription *pDestTypeDescr, void *pSource, typelib_TypeDescriptionReference *pSourceType, typelib_TypeDescription *pSourceTypeDescr, uno_QueryInterfaceFunc queryInterface, uno_AcquireFunc acquire, uno_ReleaseFunc release)
Definition: assign.hxx:96
void idestructSequence(uno_Sequence *pSeq, typelib_TypeDescriptionReference *pType, typelib_TypeDescription *pTypeDescr, uno_ReleaseFunc release)
Definition: destr.hxx:291
void * binuno_queryInterface(void *pUnoI, typelib_TypeDescriptionReference *pDestType)
Definition: data.cxx:40
void * _queryInterface(void *pSource, typelib_TypeDescriptionReference *pDestType, uno_QueryInterfaceFunc queryInterface)
Definition: assign.hxx:45
void _destructAny(uno_Any *pAny, uno_ReleaseFunc release)
Definition: destr.hxx:70
bool _type_equals(typelib_TypeDescriptionReference const *pType1, typelib_TypeDescriptionReference const *pType2)
Definition: prim.hxx:141
bool _assignStruct(void *pDest, void *pSource, typelib_CompoundTypeDescription *pTypeDescr, uno_QueryInterfaceFunc queryInterface, uno_AcquireFunc acquire, uno_ReleaseFunc release)
Definition: assign.hxx:64
#define TYPE_ACQUIRE(pType)
Definition: prim.hxx:133
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