tdf#130857 qt weld: Implement QtInstanceWidget::get_text_height
[LibreOffice.git] / cppu / source / uno / assign.hxx
blob3e2893fde8247f47f6981c156cfae06786220c19
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 #pragma once
21 #include "prim.hxx"
22 #include "destr.hxx"
23 #include "constr.hxx"
24 #include "copy.hxx"
27 namespace cppu
31 //#### assignment ##################################################################################
34 inline void _assignInterface(
35 void ** ppDest, void * pSource,
36 uno_AcquireFunc acquire, uno_ReleaseFunc release )
39 _acquire( pSource, acquire );
40 void * const pToBeReleased = *ppDest;
41 *ppDest = pSource;
42 _release( pToBeReleased, release );
45 inline void * _queryInterface(
46 void * pSource,
47 typelib_TypeDescriptionReference * pDestType,
48 uno_QueryInterfaceFunc queryInterface )
50 if (pSource)
52 if (nullptr == queryInterface)
53 queryInterface = binuno_queryInterface;
54 pSource = (*queryInterface)( pSource, pDestType );
56 return pSource;
59 bool assignStruct(
60 void * pDest, void * pSource,
61 typelib_CompoundTypeDescription * pTypeDescr,
62 uno_QueryInterfaceFunc queryInterface, uno_AcquireFunc acquire, uno_ReleaseFunc release );
64 inline bool _assignStruct(
65 void * pDest, void * pSource,
66 typelib_CompoundTypeDescription * pTypeDescr,
67 uno_QueryInterfaceFunc queryInterface, uno_AcquireFunc acquire, uno_ReleaseFunc release )
69 if (pTypeDescr->pBaseTypeDescription)
71 // copy base value
72 if (! assignStruct( pDest, pSource, pTypeDescr->pBaseTypeDescription,
73 queryInterface, acquire, release ))
75 return false;
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--)
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 ))
90 return false;
93 return true;
96 inline 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 )
103 if (pDest == pSource)
104 return _type_equals( pDestType, pSourceType );
106 if (! pSource)
108 _destructData( pDest, pDestType, pDestTypeDescr, release );
109 _defaultConstructData( pDest, pDestType, pDestTypeDescr );
110 return true;
112 while (typelib_TypeClass_ANY == pSourceType->eTypeClass)
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;
121 switch (pDestType->eTypeClass)
123 case typelib_TypeClass_VOID:
124 return pSourceType->eTypeClass == typelib_TypeClass_VOID;
125 case typelib_TypeClass_CHAR:
126 switch (pSourceType->eTypeClass)
128 case typelib_TypeClass_CHAR:
129 *static_cast<sal_Unicode *>(pDest) = *static_cast<sal_Unicode *>(pSource);
130 return true;
131 default:
132 return false;
134 case typelib_TypeClass_BOOLEAN:
135 switch (pSourceType->eTypeClass)
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;
143 case typelib_TypeClass_BYTE:
144 switch (pSourceType->eTypeClass)
146 case typelib_TypeClass_BYTE:
147 *static_cast<sal_Int8 *>(pDest) = *static_cast<sal_Int8 *>(pSource);
148 return true;
149 default:
150 return false;
152 case typelib_TypeClass_SHORT:
153 switch (pSourceType->eTypeClass)
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;
165 case typelib_TypeClass_UNSIGNED_SHORT:
166 switch (pSourceType->eTypeClass)
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;
178 case typelib_TypeClass_LONG:
179 switch (pSourceType->eTypeClass)
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;
197 case typelib_TypeClass_UNSIGNED_LONG:
198 switch (pSourceType->eTypeClass)
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;
216 case typelib_TypeClass_HYPER:
217 switch (pSourceType->eTypeClass)
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;
241 case typelib_TypeClass_UNSIGNED_HYPER:
242 switch (pSourceType->eTypeClass)
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;
266 case typelib_TypeClass_FLOAT:
267 switch (pSourceType->eTypeClass)
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;
284 case typelib_TypeClass_DOUBLE:
285 switch (pSourceType->eTypeClass)
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;
311 case typelib_TypeClass_STRING:
312 switch (pSourceType->eTypeClass)
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;
320 case typelib_TypeClass_TYPE:
321 switch (pSourceType->eTypeClass)
323 case typelib_TypeClass_TYPE:
325 typelib_TypeDescriptionReference ** pp = static_cast<typelib_TypeDescriptionReference **>(pDest);
326 ::typelib_typedescriptionreference_release( *pp );
327 *pp = *static_cast<typelib_TypeDescriptionReference **>(pSource);
328 TYPE_ACQUIRE( *pp );
329 return true;
331 default:
332 return false;
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 ))
341 *static_cast<sal_Int32 *>(pDest) = *static_cast<sal_Int32 *>(pSource);
342 return true;
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)
350 bool bRet = false;
351 if (pSourceTypeDescr)
353 typelib_CompoundTypeDescription * pTypeDescr =
354 reinterpret_cast<typelib_CompoundTypeDescription *>(pSourceTypeDescr);
355 while (pTypeDescr &&
356 !_type_equals(pTypeDescr->aBase.pWeakRef, pDestType))
358 pTypeDescr = pTypeDescr->pBaseTypeDescription;
360 if (pTypeDescr)
362 bRet = _assignStruct(
363 pDest, pSource, pTypeDescr, queryInterface, acquire, release );
366 else
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))
374 pTypeDescr = pTypeDescr->pBaseTypeDescription;
376 if (pTypeDescr)
378 bRet = _assignStruct(
379 pDest, pSource, pTypeDescr, queryInterface, acquire, release );
381 TYPELIB_DANGER_RELEASE( pSourceTypeDescr );
383 return bRet;
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))
392 osl_atomic_increment( &(*static_cast<uno_Sequence **>(pSource))->nRefCount );
393 idestructSequence(
394 *static_cast<uno_Sequence **>(pDest), pDestType, pDestTypeDescr, release );
395 *static_cast<uno_Sequence **>(pDest) = *static_cast<uno_Sequence **>(pSource);
397 return true;
398 case typelib_TypeClass_INTERFACE:
399 if (typelib_TypeClass_INTERFACE != pSourceType->eTypeClass)
400 return false;
401 if (_type_equals( pDestType, pSourceType ))
403 _assignInterface( static_cast<void **>(pDest), *static_cast<void **>(pSource), acquire, release );
404 return true;
406 else if (*static_cast< void ** >(pSource) == nullptr)
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;
415 else
417 if (pSourceTypeDescr)
419 typelib_TypeDescription * pTD = pSourceTypeDescr;
420 while (pTD && !_type_equals( pTD->pWeakRef, pDestType ))
422 pTD = &reinterpret_cast<typelib_InterfaceTypeDescription *>(pTD)->pBaseTypeDescription->aBase;
424 if (pTD) // is base of dest
426 _assignInterface( static_cast<void **>(pDest), *static_cast<void **>(pSource), acquire, release );
427 return true;
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 );
439 return (pQueried != nullptr);
441 default:
442 OSL_ASSERT(false);
443 return false;
449 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */