AlterOffice
AlterOffice 3.4 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 #ifndef INCLUDED_COM_SUN_STAR_UNO_SEQUENCE_HXX
4 #define INCLUDED_COM_SUN_STAR_UNO_SEQUENCE_HXX
5 
6 #include "sal/config.h"
7 
8 #include <cassert>
9 #include <cstddef>
10 #if defined LIBO_INTERNAL_ONLY
11 # include <type_traits>
12 # include <ostream>
13 # include <utility>
14 #endif
15 
16 #include "osl/interlck.h"
19 #include "uno/data.h"
21 #include "cppu/unotype.hxx"
22 
23 namespace com
24 {
25 namespace sun
26 {
27 namespace star
28 {
29 namespace uno
30 {
31 
33 template< class E >
34 typelib_TypeDescriptionReference * Sequence< E >::s_pType = NULL;
36 
37 template< class E >
39 {
40  const Type & rType = ::cppu::getTypeFavourUnsigned( this );
42  &_pSequence, rType.getTypeLibType(),
43  NULL, 0, cpp_acquire );
44  // no bad_alloc, because empty sequence is statically allocated in cppu
45 }
46 
47 template< class E >
48 inline Sequence< E >::Sequence( const Sequence & rSeq )
49 {
50  osl_atomic_increment( &rSeq._pSequence->nRefCount );
51  _pSequence = rSeq._pSequence;
52 }
53 
54 template< class E >
56  uno_Sequence * pSequence, __sal_NoAcquire )
57  : _pSequence( pSequence )
58 {
59 }
60 
61 template< class E >
62 inline Sequence< E >::Sequence( const E * pElements, sal_Int32 len )
63 {
64  const Type & rType = ::cppu::getTypeFavourUnsigned( this );
65 
66  bool success =
68  &_pSequence, rType.getTypeLibType(),
69  const_cast< E * >( pElements ), len, cpp_acquire );
70  if (! success)
71  throw ::std::bad_alloc();
72 }
73 
74 template< class E >
75 inline Sequence< E >::Sequence( sal_Int32 len )
76 {
77  const Type & rType = ::cppu::getTypeFavourUnsigned( this );
78  bool success =
80  &_pSequence, rType.getTypeLibType(),
81  NULL, len, cpp_acquire );
82  if (! success)
83  throw ::std::bad_alloc();
84 }
85 
86 #if defined LIBO_INTERNAL_ONLY
87 template<typename E> Sequence<E>::Sequence(std::initializer_list<E> init) {
89  &_pSequence, cppu::getTypeFavourUnsigned(this).getTypeLibType(),
90  const_cast<E *>(init.begin()), init.size(), cpp_acquire))
91  {
92  throw std::bad_alloc();
93  }
94 }
95 #endif
96 
97 template< class E >
99 {
100  if (osl_atomic_decrement( &_pSequence->nRefCount ) == 0)
101  {
102  const Type & rType = ::cppu::getTypeFavourUnsigned( this );
104  _pSequence, rType.getTypeLibType(), cpp_release );
105  }
106 }
107 
108 template< class E >
110 {
111  const Type & rType = ::cppu::getTypeFavourUnsigned( this );
113  &_pSequence, rSeq._pSequence, rType.getTypeLibType(), cpp_release );
114  return *this;
115 }
116 
117 #if defined LIBO_INTERNAL_ONLY
118 template<typename E> Sequence<E> & Sequence<E>::operator =(Sequence && other) {
119  std::swap(_pSequence, other._pSequence);
120  return *this;
121 }
122 #endif
123 
124 template< class E >
125 inline bool Sequence< E >::operator == ( const Sequence & rSeq ) const
126 {
127  if (_pSequence == rSeq._pSequence)
128  return true;
129  if (_pSequence->nElements != rSeq._pSequence->nElements)
130  return false;
131  const Type & rType = ::cppu::getTypeFavourUnsigned( this );
133  const_cast< Sequence * >( this ), rType.getTypeLibType(),
134  const_cast< Sequence * >( &rSeq ), rType.getTypeLibType(),
136  cpp_release );
137 }
138 
139 template< class E >
140 inline bool Sequence< E >::operator != ( const Sequence & rSeq ) const
141 {
142  return (! operator == ( rSeq ));
143 }
144 
145 template< class E >
147 {
148  const Type & rType = ::cppu::getTypeFavourUnsigned( this );
149  bool success =
151  &_pSequence, rType.getTypeLibType(),
153  if (! success)
154  throw ::std::bad_alloc();
155  return reinterpret_cast< E * >( _pSequence->elements );
156 }
157 
158 #if !defined LIBO_INTERNAL_ONLY
159 template<class E> E * Sequence<E>::begin() { return getArray(); }
160 #endif
161 
162 template<class E> E const * Sequence<E>::begin() const
163 { return getConstArray(); }
164 
165 #if !defined LIBO_INTERNAL_ONLY
166 template<class E> E * Sequence<E>::end() { return begin() + getLength(); }
167 #endif
168 
169 template<class E> E const * Sequence<E>::end() const
170 { return begin() + getLength(); }
171 
172 #if !defined LIBO_INTERNAL_ONLY
173 template< class E >
174 inline E & Sequence< E >::operator [] ( sal_Int32 nIndex )
175 {
176  // silence spurious -Werror=strict-overflow warnings from GCC 4.8.2
177  assert(nIndex >= 0 && static_cast<sal_uInt32>(nIndex) < static_cast<sal_uInt32>(getLength()));
178  return getArray()[ nIndex ];
179 }
180 #endif
181 
182 template< class E >
183 inline const E & Sequence< E >::operator [] ( sal_Int32 nIndex ) const
184 {
185  // silence spurious -Werror=strict-overflow warnings from GCC 4.8.2
186  assert(nIndex >= 0 && static_cast<sal_uInt32>(nIndex) < static_cast<sal_uInt32>(getLength()));
187  return reinterpret_cast< const E * >( _pSequence->elements )[ nIndex ];
188 }
189 
190 template< class E >
191 inline void Sequence< E >::realloc( sal_Int32 nSize )
192 {
193  const Type & rType = ::cppu::getTypeFavourUnsigned( this );
194  bool success =
196  &_pSequence, rType.getTypeLibType(), nSize,
198  if (!success)
199  throw ::std::bad_alloc();
200 }
201 
202 #if defined LIBO_INTERNAL_ONLY
203 template <class E> inline void Sequence<E>::swap(Sequence& other)
204 {
205  std::swap(_pSequence, other._pSequence);
206 }
207 #endif
208 
209 inline ::com::sun::star::uno::Sequence< sal_Int8 > SAL_CALL toUnoSequence(
210  const ::rtl::ByteSequence & rByteSequence )
211 {
212  return * reinterpret_cast< const ::com::sun::star::uno::Sequence< sal_Int8 > * >( &rByteSequence );
213 }
214 
215 #if defined LIBO_INTERNAL_ONLY
216 
218 
219 namespace uno_detail {
220 
221 template< typename value_t, typename charT, typename traits >
222 void sequence_output_elems( std::basic_ostream<charT, traits> &os, const value_t *pAry, sal_Int32 nLen, std::true_type )
223 {
224  // for integral types, use hex notation
225  auto const flags = os.setf(std::ios_base::hex);
226  for(sal_Int32 i=0; i<nLen-1; ++i)
227  os << "0x" << *pAry++ << ", ";
228  if( nLen > 1 )
229  os << "0x" << *pAry++;
230  os.setf(flags);
231 }
232 
233 template< typename value_t, typename charT, typename traits >
234 void sequence_output_elems( std::basic_ostream<charT, traits> &os, const value_t *pAry, sal_Int32 nLen, std::false_type )
235 {
236  // every other type: rely on their own ostream operator<<
237  for(sal_Int32 i=0; i<nLen-1; ++i)
238  os << *pAry++ << ", ";
239  if( nLen > 1 )
240  os << *pAry++;
241 }
242 
243 template< typename value_t, typename charT, typename traits >
244 void sequence_output_bytes( std::basic_ostream<charT, traits> &os, const value_t *pAry, sal_Int32 nLen )
245 {
246  // special case bytes - ostream operator<< outputs those as char
247  // values, but we need raw ints here
248  auto const flags = os.setf(std::ios_base::hex);
249  for(sal_Int32 i=0; i<nLen-1; ++i)
250  os << "0x" << (0xFF & +*pAry++) << ", ";
251  if( nLen > 1 )
252  os << "0x" << (0xFF & +*pAry++);
253  os.setf(flags);
254 }
255 
256 }
257 
264 template< typename value_t, typename charT, typename traits >
265 inline std::basic_ostream<charT, traits> &operator<<(std::basic_ostream<charT, traits> &os, css::uno::Sequence<value_t> const& v)
266 {
267  const value_t *pAry = v.getConstArray();
268  sal_Int32 nLen = v.getLength();
269  if constexpr (std::is_same<sal_Int8, value_t>::value) {
270  uno_detail::sequence_output_bytes(os, pAry, nLen);
271  } else {
272  uno_detail::sequence_output_elems(os, pAry, nLen, std::is_integral<value_t>());
273  }
274  return os;
275 }
276 
277 template <class E> inline auto asNonConstRange(css::uno::Sequence<E>& s)
278 {
279  // Two iterators [begin, end] representing the non-const range of the Sequence.
280  // It only calls Sequence::getArray once, to avoid the second COW overhead when
281  // Sequence::begin() and Sequence::end() are called in pairs.
282  // Inheriting from pair allows to use std::tie to unpack the two iterators.
283  struct SequenceRange : public std::pair<E*, E*>
284  {
285  SequenceRange(E* ptr, sal_Int32 len) : std::pair<E*, E*>(ptr, ptr + len) {}
286  // These allow to pass it as range-expression to range-based for loops
287  E* begin() { return std::pair<E*, E*>::first; }
288  E* end() { return std::pair<E*, E*>::second; }
289  E& operator[](sal_Int32 i) { assert(i >= 0 && i < end() - begin()); return begin()[i]; }
290  };
291  return SequenceRange(s.getLength() ? s.getArray() : nullptr, s.getLength());
292 };
293 
295 
296 #endif
297 
298 }
299 }
300 }
301 }
302 
303 namespace cppu {
304 
305 template< typename T > inline ::com::sun::star::uno::Type const &
307  SAL_UNUSED_PARAMETER ::com::sun::star::uno::Sequence< T > const *)
308 {
313  static_cast<
314  typename ::com::sun::star::uno::Sequence< T >::ElementType * >(
315  NULL)).
316  getTypeLibType()));
317  }
320 }
321 
322 template< typename T > inline ::com::sun::star::uno::Type const &
324  SAL_UNUSED_PARAMETER ::com::sun::star::uno::Sequence< T > const *)
325 {
326  //TODO On certain platforms with weak memory models, the following code can
327  // result in some threads observing that td points to garbage:
328  static typelib_TypeDescriptionReference * td = NULL;
329  if (td == NULL) {
331  &td,
333  static_cast<
334  typename ::com::sun::star::uno::Sequence< T >::ElementType * >(
335  NULL)).
336  getTypeLibType()));
337  }
339 }
340 
341 }
342 
343 // generic sequence template
344 template< class E >
345 inline const ::com::sun::star::uno::Type &
346 SAL_CALL getCppuType(
347  SAL_UNUSED_PARAMETER const ::com::sun::star::uno::Sequence< E > * )
348 {
350  static_cast< ::com::sun::star::uno::Sequence< E > * >(0));
351 }
352 
353 // generic sequence template for given element type (e.g. C++ arrays)
354 template< class E >
355 inline const ::com::sun::star::uno::Type &
356 SAL_CALL getCppuSequenceType( const ::com::sun::star::uno::Type & rElementType )
357 {
359  {
362  rElementType.getTypeLibType() );
363  }
364  return * reinterpret_cast< const ::com::sun::star::uno::Type * >(
366 }
367 
368 // char sequence
369 inline const ::com::sun::star::uno::Type &
371 {
372  static typelib_TypeDescriptionReference * s_pType_com_sun_star_uno_Sequence_Char = NULL;
373  if (! s_pType_com_sun_star_uno_Sequence_Char)
374  {
375  const ::com::sun::star::uno::Type & rElementType = cppu::UnoType<cppu::UnoCharType>::get();
377  & s_pType_com_sun_star_uno_Sequence_Char,
378  rElementType.getTypeLibType() );
379  }
380  return * reinterpret_cast< const ::com::sun::star::uno::Type * >(
381  & s_pType_com_sun_star_uno_Sequence_Char );
382 }
383 
384 #endif
385 
386 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
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 cpp_release(void *pCppI)
Function to release a C++ interface.
Definition: genfunc.hxx:30
E & operator[](sal_Int32 nIndex)
Non-const index operator: Obtains a reference to element indexed at given position.
Definition: Sequence.hxx:174
bool operator!=(const Sequence &rSeq) const
Inequality operator: Compares two sequences.
Definition: Sequence.hxx:140
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.
static css::uno::Type const & get()
Definition: unotype.hxx:272
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.
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:24
struct SAL_DLLPUBLIC_RTTI _typelib_TypeDescriptionReference typelib_TypeDescriptionReference
Holds a weak reference to a type description.
__sal_NoAcquire
Definition: types.h:332
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:470
typelib_TypeDescriptionReference * getTypeLibType() const
Gets the C typelib type description reference pointer.
Definition: Type.h:142
css::uno::Type const & getTypeFromTypeDescriptionReference(::typelib_TypeDescriptionReference *const *tdr)
Definition: unotype.hxx:85
#define SAL_UNUSED_PARAMETER
Annotate unused but required C++ function parameters.
Definition: types.h:548
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:209
Sequence & operator=(const Sequence &rSeq)
Assignment operator: Acquires given sequence handle and releases previously set handle.
Definition: Sequence.hxx:109
E * end()
This function allows to use Sequence in standard algorithms, like std::find and others.
Definition: Sequence.hxx:166
void cpp_acquire(void *pCppI)
Function to acquire a C++ interface.
Definition: genfunc.hxx:25
~Sequence()
Destructor: Releases sequence handle.
Definition: Sequence.hxx:98
C++ class representing an IDL meta type.
Definition: Type.h:38
E * begin()
This function allows to use Sequence in standard algorithms, like std::find and others.
Definition: Sequence.hxx:159
const ::com::sun::star::uno::Type & getCharSequenceCppuType()
Gets the meta type of IDL sequence&lt; char &gt;.
Definition: Sequence.hxx:370
void realloc(sal_Int32 nSize)
Reallocates sequence to new length.
Definition: Sequence.hxx:191
This is the binary specification of a SAL sequence.
Definition: types.h:283
sal_Int32 nRefCount
reference count of sequence
Definition: types.h:287
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.
Sequence()
Default constructor: Creates an empty sequence.
Definition: Sequence.hxx:38
void * cpp_queryInterface(void *pCppI, typelib_TypeDescriptionReference *pType)
Function to query for a C++ interface.
Definition: genfunc.hxx:35
sal_Int32 nElements
element count
Definition: types.h:290
const ::com::sun::star::uno::Type & getCppuSequenceType(const ::com::sun::star::uno::Type &rElementType)
Gets the meta type of IDL sequence.
Definition: Sequence.hxx:356
css::uno::Type const & getTypeFavourUnsigned(SAL_UNUSED_PARAMETER T const *)
A working replacement for getCppuType (see there).
Definition: unotype.hxx:304
::com::sun::star::uno::Type const & getTypeFavourChar(SAL_UNUSED_PARAMETER::com::sun::star::uno::Sequence< T > const *)
Definition: Sequence.hxx:323
E * getArray()
Gets a pointer to elements array for reading and writing.
Definition: Sequence.hxx:146
bool operator==(const Sequence &rSeq) const
Equality operator: Compares two sequences.
Definition: Sequence.hxx:125
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.