1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir #ifndef _COM_SUN_STAR_UNO_SEQUENCE_H_ 28*cdf0e10cSrcweir #define _COM_SUN_STAR_UNO_SEQUENCE_H_ 29*cdf0e10cSrcweir 30*cdf0e10cSrcweir #include "typelib/typedescription.h" 31*cdf0e10cSrcweir #include "uno/sequence2.h" 32*cdf0e10cSrcweir #include "com/sun/star/uno/Type.h" 33*cdf0e10cSrcweir #include "rtl/alloc.h" 34*cdf0e10cSrcweir 35*cdf0e10cSrcweir #if ! defined EXCEPTIONS_OFF 36*cdf0e10cSrcweir #include <new> 37*cdf0e10cSrcweir #endif 38*cdf0e10cSrcweir 39*cdf0e10cSrcweir 40*cdf0e10cSrcweir namespace rtl 41*cdf0e10cSrcweir { 42*cdf0e10cSrcweir class ByteSequence; 43*cdf0e10cSrcweir } 44*cdf0e10cSrcweir 45*cdf0e10cSrcweir namespace com 46*cdf0e10cSrcweir { 47*cdf0e10cSrcweir namespace sun 48*cdf0e10cSrcweir { 49*cdf0e10cSrcweir namespace star 50*cdf0e10cSrcweir { 51*cdf0e10cSrcweir namespace uno 52*cdf0e10cSrcweir { 53*cdf0e10cSrcweir 54*cdf0e10cSrcweir /** Template C++ class representing an IDL sequence. Template argument is the 55*cdf0e10cSrcweir sequence element type. C++ Sequences are reference counted and shared, 56*cdf0e10cSrcweir so the sequence keeps a handle to its data. To keep value semantics, 57*cdf0e10cSrcweir copies are only generated if the sequence is to be modified (new handle). 58*cdf0e10cSrcweir 59*cdf0e10cSrcweir @tplparam E element type of sequence 60*cdf0e10cSrcweir */ 61*cdf0e10cSrcweir template< class E > 62*cdf0e10cSrcweir class Sequence 63*cdf0e10cSrcweir { 64*cdf0e10cSrcweir /** sequence handle 65*cdf0e10cSrcweir @internal 66*cdf0e10cSrcweir */ 67*cdf0e10cSrcweir uno_Sequence * _pSequence; 68*cdf0e10cSrcweir 69*cdf0e10cSrcweir public: 70*cdf0e10cSrcweir // these are here to force memory de/allocation to sal lib. 71*cdf0e10cSrcweir /** @internal */ 72*cdf0e10cSrcweir inline static void * SAL_CALL operator new ( size_t nSize ) 73*cdf0e10cSrcweir SAL_THROW( () ) 74*cdf0e10cSrcweir { return ::rtl_allocateMemory( nSize ); } 75*cdf0e10cSrcweir /** @internal */ 76*cdf0e10cSrcweir inline static void SAL_CALL operator delete ( void * pMem ) 77*cdf0e10cSrcweir SAL_THROW( () ) 78*cdf0e10cSrcweir { ::rtl_freeMemory( pMem ); } 79*cdf0e10cSrcweir /** @internal */ 80*cdf0e10cSrcweir inline static void * SAL_CALL operator new ( size_t, void * pMem ) 81*cdf0e10cSrcweir SAL_THROW( () ) 82*cdf0e10cSrcweir { return pMem; } 83*cdf0e10cSrcweir /** @internal */ 84*cdf0e10cSrcweir inline static void SAL_CALL operator delete ( void *, void * ) 85*cdf0e10cSrcweir SAL_THROW( () ) 86*cdf0e10cSrcweir {} 87*cdf0e10cSrcweir 88*cdf0e10cSrcweir /** Static pointer to typelib type of sequence. 89*cdf0e10cSrcweir Don't use directly, call getCppuType(). 90*cdf0e10cSrcweir @internal 91*cdf0e10cSrcweir */ 92*cdf0e10cSrcweir static typelib_TypeDescriptionReference * s_pType; 93*cdf0e10cSrcweir 94*cdf0e10cSrcweir /** typedefs the element type of the sequence 95*cdf0e10cSrcweir */ 96*cdf0e10cSrcweir typedef E ElementType; 97*cdf0e10cSrcweir 98*cdf0e10cSrcweir /** Default constructor: Creates an empty sequence. 99*cdf0e10cSrcweir */ 100*cdf0e10cSrcweir inline Sequence() SAL_THROW( () ); 101*cdf0e10cSrcweir 102*cdf0e10cSrcweir /** Copy constructor: Creates a copy of given sequence. 103*cdf0e10cSrcweir 104*cdf0e10cSrcweir @param rSeq another sequence of same type 105*cdf0e10cSrcweir */ 106*cdf0e10cSrcweir inline Sequence( const Sequence< E > & rSeq ) SAL_THROW( () ); 107*cdf0e10cSrcweir 108*cdf0e10cSrcweir /** Constructor: Takes over ownership of given sequence. 109*cdf0e10cSrcweir 110*cdf0e10cSrcweir @param pSequence a sequence 111*cdf0e10cSrcweir @param dummy SAL_NO_ACQUIRE to force obvious distinction to other 112*cdf0e10cSrcweir constructors 113*cdf0e10cSrcweir */ 114*cdf0e10cSrcweir inline Sequence( uno_Sequence * pSequence, __sal_NoAcquire ) 115*cdf0e10cSrcweir SAL_THROW( () ); 116*cdf0e10cSrcweir 117*cdf0e10cSrcweir /** Constructor: Creates a copy of given elements. 118*cdf0e10cSrcweir 119*cdf0e10cSrcweir @param pElement an array of elements 120*cdf0e10cSrcweir @param len length of array 121*cdf0e10cSrcweir */ 122*cdf0e10cSrcweir inline Sequence( const E * pElements, sal_Int32 len ); 123*cdf0e10cSrcweir 124*cdf0e10cSrcweir /** Constructor: Creates a default constructed sequence of given length. 125*cdf0e10cSrcweir 126*cdf0e10cSrcweir @param len initial sequence length 127*cdf0e10cSrcweir */ 128*cdf0e10cSrcweir inline explicit Sequence( sal_Int32 len ); 129*cdf0e10cSrcweir 130*cdf0e10cSrcweir /** Destructor: Releases sequence handle. Last handle will destruct 131*cdf0e10cSrcweir elements and free memory. 132*cdf0e10cSrcweir */ 133*cdf0e10cSrcweir inline ~Sequence() SAL_THROW( () ); 134*cdf0e10cSrcweir 135*cdf0e10cSrcweir /** Assignment operator: Acquires given sequence handle and releases 136*cdf0e10cSrcweir previously set handle. 137*cdf0e10cSrcweir 138*cdf0e10cSrcweir @param rSeq another sequence of same type 139*cdf0e10cSrcweir @return this sequence 140*cdf0e10cSrcweir */ 141*cdf0e10cSrcweir inline Sequence< E > & SAL_CALL operator = ( const Sequence< E > & rSeq ) 142*cdf0e10cSrcweir SAL_THROW( () ); 143*cdf0e10cSrcweir 144*cdf0e10cSrcweir /** Gets length of the sequence. 145*cdf0e10cSrcweir 146*cdf0e10cSrcweir @return length of sequence 147*cdf0e10cSrcweir */ 148*cdf0e10cSrcweir inline sal_Int32 SAL_CALL getLength() const SAL_THROW( () ) 149*cdf0e10cSrcweir { return _pSequence->nElements; } 150*cdf0e10cSrcweir 151*cdf0e10cSrcweir /** Tests whether the sequence has elements, i.e. elements count is 152*cdf0e10cSrcweir greater than zero. 153*cdf0e10cSrcweir 154*cdf0e10cSrcweir @return true, if elements count is greater than zero 155*cdf0e10cSrcweir */ 156*cdf0e10cSrcweir inline sal_Bool SAL_CALL hasElements() const SAL_THROW( () ) 157*cdf0e10cSrcweir { return (_pSequence->nElements > 0); } 158*cdf0e10cSrcweir 159*cdf0e10cSrcweir /** Gets a pointer to elements array for reading. 160*cdf0e10cSrcweir If the sequence has a length of 0, then the returned pointer is 161*cdf0e10cSrcweir undefined. 162*cdf0e10cSrcweir 163*cdf0e10cSrcweir @return pointer to elements array 164*cdf0e10cSrcweir */ 165*cdf0e10cSrcweir inline const E * SAL_CALL getConstArray() const SAL_THROW( () ) 166*cdf0e10cSrcweir { return reinterpret_cast< const E * >( _pSequence->elements ); } 167*cdf0e10cSrcweir 168*cdf0e10cSrcweir /** Gets a pointer to elements array for reading and writing. 169*cdf0e10cSrcweir In general if the sequence has a handle acquired by other sequences 170*cdf0e10cSrcweir (reference count > 1), then a new sequence is created copy constructing 171*cdf0e10cSrcweir all elements to keep value semantics! 172*cdf0e10cSrcweir If the sequence has a length of 0, then the returned pointer is 173*cdf0e10cSrcweir undefined. 174*cdf0e10cSrcweir 175*cdf0e10cSrcweir @return pointer to elements array 176*cdf0e10cSrcweir */ 177*cdf0e10cSrcweir inline E * SAL_CALL getArray(); 178*cdf0e10cSrcweir 179*cdf0e10cSrcweir /** Non-const index operator: Obtains a reference to element indexed at 180*cdf0e10cSrcweir given position. 181*cdf0e10cSrcweir The implementation does not check for array bounds! 182*cdf0e10cSrcweir In general if the sequence has a handle acquired by other sequences 183*cdf0e10cSrcweir (reference count > 1), then a new sequence is created copy constructing 184*cdf0e10cSrcweir all elements to keep value semantics! 185*cdf0e10cSrcweir 186*cdf0e10cSrcweir @param nIndex index 187*cdf0e10cSrcweir @return non-const C++ reference to element 188*cdf0e10cSrcweir */ 189*cdf0e10cSrcweir inline E & SAL_CALL operator [] ( sal_Int32 nIndex ); 190*cdf0e10cSrcweir 191*cdf0e10cSrcweir /** Const index operator: Obtains a reference to element indexed at 192*cdf0e10cSrcweir given position. The implementation does not check for array bounds! 193*cdf0e10cSrcweir 194*cdf0e10cSrcweir @param nIndex index 195*cdf0e10cSrcweir @return const C++ reference to element 196*cdf0e10cSrcweir */ 197*cdf0e10cSrcweir inline const E & SAL_CALL operator [] ( sal_Int32 nIndex ) const 198*cdf0e10cSrcweir SAL_THROW( () ); 199*cdf0e10cSrcweir 200*cdf0e10cSrcweir /** Equality operator: Compares two sequences. 201*cdf0e10cSrcweir 202*cdf0e10cSrcweir @param rSeq another sequence of same type (right side) 203*cdf0e10cSrcweir @return true if both sequences are equal, false otherwise 204*cdf0e10cSrcweir */ 205*cdf0e10cSrcweir inline sal_Bool SAL_CALL operator == ( const Sequence< E > & rSeq ) const 206*cdf0e10cSrcweir SAL_THROW( () ); 207*cdf0e10cSrcweir 208*cdf0e10cSrcweir /** Unequality operator: Compares two sequences. 209*cdf0e10cSrcweir 210*cdf0e10cSrcweir @param rSeq another sequence of same type (right side) 211*cdf0e10cSrcweir @return false if both sequences are equal, true otherwise 212*cdf0e10cSrcweir */ 213*cdf0e10cSrcweir inline sal_Bool SAL_CALL operator != ( const Sequence< E > & rSeq ) const 214*cdf0e10cSrcweir SAL_THROW( () ); 215*cdf0e10cSrcweir 216*cdf0e10cSrcweir /** Reallocates sequence to new length. 217*cdf0e10cSrcweir If the new length is smaller than the former, then upper elements will 218*cdf0e10cSrcweir be destructed (and their memory freed). If the new length is greater 219*cdf0e10cSrcweir than the former, then upper (new) elements are default constructed. 220*cdf0e10cSrcweir If the sequence has a handle acquired by other sequences 221*cdf0e10cSrcweir (reference count > 1), then the remaining elements are copy constructed 222*cdf0e10cSrcweir to a new sequence handle to keep value semantics! 223*cdf0e10cSrcweir 224*cdf0e10cSrcweir @param nSize new size of sequence 225*cdf0e10cSrcweir */ 226*cdf0e10cSrcweir inline void SAL_CALL realloc( sal_Int32 nSize ); 227*cdf0e10cSrcweir 228*cdf0e10cSrcweir /** Provides UNacquired sequence handle. 229*cdf0e10cSrcweir 230*cdf0e10cSrcweir @return UNacquired sequence handle 231*cdf0e10cSrcweir */ 232*cdf0e10cSrcweir inline uno_Sequence * SAL_CALL get() const SAL_THROW( () ) 233*cdf0e10cSrcweir { return _pSequence; } 234*cdf0e10cSrcweir }; 235*cdf0e10cSrcweir 236*cdf0e10cSrcweir /** Creates a UNO byte sequence from a SAL byte sequence. 237*cdf0e10cSrcweir 238*cdf0e10cSrcweir @param rByteSequence a byte sequence 239*cdf0e10cSrcweir @return a UNO byte sequence 240*cdf0e10cSrcweir */ 241*cdf0e10cSrcweir inline ::com::sun::star::uno::Sequence< sal_Int8 > SAL_CALL toUnoSequence( 242*cdf0e10cSrcweir const ::rtl::ByteSequence & rByteSequence ) SAL_THROW( () ); 243*cdf0e10cSrcweir 244*cdf0e10cSrcweir } 245*cdf0e10cSrcweir } 246*cdf0e10cSrcweir } 247*cdf0e10cSrcweir } 248*cdf0e10cSrcweir 249*cdf0e10cSrcweir /** Gets the meta type of IDL sequence. 250*cdf0e10cSrcweir 251*cdf0e10cSrcweir There are cases (involving templates) where uses of getCppuType are known to 252*cdf0e10cSrcweir not compile. Use cppu::UnoType or cppu::getTypeFavourUnsigned instead. 253*cdf0e10cSrcweir 254*cdf0e10cSrcweir @tplparam E element type of sequence 255*cdf0e10cSrcweir @param dummy typed pointer for function signature 256*cdf0e10cSrcweir @return type of IDL sequence 257*cdf0e10cSrcweir */ 258*cdf0e10cSrcweir template< class E > 259*cdf0e10cSrcweir inline const ::com::sun::star::uno::Type & 260*cdf0e10cSrcweir SAL_CALL getCppuType( const ::com::sun::star::uno::Sequence< E > * ) 261*cdf0e10cSrcweir SAL_THROW( () ); 262*cdf0e10cSrcweir 263*cdf0e10cSrcweir /** Gets the meta type of IDL sequence. 264*cdf0e10cSrcweir This function has been introduced, because one cannot get the (templated) 265*cdf0e10cSrcweir cppu type out of C++ array types. Array types have special 266*cdf0e10cSrcweir getCppuArrayTypeN() functions. 267*cdf0e10cSrcweir 268*cdf0e10cSrcweir @attention 269*cdf0e10cSrcweir the given element type must be the same as the template argument type! 270*cdf0e10cSrcweir @tplparam E element type of sequence 271*cdf0e10cSrcweir @param rElementType element type of sequence 272*cdf0e10cSrcweir @return type of IDL sequence 273*cdf0e10cSrcweir */ 274*cdf0e10cSrcweir template< class E > 275*cdf0e10cSrcweir inline const ::com::sun::star::uno::Type & 276*cdf0e10cSrcweir SAL_CALL getCppuSequenceType( const ::com::sun::star::uno::Type & rElementType ) 277*cdf0e10cSrcweir SAL_THROW( () ); 278*cdf0e10cSrcweir 279*cdf0e10cSrcweir /** Gets the meta type of IDL sequence< char >. 280*cdf0e10cSrcweir This function has been introduced due to ambiguities with unsigned short. 281*cdf0e10cSrcweir 282*cdf0e10cSrcweir @param dummy typed pointer for function signature 283*cdf0e10cSrcweir @return type of IDL sequence< char > 284*cdf0e10cSrcweir */ 285*cdf0e10cSrcweir inline const ::com::sun::star::uno::Type & 286*cdf0e10cSrcweir SAL_CALL getCharSequenceCppuType() SAL_THROW( () ); 287*cdf0e10cSrcweir 288*cdf0e10cSrcweir #endif 289