1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
19 #ifndef INCLUDED_COM_SUN_STAR_UNO_SEQUENCE_HXX
20 #define INCLUDED_COM_SUN_STAR_UNO_SEQUENCE_HXX
22 #include <sal/config.h>
26 #include <osl/interlck.h>
27 #include <com/sun/star/uno/Sequence.h>
28 #include <typelib/typedescription.h>
30 #include <com/sun/star/uno/genfunc.hxx>
31 #include <cppu/unotype.hxx>
44 typelib_TypeDescriptionReference
* Sequence
< E
>::s_pType
= 0;
48 inline Sequence
< E
>::Sequence() SAL_THROW(())
50 const Type
& rType
= ::cppu::getTypeFavourUnsigned( this );
51 ::uno_type_sequence_construct(
52 &_pSequence
, rType
.getTypeLibType(),
53 0, 0, (uno_AcquireFunc
)cpp_acquire
);
54 // no bad_alloc, because empty sequence is statically allocated in cppu
58 inline Sequence
< E
>::Sequence( const Sequence
< E
> & rSeq
) SAL_THROW(())
60 osl_atomic_increment( &rSeq
._pSequence
->nRefCount
);
61 _pSequence
= rSeq
._pSequence
;
65 inline Sequence
< E
>::Sequence(
66 uno_Sequence
* pSequence
, __sal_NoAcquire
) SAL_THROW(())
67 : _pSequence( pSequence
)
72 inline Sequence
< E
>::Sequence( const E
* pElements
, sal_Int32 len
)
74 const Type
& rType
= ::cppu::getTypeFavourUnsigned( this );
76 ::uno_type_sequence_construct(
77 &_pSequence
, rType
.getTypeLibType(),
78 const_cast< E
* >( pElements
), len
, (uno_AcquireFunc
)cpp_acquire
);
80 throw ::std::bad_alloc();
84 inline Sequence
< E
>::Sequence( sal_Int32 len
)
86 const Type
& rType
= ::cppu::getTypeFavourUnsigned( this );
88 ::uno_type_sequence_construct(
89 &_pSequence
, rType
.getTypeLibType(),
90 0, len
, (uno_AcquireFunc
)cpp_acquire
);
92 throw ::std::bad_alloc();
96 inline Sequence
< E
>::~Sequence() SAL_THROW(())
98 const Type
& rType
= ::cppu::getTypeFavourUnsigned( this );
99 ::uno_type_destructData(
100 this, rType
.getTypeLibType(), (uno_ReleaseFunc
)cpp_release
);
104 inline Sequence
< E
> & Sequence
< E
>::operator = ( const Sequence
< E
> & rSeq
) SAL_THROW(())
106 const Type
& rType
= ::cppu::getTypeFavourUnsigned( this );
107 ::uno_type_sequence_assign(
108 &_pSequence
, rSeq
._pSequence
, rType
.getTypeLibType(), (uno_ReleaseFunc
)cpp_release
);
113 inline sal_Bool Sequence
< E
>::operator == ( const Sequence
< E
> & rSeq
) const
116 if (_pSequence
== rSeq
._pSequence
)
118 const Type
& rType
= ::cppu::getTypeFavourUnsigned( this );
119 return ::uno_type_equalData(
120 const_cast< Sequence
< E
> * >( this ), rType
.getTypeLibType(),
121 const_cast< Sequence
< E
> * >( &rSeq
), rType
.getTypeLibType(),
122 (uno_QueryInterfaceFunc
)cpp_queryInterface
,
123 (uno_ReleaseFunc
)cpp_release
);
127 inline sal_Bool Sequence
< E
>::operator != ( const Sequence
< E
> & rSeq
) const
130 return (! operator == ( rSeq
));
134 inline E
* Sequence
< E
>::getArray()
136 const Type
& rType
= ::cppu::getTypeFavourUnsigned( this );
138 ::uno_type_sequence_reference2One(
139 &_pSequence
, rType
.getTypeLibType(),
140 (uno_AcquireFunc
)cpp_acquire
, (uno_ReleaseFunc
)cpp_release
);
142 throw ::std::bad_alloc();
143 return reinterpret_cast< E
* >( _pSequence
->elements
);
146 template<class E
> E
* Sequence
<E
>::begin() { return getArray(); }
148 template<class E
> E
const * Sequence
<E
>::begin() const
149 { return getConstArray(); }
151 template<class E
> E
* Sequence
<E
>::end() { return begin() + getLength(); }
153 template<class E
> E
const * Sequence
<E
>::end() const
154 { return begin() + getLength(); }
157 inline E
& Sequence
< E
>::operator [] ( sal_Int32 nIndex
)
159 assert( nIndex
>= 0 && nIndex
< getLength() );
160 return getArray()[ nIndex
];
164 inline const E
& Sequence
< E
>::operator [] ( sal_Int32 nIndex
) const
167 assert( nIndex
>= 0 && nIndex
< getLength() );
168 return reinterpret_cast< const E
* >( _pSequence
->elements
)[ nIndex
];
172 inline void Sequence
< E
>::realloc( sal_Int32 nSize
)
174 const Type
& rType
= ::cppu::getTypeFavourUnsigned( this );
176 ::uno_type_sequence_realloc(
177 &_pSequence
, rType
.getTypeLibType(), nSize
,
178 (uno_AcquireFunc
)cpp_acquire
, (uno_ReleaseFunc
)cpp_release
);
180 throw ::std::bad_alloc();
183 inline ::com::sun::star::uno::Sequence
< sal_Int8
> SAL_CALL
toUnoSequence(
184 const ::rtl::ByteSequence
& rByteSequence
) SAL_THROW(())
186 return ::com::sun::star::uno::Sequence
< sal_Int8
>(
187 * reinterpret_cast< const ::com::sun::star::uno::Sequence
< sal_Int8
> * >( &rByteSequence
) );
197 template< typename T
> inline ::com::sun::star::uno::Type
const &
198 getTypeFavourUnsigned(
199 SAL_UNUSED_PARAMETER ::com::sun::star::uno::Sequence
< T
> const *)
201 if (::com::sun::star::uno::Sequence
< T
>::s_pType
== 0) {
202 ::typelib_static_sequence_type_init(
203 &::com::sun::star::uno::Sequence
< T
>::s_pType
,
204 (::cppu::getTypeFavourUnsigned(
206 typename ::com::sun::star::uno::Sequence
< T
>::ElementType
* >(
210 return detail::getTypeFromTypeDescriptionReference(
211 &::com::sun::star::uno::Sequence
< T
>::s_pType
);
214 template< typename T
> inline ::com::sun::star::uno::Type
const &
216 SAL_UNUSED_PARAMETER ::com::sun::star::uno::Sequence
< T
> const *)
218 //TODO On certain platforms with weak memory models, the following code can
219 // result in some threads observing that td points to garbage:
220 static typelib_TypeDescriptionReference
* td
= 0;
222 ::typelib_static_sequence_type_init(
224 (::cppu::getTypeFavourChar(
226 typename ::com::sun::star::uno::Sequence
< T
>::ElementType
* >(
230 return detail::getTypeFromTypeDescriptionReference(&td
);
235 // generic sequence template
237 inline const ::com::sun::star::uno::Type
&
238 SAL_CALL
getCppuType(
239 SAL_UNUSED_PARAMETER
const ::com::sun::star::uno::Sequence
< E
> * )
242 return ::cppu::getTypeFavourUnsigned(
243 static_cast< ::com::sun::star::uno::Sequence
< E
> * >(0));
246 // generic sequence template for given element type (e.g. C++ arrays)
248 inline const ::com::sun::star::uno::Type
&
249 SAL_CALL
getCppuSequenceType( const ::com::sun::star::uno::Type
& rElementType
)
252 if (! ::com::sun::star::uno::Sequence
< E
>::s_pType
)
254 ::typelib_static_sequence_type_init(
255 & ::com::sun::star::uno::Sequence
< E
>::s_pType
,
256 rElementType
.getTypeLibType() );
258 return * reinterpret_cast< const ::com::sun::star::uno::Type
* >(
259 & ::com::sun::star::uno::Sequence
< E
>::s_pType
);
262 #if (defined(__SUNPRO_CC) && (__SUNPRO_CC == 0x500))
263 static typelib_TypeDescriptionReference
* s_pType_com_sun_star_uno_Sequence_Char
= 0;
267 inline const ::com::sun::star::uno::Type
&
268 SAL_CALL
getCharSequenceCppuType() SAL_THROW(())
270 #if !( defined(__SUNPRO_CC) && (__SUNPRO_CC == 0x500))
271 static typelib_TypeDescriptionReference
* s_pType_com_sun_star_uno_Sequence_Char
= 0;
273 if (! s_pType_com_sun_star_uno_Sequence_Char
)
275 const ::com::sun::star::uno::Type
& rElementType
= ::getCharCppuType();
276 ::typelib_static_sequence_type_init(
277 & s_pType_com_sun_star_uno_Sequence_Char
,
278 rElementType
.getTypeLibType() );
280 return * reinterpret_cast< const ::com::sun::star::uno::Type
* >(
281 & s_pType_com_sun_star_uno_Sequence_Char
);
286 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */