Gtk-WARNING gtktreestore.c:1047: Invalid column number 1 added to iter
[LibreOffice.git] / cppu / source / uno / sequence.cxx
blobe249f75497808e91a91dcb3657582de3f587d7ae
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 .
20 #include <sal/config.h>
22 #include <cassert>
23 #include <cstdlib>
24 #include <string.h>
26 #include <osl/diagnose.h>
27 #include <osl/interlck.h>
28 #include <typelib/typedescription.h>
29 #include <uno/data.h>
30 #include <uno/sequence2.h>
32 #include "constr.hxx"
33 #include "copy.hxx"
34 #include "destr.hxx"
37 using namespace cppu;
39 namespace cppu
43 static uno_Sequence * reallocSeq(
44 uno_Sequence * pReallocate, std::size_t nElementSize, sal_Int32 nElements )
46 OSL_ASSERT( nElements >= 0 );
47 uno_Sequence * pNew = nullptr;
48 sal_uInt32 nSize = calcSeqMemSize( nElementSize, nElements );
49 if (nSize > 0)
51 if (pReallocate == nullptr)
53 pNew = static_cast<uno_Sequence *>(std::malloc( nSize ));
55 else
57 pNew = static_cast<uno_Sequence *>(std::realloc( pReallocate, nSize ));
59 if (pNew != nullptr)
61 // header init
62 pNew->nRefCount = 1;
63 pNew->nElements = nElements;
66 return pNew;
70 static bool idefaultConstructElements(
71 uno_Sequence ** ppSeq,
72 typelib_TypeDescriptionReference * pElementType,
73 sal_Int32 nStartIndex, sal_Int32 nStopIndex,
74 sal_Int32 nAlloc ) // >= 0 means (re)alloc memory for nAlloc elements
76 uno_Sequence * pSeq = *ppSeq;
77 switch (pElementType->eTypeClass)
79 case typelib_TypeClass_CHAR:
80 if (nAlloc >= 0)
81 pSeq = reallocSeq( pSeq, sizeof(sal_Unicode), nAlloc );
82 if (pSeq != nullptr)
84 memset(
85 pSeq->elements + (sizeof(sal_Unicode) * nStartIndex),
87 sizeof(sal_Unicode) * (nStopIndex - nStartIndex) );
89 break;
90 case typelib_TypeClass_BOOLEAN:
91 if (nAlloc >= 0)
92 pSeq = reallocSeq( pSeq, sizeof(sal_Bool), nAlloc );
93 if (pSeq != nullptr)
95 memset(
96 pSeq->elements + (sizeof(sal_Bool) * nStartIndex),
98 sizeof(sal_Bool) * (nStopIndex - nStartIndex) );
100 break;
101 case typelib_TypeClass_BYTE:
102 if (nAlloc >= 0)
103 pSeq = reallocSeq( pSeq, sizeof(sal_Int8), nAlloc );
104 if (pSeq != nullptr)
106 memset(
107 pSeq->elements + (sizeof(sal_Int8) * nStartIndex),
109 sizeof(sal_Int8) * (nStopIndex - nStartIndex) );
111 break;
112 case typelib_TypeClass_SHORT:
113 case typelib_TypeClass_UNSIGNED_SHORT:
114 if (nAlloc >= 0)
115 pSeq = reallocSeq( pSeq, sizeof(sal_Int16), nAlloc );
116 if (pSeq != nullptr)
118 memset(
119 pSeq->elements + (sizeof(sal_Int16) * nStartIndex),
121 sizeof(sal_Int16) * (nStopIndex - nStartIndex) );
123 break;
124 case typelib_TypeClass_LONG:
125 case typelib_TypeClass_UNSIGNED_LONG:
126 if (nAlloc >= 0)
127 pSeq = reallocSeq( pSeq, sizeof(sal_Int32), nAlloc );
128 if (pSeq != nullptr)
130 memset(
131 pSeq->elements + (sizeof(sal_Int32) * nStartIndex),
133 sizeof(sal_Int32) * (nStopIndex - nStartIndex) );
135 break;
136 case typelib_TypeClass_HYPER:
137 case typelib_TypeClass_UNSIGNED_HYPER:
138 if (nAlloc >= 0)
139 pSeq = reallocSeq( pSeq, sizeof(sal_Int64), nAlloc );
140 if (pSeq != nullptr)
142 memset(
143 pSeq->elements + (sizeof(sal_Int64) * nStartIndex),
145 sizeof(sal_Int64) * (nStopIndex - nStartIndex) );
147 break;
148 case typelib_TypeClass_FLOAT:
150 if (nAlloc >= 0)
151 pSeq = reallocSeq( pSeq, sizeof(float), nAlloc );
152 if (pSeq != nullptr)
154 float * pElements = reinterpret_cast<float *>(pSeq->elements);
155 for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
157 pElements[nPos] = 0.0;
160 break;
162 case typelib_TypeClass_DOUBLE:
164 if (nAlloc >= 0)
165 pSeq = reallocSeq( pSeq, sizeof(double), nAlloc );
166 if (pSeq != nullptr)
168 double * pElements = reinterpret_cast<double *>(pSeq->elements);
169 for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
171 pElements[nPos] = 0.0;
174 break;
176 case typelib_TypeClass_STRING:
178 if (nAlloc >= 0)
179 pSeq = reallocSeq( pSeq, sizeof(rtl_uString *), nAlloc );
180 if (pSeq != nullptr)
182 rtl_uString ** pElements = reinterpret_cast<rtl_uString **>(pSeq->elements);
183 for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
185 pElements[nPos] = nullptr;
186 rtl_uString_new( &pElements[nPos] );
189 break;
191 case typelib_TypeClass_TYPE:
193 if (nAlloc >= 0)
195 pSeq = reallocSeq(
196 pSeq, sizeof(typelib_TypeDescriptionReference *), nAlloc );
198 if (pSeq != nullptr)
200 typelib_TypeDescriptionReference ** pElements =
201 reinterpret_cast<typelib_TypeDescriptionReference **>(pSeq->elements);
202 for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
204 pElements[nPos] = _getVoidType();
207 break;
209 case typelib_TypeClass_ANY:
211 if (nAlloc >= 0)
212 pSeq = reallocSeq( pSeq, sizeof(uno_Any), nAlloc );
213 if (pSeq != nullptr)
215 uno_Any * pElements = reinterpret_cast<uno_Any *>(pSeq->elements);
216 for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
218 CONSTRUCT_EMPTY_ANY( &pElements[nPos] );
221 break;
223 case typelib_TypeClass_ENUM:
225 if (nAlloc >= 0)
226 pSeq = reallocSeq( pSeq, sizeof(sal_Int32), nAlloc );
227 if (pSeq != nullptr)
229 typelib_TypeDescription * pElementTypeDescr = nullptr;
230 TYPELIB_DANGER_GET( &pElementTypeDescr, pElementType );
231 if (pElementTypeDescr == nullptr) {
232 std::abort();
234 sal_Int32 eEnum =
235 reinterpret_cast<typelib_EnumTypeDescription *>(
236 pElementTypeDescr)->nDefaultEnumValue;
237 TYPELIB_DANGER_RELEASE( pElementTypeDescr );
239 sal_Int32 * pElements = reinterpret_cast<sal_Int32 *>(pSeq->elements);
240 for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
242 pElements[nPos] = eEnum;
245 break;
247 case typelib_TypeClass_STRUCT:
248 case typelib_TypeClass_EXCEPTION:
250 typelib_TypeDescription * pElementTypeDescr = nullptr;
251 TYPELIB_DANGER_GET( &pElementTypeDescr, pElementType );
252 if (pElementTypeDescr == nullptr) {
253 std::abort();
255 sal_Int32 nElementSize = pElementTypeDescr->nSize;
257 if (nAlloc >= 0)
258 pSeq = reallocSeq( pSeq, nElementSize, nAlloc );
259 if (pSeq != nullptr)
261 char * pElements = pSeq->elements;
262 for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
264 _defaultConstructStruct(
265 pElements + (nElementSize * nPos),
266 reinterpret_cast<typelib_CompoundTypeDescription *>(pElementTypeDescr) );
270 TYPELIB_DANGER_RELEASE( pElementTypeDescr );
271 break;
273 case typelib_TypeClass_SEQUENCE:
275 if (nAlloc >= 0)
277 // coverity[suspicious_sizeof : FALSE] - sizeof(uno_Sequence*) is correct here
278 pSeq = reallocSeq(pSeq, sizeof(uno_Sequence*), nAlloc);
280 if (pSeq != nullptr)
282 uno_Sequence ** pElements =
283 reinterpret_cast<uno_Sequence **>(pSeq->elements);
284 for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
286 pElements[nPos] = createEmptySequence();
289 break;
291 case typelib_TypeClass_INTERFACE: // either C++ or C-UNO interface
292 if (nAlloc >= 0)
293 pSeq = reallocSeq( pSeq, sizeof(void *), nAlloc );
294 if (pSeq != nullptr)
296 memset(
297 pSeq->elements + (sizeof(void *) * nStartIndex),
299 sizeof(void *) * (nStopIndex - nStartIndex) );
301 break;
302 default:
303 OSL_FAIL( "### unexpected element type!" );
304 pSeq = nullptr;
305 break;
308 if (pSeq == nullptr)
310 OSL_ASSERT( nAlloc >= 0 ); // must have been an allocation failure
311 return false;
313 *ppSeq = pSeq;
314 return true;
317 // coverity[ -tainted_data_sink : arg-1 ]
318 static bool icopyConstructFromElements(
319 uno_Sequence ** ppSeq, void * pSourceElements,
320 typelib_TypeDescriptionReference * pElementType,
321 sal_Int32 nStopIndex,
322 uno_AcquireFunc acquire,
323 sal_Int32 nAlloc )
325 uno_Sequence * pSeq = *ppSeq;
326 switch (pElementType->eTypeClass)
328 case typelib_TypeClass_CHAR:
329 pSeq = reallocSeq( pSeq, sizeof(sal_Unicode), nAlloc );
330 if (pSeq != nullptr)
332 memcpy(
333 pSeq->elements,
334 pSourceElements,
335 sizeof(sal_Unicode) * nStopIndex );
337 break;
338 case typelib_TypeClass_BOOLEAN:
339 pSeq = reallocSeq( pSeq, sizeof(sal_Bool), nAlloc );
340 if (pSeq != nullptr)
342 memcpy(
343 pSeq->elements,
344 pSourceElements,
345 sizeof(sal_Bool) * nStopIndex );
347 break;
348 case typelib_TypeClass_BYTE:
349 pSeq = reallocSeq( pSeq, sizeof(sal_Int8), nAlloc );
350 if (pSeq != nullptr)
352 memcpy(
353 pSeq->elements,
354 pSourceElements,
355 sizeof(sal_Int8) * nStopIndex );
357 break;
358 case typelib_TypeClass_SHORT:
359 case typelib_TypeClass_UNSIGNED_SHORT:
360 pSeq = reallocSeq( pSeq, sizeof(sal_Int16), nAlloc );
361 if (pSeq != nullptr)
363 memcpy(
364 pSeq->elements,
365 pSourceElements,
366 sizeof(sal_Int16) * nStopIndex );
368 break;
369 case typelib_TypeClass_LONG:
370 case typelib_TypeClass_UNSIGNED_LONG:
371 pSeq = reallocSeq( pSeq, sizeof(sal_Int32), nAlloc );
372 if (pSeq != nullptr)
374 memcpy(
375 pSeq->elements,
376 pSourceElements,
377 sizeof(sal_Int32) * nStopIndex );
379 break;
380 case typelib_TypeClass_HYPER:
381 case typelib_TypeClass_UNSIGNED_HYPER:
382 pSeq = reallocSeq( pSeq, sizeof(sal_Int64), nAlloc );
383 if (pSeq != nullptr)
385 memcpy(
386 pSeq->elements,
387 pSourceElements,
388 sizeof(sal_Int64) * nStopIndex );
390 break;
391 case typelib_TypeClass_FLOAT:
392 pSeq = reallocSeq( pSeq, sizeof(float), nAlloc );
393 if (pSeq != nullptr)
395 memcpy(
396 pSeq->elements,
397 pSourceElements,
398 sizeof(float) * nStopIndex );
400 break;
401 case typelib_TypeClass_DOUBLE:
402 pSeq = reallocSeq( pSeq, sizeof(double), nAlloc );
403 if (pSeq != nullptr)
405 memcpy(
406 pSeq->elements,
407 pSourceElements,
408 sizeof(double) * nStopIndex );
410 break;
411 case typelib_TypeClass_ENUM:
412 pSeq = reallocSeq( pSeq, sizeof(sal_Int32), nAlloc );
413 if (pSeq != nullptr)
415 memcpy(
416 pSeq->elements,
417 pSourceElements,
418 sizeof(sal_Int32) * nStopIndex );
420 break;
421 case typelib_TypeClass_STRING:
423 pSeq = reallocSeq( pSeq, sizeof(rtl_uString *), nAlloc );
424 if (pSeq != nullptr)
426 rtl_uString ** pDestElements = reinterpret_cast<rtl_uString **>(pSeq->elements);
427 for ( sal_Int32 nPos = 0; nPos < nStopIndex; ++nPos )
429 // This code tends to trigger coverity's overrun-buffer-arg warning
430 // coverity[index_parm_via_loop_bound] - https://communities.coverity.com/thread/2993
431 ::rtl_uString_acquire(
432 static_cast<rtl_uString **>(pSourceElements)[nPos] );
433 pDestElements[nPos] = static_cast<rtl_uString **>(pSourceElements)[nPos];
436 break;
438 case typelib_TypeClass_TYPE:
440 pSeq = reallocSeq(
441 pSeq, sizeof(typelib_TypeDescriptionReference *), nAlloc );
442 if (pSeq != nullptr)
444 typelib_TypeDescriptionReference ** pDestElements =
445 reinterpret_cast<typelib_TypeDescriptionReference **>(pSeq->elements);
446 for ( sal_Int32 nPos = 0; nPos < nStopIndex; ++nPos )
448 TYPE_ACQUIRE(
449 static_cast<typelib_TypeDescriptionReference **>(
450 pSourceElements)[nPos] );
451 pDestElements[nPos] =
452 static_cast<typelib_TypeDescriptionReference **>(
453 pSourceElements)[nPos];
456 break;
458 case typelib_TypeClass_ANY:
460 pSeq = reallocSeq( pSeq, sizeof(uno_Any), nAlloc );
461 if (pSeq != nullptr)
463 uno_Any * pDestElements = reinterpret_cast<uno_Any *>(pSeq->elements);
464 for ( sal_Int32 nPos = 0; nPos < nStopIndex; ++nPos )
466 uno_Any * pSource = static_cast<uno_Any *>(pSourceElements) + nPos;
467 _copyConstructAny(
468 &pDestElements[nPos],
469 pSource->pData,
470 pSource->pType, nullptr,
471 acquire, nullptr );
474 break;
476 case typelib_TypeClass_STRUCT:
477 case typelib_TypeClass_EXCEPTION:
479 typelib_TypeDescription * pElementTypeDescr = nullptr;
480 TYPELIB_DANGER_GET( &pElementTypeDescr, pElementType );
481 if (pElementTypeDescr == nullptr) {
482 std::abort();
484 sal_Int32 nElementSize = pElementTypeDescr->nSize;
486 pSeq = reallocSeq( pSeq, nElementSize, nAlloc );
487 if (pSeq != nullptr)
489 char * pDestElements = pSeq->elements;
491 typelib_CompoundTypeDescription * pTypeDescr =
492 reinterpret_cast<typelib_CompoundTypeDescription *>(pElementTypeDescr);
493 for ( sal_Int32 nPos = 0; nPos < nStopIndex; ++nPos )
495 char * pDest =
496 pDestElements + (nElementSize * nPos);
497 char * pSource =
498 static_cast<char *>(pSourceElements) + (nElementSize * nPos);
500 if (pTypeDescr->pBaseTypeDescription)
502 // copy base value
503 _copyConstructStruct(
504 pDest, pSource,
505 pTypeDescr->pBaseTypeDescription, acquire, nullptr );
508 // then copy members
509 typelib_TypeDescriptionReference ** ppTypeRefs =
510 pTypeDescr->ppTypeRefs;
511 sal_Int32 * pMemberOffsets = pTypeDescr->pMemberOffsets;
512 sal_Int32 nDescr = pTypeDescr->nMembers;
514 while (nDescr--)
516 ::uno_type_copyData(
517 pDest + pMemberOffsets[nDescr],
518 pSource + pMemberOffsets[nDescr],
519 ppTypeRefs[nDescr], acquire );
524 TYPELIB_DANGER_RELEASE( pElementTypeDescr );
525 break;
527 case typelib_TypeClass_SEQUENCE: // sequence of sequence
529 // coverity[suspicious_sizeof : FALSE] - sizeof(uno_Sequence*) is correct here
530 pSeq = reallocSeq(pSeq, sizeof(uno_Sequence*), nAlloc);
531 if (pSeq != nullptr)
533 typelib_TypeDescription * pElementTypeDescr = nullptr;
534 TYPELIB_DANGER_GET( &pElementTypeDescr, pElementType );
535 if (pElementTypeDescr == nullptr) {
536 std::abort();
538 typelib_TypeDescriptionReference * pSeqElementType =
539 reinterpret_cast<typelib_IndirectTypeDescription *>(pElementTypeDescr)->pType;
540 uno_Sequence ** pDestElements = reinterpret_cast<uno_Sequence **>(pSeq->elements);
541 for ( sal_Int32 nPos = 0; nPos < nStopIndex; ++nPos )
543 uno_Sequence * pNew = icopyConstructSequence(
544 static_cast<uno_Sequence **>(pSourceElements)[nPos],
545 pSeqElementType, acquire, nullptr );
546 OSL_ASSERT( pNew != nullptr );
547 // ought never be a memory allocation problem,
548 // because of reference counted sequence handles
549 pDestElements[ nPos ] = pNew;
551 TYPELIB_DANGER_RELEASE( pElementTypeDescr );
553 break;
555 case typelib_TypeClass_INTERFACE:
557 pSeq = reallocSeq( pSeq, sizeof(void *), nAlloc );
558 if (pSeq != nullptr)
560 void ** pDestElements = reinterpret_cast<void **>(pSeq->elements);
561 for ( sal_Int32 nPos = 0; nPos < nStopIndex; ++nPos )
563 pDestElements[nPos] = static_cast<void **>(pSourceElements)[nPos];
564 _acquire( pDestElements[nPos], acquire );
567 break;
569 default:
570 OSL_FAIL( "### unexpected element type!" );
571 pSeq = nullptr;
572 break;
575 if (pSeq == nullptr)
577 return false; // allocation failure
579 *ppSeq = pSeq;
580 return true;
584 static bool ireallocSequence(
585 uno_Sequence ** ppSequence,
586 typelib_TypeDescriptionReference * pElementType,
587 sal_Int32 nSize,
588 uno_AcquireFunc acquire, uno_ReleaseFunc release )
590 bool ret = true;
591 uno_Sequence * pSeq = *ppSequence;
592 sal_Int32 nElements = pSeq->nElements;
594 if (pSeq->nRefCount > 1 ||
595 // not mem-copyable elements?
596 typelib_TypeClass_ANY == pElementType->eTypeClass ||
597 typelib_TypeClass_STRUCT == pElementType->eTypeClass ||
598 typelib_TypeClass_EXCEPTION == pElementType->eTypeClass)
600 // split sequence and construct new one from scratch
601 uno_Sequence * pNew = nullptr;
603 sal_Int32 nRest = nSize - nElements;
604 sal_Int32 nCopy = (nRest > 0 ? nElements : nSize);
606 if (nCopy >= 0)
608 ret = icopyConstructFromElements(
609 &pNew, pSeq->elements, pElementType,
610 nCopy, acquire,
611 nSize ); // alloc to nSize
613 if (ret && nRest > 0)
615 ret = idefaultConstructElements(
616 &pNew, pElementType,
617 nCopy, nSize,
618 nCopy >= 0 ? -1 /* no mem allocation */ : nSize );
621 if (ret)
623 // destruct sequence
624 if (osl_atomic_decrement( &pSeq->nRefCount ) == 0)
626 if (nElements > 0)
628 idestructElements(
629 pSeq->elements, pElementType,
630 0, nElements, release );
632 std::free( pSeq );
634 *ppSequence = pNew;
637 else
639 OSL_ASSERT( pSeq->nRefCount == 1 );
640 if (nSize > nElements) // default construct the rest
642 ret = idefaultConstructElements(
643 ppSequence, pElementType,
644 nElements, nSize,
645 nSize ); // realloc to nSize
647 else // or destruct the rest and realloc mem
649 sal_Int32 nElementSize = idestructElements(
650 pSeq->elements, pElementType,
651 nSize, nElements, release );
652 // warning: it is assumed that the following will never fail,
653 // else this leads to a sequence null handle
654 *ppSequence = reallocSeq( pSeq, nElementSize, nSize );
655 OSL_ASSERT( *ppSequence != nullptr );
656 ret = (*ppSequence != nullptr);
660 return ret;
665 extern "C"
668 sal_Bool SAL_CALL uno_type_sequence_construct(
669 uno_Sequence ** ppSequence, typelib_TypeDescriptionReference * pType,
670 void * pElements, sal_Int32 len,
671 uno_AcquireFunc acquire ) noexcept
673 assert( len >= 0 );
674 bool ret;
675 if (len)
677 typelib_TypeDescription * pTypeDescr = nullptr;
678 TYPELIB_DANGER_GET( &pTypeDescr, pType );
679 if (pTypeDescr == nullptr) {
680 std::abort();
683 typelib_TypeDescriptionReference * pElementType =
684 reinterpret_cast<typelib_IndirectTypeDescription *>(pTypeDescr)->pType;
686 *ppSequence = nullptr;
687 if (pElements == nullptr)
689 ret = idefaultConstructElements(
690 ppSequence, pElementType,
691 0, len,
692 len ); // alloc to len
694 else
696 ret = icopyConstructFromElements(
697 ppSequence, pElements, pElementType,
698 len, acquire,
699 len ); // alloc to len
702 TYPELIB_DANGER_RELEASE( pTypeDescr );
704 else
706 *ppSequence = createEmptySequence();
707 ret = true;
710 OSL_ASSERT( (*ppSequence != nullptr) == ret );
711 return ret;
715 sal_Bool SAL_CALL uno_sequence_construct(
716 uno_Sequence ** ppSequence, typelib_TypeDescription * pTypeDescr,
717 void * pElements, sal_Int32 len,
718 uno_AcquireFunc acquire ) noexcept
720 bool ret;
721 if (len > 0)
723 typelib_TypeDescriptionReference * pElementType =
724 reinterpret_cast<typelib_IndirectTypeDescription *>(pTypeDescr)->pType;
726 *ppSequence = nullptr;
727 if (pElements == nullptr)
729 ret = idefaultConstructElements(
730 ppSequence, pElementType,
731 0, len,
732 len ); // alloc to len
734 else
736 ret = icopyConstructFromElements(
737 ppSequence, pElements, pElementType,
738 len, acquire,
739 len ); // alloc to len
742 else
744 *ppSequence = createEmptySequence();
745 ret = true;
748 OSL_ASSERT( (*ppSequence != nullptr) == ret );
749 return ret;
753 sal_Bool SAL_CALL uno_type_sequence_realloc(
754 uno_Sequence ** ppSequence, typelib_TypeDescriptionReference * pType,
755 sal_Int32 nSize, uno_AcquireFunc acquire, uno_ReleaseFunc release ) noexcept
757 assert(ppSequence && "### null ptr!");
758 assert(nSize >= 0 && "### new size must be at least 0!");
760 bool ret = true;
761 if (nSize != (*ppSequence)->nElements)
763 typelib_TypeDescription * pTypeDescr = nullptr;
764 TYPELIB_DANGER_GET( &pTypeDescr, pType );
765 ret = ireallocSequence(
766 ppSequence, reinterpret_cast<typelib_IndirectTypeDescription *>(pTypeDescr)->pType,
767 nSize, acquire, release );
768 TYPELIB_DANGER_RELEASE( pTypeDescr );
770 return ret;
774 sal_Bool SAL_CALL uno_sequence_realloc(
775 uno_Sequence ** ppSequence, typelib_TypeDescription * pTypeDescr,
776 sal_Int32 nSize, uno_AcquireFunc acquire, uno_ReleaseFunc release ) noexcept
778 assert(ppSequence && "### null ptr!");
779 OSL_ENSURE( nSize >= 0, "### new size must be at least 0!" );
781 bool ret = true;
782 if (nSize != (*ppSequence)->nElements)
784 ret = ireallocSequence(
785 ppSequence, reinterpret_cast<typelib_IndirectTypeDescription *>(pTypeDescr)->pType,
786 nSize, acquire, release );
788 return ret;
792 sal_Bool SAL_CALL uno_type_sequence_reference2One(
793 uno_Sequence ** ppSequence,
794 typelib_TypeDescriptionReference * pType,
795 uno_AcquireFunc acquire, uno_ReleaseFunc release ) noexcept
797 assert(ppSequence && "### null ptr!");
798 bool ret = true;
799 uno_Sequence * pSequence = *ppSequence;
800 if (pSequence->nRefCount > 1)
802 uno_Sequence * pNew = nullptr;
803 if (pSequence->nElements > 0)
805 typelib_TypeDescription * pTypeDescr = nullptr;
806 TYPELIB_DANGER_GET( &pTypeDescr, pType );
808 ret = icopyConstructFromElements(
809 &pNew, pSequence->elements,
810 reinterpret_cast<typelib_IndirectTypeDescription *>(pTypeDescr)->pType,
811 pSequence->nElements, acquire,
812 pSequence->nElements ); // alloc nElements
813 if (ret)
815 idestructSequence( *ppSequence, pType, pTypeDescr, release );
816 *ppSequence = pNew;
819 TYPELIB_DANGER_RELEASE( pTypeDescr );
821 else
823 pNew = allocSeq( 0, 0 );
824 ret = (pNew != nullptr);
825 if (ret)
827 // easy destruction of empty sequence:
828 if (osl_atomic_decrement( &pSequence->nRefCount ) == 0)
829 std::free( pSequence );
830 *ppSequence = pNew;
834 return ret;
838 sal_Bool SAL_CALL uno_sequence_reference2One(
839 uno_Sequence ** ppSequence,
840 typelib_TypeDescription * pTypeDescr,
841 uno_AcquireFunc acquire, uno_ReleaseFunc release ) noexcept
843 assert(ppSequence && "### null ptr!");
844 bool ret = true;
845 uno_Sequence * pSequence = *ppSequence;
846 if (pSequence->nRefCount > 1)
848 uno_Sequence * pNew = nullptr;
849 if (pSequence->nElements > 0)
851 ret = icopyConstructFromElements(
852 &pNew, pSequence->elements,
853 reinterpret_cast<typelib_IndirectTypeDescription *>(pTypeDescr)->pType,
854 pSequence->nElements, acquire,
855 pSequence->nElements ); // alloc nElements
856 if (ret)
858 idestructSequence(
859 pSequence, pTypeDescr->pWeakRef, pTypeDescr, release );
860 *ppSequence = pNew;
863 else
865 pNew = allocSeq( 0, 0 );
866 ret = (pNew != nullptr);
867 if (ret)
869 // easy destruction of empty sequence:
870 if (osl_atomic_decrement( &pSequence->nRefCount ) == 0)
871 std::free( pSequence );
872 *ppSequence = pNew;
877 return ret;
881 void SAL_CALL uno_sequence_assign(
882 uno_Sequence ** ppDest,
883 uno_Sequence * pSource,
884 typelib_TypeDescription * pTypeDescr,
885 uno_ReleaseFunc release ) noexcept
887 if (*ppDest != pSource)
889 osl_atomic_increment( &pSource->nRefCount );
890 idestructSequence( *ppDest, pTypeDescr->pWeakRef, pTypeDescr, release );
891 *ppDest = pSource;
896 void SAL_CALL uno_type_sequence_assign(
897 uno_Sequence ** ppDest,
898 uno_Sequence * pSource,
899 typelib_TypeDescriptionReference * pType,
900 uno_ReleaseFunc release ) noexcept
902 if (*ppDest != pSource)
904 osl_atomic_increment( &pSource->nRefCount );
905 idestructSequence( *ppDest, pType, nullptr, release );
906 *ppDest = pSource;
910 void uno_type_sequence_destroy(
911 uno_Sequence * sequence, typelib_TypeDescriptionReference * type,
912 uno_ReleaseFunc release) noexcept
914 idestroySequence(sequence, type, nullptr, release);
919 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */