LibreOffice
LibreOffice 7.2 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 
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 #endif
34 
35 #include "osl/interlck.h"
38 #include "uno/data.h"
40 #include "cppu/unotype.hxx"
41 
42 namespace com
43 {
44 namespace sun
45 {
46 namespace star
47 {
48 namespace uno
49 {
50 
52 template< class E >
53 typelib_TypeDescriptionReference * Sequence< E >::s_pType = NULL;
55 
56 template< class E >
58 {
59  const Type & rType = ::cppu::getTypeFavourUnsigned( this );
61  &_pSequence, rType.getTypeLibType(),
62  NULL, 0, cpp_acquire );
63  // no bad_alloc, because empty sequence is statically allocated in cppu
64 }
65 
66 template< class E >
67 inline Sequence< E >::Sequence( const Sequence & rSeq )
68 {
69  osl_atomic_increment( &rSeq._pSequence->nRefCount );
70  _pSequence = rSeq._pSequence;
71 }
72 
73 template< class E >
75  uno_Sequence * pSequence, __sal_NoAcquire )
76  : _pSequence( pSequence )
77 {
78 }
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  bool success =
87  &_pSequence, rType.getTypeLibType(),
88  const_cast< E * >( pElements ), len, cpp_acquire );
89  if (! success)
90  throw ::std::bad_alloc();
91 }
92 
93 template< class E >
94 inline Sequence< E >::Sequence( sal_Int32 len )
95 {
96  const Type & rType = ::cppu::getTypeFavourUnsigned( this );
97  bool success =
99  &_pSequence, rType.getTypeLibType(),
100  NULL, len, cpp_acquire );
101  if (! success)
102  throw ::std::bad_alloc();
103 }
104 
105 #if defined LIBO_INTERNAL_ONLY
106 template<typename E> Sequence<E>::Sequence(std::initializer_list<E> init) {
108  &_pSequence, cppu::getTypeFavourUnsigned(this).getTypeLibType(),
109  const_cast<E *>(init.begin()), init.size(), cpp_acquire))
110  {
111  throw std::bad_alloc();
112  }
113 }
114 #endif
115 
116 template< class E >
118 {
119  if (osl_atomic_decrement( &_pSequence->nRefCount ) == 0)
120  {
121  const Type & rType = ::cppu::getTypeFavourUnsigned( this );
123  _pSequence, rType.getTypeLibType(), cpp_release );
124  }
125 }
126 
127 template< class E >
129 {
130  const Type & rType = ::cppu::getTypeFavourUnsigned( this );
132  &_pSequence, rSeq._pSequence, rType.getTypeLibType(), cpp_release );
133  return *this;
134 }
135 
136 template< class E >
137 inline bool Sequence< E >::operator == ( const Sequence & rSeq ) const
138 {
139  if (_pSequence == rSeq._pSequence)
140  return true;
141  const Type & rType = ::cppu::getTypeFavourUnsigned( this );
143  const_cast< Sequence * >( this ), rType.getTypeLibType(),
144  const_cast< Sequence * >( &rSeq ), rType.getTypeLibType(),
146  cpp_release );
147 }
148 
149 template< class E >
150 inline bool Sequence< E >::operator != ( const Sequence & rSeq ) const
151 {
152  return (! operator == ( rSeq ));
153 }
154 
155 template< class E >
157 {
158  const Type & rType = ::cppu::getTypeFavourUnsigned( this );
159  bool success =
161  &_pSequence, rType.getTypeLibType(),
163  if (! success)
164  throw ::std::bad_alloc();
165  return reinterpret_cast< E * >( _pSequence->elements );
166 }
167 
168 template<class E> E * Sequence<E>::begin() { return getArray(); }
169 
170 template<class E> E const * Sequence<E>::begin() const
171 { return getConstArray(); }
172 
173 template<class E> E * Sequence<E>::end() { return begin() + getLength(); }
174 
175 template<class E> E const * Sequence<E>::end() const
176 { return begin() + getLength(); }
177 
178 template< class E >
179 inline E & Sequence< E >::operator [] ( sal_Int32 nIndex )
180 {
181  // silence spurious -Werror=strict-overflow warnings from GCC 4.8.2
182  assert(nIndex >= 0 && static_cast<sal_uInt32>(nIndex) < static_cast<sal_uInt32>(getLength()));
183  return getArray()[ nIndex ];
184 }
185 
186 template< class E >
187 inline const E & Sequence< E >::operator [] ( sal_Int32 nIndex ) const
188 {
189  // silence spurious -Werror=strict-overflow warnings from GCC 4.8.2
190  assert(nIndex >= 0 && static_cast<sal_uInt32>(nIndex) < static_cast<sal_uInt32>(getLength()));
191  return reinterpret_cast< const E * >( _pSequence->elements )[ nIndex ];
192 }
193 
194 template< class E >
195 inline void Sequence< E >::realloc( sal_Int32 nSize )
196 {
197  const Type & rType = ::cppu::getTypeFavourUnsigned( this );
198  bool success =
200  &_pSequence, rType.getTypeLibType(), nSize,
202  if (!success)
203  throw ::std::bad_alloc();
204 }
205 
206 inline ::com::sun::star::uno::Sequence< sal_Int8 > SAL_CALL toUnoSequence(
207  const ::rtl::ByteSequence & rByteSequence )
208 {
209  return * reinterpret_cast< const ::com::sun::star::uno::Sequence< sal_Int8 > * >( &rByteSequence );
210 }
211 
212 #if defined LIBO_INTERNAL_ONLY
213 
215 
216 namespace uno_detail {
217 
218 template< typename value_t, typename charT, typename traits >
219 void sequence_output_elems( std::basic_ostream<charT, traits> &os, const value_t *pAry, sal_Int32 nLen, std::true_type )
220 {
221  // for integral types, use hex notation
222  auto const flags = os.setf(std::ios_base::hex);
223  for(sal_Int32 i=0; i<nLen-1; ++i)
224  os << "0x" << *pAry++ << ", ";
225  if( nLen > 1 )
226  os << "0x" << *pAry++;
227  os.setf(flags);
228 }
229 
230 template< typename value_t, typename charT, typename traits >
231 void sequence_output_elems( std::basic_ostream<charT, traits> &os, const value_t *pAry, sal_Int32 nLen, std::false_type )
232 {
233  // every other type: rely on their own ostream operator<<
234  for(sal_Int32 i=0; i<nLen-1; ++i)
235  os << *pAry++ << ", ";
236  if( nLen > 1 )
237  os << *pAry++;
238 }
239 
240 template< typename value_t, typename charT, typename traits >
241 void sequence_output_bytes( std::basic_ostream<charT, traits> &os, const value_t *pAry, sal_Int32 nLen )
242 {
243  // special case bytes - ostream operator<< outputs those as char
244  // values, but we need raw ints here
245  auto const flags = os.setf(std::ios_base::hex);
246  for(sal_Int32 i=0; i<nLen-1; ++i)
247  os << "0x" << (0xFF & +*pAry++) << ", ";
248  if( nLen > 1 )
249  os << "0x" << (0xFF & +*pAry++);
250  os.setf(flags);
251 }
252 
253 }
254 
261 template< typename value_t, typename charT, typename traits >
262 inline std::basic_ostream<charT, traits> &operator<<(std::basic_ostream<charT, traits> &os, css::uno::Sequence<value_t> const& v)
263 {
264  const value_t *pAry = v.getConstArray();
265  sal_Int32 nLen = v.getLength();
266  if constexpr (std::is_same<sal_Int8, value_t>::value) {
267  uno_detail::sequence_output_bytes(os, pAry, nLen);
268  } else {
269  uno_detail::sequence_output_elems(os, pAry, nLen, std::is_integral<value_t>());
270  }
271  return os;
272 }
273 
275 
276 #endif
277 
278 }
279 }
280 }
281 }
282 
283 namespace cppu {
284 
285 template< typename T > inline ::com::sun::star::uno::Type const &
287  SAL_UNUSED_PARAMETER ::com::sun::star::uno::Sequence< T > const *)
288 {
293  static_cast<
294  typename ::com::sun::star::uno::Sequence< T >::ElementType * >(
295  0)).
296  getTypeLibType()));
297  }
300 }
301 
302 template< typename T > inline ::com::sun::star::uno::Type const &
304  SAL_UNUSED_PARAMETER ::com::sun::star::uno::Sequence< T > const *)
305 {
306  //TODO On certain platforms with weak memory models, the following code can
307  // result in some threads observing that td points to garbage:
308  static typelib_TypeDescriptionReference * td = NULL;
309  if (td == NULL) {
311  &td,
313  static_cast<
314  typename ::com::sun::star::uno::Sequence< T >::ElementType * >(
315  0)).
316  getTypeLibType()));
317  }
319 }
320 
321 }
322 
323 // generic sequence template
324 template< class E >
325 inline const ::com::sun::star::uno::Type &
326 SAL_CALL getCppuType(
327  SAL_UNUSED_PARAMETER const ::com::sun::star::uno::Sequence< E > * )
328 {
330  static_cast< ::com::sun::star::uno::Sequence< E > * >(0));
331 }
332 
333 // generic sequence template for given element type (e.g. C++ arrays)
334 template< class E >
335 inline const ::com::sun::star::uno::Type &
336 SAL_CALL getCppuSequenceType( const ::com::sun::star::uno::Type & rElementType )
337 {
339  {
342  rElementType.getTypeLibType() );
343  }
344  return * reinterpret_cast< const ::com::sun::star::uno::Type * >(
346 }
347 
348 // char sequence
349 inline const ::com::sun::star::uno::Type &
351 {
352  static typelib_TypeDescriptionReference * s_pType_com_sun_star_uno_Sequence_Char = NULL;
353  if (! s_pType_com_sun_star_uno_Sequence_Char)
354  {
355  const ::com::sun::star::uno::Type & rElementType = cppu::UnoType<cppu::UnoCharType>::get();
357  & s_pType_com_sun_star_uno_Sequence_Char,
358  rElementType.getTypeLibType() );
359  }
360  return * reinterpret_cast< const ::com::sun::star::uno::Type * >(
361  & s_pType_com_sun_star_uno_Sequence_Char );
362 }
363 
364 #endif
365 
366 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void cpp_release(void *pCppI)
Function to release a C++ interface.
Definition: genfunc.hxx:50
E * begin()
This function allows to use Sequence in standard algorithms, like std::find and others.
Definition: Sequence.hxx:168
~Sequence()
Destructor: Releases sequence handle.
Definition: Sequence.hxx:117
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.
static css::uno::Type const & get()
Definition: unotype.hxx:292
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.
Template C++ class representing an IDL sequence.
Definition: unotype.hxx:44
const ::com::sun::star::uno::Type & getCharSequenceCppuType()
Gets the meta type of IDL sequence< char >.
Definition: Sequence.hxx:350
const ::com::sun::star::uno::Type & getCppuType(SAL_UNUSED_PARAMETER const ::com::sun::star::uno::Sequence< E > *)
Definition: Sequence.hxx:326
typelib_TypeDescriptionReference * getTypeLibType() const
Gets the C typelib type description reference pointer.
Definition: Type.h:158
This is the binary specification of a SAL sequence.
Definition: types.h:303
const ::com::sun::star::uno::Type & getCppuSequenceType(const ::com::sun::star::uno::Type &rElementType)
Gets the meta type of IDL sequence.
Definition: Sequence.hxx:336
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:55
void realloc(sal_Int32 nSize)
Reallocates sequence to new length.
Definition: Sequence.hxx:195
#define SAL_UNUSED_PARAMETER
Annotate unused but required C++ function parameters.
Definition: types.h:568
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.
bool operator==(const Sequence &rSeq) const
Equality operator: Compares two sequences.
Definition: Sequence.hxx:137
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:303
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:359
css::uno::Type const & getTypeFromTypeDescriptionReference(::typelib_TypeDescriptionReference *const *tdr)
Definition: unotype.hxx:105
void cpp_acquire(void *pCppI)
Function to acquire a C++ interface.
Definition: genfunc.hxx:45
__sal_NoAcquire
Definition: types.h:352
sal_Int32 nRefCount
reference count of sequence
Definition: types.h:307
Definition: Enterable.hxx:30
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:206
Sequence()
Default constructor: Creates an empty sequence.
Definition: Sequence.hxx:57
::com::sun::star::uno::Type const & getTypeFavourUnsigned(SAL_UNUSED_PARAMETER ::com::sun::star::uno::Sequence< T > const *)
Definition: Sequence.hxx:286
E * getArray()
Gets a pointer to elements array for reading and writing.
Definition: Sequence.hxx:156
E * end()
This function allows to use Sequence in standard algorithms, like std::find and others.
Definition: Sequence.hxx:173
bool operator!=(const Sequence &rSeq) const
Inequality operator: Compares two sequences.
Definition: Sequence.hxx:150
E & operator[](sal_Int32 nIndex)
Non-const index operator: Obtains a reference to element indexed at given position.
Definition: Sequence.hxx:179
css::uno::Type const & getTypeFavourUnsigned(SAL_UNUSED_PARAMETER T const *)
A working replacement for getCppuType (see there).
Definition: unotype.hxx:324
C++ class representing an IDL meta type.
Definition: Type.h:58
Sequence & operator=(const Sequence &rSeq)
Assignment operator: Acquires given sequence handle and releases previously set handle.
Definition: Sequence.hxx:128