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