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