LibreOffice
LibreOffice 6.0 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 
27 #include <osl/interlck.h>
30 #include <uno/data.h>
32 #include <cppu/unotype.hxx>
33 
34 namespace com
35 {
36 namespace sun
37 {
38 namespace star
39 {
40 namespace uno
41 {
42 
44 template< class E >
45 typelib_TypeDescriptionReference * Sequence< E >::s_pType = NULL;
47 
48 template< class E >
50 {
51  const Type & rType = ::cppu::getTypeFavourUnsigned( this );
53  &_pSequence, rType.getTypeLibType(),
54  0, 0, cpp_acquire );
55  // no bad_alloc, because empty sequence is statically allocated in cppu
56 }
57 
58 template< class E >
59 inline Sequence< E >::Sequence( const Sequence & rSeq )
60 {
61  osl_atomic_increment( &rSeq._pSequence->nRefCount );
62  _pSequence = rSeq._pSequence;
63 }
64 
65 template< class E >
67  uno_Sequence * pSequence, __sal_NoAcquire )
68  : _pSequence( pSequence )
69 {
70 }
71 
72 template< class E >
73 inline Sequence< E >::Sequence( const E * pElements, sal_Int32 len )
74 {
75  const Type & rType = ::cppu::getTypeFavourUnsigned( this );
76  bool success =
78  &_pSequence, rType.getTypeLibType(),
79  const_cast< E * >( pElements ), len, cpp_acquire );
80  if (! success)
81  throw ::std::bad_alloc();
82 }
83 
84 template< class E >
85 inline Sequence< E >::Sequence( sal_Int32 len )
86 {
87  const Type & rType = ::cppu::getTypeFavourUnsigned( this );
88  bool success =
90  &_pSequence, rType.getTypeLibType(),
91  0, len, cpp_acquire );
92  if (! success)
93  throw ::std::bad_alloc();
94 }
95 
96 #if defined LIBO_INTERNAL_ONLY
97 template<typename E> Sequence<E>::Sequence(std::initializer_list<E> init) {
99  &_pSequence, cppu::getTypeFavourUnsigned(this).getTypeLibType(),
100  const_cast<E *>(init.begin()), init.size(), cpp_acquire))
101  {
102  throw std::bad_alloc();
103  }
104 }
105 #endif
106 
107 template< class E >
109 {
110  if (osl_atomic_decrement( &_pSequence->nRefCount ) == 0)
111  {
112  const Type & rType = ::cppu::getTypeFavourUnsigned( this );
114  _pSequence, rType.getTypeLibType(), cpp_release );
115  }
116 }
117 
118 template< class E >
120 {
121  const Type & rType = ::cppu::getTypeFavourUnsigned( this );
123  &_pSequence, rSeq._pSequence, rType.getTypeLibType(), cpp_release );
124  return *this;
125 }
126 
127 template< class E >
128 inline bool Sequence< E >::operator == ( const Sequence & rSeq ) const
129 {
130  if (_pSequence == rSeq._pSequence)
131  return true;
132  const Type & rType = ::cppu::getTypeFavourUnsigned( this );
134  const_cast< Sequence * >( this ), rType.getTypeLibType(),
135  const_cast< Sequence * >( &rSeq ), rType.getTypeLibType(),
137  cpp_release );
138 }
139 
140 template< class E >
141 inline bool Sequence< E >::operator != ( const Sequence & rSeq ) const
142 {
143  return (! operator == ( rSeq ));
144 }
145 
146 template< class E >
148 {
149  const Type & rType = ::cppu::getTypeFavourUnsigned( this );
150  bool success =
152  &_pSequence, rType.getTypeLibType(),
154  if (! success)
155  throw ::std::bad_alloc();
156  return reinterpret_cast< E * >( _pSequence->elements );
157 }
158 
159 template<class E> E * Sequence<E>::begin() { return getArray(); }
160 
161 template<class E> E const * Sequence<E>::begin() const
162 { return getConstArray(); }
163 
164 template<class E> E * Sequence<E>::end() { return begin() + getLength(); }
165 
166 template<class E> E const * Sequence<E>::end() const
167 { return begin() + getLength(); }
168 
169 template< class E >
170 inline E & Sequence< E >::operator [] ( sal_Int32 nIndex )
171 {
172  // silence spurious -Werror=strict-overflow warnings from GCC 4.8.2
173  assert(nIndex >= 0 && static_cast<sal_uInt32>(nIndex) < static_cast<sal_uInt32>(getLength()));
174  return getArray()[ nIndex ];
175 }
176 
177 template< class E >
178 inline const E & Sequence< E >::operator [] ( sal_Int32 nIndex ) const
179 {
180  // silence spurious -Werror=strict-overflow warnings from GCC 4.8.2
181  assert(nIndex >= 0 && static_cast<sal_uInt32>(nIndex) < static_cast<sal_uInt32>(getLength()));
182  return reinterpret_cast< const E * >( _pSequence->elements )[ nIndex ];
183 }
184 
185 template< class E >
186 inline void Sequence< E >::realloc( sal_Int32 nSize )
187 {
188  const Type & rType = ::cppu::getTypeFavourUnsigned( this );
189  bool success =
191  &_pSequence, rType.getTypeLibType(), nSize,
193  if (!success)
194  throw ::std::bad_alloc();
195 }
196 
197 inline ::com::sun::star::uno::Sequence< sal_Int8 > SAL_CALL toUnoSequence(
198  const ::rtl::ByteSequence & rByteSequence )
199 {
200  return * reinterpret_cast< const ::com::sun::star::uno::Sequence< sal_Int8 > * >( &rByteSequence );
201 }
202 
203 }
204 }
205 }
206 }
207 
208 namespace cppu {
209 
210 template< typename T > inline ::com::sun::star::uno::Type const &
212  SAL_UNUSED_PARAMETER ::com::sun::star::uno::Sequence< T > const *)
213 {
218  static_cast<
219  typename ::com::sun::star::uno::Sequence< T >::ElementType * >(
220  0)).
221  getTypeLibType()));
222  }
225 }
226 
227 template< typename T > inline ::com::sun::star::uno::Type const &
229  SAL_UNUSED_PARAMETER ::com::sun::star::uno::Sequence< T > const *)
230 {
231  //TODO On certain platforms with weak memory models, the following code can
232  // result in some threads observing that td points to garbage:
233  static typelib_TypeDescriptionReference * td = NULL;
234  if (td == NULL) {
236  &td,
238  static_cast<
239  typename ::com::sun::star::uno::Sequence< T >::ElementType * >(
240  0)).
241  getTypeLibType()));
242  }
244 }
245 
246 }
247 
248 // generic sequence template
249 template< class E >
250 inline const ::com::sun::star::uno::Type &
251 SAL_CALL getCppuType(
252  SAL_UNUSED_PARAMETER const ::com::sun::star::uno::Sequence< E > * )
253 {
255  static_cast< ::com::sun::star::uno::Sequence< E > * >(0));
256 }
257 
258 // generic sequence template for given element type (e.g. C++ arrays)
259 template< class E >
260 inline const ::com::sun::star::uno::Type &
261 SAL_CALL getCppuSequenceType( const ::com::sun::star::uno::Type & rElementType )
262 {
264  {
267  rElementType.getTypeLibType() );
268  }
269  return * reinterpret_cast< const ::com::sun::star::uno::Type * >(
271 }
272 
273 // char sequence
274 inline const ::com::sun::star::uno::Type &
276 {
277  static typelib_TypeDescriptionReference * s_pType_com_sun_star_uno_Sequence_Char = NULL;
278  if (! s_pType_com_sun_star_uno_Sequence_Char)
279  {
280  const ::com::sun::star::uno::Type & rElementType = cppu::UnoType<cppu::UnoCharType>::get();
282  & s_pType_com_sun_star_uno_Sequence_Char,
283  rElementType.getTypeLibType() );
284  }
285  return * reinterpret_cast< const ::com::sun::star::uno::Type * >(
286  & s_pType_com_sun_star_uno_Sequence_Char );
287 }
288 
289 #endif
290 
291 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Sequence & operator=(const Sequence &rSeq)
Assignment operator: Acquires given sequence handle and releases previously set handle.
Definition: Sequence.hxx:119
E * begin()
This function allows to use Sequence in standard algorightms, like std::find and others.
Definition: Sequence.hxx:159
Definition: Enterable.hxx:26
bool operator!=(const Sequence &rSeq) const
Unequality operator: Compares two sequences.
Definition: Sequence.hxx:141
::com::sun::star::uno::Type const & getTypeFavourChar(SAL_UNUSED_PARAMETER::com::sun::star::uno::Sequence< T > const *)
Definition: Sequence.hxx:228
void * cpp_queryInterface(void *pCppI, typelib_TypeDescriptionReference *pType)
Function to query for a C++ interface.
Definition: genfunc.hxx:51
char elements[1]
elements array
Definition: types.h:337
css::uno::Type const & getTypeFavourUnsigned(SAL_UNUSED_PARAMETER T const *)
A working replacement for getCppuType (see there).
Definition: unotype.hxx:320
Definition: types.h:383
void cpp_release(void *pCppI)
Function to release a C++ interface.
Definition: genfunc.hxx:46
void realloc(sal_Int32 nSize)
Reallocates sequence to new length.
Definition: Sequence.hxx:186
typelib_TypeDescriptionReference * getTypeLibType() const
Gets the C typelib type description reference pointer.
Definition: Type.h:154
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:197
__sal_NoAcquire
Definition: types.h:376
#define SAL_UNUSED_PARAMETER
Annotate unused but required C++ function parameters.
Definition: types.h:587
const ::com::sun::star::uno::Type & getCppuSequenceType(const ::com::sun::star::uno::Type &rElementType)
Gets the meta type of IDL sequence.
Definition: Sequence.hxx:261
E & operator[](sal_Int32 nIndex)
Non-const index operator: Obtains a reference to element indexed at given position.
Definition: Sequence.hxx:170
C++ class representing an IDL meta type.
Definition: Type.h:54
Template C++ class representing an IDL sequence.
Definition: unotype.hxx:40
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.
bool operator==(const Sequence &rSeq) const
Equality operator: Compares two sequences.
Definition: Sequence.hxx:128
css::uno::Type const & getTypeFromTypeDescriptionReference(::typelib_TypeDescriptionReference *const *tdr)
Definition: unotype.hxx:101
static css::uno::Type const & get()
Definition: unotype.hxx:288
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.
This is the binary specification of a SAL sequence.
Definition: types.h:327
sal_Int32 nRefCount
reference count of sequence
Definition: types.h:331
const E * getConstArray() const
Gets a pointer to elements array for reading.
Definition: Sequence.h:160
::com::sun::star::uno::Type const & getTypeFavourUnsigned(SAL_UNUSED_PARAMETER::com::sun::star::uno::Sequence< T > const *)
Definition: Sequence.hxx:211
CPPU_DLLPUBLIC void typelib_static_sequence_type_init(typelib_TypeDescriptionReference **ppRef, typelib_TypeDescriptionReference *pElementType) SAL_THROW_EXTERN_C()
Inits static sequence type reference.
~Sequence()
Destructor: Releases sequence handle.
Definition: Sequence.hxx:108
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:147
void cpp_acquire(void *pCppI)
Function to acquire a C++ interface.
Definition: genfunc.hxx:41
struct SAL_DLLPUBLIC_RTTI _typelib_TypeDescriptionReference typelib_TypeDescriptionReference
Holds a weak reference to a type description.
Sequence()
Default constructor: Creates an empty sequence.
Definition: Sequence.hxx:49
E * end()
This function allows to use Sequence in standard algorightms, like std::find and others.
Definition: Sequence.hxx:164
sal_Int32 getLength() const
Gets length of the sequence.
Definition: Sequence.h:143
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.
const ::com::sun::star::uno::Type & getCppuType(SAL_UNUSED_PARAMETER const ::com::sun::star::uno::Sequence< E > *)
Definition: Sequence.hxx:251
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.
const ::com::sun::star::uno::Type & getCharSequenceCppuType()
Gets the meta type of IDL sequence< char >.
Definition: Sequence.hxx:275
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.