LibreOffice
LibreOffice 7.3 SDK C/C++ API Reference
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 # include <utility>
34 #endif
35 
36 #include "osl/interlck.h"
39 #include "uno/data.h"
41 #include "cppu/unotype.hxx"
42 
43 namespace com
44 {
45 namespace sun
46 {
47 namespace star
48 {
49 namespace uno
50 {
51 
53 template< class E >
54 typelib_TypeDescriptionReference * Sequence< E >::s_pType = NULL;
56 
57 template< class E >
59 {
60  const Type & rType = ::cppu::getTypeFavourUnsigned( this );
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 >
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( this );
85 
86  bool success =
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( this );
98  bool success =
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) {
109  &_pSequence, cppu::getTypeFavourUnsigned(this).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 >
119 {
120  if (osl_atomic_decrement( &_pSequence->nRefCount ) == 0)
121  {
122  const Type & rType = ::cppu::getTypeFavourUnsigned( this );
124  _pSequence, rType.getTypeLibType(), cpp_release );
125  }
126 }
127 
128 template< class E >
130 {
131  const Type & rType = ::cppu::getTypeFavourUnsigned( this );
133  &_pSequence, rSeq._pSequence, rType.getTypeLibType(), cpp_release );
134  return *this;
135 }
136 
137 template< class E >
138 inline bool Sequence< E >::operator == ( const Sequence & rSeq ) const
139 {
140  if (_pSequence == rSeq._pSequence)
141  return true;
142  const Type & rType = ::cppu::getTypeFavourUnsigned( this );
144  const_cast< Sequence * >( this ), rType.getTypeLibType(),
145  const_cast< Sequence * >( &rSeq ), rType.getTypeLibType(),
147  cpp_release );
148 }
149 
150 template< class E >
151 inline bool Sequence< E >::operator != ( const Sequence & rSeq ) const
152 {
153  return (! operator == ( rSeq ));
154 }
155 
156 template< class E >
158 {
159  const Type & rType = ::cppu::getTypeFavourUnsigned( this );
160  bool success =
162  &_pSequence, rType.getTypeLibType(),
164  if (! success)
165  throw ::std::bad_alloc();
166  return reinterpret_cast< E * >( _pSequence->elements );
167 }
168 
169 #if !defined LIBO_INTERNAL_ONLY
170 template<class E> E * Sequence<E>::begin() { return getArray(); }
171 #endif
172 
173 template<class E> E const * Sequence<E>::begin() const
174 { return getConstArray(); }
175 
176 #if !defined LIBO_INTERNAL_ONLY
177 template<class E> E * Sequence<E>::end() { return begin() + getLength(); }
178 #endif
179 
180 template<class E> E const * Sequence<E>::end() const
181 { return begin() + getLength(); }
182 
183 #if !defined LIBO_INTERNAL_ONLY
184 template< class E >
185 inline E & Sequence< E >::operator [] ( sal_Int32 nIndex )
186 {
187  // silence spurious -Werror=strict-overflow warnings from GCC 4.8.2
188  assert(nIndex >= 0 && static_cast<sal_uInt32>(nIndex) < static_cast<sal_uInt32>(getLength()));
189  return getArray()[ nIndex ];
190 }
191 #endif
192 
193 template< class E >
194 inline const E & Sequence< E >::operator [] ( sal_Int32 nIndex ) const
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 reinterpret_cast< const E * >( _pSequence->elements )[ nIndex ];
199 }
200 
201 template< class E >
202 inline void Sequence< E >::realloc( sal_Int32 nSize )
203 {
204  const Type & rType = ::cppu::getTypeFavourUnsigned( this );
205  bool success =
207  &_pSequence, rType.getTypeLibType(), nSize,
209  if (!success)
210  throw ::std::bad_alloc();
211 }
212 
213 #if defined LIBO_INTERNAL_ONLY
214 template <class E> inline void Sequence<E>::swap(Sequence& other)
215 {
216  std::swap(_pSequence, other._pSequence);
217 }
218 #endif
219 
220 inline ::com::sun::star::uno::Sequence< sal_Int8 > SAL_CALL toUnoSequence(
221  const ::rtl::ByteSequence & rByteSequence )
222 {
223  return * reinterpret_cast< const ::com::sun::star::uno::Sequence< sal_Int8 > * >( &rByteSequence );
224 }
225 
226 #if defined LIBO_INTERNAL_ONLY
227 
229 
230 namespace uno_detail {
231 
232 template< typename value_t, typename charT, typename traits >
233 void sequence_output_elems( std::basic_ostream<charT, traits> &os, const value_t *pAry, sal_Int32 nLen, std::true_type )
234 {
235  // for integral types, use hex notation
236  auto const flags = os.setf(std::ios_base::hex);
237  for(sal_Int32 i=0; i<nLen-1; ++i)
238  os << "0x" << *pAry++ << ", ";
239  if( nLen > 1 )
240  os << "0x" << *pAry++;
241  os.setf(flags);
242 }
243 
244 template< typename value_t, typename charT, typename traits >
245 void sequence_output_elems( std::basic_ostream<charT, traits> &os, const value_t *pAry, sal_Int32 nLen, std::false_type )
246 {
247  // every other type: rely on their own ostream operator<<
248  for(sal_Int32 i=0; i<nLen-1; ++i)
249  os << *pAry++ << ", ";
250  if( nLen > 1 )
251  os << *pAry++;
252 }
253 
254 template< typename value_t, typename charT, typename traits >
255 void sequence_output_bytes( std::basic_ostream<charT, traits> &os, const value_t *pAry, sal_Int32 nLen )
256 {
257  // special case bytes - ostream operator<< outputs those as char
258  // values, but we need raw ints here
259  auto const flags = os.setf(std::ios_base::hex);
260  for(sal_Int32 i=0; i<nLen-1; ++i)
261  os << "0x" << (0xFF & +*pAry++) << ", ";
262  if( nLen > 1 )
263  os << "0x" << (0xFF & +*pAry++);
264  os.setf(flags);
265 }
266 
267 }
268 
275 template< typename value_t, typename charT, typename traits >
276 inline std::basic_ostream<charT, traits> &operator<<(std::basic_ostream<charT, traits> &os, css::uno::Sequence<value_t> const& v)
277 {
278  const value_t *pAry = v.getConstArray();
279  sal_Int32 nLen = v.getLength();
280  if constexpr (std::is_same<sal_Int8, value_t>::value) {
281  uno_detail::sequence_output_bytes(os, pAry, nLen);
282  } else {
283  uno_detail::sequence_output_elems(os, pAry, nLen, std::is_integral<value_t>());
284  }
285  return os;
286 }
287 
288 template <class E> inline auto asNonConstRange(css::uno::Sequence<E>& s)
289 {
290  // Two iterators [begin, end] representing the non-const range of the Sequence.
291  // It only calls Sequence::getArray once, to avoid the second COW overhead when
292  // Sequence::begin() and Sequence::end() are called in pairs.
293  // Inheriting from pair allows to use std::tie to unpack the two iterators.
294  struct SequenceRange : public std::pair<E*, E*>
295  {
296  SequenceRange(E* ptr, sal_Int32 len) : std::pair<E*, E*>(ptr, ptr + len) {}
297  // These allow to pass it as range-expression to range-based for loops
298  E* begin() { return std::pair<E*, E*>::first; }
299  E* end() { return std::pair<E*, E*>::second; }
300  E& operator[](sal_Int32 i) { assert(i >= 0 && i < end() - begin()); return begin()[i]; }
301  };
302  return SequenceRange(s.getLength() ? s.getArray() : nullptr, s.getLength());
303 };
304 
306 
307 #endif
308 
309 }
310 }
311 }
312 }
313 
314 namespace cppu {
315 
316 template< typename T > inline ::com::sun::star::uno::Type const &
318  SAL_UNUSED_PARAMETER ::com::sun::star::uno::Sequence< T > const *)
319 {
324  static_cast<
325  typename ::com::sun::star::uno::Sequence< T >::ElementType * >(
326  0)).
327  getTypeLibType()));
328  }
331 }
332 
333 template< typename T > inline ::com::sun::star::uno::Type const &
335  SAL_UNUSED_PARAMETER ::com::sun::star::uno::Sequence< T > const *)
336 {
337  //TODO On certain platforms with weak memory models, the following code can
338  // result in some threads observing that td points to garbage:
339  static typelib_TypeDescriptionReference * td = NULL;
340  if (td == NULL) {
342  &td,
344  static_cast<
345  typename ::com::sun::star::uno::Sequence< T >::ElementType * >(
346  0)).
347  getTypeLibType()));
348  }
350 }
351 
352 }
353 
354 // generic sequence template
355 template< class E >
356 inline const ::com::sun::star::uno::Type &
357 SAL_CALL getCppuType(
358  SAL_UNUSED_PARAMETER const ::com::sun::star::uno::Sequence< E > * )
359 {
361  static_cast< ::com::sun::star::uno::Sequence< E > * >(0));
362 }
363 
364 // generic sequence template for given element type (e.g. C++ arrays)
365 template< class E >
366 inline const ::com::sun::star::uno::Type &
367 SAL_CALL getCppuSequenceType( const ::com::sun::star::uno::Type & rElementType )
368 {
370  {
373  rElementType.getTypeLibType() );
374  }
375  return * reinterpret_cast< const ::com::sun::star::uno::Type * >(
377 }
378 
379 // char sequence
380 inline const ::com::sun::star::uno::Type &
382 {
383  static typelib_TypeDescriptionReference * s_pType_com_sun_star_uno_Sequence_Char = NULL;
384  if (! s_pType_com_sun_star_uno_Sequence_Char)
385  {
386  const ::com::sun::star::uno::Type & rElementType = cppu::UnoType<cppu::UnoCharType>::get();
388  & s_pType_com_sun_star_uno_Sequence_Char,
389  rElementType.getTypeLibType() );
390  }
391  return * reinterpret_cast< const ::com::sun::star::uno::Type * >(
392  & s_pType_com_sun_star_uno_Sequence_Char );
393 }
394 
395 #endif
396 
397 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const ::com::sun::star::uno::Type & getCppuType(SAL_UNUSED_PARAMETER const ::com::sun::star::uno::Any *)
Gets the meta type of IDL type any.
Definition: Any.h:483
E & operator[](sal_Int32 nIndex)
Non-const index operator: Obtains a reference to element indexed at given position.
Definition: Sequence.hxx:185
bool operator!=(const Sequence &rSeq) const
Inequality operator: Compares two sequences.
Definition: Sequence.hxx:151
css::uno::Type const & getTypeFavourUnsigned(SAL_UNUSED_PARAMETER T const *)
A working replacement for getCppuType (see there).
Definition: unotype.hxx:324
static css::uno::Type const & get()
Definition: unotype.hxx:292
Template C++ class representing an IDL sequence.
Definition: unotype.hxx:44
typelib_TypeDescriptionReference * getTypeLibType() const
Gets the C typelib type description reference pointer.
Definition: Type.h:162
const ::com::sun::star::uno::Type & getCppuSequenceType(const ::com::sun::star::uno::Type &rElementType)
Gets the meta type of IDL sequence.
Definition: Sequence.hxx:367
css::uno::Type const & getTypeFromTypeDescriptionReference(::typelib_TypeDescriptionReference *const *tdr)
Definition: unotype.hxx:105
#define SAL_UNUSED_PARAMETER
Annotate unused but required C++ function parameters.
Definition: types.h:568
const ::com::sun::star::uno::Type & getCharSequenceCppuType()
Gets the meta type of IDL sequence&lt; char &gt;.
Definition: Sequence.hxx:381
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.
void * cpp_queryInterface(void *pCppI, typelib_TypeDescriptionReference *pType)
Function to query for a C++ interface.
Definition: genfunc.hxx:55
Sequence & operator=(const Sequence &rSeq)
Assignment operator: Acquires given sequence handle and releases previously set handle.
Definition: Sequence.hxx:129
E * end()
This function allows to use Sequence in standard algorithms, like std::find and others.
Definition: Sequence.hxx:177
void cpp_acquire(void *pCppI)
Function to acquire a C++ interface.
Definition: genfunc.hxx:45
~Sequence()
Destructor: Releases sequence handle.
Definition: Sequence.hxx:118
C++ class representing an IDL meta type.
Definition: Type.h:58
E * begin()
This function allows to use Sequence in standard algorithms, like std::find and others.
Definition: Sequence.hxx:170
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:220
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.
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.
void realloc(sal_Int32 nSize)
Reallocates sequence to new length.
Definition: Sequence.hxx:202
void cpp_release(void *pCppI)
Function to release a C++ interface.
Definition: genfunc.hxx:50
This is the binary specification of a SAL sequence.
Definition: types.h:303
sal_Int32 nRefCount
reference count of sequence
Definition: types.h:307
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.
Sequence()
Default constructor: Creates an empty sequence.
Definition: Sequence.hxx:58
struct SAL_DLLPUBLIC_RTTI _typelib_TypeDescriptionReference typelib_TypeDescriptionReference
Holds a weak reference to a type description.
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.
::com::sun::star::uno::Type const & getTypeFavourChar(SAL_UNUSED_PARAMETER::com::sun::star::uno::Sequence< T > const *)
Definition: Sequence.hxx:334
__sal_NoAcquire
Definition: types.h:352
CPPU_DLLPUBLIC void typelib_static_sequence_type_init(typelib_TypeDescriptionReference **ppRef, typelib_TypeDescriptionReference *pElementType) SAL_THROW_EXTERN_C()
Inits static sequence type reference.
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.
E * getArray()
Gets a pointer to elements array for reading and writing.
Definition: Sequence.hxx:157
bool operator==(const Sequence &rSeq) const
Equality operator: Compares two sequences.
Definition: Sequence.hxx:138