Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 : #ifndef INCLUDED_COM_SUN_STAR_UNO_SEQUENCE_HXX
20 : #define INCLUDED_COM_SUN_STAR_UNO_SEQUENCE_HXX
21 :
22 : #include <sal/config.h>
23 :
24 : #include <cassert>
25 :
26 : #include <osl/interlck.h>
27 : #include <com/sun/star/uno/Sequence.h>
28 : #include <typelib/typedescription.h>
29 : #include <uno/data.h>
30 : #include <com/sun/star/uno/genfunc.hxx>
31 : #include <cppu/unotype.hxx>
32 :
33 : namespace com
34 : {
35 : namespace sun
36 : {
37 : namespace star
38 : {
39 : namespace uno
40 : {
41 :
42 : /// @cond INTERNAL
43 : template< class E >
44 : typelib_TypeDescriptionReference * Sequence< E >::s_pType = 0;
45 : /// @endcond
46 :
47 : template< class E >
48 29115645 : inline Sequence< E >::Sequence()
49 : {
50 29115645 : const Type & rType = ::cppu::getTypeFavourUnsigned( this );
51 29115645 : ::uno_type_sequence_construct(
52 : &_pSequence, rType.getTypeLibType(),
53 29115645 : 0, 0, (uno_AcquireFunc)cpp_acquire );
54 : // no bad_alloc, because empty sequence is statically allocated in cppu
55 29115645 : }
56 :
57 : template< class E >
58 13010094 : inline Sequence< E >::Sequence( const Sequence< E > & rSeq )
59 : {
60 13010094 : osl_atomic_increment( &rSeq._pSequence->nRefCount );
61 13010094 : _pSequence = rSeq._pSequence;
62 13010094 : }
63 :
64 : template< class E >
65 30 : inline Sequence< E >::Sequence(
66 : uno_Sequence * pSequence, __sal_NoAcquire )
67 30 : : _pSequence( pSequence )
68 : {
69 30 : }
70 :
71 : template< class E >
72 3802401 : inline Sequence< E >::Sequence( const E * pElements, sal_Int32 len )
73 : {
74 3802401 : const Type & rType = ::cppu::getTypeFavourUnsigned( this );
75 : bool success =
76 : ::uno_type_sequence_construct(
77 : &_pSequence, rType.getTypeLibType(),
78 3802401 : const_cast< E * >( pElements ), len, (uno_AcquireFunc)cpp_acquire );
79 3802401 : if (! success)
80 0 : throw ::std::bad_alloc();
81 3802401 : }
82 :
83 : template< class E >
84 25421602 : inline Sequence< E >::Sequence( sal_Int32 len )
85 : {
86 25421602 : const Type & rType = ::cppu::getTypeFavourUnsigned( this );
87 : bool success =
88 : ::uno_type_sequence_construct(
89 : &_pSequence, rType.getTypeLibType(),
90 25421603 : 0, len, (uno_AcquireFunc)cpp_acquire );
91 25421603 : if (! success)
92 0 : throw ::std::bad_alloc();
93 25421603 : }
94 :
95 : template< class E >
96 71307182 : inline Sequence< E >::~Sequence()
97 : {
98 71307182 : if (osl_atomic_decrement( &_pSequence->nRefCount ) == 0)
99 : {
100 23783809 : const Type & rType = ::cppu::getTypeFavourUnsigned( this );
101 23783809 : uno_type_sequence_destroy(
102 23783809 : _pSequence, rType.getTypeLibType(), (uno_ReleaseFunc)cpp_release );
103 : }
104 71307183 : }
105 :
106 : template< class E >
107 13272525 : inline Sequence< E > & Sequence< E >::operator = ( const Sequence< E > & rSeq )
108 : {
109 13272525 : const Type & rType = ::cppu::getTypeFavourUnsigned( this );
110 13272525 : ::uno_type_sequence_assign(
111 13272525 : &_pSequence, rSeq._pSequence, rType.getTypeLibType(), (uno_ReleaseFunc)cpp_release );
112 13272525 : return *this;
113 : }
114 :
115 : template< class E >
116 399855 : inline bool Sequence< E >::operator == ( const Sequence< E > & rSeq ) const
117 : {
118 399855 : if (_pSequence == rSeq._pSequence)
119 180051 : return true;
120 219804 : const Type & rType = ::cppu::getTypeFavourUnsigned( this );
121 : return ::uno_type_equalData(
122 : const_cast< Sequence< E > * >( this ), rType.getTypeLibType(),
123 : const_cast< Sequence< E > * >( &rSeq ), rType.getTypeLibType(),
124 : (uno_QueryInterfaceFunc)cpp_queryInterface,
125 219804 : (uno_ReleaseFunc)cpp_release );
126 : }
127 :
128 : template< class E >
129 592 : inline bool Sequence< E >::operator != ( const Sequence< E > & rSeq ) const
130 : {
131 592 : return (! operator == ( rSeq ));
132 : }
133 :
134 : template< class E >
135 168771345 : inline E * Sequence< E >::getArray()
136 : {
137 168771345 : const Type & rType = ::cppu::getTypeFavourUnsigned( this );
138 : bool success =
139 : ::uno_type_sequence_reference2One(
140 : &_pSequence, rType.getTypeLibType(),
141 168771345 : (uno_AcquireFunc)cpp_acquire, (uno_ReleaseFunc)cpp_release );
142 168771345 : if (! success)
143 0 : throw ::std::bad_alloc();
144 168771345 : return reinterpret_cast< E * >( _pSequence->elements );
145 : }
146 :
147 298866 : template<class E> E * Sequence<E>::begin() { return getArray(); }
148 :
149 13730 : template<class E> E const * Sequence<E>::begin() const
150 13730 : { return getConstArray(); }
151 :
152 204560 : template<class E> E * Sequence<E>::end() { return begin() + getLength(); }
153 :
154 8710 : template<class E> E const * Sequence<E>::end() const
155 8710 : { return begin() + getLength(); }
156 :
157 : template< class E >
158 138967764 : inline E & Sequence< E >::operator [] ( sal_Int32 nIndex )
159 : {
160 : // silence spurious -Werror=strict-overflow warnings from GCC 4.8.2
161 : assert(nIndex >= 0 && static_cast<sal_uInt32>(nIndex) < static_cast<sal_uInt32>(getLength()));
162 138967764 : return getArray()[ nIndex ];
163 : }
164 :
165 : template< class E >
166 44893697 : inline const E & Sequence< E >::operator [] ( sal_Int32 nIndex ) const
167 : {
168 : // silence spurious -Werror=strict-overflow warnings from GCC 4.8.2
169 : assert(nIndex >= 0 && static_cast<sal_uInt32>(nIndex) < static_cast<sal_uInt32>(getLength()));
170 44893697 : return reinterpret_cast< const E * >( _pSequence->elements )[ nIndex ];
171 : }
172 :
173 : template< class E >
174 17364745 : inline void Sequence< E >::realloc( sal_Int32 nSize )
175 : {
176 17364745 : const Type & rType = ::cppu::getTypeFavourUnsigned( this );
177 : bool success =
178 : ::uno_type_sequence_realloc(
179 : &_pSequence, rType.getTypeLibType(), nSize,
180 17364745 : (uno_AcquireFunc)cpp_acquire, (uno_ReleaseFunc)cpp_release );
181 17364745 : if (!success)
182 0 : throw ::std::bad_alloc();
183 17364745 : }
184 :
185 816 : inline ::com::sun::star::uno::Sequence< sal_Int8 > SAL_CALL toUnoSequence(
186 : const ::rtl::ByteSequence & rByteSequence )
187 : {
188 : return ::com::sun::star::uno::Sequence< sal_Int8 >(
189 816 : * reinterpret_cast< const ::com::sun::star::uno::Sequence< sal_Int8 > * >( &rByteSequence ) );
190 : }
191 :
192 : }
193 : }
194 : }
195 : }
196 :
197 : namespace cppu {
198 :
199 : template< typename T > inline ::com::sun::star::uno::Type const &
200 290615461 : getTypeFavourUnsigned(
201 : SAL_UNUSED_PARAMETER ::com::sun::star::uno::Sequence< T > const *)
202 : {
203 290615461 : if (::com::sun::star::uno::Sequence< T >::s_pType == 0) {
204 56770 : ::typelib_static_sequence_type_init(
205 : &::com::sun::star::uno::Sequence< T >::s_pType,
206 : (::cppu::getTypeFavourUnsigned(
207 : static_cast<
208 : typename ::com::sun::star::uno::Sequence< T >::ElementType * >(
209 56770 : 0)).
210 56770 : getTypeLibType()));
211 : }
212 : return detail::getTypeFromTypeDescriptionReference(
213 290615461 : &::com::sun::star::uno::Sequence< T >::s_pType);
214 : }
215 :
216 : template< typename T > inline ::com::sun::star::uno::Type const &
217 90 : getTypeFavourChar(
218 : SAL_UNUSED_PARAMETER ::com::sun::star::uno::Sequence< T > const *)
219 : {
220 : //TODO On certain platforms with weak memory models, the following code can
221 : // result in some threads observing that td points to garbage:
222 : static typelib_TypeDescriptionReference * td = 0;
223 90 : if (td == 0) {
224 72 : ::typelib_static_sequence_type_init(
225 : &td,
226 : (::cppu::getTypeFavourChar(
227 : static_cast<
228 : typename ::com::sun::star::uno::Sequence< T >::ElementType * >(
229 72 : 0)).
230 72 : getTypeLibType()));
231 : }
232 90 : return detail::getTypeFromTypeDescriptionReference(&td);
233 : }
234 :
235 : }
236 :
237 : // generic sequence template
238 : template< class E >
239 : inline const ::com::sun::star::uno::Type &
240 519264 : SAL_CALL getCppuType(
241 : SAL_UNUSED_PARAMETER const ::com::sun::star::uno::Sequence< E > * )
242 : {
243 : return ::cppu::getTypeFavourUnsigned(
244 519264 : static_cast< ::com::sun::star::uno::Sequence< E > * >(0));
245 : }
246 :
247 : // generic sequence template for given element type (e.g. C++ arrays)
248 : template< class E >
249 : inline const ::com::sun::star::uno::Type &
250 : SAL_CALL getCppuSequenceType( const ::com::sun::star::uno::Type & rElementType )
251 : {
252 : if (! ::com::sun::star::uno::Sequence< E >::s_pType)
253 : {
254 : ::typelib_static_sequence_type_init(
255 : & ::com::sun::star::uno::Sequence< E >::s_pType,
256 : rElementType.getTypeLibType() );
257 : }
258 : return * reinterpret_cast< const ::com::sun::star::uno::Type * >(
259 : & ::com::sun::star::uno::Sequence< E >::s_pType );
260 : }
261 :
262 : // char sequence
263 : inline const ::com::sun::star::uno::Type &
264 : SAL_CALL getCharSequenceCppuType()
265 : {
266 : static typelib_TypeDescriptionReference * s_pType_com_sun_star_uno_Sequence_Char = 0;
267 : if (! s_pType_com_sun_star_uno_Sequence_Char)
268 : {
269 : const ::com::sun::star::uno::Type & rElementType = ::getCharCppuType();
270 : ::typelib_static_sequence_type_init(
271 : & s_pType_com_sun_star_uno_Sequence_Char,
272 : rElementType.getTypeLibType() );
273 : }
274 : return * reinterpret_cast< const ::com::sun::star::uno::Type * >(
275 : & s_pType_com_sun_star_uno_Sequence_Char );
276 : }
277 :
278 : #endif
279 :
280 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|