LibreOffice
LibreOffice 6.4 SDK C/C++ API Reference
Sequence.hxx
Go to the documentation of this file.
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 #include <cstddef>
26 #if defined LIBO_INTERNAL_ONLY
27 # include <type_traits>
28 # include <ostream>
29 #endif
30 
31 #include "osl/interlck.h"
34 #include "uno/data.h"
36 #include "cppu/unotype.hxx"
37 
38 namespace com
39 {
40 namespace sun
41 {
42 namespace star
43 {
44 namespace uno
45 {
46 
48 template< class E >
49 typelib_TypeDescriptionReference * Sequence< E >::s_pType = NULL;
51 
52 template< class E >
54 {
55  const Type & rType = ::cppu::getTypeFavourUnsigned( this );
57  &_pSequence, rType.getTypeLibType(),
58  0, 0, cpp_acquire );
59  // no bad_alloc, because empty sequence is statically allocated in cppu
60 }
61 
62 template< class E >
63 inline Sequence< E >::Sequence( const Sequence & rSeq )
64 {
65  osl_atomic_increment( &rSeq._pSequence->nRefCount );
66  _pSequence = rSeq._pSequence;
67 }
68 
69 template< class E >
71  uno_Sequence * pSequence, __sal_NoAcquire )
72  : _pSequence( pSequence )
73 {
74 }
75 
76 #if defined(__COVERITY__)
77 extern "C" void __coverity_tainted_data_sanitize__(void *);
78 #endif
79 
80 template< class E >
81 inline Sequence< E >::Sequence( const E * pElements, sal_Int32 len )
82 {
83  const Type & rType = ::cppu::getTypeFavourUnsigned( this );
84 
85 #if defined(__COVERITY__)
86  // cid#1448292 coverity has difficulty with css::uno::Sequence
87  __coverity_tainted_data_sanitize__(pElements);
88 #endif
89 
90  bool success =
92  &_pSequence, rType.getTypeLibType(),
93  const_cast< E * >( pElements ), len, cpp_acquire );
94  if (! success)
95  throw ::std::bad_alloc();
96 }
97 
98 template< class E >
99 inline Sequence< E >::Sequence( sal_Int32 len )
100 {
101  const Type & rType = ::cppu::getTypeFavourUnsigned( this );
102  bool success =
104  &_pSequence, rType.getTypeLibType(),
105  0, len, cpp_acquire );
106  if (! success)
107  throw ::std::bad_alloc();
108 }
109 
110 #if defined LIBO_INTERNAL_ONLY
111 template<typename E> Sequence<E>::Sequence(std::initializer_list<E> init) {
113  &_pSequence, cppu::getTypeFavourUnsigned(this).getTypeLibType(),
114  const_cast<E *>(init.begin()), init.size(), cpp_acquire))
115  {
116  throw std::bad_alloc();
117  }
118 }
119 #endif
120 
121 template< class E >
123 {
124  if (osl_atomic_decrement( &_pSequence->nRefCount ) == 0)
125  {
126  const Type & rType = ::cppu::getTypeFavourUnsigned( this );
128  _pSequence, rType.getTypeLibType(), cpp_release );
129  }
130 }
131 
132 template< class E >
134 {
135  const Type & rType = ::cppu::getTypeFavourUnsigned( this );
137  &_pSequence, rSeq._pSequence, rType.getTypeLibType(), cpp_release );
138  return *this;
139 }
140 
141 template< class E >
142 inline bool Sequence< E >::operator == ( const Sequence & rSeq ) const
143 {
144  if (_pSequence == rSeq._pSequence)
145  return true;
146  const Type & rType = ::cppu::getTypeFavourUnsigned( this );
148  const_cast< Sequence * >( this ), rType.getTypeLibType(),
149  const_cast< Sequence * >( &rSeq ), rType.getTypeLibType(),
151  cpp_release );
152 }
153 
154 template< class E >
155 inline bool Sequence< E >::operator != ( const Sequence & rSeq ) const
156 {
157  return (! operator == ( rSeq ));
158 }
159 
160 template< class E >
162 {
163  const Type & rType = ::cppu::getTypeFavourUnsigned( this );
164  bool success =
166  &_pSequence, rType.getTypeLibType(),
168  if (! success)
169  throw ::std::bad_alloc();
170  return reinterpret_cast< E * >( _pSequence->elements );
171 }
172 
173 template<class E> E * Sequence<E>::begin() { return getArray(); }
174 
175 template<class E> E const * Sequence<E>::begin() const
176 { return getConstArray(); }
177 
178 template<class E> E * Sequence<E>::end() { return begin() + getLength(); }
179 
180 template<class E> E const * Sequence<E>::end() const
181 { return begin() + getLength(); }
182 
183 template< class E >
184 inline E & Sequence< E >::operator [] ( sal_Int32 nIndex )
185 {
186  // silence spurious -Werror=strict-overflow warnings from GCC 4.8.2
187  assert(nIndex >= 0 && static_cast<sal_uInt32>(nIndex) < static_cast<sal_uInt32>(getLength()));
188  return getArray()[ nIndex ];
189 }
190 
191 template< class E >
192 inline const E & Sequence< E >::operator [] ( sal_Int32 nIndex ) const
193 {
194  // silence spurious -Werror=strict-overflow warnings from GCC 4.8.2
195  assert(nIndex >= 0 && static_cast<sal_uInt32>(nIndex) < static_cast<sal_uInt32>(getLength()));
196  return reinterpret_cast< const E * >( _pSequence->elements )[ nIndex ];
197 }
198 
199 template< class E >
200 inline void Sequence< E >::realloc( sal_Int32 nSize )
201 {
202  const Type & rType = ::cppu::getTypeFavourUnsigned( this );
203  bool success =
205  &_pSequence, rType.getTypeLibType(), nSize,
207  if (!success)
208  throw ::std::bad_alloc();
209 }
210 
211 inline ::com::sun::star::uno::Sequence< sal_Int8 > SAL_CALL toUnoSequence(
212  const ::rtl::ByteSequence & rByteSequence )
213 {
214  return * reinterpret_cast< const ::com::sun::star::uno::Sequence< sal_Int8 > * >( &rByteSequence );
215 }
216 
217 #if defined LIBO_INTERNAL_ONLY
218 
220 
221 namespace uno_detail {
222 
223 template< typename value_t, typename charT, typename traits >
224 void sequence_output_elems( std::basic_ostream<charT, traits> &os, const value_t *pAry, sal_Int32 nLen, std::true_type )
225 {
226  // for integral types, use hex notation
227  auto const flags = os.setf(std::ios_base::hex);
228  for(sal_Int32 i=0; i<nLen-1; ++i)
229  os << "0x" << *pAry++ << ", ";
230  if( nLen > 1 )
231  os << "0x" << *pAry++;
232  os.setf(flags);
233 }
234 
235 template< typename value_t, typename charT, typename traits >
236 void sequence_output_elems( std::basic_ostream<charT, traits> &os, const value_t *pAry, sal_Int32 nLen, std::false_type )
237 {
238  // every other type: rely on their own ostream operator<<
239  for(sal_Int32 i=0; i<nLen-1; ++i)
240  os << *pAry++ << ", ";
241  if( nLen > 1 )
242  os << *pAry++;
243 }
244 
245 template< typename value_t, typename charT, typename traits >
246 void sequence_output_bytes( std::basic_ostream<charT, traits> &os, const value_t *pAry, sal_Int32 nLen )
247 {
248  // special case bytes - ostream operator<< outputs those as char
249  // values, but we need raw ints here
250  auto const flags = os.setf(std::ios_base::hex);
251  for(sal_Int32 i=0; i<nLen-1; ++i)
252  os << "0x" << (0xFF & +*pAry++) << ", ";
253  if( nLen > 1 )
254  os << "0x" << (0xFF & +*pAry++);
255  os.setf(flags);
256 }
257 
258 }
259 
266 template< typename value_t, typename charT, typename traits >
267 inline std::basic_ostream<charT, traits> &operator<<(std::basic_ostream<charT, traits> &os, css::uno::Sequence<value_t> const& v)
268 {
269  const value_t *pAry = v.getConstArray();
270  sal_Int32 nLen = v.getLength();
271  if constexpr (std::is_same<sal_Int8, value_t>::value) {
272  uno_detail::sequence_output_bytes(os, pAry, nLen);
273  } else {
274  uno_detail::sequence_output_elems(os, pAry, nLen, std::is_integral<value_t>());
275  }
276  return os;
277 }
278 
280 
281 #endif
282 
283 }
284 }
285 }
286 }
287 
288 namespace cppu {
289 
290 template< typename T > inline ::com::sun::star::uno::Type const &
292  SAL_UNUSED_PARAMETER ::com::sun::star::uno::Sequence< T > const *)
293 {
298  static_cast<
299  typename ::com::sun::star::uno::Sequence< T >::ElementType * >(
300  0)).
301  getTypeLibType()));
302  }
305 }
306 
307 template< typename T > inline ::com::sun::star::uno::Type const &
309  SAL_UNUSED_PARAMETER ::com::sun::star::uno::Sequence< T > const *)
310 {
311  //TODO On certain platforms with weak memory models, the following code can
312  // result in some threads observing that td points to garbage:
313  static typelib_TypeDescriptionReference * td = NULL;
314  if (td == NULL) {
316  &td,
318  static_cast<
319  typename ::com::sun::star::uno::Sequence< T >::ElementType * >(
320  0)).
321  getTypeLibType()));
322  }
324 }
325 
326 }
327 
328 // generic sequence template
329 template< class E >
330 inline const ::com::sun::star::uno::Type &
331 SAL_CALL getCppuType(
332  SAL_UNUSED_PARAMETER const ::com::sun::star::uno::Sequence< E > * )
333 {
335  static_cast< ::com::sun::star::uno::Sequence< E > * >(0));
336 }
337 
338 // generic sequence template for given element type (e.g. C++ arrays)
339 template< class E >
340 inline const ::com::sun::star::uno::Type &
341 SAL_CALL getCppuSequenceType( const ::com::sun::star::uno::Type & rElementType )
342 {
344  {
347  rElementType.getTypeLibType() );
348  }
349  return * reinterpret_cast< const ::com::sun::star::uno::Type * >(
351 }
352 
353 // char sequence
354 inline const ::com::sun::star::uno::Type &
356 {
357  static typelib_TypeDescriptionReference * s_pType_com_sun_star_uno_Sequence_Char = NULL;
358  if (! s_pType_com_sun_star_uno_Sequence_Char)
359  {
360  const ::com::sun::star::uno::Type & rElementType = cppu::UnoType<cppu::UnoCharType>::get();
362  & s_pType_com_sun_star_uno_Sequence_Char,
363  rElementType.getTypeLibType() );
364  }
365  return * reinterpret_cast< const ::com::sun::star::uno::Type * >(
366  & s_pType_com_sun_star_uno_Sequence_Char );
367 }
368 
369 #endif
370 
371 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void cpp_release(void *pCppI)
Function to release a C++ interface.
Definition: genfunc.hxx:46
Sequence & operator=(const Sequence &rSeq)
Assignment operator: Acquires given sequence handle and releases previously set handle.
Definition: Sequence.hxx:133
static css::uno::Type const & get()
Definition: unotype.hxx:288
CPPU_DLLPUBLIC void uno_type_sequence_assign(uno_Sequence **ppDest, uno_Sequence *pSource, struct _typelib_TypeDescriptionReference *pType, uno_ReleaseFunc release) SAL_THROW_EXTERN_C()
Assigns a sequence.
typelib_TypeDescriptionReference * getTypeLibType() const
Gets the C typelib type description reference pointer.
Definition: Type.h:154
E * end()
This function allows to use Sequence in standard algorithms, like std::find and others.
Definition: Sequence.hxx:178
CPPU_DLLPUBLIC void uno_type_sequence_destroy(uno_Sequence *sequence, struct _typelib_TypeDescriptionReference *type, uno_ReleaseFunc release) SAL_THROW_EXTERN_C()
Destroy a sequence whose reference count has dropped to zero.
const ::com::sun::star::uno::Type & getCharSequenceCppuType()
Gets the meta type of IDL sequence< char >.
Definition: Sequence.hxx:355
const ::com::sun::star::uno::Type & getCppuType(SAL_UNUSED_PARAMETER const ::com::sun::star::uno::Sequence< E > *)
Definition: Sequence.hxx:331
This is the binary specification of a SAL sequence.
Definition: types.h:321
bool operator==(const Sequence &rSeq) const
Equality operator: Compares two sequences.
Definition: Sequence.hxx:142
Template C++ class representing an IDL sequence.
Definition: unotype.hxx:40
const ::com::sun::star::uno::Type & getCppuSequenceType(const ::com::sun::star::uno::Type &rElementType)
Gets the meta type of IDL sequence.
Definition: Sequence.hxx:341
struct SAL_DLLPUBLIC_RTTI _typelib_TypeDescriptionReference typelib_TypeDescriptionReference
Holds a weak reference to a type description.
void * cpp_queryInterface(void *pCppI, typelib_TypeDescriptionReference *pType)
Function to query for a C++ interface.
Definition: genfunc.hxx:51
#define SAL_UNUSED_PARAMETER
Annotate unused but required C++ function parameters.
Definition: types.h:559
CPPU_DLLPUBLIC sal_Bool uno_type_sequence_realloc(uno_Sequence **ppSequence, struct _typelib_TypeDescriptionReference *pType, sal_Int32 nSize, uno_AcquireFunc acquire, uno_ReleaseFunc release) SAL_THROW_EXTERN_C()
Reallocates length of a sequence.
CPPU_DLLPUBLIC sal_Bool uno_type_equalData(void *pVal1, struct _typelib_TypeDescriptionReference *pVal1Type, void *pVal2, struct _typelib_TypeDescriptionReference *pVal2Type, uno_QueryInterfaceFunc queryInterface, uno_ReleaseFunc release) SAL_THROW_EXTERN_C()
Tests if two values are equal.
::com::sun::star::uno::Type const & getTypeFavourChar(SAL_UNUSED_PARAMETER ::com::sun::star::uno::Sequence< T > const *)
Definition: Sequence.hxx:308
CPPU_DLLPUBLIC sal_Bool uno_type_sequence_reference2One(uno_Sequence **ppSequence, struct _typelib_TypeDescriptionReference *pType, uno_AcquireFunc acquire, uno_ReleaseFunc release) SAL_THROW_EXTERN_C()
Assures that the reference count of the given sequence is one.
CPPU_DLLPUBLIC sal_Bool uno_type_sequence_construct(uno_Sequence **ppSequence, struct _typelib_TypeDescriptionReference *pType, void *pElements, sal_Int32 len, uno_AcquireFunc acquire) SAL_THROW_EXTERN_C()
Constructs a new sequence with given elements.
Definition: types.h:377
css::uno::Type const & getTypeFromTypeDescriptionReference(::typelib_TypeDescriptionReference *const *tdr)
Definition: unotype.hxx:101
void cpp_acquire(void *pCppI)
Function to acquire a C++ interface.
Definition: genfunc.hxx:41
__sal_NoAcquire
Definition: types.h:370
E * begin()
This function allows to use Sequence in standard algorithms, like std::find and others.
Definition: Sequence.hxx:173
E & operator[](sal_Int32 nIndex)
Non-const index operator: Obtains a reference to element indexed at given position.
Definition: Sequence.hxx:184
sal_Int32 nRefCount
reference count of sequence
Definition: types.h:325
Definition: Enterable.hxx:26
Sequence()
Default constructor: Creates an empty sequence.
Definition: Sequence.hxx:53
CPPU_DLLPUBLIC void typelib_static_sequence_type_init(typelib_TypeDescriptionReference **ppRef, typelib_TypeDescriptionReference *pElementType) SAL_THROW_EXTERN_C()
Inits static sequence type reference.
inline ::com::sun::star::uno::Sequence< sal_Int8 > toUnoSequence(const ::rtl::ByteSequence &rByteSequence)
Creates a UNO byte sequence from a SAL byte sequence.
Definition: Sequence.hxx:211
::com::sun::star::uno::Type const & getTypeFavourUnsigned(SAL_UNUSED_PARAMETER ::com::sun::star::uno::Sequence< T > const *)
Definition: Sequence.hxx:291
void realloc(sal_Int32 nSize)
Reallocates sequence to new length.
Definition: Sequence.hxx:200
css::uno::Type const & getTypeFavourUnsigned(SAL_UNUSED_PARAMETER T const *)
A working replacement for getCppuType (see there).
Definition: unotype.hxx:320
E * getArray()
Gets a pointer to elements array for reading and writing.
Definition: Sequence.hxx:161
~Sequence()
Destructor: Releases sequence handle.
Definition: Sequence.hxx:122
bool operator!=(const Sequence &rSeq) const
Inequality operator: Compares two sequences.
Definition: Sequence.hxx:155
C++ class representing an IDL meta type.
Definition: Type.h:54