Files
libreoffice/cppu/source/uno/sequence.cxx

942 lines
29 KiB
C++
Raw Normal View History

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2012-06-13 14:17:57 +01:00
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
2000-09-18 14:29:57 +00:00
#include "sal/config.h"
#include <cassert>
#include <string.h>
2000-09-18 14:29:57 +00:00
#include <rtl/alloc.h>
#include <osl/diagnose.h>
#include <osl/interlck.h>
#include <typelib/typedescription.h>
#include <uno/data.h>
#include <uno/dispatcher.h>
#include <uno/sequence2.h>
#include "constr.hxx"
#include "copy.hxx"
#include "destr.hxx"
2000-09-18 14:29:57 +00:00
using namespace cppu;
namespace cppu
{
static inline uno_Sequence * reallocSeq(
uno_Sequence * pReallocate, sal_Size nElementSize, sal_Int32 nElements )
2000-09-18 14:29:57 +00:00
{
OSL_ASSERT( nElements >= 0 );
uno_Sequence * pNew = 0;
sal_uInt32 nSize = calcSeqMemSize( nElementSize, nElements );
if (nSize > 0)
{
if (pReallocate == 0)
{
pNew = static_cast<uno_Sequence *>(rtl_allocateMemory( nSize ));
}
else
{
pNew = static_cast<uno_Sequence *>(rtl_reallocateMemory( pReallocate, nSize ));
}
if (pNew != 0)
{
// header init
pNew->nRefCount = 1;
pNew->nElements = nElements;
}
2000-09-18 14:29:57 +00:00
}
return pNew;
2000-09-18 14:29:57 +00:00
}
static inline bool idefaultConstructElements(
uno_Sequence ** ppSeq,
2000-09-18 14:29:57 +00:00
typelib_TypeDescriptionReference * pElementType,
sal_Int32 nStartIndex, sal_Int32 nStopIndex,
2001-02-05 10:19:44 +00:00
sal_Int32 nAlloc = -1 ) // >= 0 means (re)alloc memory for nAlloc elements
2000-09-18 14:29:57 +00:00
{
uno_Sequence * pSeq = *ppSeq;
2000-09-18 14:29:57 +00:00
switch (pElementType->eTypeClass)
{
case typelib_TypeClass_CHAR:
if (nAlloc >= 0)
pSeq = reallocSeq( pSeq, sizeof(sal_Unicode), nAlloc );
if (pSeq != 0)
{
memset(
pSeq->elements + (sizeof(sal_Unicode) * nStartIndex),
0,
sizeof(sal_Unicode) * (nStopIndex - nStartIndex) );
}
2000-09-18 14:29:57 +00:00
break;
case typelib_TypeClass_BOOLEAN:
if (nAlloc >= 0)
pSeq = reallocSeq( pSeq, sizeof(sal_Bool), nAlloc );
if (pSeq != 0)
{
memset(
pSeq->elements + (sizeof(sal_Bool) * nStartIndex),
0,
sizeof(sal_Bool) * (nStopIndex - nStartIndex) );
}
2000-09-18 14:29:57 +00:00
break;
case typelib_TypeClass_BYTE:
if (nAlloc >= 0)
pSeq = reallocSeq( pSeq, sizeof(sal_Int8), nAlloc );
if (pSeq != 0)
{
memset(
pSeq->elements + (sizeof(sal_Int8) * nStartIndex),
0,
sizeof(sal_Int8) * (nStopIndex - nStartIndex) );
}
2000-09-18 14:29:57 +00:00
break;
case typelib_TypeClass_SHORT:
case typelib_TypeClass_UNSIGNED_SHORT:
if (nAlloc >= 0)
pSeq = reallocSeq( pSeq, sizeof(sal_Int16), nAlloc );
if (pSeq != 0)
{
memset(
pSeq->elements + (sizeof(sal_Int16) * nStartIndex),
0,
sizeof(sal_Int16) * (nStopIndex - nStartIndex) );
}
2000-09-18 14:29:57 +00:00
break;
case typelib_TypeClass_LONG:
case typelib_TypeClass_UNSIGNED_LONG:
if (nAlloc >= 0)
pSeq = reallocSeq( pSeq, sizeof(sal_Int32), nAlloc );
if (pSeq != 0)
{
memset(
pSeq->elements + (sizeof(sal_Int32) * nStartIndex),
0,
sizeof(sal_Int32) * (nStopIndex - nStartIndex) );
}
2000-09-18 14:29:57 +00:00
break;
case typelib_TypeClass_HYPER:
case typelib_TypeClass_UNSIGNED_HYPER:
if (nAlloc >= 0)
pSeq = reallocSeq( pSeq, sizeof(sal_Int64), nAlloc );
if (pSeq != 0)
{
memset(
pSeq->elements + (sizeof(sal_Int64) * nStartIndex),
0,
sizeof(sal_Int64) * (nStopIndex - nStartIndex) );
}
2000-09-18 14:29:57 +00:00
break;
case typelib_TypeClass_FLOAT:
{
if (nAlloc >= 0)
pSeq = reallocSeq( pSeq, sizeof(float), nAlloc );
if (pSeq != 0)
2000-09-18 14:29:57 +00:00
{
float * pElements = reinterpret_cast<float *>(pSeq->elements);
for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
{
pElements[nPos] = 0.0;
}
2000-09-18 14:29:57 +00:00
}
break;
}
case typelib_TypeClass_DOUBLE:
{
if (nAlloc >= 0)
pSeq = reallocSeq( pSeq, sizeof(double), nAlloc );
if (pSeq != 0)
2000-09-18 14:29:57 +00:00
{
double * pElements = reinterpret_cast<double *>(pSeq->elements);
for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
{
pElements[nPos] = 0.0;
}
2000-09-18 14:29:57 +00:00
}
break;
}
case typelib_TypeClass_STRING:
{
if (nAlloc >= 0)
pSeq = reallocSeq( pSeq, sizeof(rtl_uString *), nAlloc );
if (pSeq != 0)
2000-09-18 14:29:57 +00:00
{
rtl_uString ** pElements = reinterpret_cast<rtl_uString **>(pSeq->elements);
for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
{
pElements[nPos] = 0;
rtl_uString_new( &pElements[nPos] );
}
2000-09-18 14:29:57 +00:00
}
break;
}
case typelib_TypeClass_TYPE:
{
if (nAlloc >= 0)
2000-09-18 14:29:57 +00:00
{
pSeq = reallocSeq(
pSeq, sizeof(typelib_TypeDescriptionReference *), nAlloc );
}
if (pSeq != 0)
{
typelib_TypeDescriptionReference ** pElements =
reinterpret_cast<typelib_TypeDescriptionReference **>(pSeq->elements);
for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
{
pElements[nPos] = _getVoidType();
}
2000-09-18 14:29:57 +00:00
}
break;
}
case typelib_TypeClass_ANY:
{
if (nAlloc >= 0)
pSeq = reallocSeq( pSeq, sizeof(uno_Any), nAlloc );
if (pSeq != 0)
2000-09-18 14:29:57 +00:00
{
uno_Any * pElements = reinterpret_cast<uno_Any *>(pSeq->elements);
for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
{
CONSTRUCT_EMPTY_ANY( &pElements[nPos] );
}
2000-09-18 14:29:57 +00:00
}
break;
}
case typelib_TypeClass_ENUM:
{
if (nAlloc >= 0)
pSeq = reallocSeq( pSeq, sizeof(sal_Int32), nAlloc );
if (pSeq != 0)
2000-09-18 14:29:57 +00:00
{
typelib_TypeDescription * pElementTypeDescr = 0;
TYPELIB_DANGER_GET( &pElementTypeDescr, pElementType );
sal_Int32 eEnum =
reinterpret_cast<typelib_EnumTypeDescription *>(
pElementTypeDescr)->nDefaultEnumValue;
TYPELIB_DANGER_RELEASE( pElementTypeDescr );
sal_Int32 * pElements = reinterpret_cast<sal_Int32 *>(pSeq->elements);
for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
{
pElements[nPos] = eEnum;
}
2000-09-18 14:29:57 +00:00
}
break;
}
case typelib_TypeClass_STRUCT:
case typelib_TypeClass_EXCEPTION:
{
typelib_TypeDescription * pElementTypeDescr = 0;
TYPELIB_DANGER_GET( &pElementTypeDescr, pElementType );
sal_Int32 nElementSize = pElementTypeDescr->nSize;
if (nAlloc >= 0)
pSeq = reallocSeq( pSeq, nElementSize, nAlloc );
if (pSeq != 0)
2000-09-18 14:29:57 +00:00
{
char * pElements = pSeq->elements;
for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
{
_defaultConstructStruct(
pElements + (nElementSize * nPos),
reinterpret_cast<typelib_CompoundTypeDescription *>(pElementTypeDescr) );
}
2000-09-18 14:29:57 +00:00
}
2000-09-18 14:29:57 +00:00
TYPELIB_DANGER_RELEASE( pElementTypeDescr );
break;
}
case typelib_TypeClass_SEQUENCE:
{
if (nAlloc >= 0)
{
// coverity[suspicious_sizeof] - sizeof(uno_Sequence*) is correct here
pSeq = reallocSeq(pSeq, sizeof(uno_Sequence*), nAlloc);
}
if (pSeq != 0)
2000-09-18 14:29:57 +00:00
{
uno_Sequence ** pElements =
reinterpret_cast<uno_Sequence **>(pSeq->elements);
for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
{
pElements[nPos] = createEmptySequence();
}
2000-09-18 14:29:57 +00:00
}
break;
}
case typelib_TypeClass_INTERFACE: // either C++ or C-UNO interface
if (nAlloc >= 0)
pSeq = reallocSeq( pSeq, sizeof(void *), nAlloc );
if (pSeq != 0)
{
memset(
pSeq->elements + (sizeof(void *) * nStartIndex),
0,
sizeof(void *) * (nStopIndex - nStartIndex) );
}
break;
default:
OSL_FAIL( "### unexpected element type!" );
pSeq = 0;
2000-09-18 14:29:57 +00:00
break;
}
if (pSeq == 0)
{
OSL_ASSERT( nAlloc >= 0 ); // must have been an allocation failure
return false;
}
else
{
*ppSeq = pSeq;
return true;
}
2000-09-18 14:29:57 +00:00
}
static inline bool icopyConstructFromElements(
uno_Sequence ** ppSeq, void * pSourceElements,
2000-09-18 14:29:57 +00:00
typelib_TypeDescriptionReference * pElementType,
sal_Int32 nStartIndex, sal_Int32 nStopIndex,
uno_AcquireFunc acquire,
2001-02-05 10:19:44 +00:00
sal_Int32 nAlloc = -1 ) // >= 0 means (re)alloc memory for nAlloc elements
2000-09-18 14:29:57 +00:00
{
uno_Sequence * pSeq = *ppSeq;
2000-09-18 14:29:57 +00:00
switch (pElementType->eTypeClass)
{
case typelib_TypeClass_CHAR:
if (nAlloc >= 0)
pSeq = reallocSeq( pSeq, sizeof(sal_Unicode), nAlloc );
if (pSeq != 0)
{
memcpy(
pSeq->elements + (sizeof(sal_Unicode) * nStartIndex),
static_cast<char *>(pSourceElements) + (sizeof(sal_Unicode) * nStartIndex),
sizeof(sal_Unicode) * (nStopIndex - nStartIndex) );
}
2000-09-18 14:29:57 +00:00
break;
case typelib_TypeClass_BOOLEAN:
if (nAlloc >= 0)
pSeq = reallocSeq( pSeq, sizeof(sal_Bool), nAlloc );
if (pSeq != 0)
{
memcpy(
pSeq->elements + (sizeof(sal_Bool) * nStartIndex),
static_cast<char *>(pSourceElements) + (sizeof(sal_Bool) * nStartIndex),
sizeof(sal_Bool) * (nStopIndex - nStartIndex) );
}
2000-09-18 14:29:57 +00:00
break;
case typelib_TypeClass_BYTE:
if (nAlloc >= 0)
pSeq = reallocSeq( pSeq, sizeof(sal_Int8), nAlloc );
if (pSeq != 0)
{
memcpy(
pSeq->elements + (sizeof(sal_Int8) * nStartIndex),
static_cast<char *>(pSourceElements) + (sizeof(sal_Int8) * nStartIndex),
sizeof(sal_Int8) * (nStopIndex - nStartIndex) );
}
2000-09-18 14:29:57 +00:00
break;
case typelib_TypeClass_SHORT:
case typelib_TypeClass_UNSIGNED_SHORT:
if (nAlloc >= 0)
pSeq = reallocSeq( pSeq, sizeof(sal_Int16), nAlloc );
if (pSeq != 0)
{
memcpy(
pSeq->elements + (sizeof(sal_Int16) * nStartIndex),
static_cast<char *>(pSourceElements) + (sizeof(sal_Int16) * nStartIndex),
sizeof(sal_Int16) * (nStopIndex - nStartIndex) );
}
2000-09-18 14:29:57 +00:00
break;
case typelib_TypeClass_LONG:
case typelib_TypeClass_UNSIGNED_LONG:
if (nAlloc >= 0)
pSeq = reallocSeq( pSeq, sizeof(sal_Int32), nAlloc );
if (pSeq != 0)
{
memcpy(
pSeq->elements + (sizeof(sal_Int32) * nStartIndex),
static_cast<char *>(pSourceElements) + (sizeof(sal_Int32) * nStartIndex),
sizeof(sal_Int32) * (nStopIndex - nStartIndex) );
}
2000-09-18 14:29:57 +00:00
break;
case typelib_TypeClass_HYPER:
case typelib_TypeClass_UNSIGNED_HYPER:
if (nAlloc >= 0)
pSeq = reallocSeq( pSeq, sizeof(sal_Int64), nAlloc );
if (pSeq != 0)
{
memcpy(
pSeq->elements + (sizeof(sal_Int64) * nStartIndex),
static_cast<char *>(pSourceElements) + (sizeof(sal_Int64) * nStartIndex),
sizeof(sal_Int64) * (nStopIndex - nStartIndex) );
}
2000-09-18 14:29:57 +00:00
break;
case typelib_TypeClass_FLOAT:
if (nAlloc >= 0)
pSeq = reallocSeq( pSeq, sizeof(float), nAlloc );
if (pSeq != 0)
{
memcpy(
pSeq->elements + (sizeof(float) * nStartIndex),
static_cast<char *>(pSourceElements) + (sizeof(float) * nStartIndex),
sizeof(float) * (nStopIndex - nStartIndex) );
}
2000-09-18 14:29:57 +00:00
break;
case typelib_TypeClass_DOUBLE:
if (nAlloc >= 0)
pSeq = reallocSeq( pSeq, sizeof(double), nAlloc );
if (pSeq != 0)
{
memcpy(
pSeq->elements + (sizeof(double) * nStartIndex),
static_cast<char *>(pSourceElements) + (sizeof(double) * nStartIndex),
sizeof(double) * (nStopIndex - nStartIndex) );
}
2000-09-18 14:29:57 +00:00
break;
case typelib_TypeClass_ENUM:
if (nAlloc >= 0)
pSeq = reallocSeq( pSeq, sizeof(sal_Int32), nAlloc );
if (pSeq != 0)
{
memcpy(
pSeq->elements + (sizeof(sal_Int32) * nStartIndex),
static_cast<char *>(pSourceElements) + (sizeof(sal_Int32) * nStartIndex),
sizeof(sal_Int32) * (nStopIndex - nStartIndex) );
}
2000-09-18 14:29:57 +00:00
break;
case typelib_TypeClass_STRING:
{
if (nAlloc >= 0)
pSeq = reallocSeq( pSeq, sizeof(rtl_uString *), nAlloc );
if (pSeq != 0)
2000-09-18 14:29:57 +00:00
{
rtl_uString ** pDestElements = reinterpret_cast<rtl_uString **>(pSeq->elements);
for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
{
// This code tends to trigger coverity's overrun-buffer-arg warning
// coverity[index_parm_via_loop_bound] - https://communities.coverity.com/thread/2993
::rtl_uString_acquire(
static_cast<rtl_uString **>(pSourceElements)[nPos] );
pDestElements[nPos] = static_cast<rtl_uString **>(pSourceElements)[nPos];
}
2000-09-18 14:29:57 +00:00
}
break;
}
case typelib_TypeClass_TYPE:
{
if (nAlloc >= 0)
2000-09-18 14:29:57 +00:00
{
pSeq = reallocSeq(
pSeq, sizeof(typelib_TypeDescriptionReference *), nAlloc );
}
if (pSeq != 0)
{
typelib_TypeDescriptionReference ** pDestElements =
reinterpret_cast<typelib_TypeDescriptionReference **>(pSeq->elements);
for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
{
TYPE_ACQUIRE(
static_cast<typelib_TypeDescriptionReference **>(
pSourceElements)[nPos] );
pDestElements[nPos] =
static_cast<typelib_TypeDescriptionReference **>(
pSourceElements)[nPos];
}
2000-09-18 14:29:57 +00:00
}
break;
}
case typelib_TypeClass_ANY:
{
if (nAlloc >= 0)
pSeq = reallocSeq( pSeq, sizeof(uno_Any), nAlloc );
if (pSeq != 0)
2000-09-18 14:29:57 +00:00
{
uno_Any * pDestElements = reinterpret_cast<uno_Any *>(pSeq->elements);
for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
{
uno_Any * pSource = static_cast<uno_Any *>(pSourceElements) + nPos;
_copyConstructAny(
&pDestElements[nPos],
pSource->pData,
pSource->pType, 0,
acquire, 0 );
}
2000-09-18 14:29:57 +00:00
}
break;
}
case typelib_TypeClass_STRUCT:
case typelib_TypeClass_EXCEPTION:
{
typelib_TypeDescription * pElementTypeDescr = 0;
TYPELIB_DANGER_GET( &pElementTypeDescr, pElementType );
sal_Int32 nElementSize = pElementTypeDescr->nSize;
if (nAlloc >= 0)
pSeq = reallocSeq( pSeq, nElementSize, nAlloc );
if (pSeq != 0)
2000-09-18 14:29:57 +00:00
{
char * pDestElements = pSeq->elements;
2000-09-18 14:29:57 +00:00
typelib_CompoundTypeDescription * pTypeDescr =
reinterpret_cast<typelib_CompoundTypeDescription *>(pElementTypeDescr);
for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
2000-09-18 14:29:57 +00:00
{
char * pDest =
pDestElements + (nElementSize * nPos);
char * pSource =
static_cast<char *>(pSourceElements) + (nElementSize * nPos);
2000-09-18 14:29:57 +00:00
if (pTypeDescr->pBaseTypeDescription)
{
// copy base value
_copyConstructStruct(
pDest, pSource,
pTypeDescr->pBaseTypeDescription, acquire, 0 );
}
2000-09-18 14:29:57 +00:00
// then copy members
typelib_TypeDescriptionReference ** ppTypeRefs =
pTypeDescr->ppTypeRefs;
sal_Int32 * pMemberOffsets = pTypeDescr->pMemberOffsets;
sal_Int32 nDescr = pTypeDescr->nMembers;
while (nDescr--)
{
::uno_type_copyData(
pDest + pMemberOffsets[nDescr],
pSource + pMemberOffsets[nDescr],
ppTypeRefs[nDescr], acquire );
}
2000-09-18 14:29:57 +00:00
}
}
2000-09-18 14:29:57 +00:00
TYPELIB_DANGER_RELEASE( pElementTypeDescr );
break;
}
case typelib_TypeClass_SEQUENCE: // sequence of sequence
2000-09-18 14:29:57 +00:00
{
if (nAlloc >= 0)
{
// coverity[suspicious_sizeof] - sizeof(uno_Sequence*) is correct here
pSeq = reallocSeq(pSeq, sizeof(uno_Sequence*), nAlloc);
}
if (pSeq != 0)
2000-09-18 14:29:57 +00:00
{
typelib_TypeDescription * pElementTypeDescr = 0;
TYPELIB_DANGER_GET( &pElementTypeDescr, pElementType );
typelib_TypeDescriptionReference * pSeqElementType =
reinterpret_cast<typelib_IndirectTypeDescription *>(pElementTypeDescr)->pType;
uno_Sequence ** pDestElements = reinterpret_cast<uno_Sequence **>(pSeq->elements);
for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
{
uno_Sequence * pNew = icopyConstructSequence(
static_cast<uno_Sequence **>(pSourceElements)[nPos],
pSeqElementType, acquire, 0 );
OSL_ASSERT( pNew != 0 );
// ought never be a memory allocation problem,
// because of reference counted sequence handles
pDestElements[ nPos ] = pNew;
}
TYPELIB_DANGER_RELEASE( pElementTypeDescr );
2000-09-18 14:29:57 +00:00
}
break;
}
case typelib_TypeClass_INTERFACE:
{
if (nAlloc >= 0)
pSeq = reallocSeq( pSeq, sizeof(void *), nAlloc );
if (pSeq != 0)
2000-09-18 14:29:57 +00:00
{
void ** pDestElements = reinterpret_cast<void **>(pSeq->elements);
for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
{
_acquire( pDestElements[nPos] =
static_cast<void **>(pSourceElements)[nPos], acquire );
}
2000-09-18 14:29:57 +00:00
}
break;
}
default:
OSL_FAIL( "### unexpected element type!" );
pSeq = 0;
break;
}
if (pSeq == 0)
{
OSL_ASSERT( nAlloc >= 0 ); // must have been an allocation failure
return false;
}
else
{
*ppSeq = pSeq;
return true;
2000-09-18 14:29:57 +00:00
}
}
static inline bool ireallocSequence(
2000-09-18 14:29:57 +00:00
uno_Sequence ** ppSequence,
typelib_TypeDescriptionReference * pElementType,
sal_Int32 nSize,
uno_AcquireFunc acquire, uno_ReleaseFunc release )
{
bool ret = true;
uno_Sequence * pSeq = *ppSequence;
sal_Int32 nElements = pSeq->nElements;
2000-09-18 14:29:57 +00:00
if (pSeq->nRefCount > 1 ||
2001-07-06 10:30:09 +00:00
// not mem-copyable elements?
typelib_TypeClass_ANY == pElementType->eTypeClass ||
typelib_TypeClass_STRUCT == pElementType->eTypeClass ||
typelib_TypeClass_EXCEPTION == pElementType->eTypeClass)
2000-09-18 14:29:57 +00:00
{
2001-07-06 10:30:09 +00:00
// split sequence and construct new one from scratch
2000-09-18 14:29:57 +00:00
uno_Sequence * pNew = 0;
sal_Int32 nRest = nSize - nElements;
sal_Int32 nCopy = (nRest > 0 ? nElements : nSize);
2000-09-18 14:29:57 +00:00
2001-02-05 10:19:44 +00:00
if (nCopy >= 0)
2000-09-18 14:29:57 +00:00
{
ret = icopyConstructFromElements(
&pNew, pSeq->elements, pElementType,
2000-09-18 14:29:57 +00:00
0, nCopy, acquire,
nSize ); // alloc to nSize
}
if (ret && nRest > 0)
2000-09-18 14:29:57 +00:00
{
ret = idefaultConstructElements(
2000-09-18 14:29:57 +00:00
&pNew, pElementType,
nCopy, nSize,
nCopy >= 0 ? -1 /* no mem allocation */ : nSize );
2000-09-18 14:29:57 +00:00
}
if (ret)
2000-09-18 14:29:57 +00:00
{
// destruct sequence
if (osl_atomic_decrement( &pSeq->nRefCount ) == 0)
2000-09-18 14:29:57 +00:00
{
if (nElements > 0)
{
idestructElements(
pSeq->elements, pElementType,
0, nElements, release );
}
rtl_freeMemory( pSeq );
2000-09-18 14:29:57 +00:00
}
*ppSequence = pNew;
2000-09-18 14:29:57 +00:00
}
}
else
{
OSL_ASSERT( pSeq->nRefCount == 1 );
if (nSize > nElements) // default construct the rest
2000-09-18 14:29:57 +00:00
{
ret = idefaultConstructElements(
2000-09-18 14:29:57 +00:00
ppSequence, pElementType,
nElements, nSize,
2000-09-18 14:29:57 +00:00
nSize ); // realloc to nSize
}
else // or destruct the rest and realloc mem
{
sal_Int32 nElementSize = idestructElements(
pSeq->elements, pElementType,
nSize, nElements, release );
// warning: it is assumed that the following will never fail,
// else this leads to a sequence null handle
*ppSequence = reallocSeq( pSeq, nElementSize, nSize );
OSL_ASSERT( *ppSequence != 0 );
ret = (*ppSequence != 0);
2000-09-18 14:29:57 +00:00
}
}
return ret;
2000-09-18 14:29:57 +00:00
}
}
extern "C"
{
2011-12-19 15:10:41 +00:00
sal_Bool SAL_CALL uno_type_sequence_construct(
2000-09-18 14:29:57 +00:00
uno_Sequence ** ppSequence, typelib_TypeDescriptionReference * pType,
void * pElements, sal_Int32 len,
uno_AcquireFunc acquire )
2001-03-09 11:10:57 +00:00
SAL_THROW_EXTERN_C()
2000-09-18 14:29:57 +00:00
{
assert( len >= 0 );
bool ret;
2000-09-18 14:29:57 +00:00
if (len)
{
typelib_TypeDescription * pTypeDescr = 0;
TYPELIB_DANGER_GET( &pTypeDescr, pType );
typelib_TypeDescriptionReference * pElementType =
reinterpret_cast<typelib_IndirectTypeDescription *>(pTypeDescr)->pType;
2000-09-18 14:29:57 +00:00
*ppSequence = 0;
if (pElements == 0)
2000-09-18 14:29:57 +00:00
{
ret = idefaultConstructElements(
ppSequence, pElementType,
0, len,
2000-09-18 14:29:57 +00:00
len ); // alloc to len
}
else
{
ret = icopyConstructFromElements(
ppSequence, pElements, pElementType,
0, len, acquire,
2000-09-18 14:29:57 +00:00
len ); // alloc to len
}
TYPELIB_DANGER_RELEASE( pTypeDescr );
}
else
{
*ppSequence = createEmptySequence();
ret = true;
2000-09-18 14:29:57 +00:00
}
OSL_ASSERT( (*ppSequence != 0) == ret );
return ret;
2000-09-18 14:29:57 +00:00
}
2011-12-19 15:10:41 +00:00
sal_Bool SAL_CALL uno_sequence_construct(
2000-09-18 14:29:57 +00:00
uno_Sequence ** ppSequence, typelib_TypeDescription * pTypeDescr,
void * pElements, sal_Int32 len,
uno_AcquireFunc acquire )
2001-03-09 11:10:57 +00:00
SAL_THROW_EXTERN_C()
2000-09-18 14:29:57 +00:00
{
bool ret;
if (len > 0)
2000-09-18 14:29:57 +00:00
{
typelib_TypeDescriptionReference * pElementType =
reinterpret_cast<typelib_IndirectTypeDescription *>(pTypeDescr)->pType;
2000-09-18 14:29:57 +00:00
*ppSequence = 0;
if (pElements == 0)
2000-09-18 14:29:57 +00:00
{
ret = idefaultConstructElements(
ppSequence, pElementType,
0, len,
2000-09-18 14:29:57 +00:00
len ); // alloc to len
}
else
{
ret = icopyConstructFromElements(
ppSequence, pElements, pElementType,
0, len, acquire,
2000-09-18 14:29:57 +00:00
len ); // alloc to len
}
}
else
{
*ppSequence = createEmptySequence();
ret = true;
2000-09-18 14:29:57 +00:00
}
OSL_ASSERT( (*ppSequence != 0) == ret );
return ret;
2000-09-18 14:29:57 +00:00
}
2011-12-19 15:10:41 +00:00
sal_Bool SAL_CALL uno_type_sequence_realloc(
uno_Sequence ** ppSequence, typelib_TypeDescriptionReference * pType,
sal_Int32 nSize, uno_AcquireFunc acquire, uno_ReleaseFunc release )
2001-03-09 11:10:57 +00:00
SAL_THROW_EXTERN_C()
2000-09-18 14:29:57 +00:00
{
2001-03-12 12:28:14 +00:00
OSL_ENSURE( ppSequence, "### null ptr!" );
OSL_ENSURE( nSize >= 0, "### new size must be at least 0!" );
2000-09-18 14:29:57 +00:00
bool ret = true;
2000-09-18 14:29:57 +00:00
if (nSize != (*ppSequence)->nElements)
{
typelib_TypeDescription * pTypeDescr = 0;
TYPELIB_DANGER_GET( &pTypeDescr, pType );
ret = ireallocSequence(
ppSequence, reinterpret_cast<typelib_IndirectTypeDescription *>(pTypeDescr)->pType,
2000-09-18 14:29:57 +00:00
nSize, acquire, release );
TYPELIB_DANGER_RELEASE( pTypeDescr );
}
return ret;
2000-09-18 14:29:57 +00:00
}
2011-12-19 15:10:41 +00:00
sal_Bool SAL_CALL uno_sequence_realloc(
uno_Sequence ** ppSequence, typelib_TypeDescription * pTypeDescr,
sal_Int32 nSize, uno_AcquireFunc acquire, uno_ReleaseFunc release )
2001-03-09 11:10:57 +00:00
SAL_THROW_EXTERN_C()
2000-09-18 14:29:57 +00:00
{
2001-03-12 12:28:14 +00:00
OSL_ENSURE( ppSequence, "### null ptr!" );
OSL_ENSURE( nSize >= 0, "### new size must be at least 0!" );
2000-09-18 14:29:57 +00:00
bool ret = true;
2000-09-18 14:29:57 +00:00
if (nSize != (*ppSequence)->nElements)
{
ret = ireallocSequence(
ppSequence, reinterpret_cast<typelib_IndirectTypeDescription *>(pTypeDescr)->pType,
2000-09-18 14:29:57 +00:00
nSize, acquire, release );
}
return ret;
2000-09-18 14:29:57 +00:00
}
2011-12-19 15:10:41 +00:00
sal_Bool SAL_CALL uno_type_sequence_reference2One(
2000-09-18 14:29:57 +00:00
uno_Sequence ** ppSequence,
typelib_TypeDescriptionReference * pType,
uno_AcquireFunc acquire, uno_ReleaseFunc release )
2001-03-09 11:10:57 +00:00
SAL_THROW_EXTERN_C()
2000-09-18 14:29:57 +00:00
{
2001-03-12 12:28:14 +00:00
OSL_ENSURE( ppSequence, "### null ptr!" );
bool ret = true;
2000-09-18 14:29:57 +00:00
uno_Sequence * pSequence = *ppSequence;
if (pSequence->nRefCount > 1)
{
uno_Sequence * pNew = 0;
if (pSequence->nElements > 0)
2000-09-18 14:29:57 +00:00
{
typelib_TypeDescription * pTypeDescr = 0;
TYPELIB_DANGER_GET( &pTypeDescr, pType );
ret = icopyConstructFromElements(
2000-09-18 14:29:57 +00:00
&pNew, pSequence->elements,
reinterpret_cast<typelib_IndirectTypeDescription *>(pTypeDescr)->pType,
2000-09-18 14:29:57 +00:00
0, pSequence->nElements, acquire,
pSequence->nElements ); // alloc nElements
if (ret)
{
idestructSequence( *ppSequence, pType, pTypeDescr, release );
*ppSequence = pNew;
}
2000-09-18 14:29:57 +00:00
TYPELIB_DANGER_RELEASE( pTypeDescr );
}
else
{
pNew = allocSeq( 0, 0 );
ret = (pNew != 0);
if (ret)
{
// easy destruction of empty sequence:
if (osl_atomic_decrement( &pSequence->nRefCount ) == 0)
rtl_freeMemory( pSequence );
*ppSequence = pNew;
}
2000-09-18 14:29:57 +00:00
}
}
return ret;
2000-09-18 14:29:57 +00:00
}
2011-12-19 15:10:41 +00:00
sal_Bool SAL_CALL uno_sequence_reference2One(
2000-09-18 14:29:57 +00:00
uno_Sequence ** ppSequence,
typelib_TypeDescription * pTypeDescr,
uno_AcquireFunc acquire, uno_ReleaseFunc release )
2001-03-09 11:10:57 +00:00
SAL_THROW_EXTERN_C()
2000-09-18 14:29:57 +00:00
{
2001-03-12 12:28:14 +00:00
OSL_ENSURE( ppSequence, "### null ptr!" );
bool ret = true;
2000-09-18 14:29:57 +00:00
uno_Sequence * pSequence = *ppSequence;
if (pSequence->nRefCount > 1)
{
uno_Sequence * pNew = 0;
if (pSequence->nElements > 0)
2000-09-18 14:29:57 +00:00
{
ret = icopyConstructFromElements(
2000-09-18 14:29:57 +00:00
&pNew, pSequence->elements,
reinterpret_cast<typelib_IndirectTypeDescription *>(pTypeDescr)->pType,
2000-09-18 14:29:57 +00:00
0, pSequence->nElements, acquire,
pSequence->nElements ); // alloc nElements
if (ret)
{
idestructSequence(
pSequence, pTypeDescr->pWeakRef, pTypeDescr, release );
*ppSequence = pNew;
}
2000-09-18 14:29:57 +00:00
}
else
{
pNew = allocSeq( 0, 0 );
ret = (pNew != 0);
if (ret)
{
// easy destruction of empty sequence:
if (osl_atomic_decrement( &pSequence->nRefCount ) == 0)
rtl_freeMemory( pSequence );
*ppSequence = pNew;
}
2000-09-18 14:29:57 +00:00
}
2000-09-18 14:29:57 +00:00
}
return ret;
2000-09-18 14:29:57 +00:00
}
2011-12-19 15:10:41 +00:00
void SAL_CALL uno_sequence_assign(
2000-09-18 14:29:57 +00:00
uno_Sequence ** ppDest,
uno_Sequence * pSource,
typelib_TypeDescription * pTypeDescr,
uno_ReleaseFunc release )
2001-03-09 11:10:57 +00:00
SAL_THROW_EXTERN_C()
2000-09-18 14:29:57 +00:00
{
if (*ppDest != pSource)
{
osl_atomic_increment( &pSource->nRefCount );
idestructSequence( *ppDest, pTypeDescr->pWeakRef, pTypeDescr, release );
2000-09-18 14:29:57 +00:00
*ppDest = pSource;
}
}
2011-12-19 15:10:41 +00:00
void SAL_CALL uno_type_sequence_assign(
2000-09-18 14:29:57 +00:00
uno_Sequence ** ppDest,
uno_Sequence * pSource,
typelib_TypeDescriptionReference * pType,
uno_ReleaseFunc release )
2001-03-09 11:10:57 +00:00
SAL_THROW_EXTERN_C()
2000-09-18 14:29:57 +00:00
{
if (*ppDest != pSource)
{
osl_atomic_increment( &pSource->nRefCount );
idestructSequence( *ppDest, pType, 0, release );
2000-09-18 14:29:57 +00:00
*ppDest = pSource;
}
}
void uno_type_sequence_destroy(
uno_Sequence * sequence, typelib_TypeDescriptionReference * type,
uno_ReleaseFunc release)
SAL_THROW_EXTERN_C()
{
idestroySequence(sequence, type, nullptr, release);
}
2000-09-18 14:29:57 +00:00
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */