Debian Codegraph @

include/com/sun/star/uno/Sequence.hxx

raw
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
20 /*
21 * This file is part of LibreOffice published API.
22 */
23 #ifndef INCLUDED_COM_SUN_STAR_UNO_SEQUENCE_HXX
24 #define INCLUDED_COM_SUN_STAR_UNO_SEQUENCE_HXX
25
26 #include "sal/config.h"
27
28 #include <cassert>
29 #include <cstddef>
30 #if defined LIBO_INTERNAL_ONLY
31 # include <type_traits>
32 # include <ostream>
33 # include <utility>
34 #endif
35
36 #include "osl/interlck.h"
37 #include "com/sun/star/uno/Sequence.h"
38 #include "typelib/typedescription.h"
39 #include "uno/data.h"
40 #include "com/sun/star/uno/genfunc.hxx"
41 #include "cppu/unotype.hxx"
42
43 namespace com
44 {
45 namespace sun
46 {
47 namespace star
48 {
49 namespace uno
50 {
51
52 /// @cond INTERNAL
53 template< class E >
54 typelib_TypeDescriptionReference * Sequence< E >::s_pType = NULL;
55 /// @endcond
56
57 template< class E >
58 inline Sequence< E >::Sequence()
59 {
60 const Type & rType = ::cppu::getTypeFavourUnsigned( static_cast<Sequence const *>(NULL) );
61 ::uno_type_sequence_construct(
62 &_pSequence, rType.getTypeLibType(),
63 NULL, 0, cpp_acquire );
64 // no bad_alloc, because empty sequence is statically allocated in cppu
65 }
66
67 template< class E >
68 inline Sequence< E >::Sequence( const Sequence & rSeq )
69 {
70 osl_atomic_increment( &rSeq._pSequence->nRefCount );
71 _pSequence = rSeq._pSequence;
72 }
73
74 template< class E >
75 inline Sequence< E >::Sequence(
76 uno_Sequence * pSequence, __sal_NoAcquire )
77 : _pSequence( pSequence )
78 {
79 }
80
81 template< class E >
82 inline Sequence< E >::Sequence( const E * pElements, sal_Int32 len )
83 {
84 const Type & rType = ::cppu::getTypeFavourUnsigned( static_cast<Sequence const *>(NULL) );
85
86 bool success =
87 ::uno_type_sequence_construct(
88 &_pSequence, rType.getTypeLibType(),
89 const_cast< E * >( pElements ), len, cpp_acquire );
90 if (! success)
91 throw ::std::bad_alloc();
92 }
93
94 template< class E >
95 inline Sequence< E >::Sequence( sal_Int32 len )
96 {
97 const Type & rType = ::cppu::getTypeFavourUnsigned( static_cast<Sequence const *>(NULL) );
98 bool success =
99 ::uno_type_sequence_construct(
100 &_pSequence, rType.getTypeLibType(),
101 NULL, len, cpp_acquire );
102 if (! success)
103 throw ::std::bad_alloc();
104 }
105
106 #if defined LIBO_INTERNAL_ONLY
107 template<typename E> Sequence<E>::Sequence(std::initializer_list<E> init) {
108 if (!uno_type_sequence_construct(
109 &_pSequence, cppu::getTypeFavourUnsigned(static_cast<Sequence const *>(NULL)).getTypeLibType(),
110 const_cast<E *>(init.begin()), init.size(), cpp_acquire))
111 {
112 throw std::bad_alloc();
113 }
114 }
115 #endif
116
117 template< class E >
118 inline Sequence< E >::~Sequence()
119 {
120 if (osl_atomic_decrement( &_pSequence->nRefCount ) == 0)
121 {
122 const Type & rType = ::cppu::getTypeFavourUnsigned( static_cast<Sequence const *>(NULL) );
123 uno_type_sequence_destroy(
124 _pSequence, rType.getTypeLibType(), cpp_release );
125 }
126 }
127
128 template< class E >
129 inline Sequence< E > & Sequence< E >::operator = ( const Sequence & rSeq )
130 {
131 const Type & rType = ::cppu::getTypeFavourUnsigned( static_cast<Sequence const *>(NULL) );
132 ::uno_type_sequence_assign(
133 &_pSequence, rSeq._pSequence, rType.getTypeLibType(), cpp_release );
134 return *this;
135 }
136
137 #if defined LIBO_INTERNAL_ONLY
138 template<typename E> Sequence<E> & Sequence<E>::operator =(Sequence && other) {
139 std::swap(_pSequence, other._pSequence);
140 return *this;
141 }
142 #endif
143
144 template< class E >
145 inline bool Sequence< E >::operator == ( const Sequence & rSeq ) const
146 {
147 if (_pSequence == rSeq._pSequence)
148 return true;
149 if (_pSequence->nElements != rSeq._pSequence->nElements)
150 return false;
151 const Type & rType = ::cppu::getTypeFavourUnsigned( static_cast<Sequence const *>(NULL) );
152 return ::uno_type_equalData(
153 const_cast< Sequence * >( this ), rType.getTypeLibType(),
154 const_cast< Sequence * >( &rSeq ), rType.getTypeLibType(),
155 cpp_queryInterface,
156 cpp_release );
157 }
158
159 template< class E >
160 inline bool Sequence< E >::operator != ( const Sequence & rSeq ) const
161 {
162 return (! operator == ( rSeq ));
163 }
164
165 template< class E >
166 inline E * Sequence< E >::getArray()
167 {
168 const Type & rType = ::cppu::getTypeFavourUnsigned( static_cast<Sequence const *>(NULL) );
169 bool success =
170 ::uno_type_sequence_reference2One(
171 &_pSequence, rType.getTypeLibType(),
172 cpp_acquire, cpp_release );
173 if (! success)
174 throw ::std::bad_alloc();
175 return reinterpret_cast< E * >( _pSequence->elements );
176 }
177
178 #if !defined LIBO_INTERNAL_ONLY
179 template<class E> E * Sequence<E>::begin() { return getArray(); }
180 #endif
181
182 template<class E> E const * Sequence<E>::begin() const
183 { return getConstArray(); }
184
185 #if !defined LIBO_INTERNAL_ONLY
186 template<class E> E * Sequence<E>::end() { return begin() + getLength(); }
187 #endif
188
189 template<class E> E const * Sequence<E>::end() const
190 { return begin() + getLength(); }
191
192 #if !defined LIBO_INTERNAL_ONLY
193 template< class E >
194 inline E & Sequence< E >::operator [] ( sal_Int32 nIndex )
195 {
196 // silence spurious -Werror=strict-overflow warnings from GCC 4.8.2
197 assert(nIndex >= 0 && static_cast<sal_uInt32>(nIndex) < static_cast<sal_uInt32>(getLength()));
198 return getArray()[ nIndex ];
199 }
200 #endif
201
202 template< class E >
203 inline const E & Sequence< E >::operator [] ( sal_Int32 nIndex ) const
204 {
205 // silence spurious -Werror=strict-overflow warnings from GCC 4.8.2
206 assert(nIndex >= 0 && static_cast<sal_uInt32>(nIndex) < static_cast<sal_uInt32>(getLength()));
207 return reinterpret_cast< const E * >( _pSequence->elements )[ nIndex ];
208 }
209
210 template< class E >
211 inline void Sequence< E >::realloc( sal_Int32 nSize )
212 {
213 const Type & rType = ::cppu::getTypeFavourUnsigned( static_cast<Sequence const *>(NULL) );
214 bool success =
215 ::uno_type_sequence_realloc(
216 &_pSequence, rType.getTypeLibType(), nSize,
217 cpp_acquire, cpp_release );
218 if (!success)
219 throw ::std::bad_alloc();
220 }
221
222 #if defined LIBO_INTERNAL_ONLY
223 template <class E> inline void Sequence<E>::swap(Sequence& other)
224 {
225 std::swap(_pSequence, other._pSequence);
226 }
227 #endif
228
229 inline ::com::sun::star::uno::Sequence< sal_Int8 > SAL_CALL toUnoSequence(
230 const ::rtl::ByteSequence & rByteSequence )
231 {
232 return * reinterpret_cast< const ::com::sun::star::uno::Sequence< sal_Int8 > * >( &rByteSequence );
233 }
234
235 #if defined LIBO_INTERNAL_ONLY
236
237 /// @cond INTERNAL
238
239 namespace uno_detail {
240
241 template< typename value_t, typename charT, typename traits >
242 void sequence_output_elems( std::basic_ostream<charT, traits> &os, const value_t *pAry, sal_Int32 nLen, std::true_type )
243 {
244 // for integral types, use hex notation
245 auto const flags = os.setf(std::ios_base::hex);
246 for(sal_Int32 i=0; i<nLen-1; ++i)
247 os << "0x" << *pAry++ << ", ";
248 if( nLen > 1 )
249 os << "0x" << *pAry++;
250 os.setf(flags);
251 }
252
253 template< typename value_t, typename charT, typename traits >
254 void sequence_output_elems( std::basic_ostream<charT, traits> &os, const value_t *pAry, sal_Int32 nLen, std::false_type )
255 {
256 // every other type: rely on their own ostream operator<<
257 for(sal_Int32 i=0; i<nLen-1; ++i)
258 os << *pAry++ << ", ";
259 if( nLen > 1 )
260 os << *pAry++;
261 }
262
263 template< typename value_t, typename charT, typename traits >
264 void sequence_output_bytes( std::basic_ostream<charT, traits> &os, const value_t *pAry, sal_Int32 nLen )
265 {
266 // special case bytes - ostream operator<< outputs those as char
267 // values, but we need raw ints here
268 auto const flags = os.setf(std::ios_base::hex);
269 for(sal_Int32 i=0; i<nLen-1; ++i)
270 os << "0x" << (0xFF & +*pAry++) << ", ";
271 if( nLen > 1 )
272 os << "0x" << (0xFF & +*pAry++);
273 os.setf(flags);
274 }
275
276 }
277
278 /**
279 Support for Sequence in std::ostream (and thus in CPPUNIT_ASSERT or SAL_INFO
280 macros, for example).
281
282 @since LibreOffice 6.1
283 */
284 template< typename value_t, typename charT, typename traits >
285 inline std::basic_ostream<charT, traits> &operator<<(std::basic_ostream<charT, traits> &os, css::uno::Sequence<value_t> const& v)
286 {
287 const value_t *pAry = v.getConstArray();
288 sal_Int32 nLen = v.getLength();
289 if constexpr (std::is_same<sal_Int8, value_t>::value) {
290 uno_detail::sequence_output_bytes(os, pAry, nLen);
291 } else {
292 uno_detail::sequence_output_elems(os, pAry, nLen, std::is_integral<value_t>());
293 }
294 return os;
295 }
296
297 template <class E> inline auto asNonConstRange(css::uno::Sequence<E>& s)
298 {
299 // Two iterators [begin, end] representing the non-const range of the Sequence.
300 // It only calls Sequence::getArray once, to avoid the second COW overhead when
301 // Sequence::begin() and Sequence::end() are called in pairs.
302 // Inheriting from pair allows to use std::tie to unpack the two iterators.
303 struct SequenceRange : public std::pair<E*, E*>
304 {
305 SequenceRange(E* ptr, sal_Int32 len) : std::pair<E*, E*>(ptr, ptr + len) {}
306 // These allow to pass it as range-expression to range-based for loops
307 E* begin() { return std::pair<E*, E*>::first; }
308 E* end() { return std::pair<E*, E*>::second; }
309 E& operator[](sal_Int32 i) { assert(i >= 0 && i < end() - begin()); return begin()[i]; }
310 };
311 return SequenceRange(s.getLength() ? s.getArray() : nullptr, s.getLength());
312 };
313
314 /// @endcond
315
316 #endif
317
318 }
319 }
320 }
321 }
322
323 namespace cppu {
324
325 template< typename T > inline ::com::sun::star::uno::Type const &
326 getTypeFavourUnsigned(
327 SAL_UNUSED_PARAMETER ::com::sun::star::uno::Sequence< T > const *)
328 {
329 if (::com::sun::star::uno::Sequence< T >::s_pType == NULL) {
330 ::typelib_static_sequence_type_init(
331 &::com::sun::star::uno::Sequence< T >::s_pType,
332 (::cppu::getTypeFavourUnsigned(
333 static_cast<
334 typename ::com::sun::star::uno::Sequence< T >::ElementType * >(
335 NULL)).
336 getTypeLibType()));
337 }
338 return detail::getTypeFromTypeDescriptionReference(
339 &::com::sun::star::uno::Sequence< T >::s_pType);
340 }
341
342 template< typename T > inline ::com::sun::star::uno::Type const &
343 getTypeFavourChar(
344 SAL_UNUSED_PARAMETER ::com::sun::star::uno::Sequence< T > const *)
345 {
346 //TODO On certain platforms with weak memory models, the following code can
347 // result in some threads observing that td points to garbage:
348 static typelib_TypeDescriptionReference * td = NULL;
349 if (td == NULL) {
350 ::typelib_static_sequence_type_init(
351 &td,
352 (::cppu::getTypeFavourChar(
353 static_cast<
354 typename ::com::sun::star::uno::Sequence< T >::ElementType * >(
355 NULL)).
356 getTypeLibType()));
357 }
358 return detail::getTypeFromTypeDescriptionReference(&td);
359 }
360
361 }
362
363 // generic sequence template
364 template< class E >
365 inline const ::com::sun::star::uno::Type &
366 SAL_CALL getCppuType(
367 SAL_UNUSED_PARAMETER const ::com::sun::star::uno::Sequence< E > * )
368 {
369 return ::cppu::getTypeFavourUnsigned(
370 static_cast< ::com::sun::star::uno::Sequence< E > * >(0));
371 }
372
373 // generic sequence template for given element type (e.g. C++ arrays)
374 template< class E >
375 inline const ::com::sun::star::uno::Type &
376 SAL_CALL getCppuSequenceType( const ::com::sun::star::uno::Type & rElementType )
377 {
378 if (! ::com::sun::star::uno::Sequence< E >::s_pType)
379 {
380 ::typelib_static_sequence_type_init(
381 & ::com::sun::star::uno::Sequence< E >::s_pType,
382 rElementType.getTypeLibType() );
383 }
384 return * reinterpret_cast< const ::com::sun::star::uno::Type * >(
385 & ::com::sun::star::uno::Sequence< E >::s_pType );
386 }
387
388 // char sequence
389 inline const ::com::sun::star::uno::Type &
390 SAL_CALL getCharSequenceCppuType()
391 {
392 static typelib_TypeDescriptionReference * s_pType_com_sun_star_uno_Sequence_Char = NULL;
393 if (! s_pType_com_sun_star_uno_Sequence_Char)
394 {
395 const ::com::sun::star::uno::Type & rElementType = cppu::UnoType<cppu::UnoCharType>::get();
396 ::typelib_static_sequence_type_init(
397 & s_pType_com_sun_star_uno_Sequence_Char,
398 rElementType.getTypeLibType() );
399 }
400 return * reinterpret_cast< const ::com::sun::star::uno::Type * >(
401 & s_pType_com_sun_star_uno_Sequence_Char );
402 }
403
404 #endif
405
406 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */