xref: /AOO41X/main/cppu/inc/com/sun/star/uno/Sequence.hxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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_HXX_
28*cdf0e10cSrcweir #define _COM_SUN_STAR_UNO_SEQUENCE_HXX_
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir #include "osl/diagnose.h"
31*cdf0e10cSrcweir #include "osl/interlck.h"
32*cdf0e10cSrcweir #include "com/sun/star/uno/Sequence.h"
33*cdf0e10cSrcweir #include "typelib/typedescription.h"
34*cdf0e10cSrcweir #include "uno/data.h"
35*cdf0e10cSrcweir #include "com/sun/star/uno/genfunc.hxx"
36*cdf0e10cSrcweir #include "cppu/unotype.hxx"
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir namespace com
39*cdf0e10cSrcweir {
40*cdf0e10cSrcweir namespace sun
41*cdf0e10cSrcweir {
42*cdf0e10cSrcweir namespace star
43*cdf0e10cSrcweir {
44*cdf0e10cSrcweir namespace uno
45*cdf0e10cSrcweir {
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir //______________________________________________________________________________
48*cdf0e10cSrcweir template< class E >
49*cdf0e10cSrcweir typelib_TypeDescriptionReference * Sequence< E >::s_pType = 0;
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir //______________________________________________________________________________
52*cdf0e10cSrcweir template< class E >
53*cdf0e10cSrcweir inline Sequence< E >::Sequence() SAL_THROW( () )
54*cdf0e10cSrcweir {
55*cdf0e10cSrcweir     const Type & rType = ::cppu::getTypeFavourUnsigned( this );
56*cdf0e10cSrcweir     ::uno_type_sequence_construct(
57*cdf0e10cSrcweir         &_pSequence, rType.getTypeLibType(),
58*cdf0e10cSrcweir         0, 0, (uno_AcquireFunc)cpp_acquire );
59*cdf0e10cSrcweir     // no bad_alloc, because empty sequence is statically allocated in cppu
60*cdf0e10cSrcweir }
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir //______________________________________________________________________________
63*cdf0e10cSrcweir template< class E >
64*cdf0e10cSrcweir inline Sequence< E >::Sequence( const Sequence< E > & rSeq ) SAL_THROW( () )
65*cdf0e10cSrcweir {
66*cdf0e10cSrcweir     ::osl_incrementInterlockedCount( &rSeq._pSequence->nRefCount );
67*cdf0e10cSrcweir     _pSequence = rSeq._pSequence;
68*cdf0e10cSrcweir }
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir //______________________________________________________________________________
71*cdf0e10cSrcweir template< class E >
72*cdf0e10cSrcweir inline Sequence< E >::Sequence(
73*cdf0e10cSrcweir     uno_Sequence * pSequence, __sal_NoAcquire ) SAL_THROW( () )
74*cdf0e10cSrcweir         : _pSequence( pSequence )
75*cdf0e10cSrcweir {
76*cdf0e10cSrcweir }
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir //______________________________________________________________________________
79*cdf0e10cSrcweir template< class E >
80*cdf0e10cSrcweir inline Sequence< E >::Sequence( const E * pElements, sal_Int32 len )
81*cdf0e10cSrcweir {
82*cdf0e10cSrcweir     const Type & rType = ::cppu::getTypeFavourUnsigned( this );
83*cdf0e10cSrcweir #if ! defined EXCEPTIONS_OFF
84*cdf0e10cSrcweir     sal_Bool success =
85*cdf0e10cSrcweir #endif
86*cdf0e10cSrcweir     ::uno_type_sequence_construct(
87*cdf0e10cSrcweir         &_pSequence, rType.getTypeLibType(),
88*cdf0e10cSrcweir         const_cast< E * >( pElements ), len, (uno_AcquireFunc)cpp_acquire );
89*cdf0e10cSrcweir #if ! defined EXCEPTIONS_OFF
90*cdf0e10cSrcweir     if (! success)
91*cdf0e10cSrcweir         throw ::std::bad_alloc();
92*cdf0e10cSrcweir #endif
93*cdf0e10cSrcweir }
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir //______________________________________________________________________________
96*cdf0e10cSrcweir template< class E >
97*cdf0e10cSrcweir inline Sequence< E >::Sequence( sal_Int32 len )
98*cdf0e10cSrcweir {
99*cdf0e10cSrcweir     const Type & rType = ::cppu::getTypeFavourUnsigned( this );
100*cdf0e10cSrcweir #if ! defined EXCEPTIONS_OFF
101*cdf0e10cSrcweir     sal_Bool success =
102*cdf0e10cSrcweir #endif
103*cdf0e10cSrcweir     ::uno_type_sequence_construct(
104*cdf0e10cSrcweir         &_pSequence, rType.getTypeLibType(),
105*cdf0e10cSrcweir         0, len, (uno_AcquireFunc)cpp_acquire );
106*cdf0e10cSrcweir #if ! defined EXCEPTIONS_OFF
107*cdf0e10cSrcweir     if (! success)
108*cdf0e10cSrcweir         throw ::std::bad_alloc();
109*cdf0e10cSrcweir #endif
110*cdf0e10cSrcweir }
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir //______________________________________________________________________________
113*cdf0e10cSrcweir template< class E >
114*cdf0e10cSrcweir inline Sequence< E >::~Sequence() SAL_THROW( () )
115*cdf0e10cSrcweir {
116*cdf0e10cSrcweir     const Type & rType = ::cppu::getTypeFavourUnsigned( this );
117*cdf0e10cSrcweir     ::uno_type_destructData(
118*cdf0e10cSrcweir         this, rType.getTypeLibType(), (uno_ReleaseFunc)cpp_release );
119*cdf0e10cSrcweir }
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir //______________________________________________________________________________
122*cdf0e10cSrcweir template< class E >
123*cdf0e10cSrcweir inline Sequence< E > & Sequence< E >::operator = ( const Sequence< E > & rSeq ) SAL_THROW( () )
124*cdf0e10cSrcweir {
125*cdf0e10cSrcweir     const Type & rType = ::cppu::getTypeFavourUnsigned( this );
126*cdf0e10cSrcweir     ::uno_type_sequence_assign(
127*cdf0e10cSrcweir         &_pSequence, rSeq._pSequence, rType.getTypeLibType(), (uno_ReleaseFunc)cpp_release );
128*cdf0e10cSrcweir     return *this;
129*cdf0e10cSrcweir }
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir //______________________________________________________________________________
132*cdf0e10cSrcweir template< class E >
133*cdf0e10cSrcweir inline sal_Bool Sequence< E >::operator == ( const Sequence< E > & rSeq ) const
134*cdf0e10cSrcweir     SAL_THROW( () )
135*cdf0e10cSrcweir {
136*cdf0e10cSrcweir     if (_pSequence == rSeq._pSequence)
137*cdf0e10cSrcweir         return sal_True;
138*cdf0e10cSrcweir     const Type & rType = ::cppu::getTypeFavourUnsigned( this );
139*cdf0e10cSrcweir     return ::uno_type_equalData(
140*cdf0e10cSrcweir         const_cast< Sequence< E > * >( this ), rType.getTypeLibType(),
141*cdf0e10cSrcweir         const_cast< Sequence< E > * >( &rSeq ), rType.getTypeLibType(),
142*cdf0e10cSrcweir         (uno_QueryInterfaceFunc)cpp_queryInterface,
143*cdf0e10cSrcweir         (uno_ReleaseFunc)cpp_release );
144*cdf0e10cSrcweir }
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir //______________________________________________________________________________
147*cdf0e10cSrcweir template< class E >
148*cdf0e10cSrcweir inline sal_Bool Sequence< E >::operator != ( const Sequence< E > & rSeq ) const
149*cdf0e10cSrcweir     SAL_THROW( () )
150*cdf0e10cSrcweir {
151*cdf0e10cSrcweir     return (! operator == ( rSeq ));
152*cdf0e10cSrcweir }
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir //______________________________________________________________________________
155*cdf0e10cSrcweir template< class E >
156*cdf0e10cSrcweir inline E * Sequence< E >::getArray()
157*cdf0e10cSrcweir {
158*cdf0e10cSrcweir     const Type & rType = ::cppu::getTypeFavourUnsigned( this );
159*cdf0e10cSrcweir #if ! defined EXCEPTIONS_OFF
160*cdf0e10cSrcweir     sal_Bool success =
161*cdf0e10cSrcweir #endif
162*cdf0e10cSrcweir     ::uno_type_sequence_reference2One(
163*cdf0e10cSrcweir         &_pSequence, rType.getTypeLibType(),
164*cdf0e10cSrcweir         (uno_AcquireFunc)cpp_acquire, (uno_ReleaseFunc)cpp_release );
165*cdf0e10cSrcweir #if ! defined EXCEPTIONS_OFF
166*cdf0e10cSrcweir     if (! success)
167*cdf0e10cSrcweir         throw ::std::bad_alloc();
168*cdf0e10cSrcweir #endif
169*cdf0e10cSrcweir     return reinterpret_cast< E * >( _pSequence->elements );
170*cdf0e10cSrcweir }
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir //______________________________________________________________________________
173*cdf0e10cSrcweir template< class E >
174*cdf0e10cSrcweir inline E & Sequence< E >::operator [] ( sal_Int32 nIndex )
175*cdf0e10cSrcweir {
176*cdf0e10cSrcweir     OSL_ENSURE(
177*cdf0e10cSrcweir         nIndex >= 0 && nIndex < getLength(),
178*cdf0e10cSrcweir         "### illegal index of sequence!" );
179*cdf0e10cSrcweir     return getArray()[ nIndex ];
180*cdf0e10cSrcweir }
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir //______________________________________________________________________________
183*cdf0e10cSrcweir template< class E >
184*cdf0e10cSrcweir inline const E & Sequence< E >::operator [] ( sal_Int32 nIndex ) const
185*cdf0e10cSrcweir     SAL_THROW( () )
186*cdf0e10cSrcweir {
187*cdf0e10cSrcweir     OSL_ENSURE(
188*cdf0e10cSrcweir         nIndex >= 0 && nIndex < getLength(),
189*cdf0e10cSrcweir         "### illegal index of sequence!" );
190*cdf0e10cSrcweir     return reinterpret_cast< const E * >( _pSequence->elements )[ nIndex ];
191*cdf0e10cSrcweir }
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir //______________________________________________________________________________
194*cdf0e10cSrcweir template< class E >
195*cdf0e10cSrcweir inline void Sequence< E >::realloc( sal_Int32 nSize )
196*cdf0e10cSrcweir {
197*cdf0e10cSrcweir     const Type & rType = ::cppu::getTypeFavourUnsigned( this );
198*cdf0e10cSrcweir #if !defined EXCEPTIONS_OFF
199*cdf0e10cSrcweir     sal_Bool success =
200*cdf0e10cSrcweir #endif
201*cdf0e10cSrcweir     ::uno_type_sequence_realloc(
202*cdf0e10cSrcweir         &_pSequence, rType.getTypeLibType(), nSize,
203*cdf0e10cSrcweir         (uno_AcquireFunc)cpp_acquire, (uno_ReleaseFunc)cpp_release );
204*cdf0e10cSrcweir #if !defined EXCEPTIONS_OFF
205*cdf0e10cSrcweir     if (!success)
206*cdf0e10cSrcweir         throw ::std::bad_alloc();
207*cdf0e10cSrcweir #endif
208*cdf0e10cSrcweir }
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir //------------------------------------------------------------------------------
211*cdf0e10cSrcweir inline ::com::sun::star::uno::Sequence< sal_Int8 > SAL_CALL toUnoSequence(
212*cdf0e10cSrcweir     const ::rtl::ByteSequence & rByteSequence ) SAL_THROW( () )
213*cdf0e10cSrcweir {
214*cdf0e10cSrcweir     return ::com::sun::star::uno::Sequence< sal_Int8 >(
215*cdf0e10cSrcweir         * reinterpret_cast< const ::com::sun::star::uno::Sequence< sal_Int8 > * >( &rByteSequence ) );
216*cdf0e10cSrcweir }
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir }
219*cdf0e10cSrcweir }
220*cdf0e10cSrcweir }
221*cdf0e10cSrcweir }
222*cdf0e10cSrcweir 
223*cdf0e10cSrcweir namespace cppu {
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir template< typename T > inline ::com::sun::star::uno::Type const &
226*cdf0e10cSrcweir getTypeFavourUnsigned(::com::sun::star::uno::Sequence< T > const *) {
227*cdf0e10cSrcweir     if (::com::sun::star::uno::Sequence< T >::s_pType == 0) {
228*cdf0e10cSrcweir         ::typelib_static_sequence_type_init(
229*cdf0e10cSrcweir             &::com::sun::star::uno::Sequence< T >::s_pType,
230*cdf0e10cSrcweir             (::cppu::getTypeFavourUnsigned(
231*cdf0e10cSrcweir                 static_cast<
232*cdf0e10cSrcweir                 typename ::com::sun::star::uno::Sequence< T >::ElementType * >(
233*cdf0e10cSrcweir                     0)).
234*cdf0e10cSrcweir              getTypeLibType()));
235*cdf0e10cSrcweir     }
236*cdf0e10cSrcweir     return detail::getTypeFromTypeDescriptionReference(
237*cdf0e10cSrcweir         &::com::sun::star::uno::Sequence< T >::s_pType);
238*cdf0e10cSrcweir }
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir template< typename T > inline ::com::sun::star::uno::Type const &
241*cdf0e10cSrcweir getTypeFavourChar(::com::sun::star::uno::Sequence< T > const *) {
242*cdf0e10cSrcweir     //TODO  On certain platforms with weak memory models, the following code can
243*cdf0e10cSrcweir     // result in some threads observing that td points to garbage:
244*cdf0e10cSrcweir     static typelib_TypeDescriptionReference * td = 0;
245*cdf0e10cSrcweir     if (td == 0) {
246*cdf0e10cSrcweir         ::typelib_static_sequence_type_init(
247*cdf0e10cSrcweir             &td,
248*cdf0e10cSrcweir             (::cppu::getTypeFavourChar(
249*cdf0e10cSrcweir                 static_cast<
250*cdf0e10cSrcweir                 typename ::com::sun::star::uno::Sequence< T >::ElementType * >(
251*cdf0e10cSrcweir                     0)).
252*cdf0e10cSrcweir              getTypeLibType()));
253*cdf0e10cSrcweir     }
254*cdf0e10cSrcweir     return detail::getTypeFromTypeDescriptionReference(&td);
255*cdf0e10cSrcweir }
256*cdf0e10cSrcweir 
257*cdf0e10cSrcweir }
258*cdf0e10cSrcweir 
259*cdf0e10cSrcweir // generic sequence template
260*cdf0e10cSrcweir template< class E >
261*cdf0e10cSrcweir inline const ::com::sun::star::uno::Type &
262*cdf0e10cSrcweir SAL_CALL getCppuType( const ::com::sun::star::uno::Sequence< E > * )
263*cdf0e10cSrcweir     SAL_THROW( () )
264*cdf0e10cSrcweir {
265*cdf0e10cSrcweir     return ::cppu::getTypeFavourUnsigned(
266*cdf0e10cSrcweir         static_cast< ::com::sun::star::uno::Sequence< E > * >(0));
267*cdf0e10cSrcweir }
268*cdf0e10cSrcweir 
269*cdf0e10cSrcweir // generic sequence template for given element type (e.g. C++ arrays)
270*cdf0e10cSrcweir template< class E >
271*cdf0e10cSrcweir inline const ::com::sun::star::uno::Type &
272*cdf0e10cSrcweir SAL_CALL getCppuSequenceType( const ::com::sun::star::uno::Type & rElementType )
273*cdf0e10cSrcweir     SAL_THROW( () )
274*cdf0e10cSrcweir {
275*cdf0e10cSrcweir     if (! ::com::sun::star::uno::Sequence< E >::s_pType)
276*cdf0e10cSrcweir     {
277*cdf0e10cSrcweir         ::typelib_static_sequence_type_init(
278*cdf0e10cSrcweir             & ::com::sun::star::uno::Sequence< E >::s_pType,
279*cdf0e10cSrcweir             rElementType.getTypeLibType() );
280*cdf0e10cSrcweir     }
281*cdf0e10cSrcweir     return * reinterpret_cast< const ::com::sun::star::uno::Type * >(
282*cdf0e10cSrcweir         & ::com::sun::star::uno::Sequence< E >::s_pType );
283*cdf0e10cSrcweir }
284*cdf0e10cSrcweir 
285*cdf0e10cSrcweir #if (defined(__SUNPRO_CC) && (__SUNPRO_CC == 0x500))
286*cdf0e10cSrcweir static typelib_TypeDescriptionReference * s_pType_com_sun_star_uno_Sequence_Char = 0;
287*cdf0e10cSrcweir #endif
288*cdf0e10cSrcweir 
289*cdf0e10cSrcweir // char sequence
290*cdf0e10cSrcweir inline const ::com::sun::star::uno::Type &
291*cdf0e10cSrcweir SAL_CALL getCharSequenceCppuType() SAL_THROW( () )
292*cdf0e10cSrcweir {
293*cdf0e10cSrcweir #if !( defined(__SUNPRO_CC) && (__SUNPRO_CC == 0x500))
294*cdf0e10cSrcweir 	static typelib_TypeDescriptionReference * s_pType_com_sun_star_uno_Sequence_Char = 0;
295*cdf0e10cSrcweir #endif
296*cdf0e10cSrcweir     if (! s_pType_com_sun_star_uno_Sequence_Char)
297*cdf0e10cSrcweir     {
298*cdf0e10cSrcweir         const ::com::sun::star::uno::Type & rElementType = ::getCharCppuType();
299*cdf0e10cSrcweir         ::typelib_static_sequence_type_init(
300*cdf0e10cSrcweir             & s_pType_com_sun_star_uno_Sequence_Char,
301*cdf0e10cSrcweir             rElementType.getTypeLibType() );
302*cdf0e10cSrcweir     }
303*cdf0e10cSrcweir     return * reinterpret_cast< const ::com::sun::star::uno::Type * >(
304*cdf0e10cSrcweir         & s_pType_com_sun_star_uno_Sequence_Char );
305*cdf0e10cSrcweir }
306*cdf0e10cSrcweir 
307*cdf0e10cSrcweir #endif
308