LibreOffice
LibreOffice 4.1 SDK C/C++ API Reference
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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 #ifndef _COM_SUN_STAR_UNO_SEQUENCE_HXX_
20 #define _COM_SUN_STAR_UNO_SEQUENCE_HXX_
21 
22 #include "sal/config.h"
23 
24 #include <cassert>
25 
26 #include "osl/interlck.h"
29 #include "uno/data.h"
31 #include "cppu/unotype.hxx"
32 
33 namespace com
34 {
35 namespace sun
36 {
37 namespace star
38 {
39 namespace uno
40 {
41 
43 template< class E >
44 typelib_TypeDescriptionReference * Sequence< E >::s_pType = 0;
46 
47 //______________________________________________________________________________
48 template< class E >
50 {
51  const Type & rType = ::cppu::getTypeFavourUnsigned( this );
53  &_pSequence, rType.getTypeLibType(),
55  // no bad_alloc, because empty sequence is statically allocated in cppu
56 }
57 
58 //______________________________________________________________________________
59 template< class E >
61 {
62  osl_atomic_increment( &rSeq._pSequence->nRefCount );
63  _pSequence = rSeq._pSequence;
64 }
65 
66 //______________________________________________________________________________
67 template< class E >
69  uno_Sequence * pSequence, __sal_NoAcquire ) SAL_THROW(())
70  : _pSequence( pSequence )
71 {
72 }
73 
74 //______________________________________________________________________________
75 template< class E >
76 inline Sequence< E >::Sequence( const E * pElements, sal_Int32 len )
77 {
78  const Type & rType = ::cppu::getTypeFavourUnsigned( this );
79 #if ! defined EXCEPTIONS_OFF
80  sal_Bool success =
81 #endif
83  &_pSequence, rType.getTypeLibType(),
84  const_cast< E * >( pElements ), len, (uno_AcquireFunc)cpp_acquire );
85 #if ! defined EXCEPTIONS_OFF
86  if (! success)
87  throw ::std::bad_alloc();
88 #endif
89 }
90 
91 //______________________________________________________________________________
92 template< class E >
93 inline Sequence< E >::Sequence( sal_Int32 len )
94 {
95  const Type & rType = ::cppu::getTypeFavourUnsigned( this );
96 #if ! defined EXCEPTIONS_OFF
97  sal_Bool success =
98 #endif
100  &_pSequence, rType.getTypeLibType(),
101  0, len, (uno_AcquireFunc)cpp_acquire );
102 #if ! defined EXCEPTIONS_OFF
103  if (! success)
104  throw ::std::bad_alloc();
105 #endif
106 }
107 
108 //______________________________________________________________________________
109 template< class E >
111 {
112  const Type & rType = ::cppu::getTypeFavourUnsigned( this );
114  this, rType.getTypeLibType(), (uno_ReleaseFunc)cpp_release );
115 }
116 
117 //______________________________________________________________________________
118 template< class E >
120 {
121  const Type & rType = ::cppu::getTypeFavourUnsigned( this );
123  &_pSequence, rSeq._pSequence, rType.getTypeLibType(), (uno_ReleaseFunc)cpp_release );
124  return *this;
125 }
126 
127 //______________________________________________________________________________
128 template< class E >
130  SAL_THROW(())
131 {
132  if (_pSequence == rSeq._pSequence)
133  return sal_True;
134  const Type & rType = ::cppu::getTypeFavourUnsigned( this );
136  const_cast< Sequence< E > * >( this ), rType.getTypeLibType(),
137  const_cast< Sequence< E > * >( &rSeq ), rType.getTypeLibType(),
140 }
141 
142 //______________________________________________________________________________
143 template< class E >
145  SAL_THROW(())
146 {
147  return (! operator == ( rSeq ));
148 }
149 
150 //______________________________________________________________________________
151 template< class E >
153 {
154  const Type & rType = ::cppu::getTypeFavourUnsigned( this );
155 #if ! defined EXCEPTIONS_OFF
156  sal_Bool success =
157 #endif
159  &_pSequence, rType.getTypeLibType(),
161 #if ! defined EXCEPTIONS_OFF
162  if (! success)
163  throw ::std::bad_alloc();
164 #endif
165  return reinterpret_cast< E * >( _pSequence->elements );
166 }
167 
168 //______________________________________________________________________________
169 template< class E >
170 inline E & Sequence< E >::operator [] ( sal_Int32 nIndex )
171 {
172  assert( nIndex >= 0 && nIndex < getLength() );
173  return getArray()[ nIndex ];
174 }
175 
176 //______________________________________________________________________________
177 template< class E >
178 inline const E & Sequence< E >::operator [] ( sal_Int32 nIndex ) const
179  SAL_THROW(())
180 {
181  assert( nIndex >= 0 && nIndex < getLength() );
182  return reinterpret_cast< const E * >( _pSequence->elements )[ nIndex ];
183 }
184 
185 //______________________________________________________________________________
186 template< class E >
187 inline void Sequence< E >::realloc( sal_Int32 nSize )
188 {
189  const Type & rType = ::cppu::getTypeFavourUnsigned( this );
190 #if !defined EXCEPTIONS_OFF
191  sal_Bool success =
192 #endif
194  &_pSequence, rType.getTypeLibType(), nSize,
196 #if !defined EXCEPTIONS_OFF
197  if (!success)
198  throw ::std::bad_alloc();
199 #endif
200 }
201 
202 //------------------------------------------------------------------------------
203 inline ::com::sun::star::uno::Sequence< sal_Int8 > SAL_CALL toUnoSequence(
204  const ::rtl::ByteSequence & rByteSequence ) SAL_THROW(())
205 {
206  return ::com::sun::star::uno::Sequence< sal_Int8 >(
207  * reinterpret_cast< const ::com::sun::star::uno::Sequence< sal_Int8 > * >( &rByteSequence ) );
208 }
209 
210 }
211 }
212 }
213 }
214 
215 namespace cppu {
216 
217 template< typename T > inline ::com::sun::star::uno::Type const &
219  SAL_UNUSED_PARAMETER ::com::sun::star::uno::Sequence< T > const *)
220 {
225  static_cast<
226  typename ::com::sun::star::uno::Sequence< T >::ElementType * >(
227  0)).
228  getTypeLibType()));
229  }
232 }
233 
234 template< typename T > inline ::com::sun::star::uno::Type const &
236  SAL_UNUSED_PARAMETER ::com::sun::star::uno::Sequence< T > const *)
237 {
238  //TODO On certain platforms with weak memory models, the following code can
239  // result in some threads observing that td points to garbage:
240  static typelib_TypeDescriptionReference * td = 0;
241  if (td == 0) {
243  &td,
245  static_cast<
246  typename ::com::sun::star::uno::Sequence< T >::ElementType * >(
247  0)).
248  getTypeLibType()));
249  }
251 }
252 
253 }
254 
255 // generic sequence template
256 template< class E >
257 inline const ::com::sun::star::uno::Type &
258 SAL_CALL getCppuType(
259  SAL_UNUSED_PARAMETER const ::com::sun::star::uno::Sequence< E > * )
260  SAL_THROW(())
261 {
263  static_cast< ::com::sun::star::uno::Sequence< E > * >(0));
264 }
265 
266 // generic sequence template for given element type (e.g. C++ arrays)
267 template< class E >
268 inline const ::com::sun::star::uno::Type &
269 SAL_CALL getCppuSequenceType( const ::com::sun::star::uno::Type & rElementType )
270  SAL_THROW(())
271 {
273  {
276  rElementType.getTypeLibType() );
277  }
278  return * reinterpret_cast< const ::com::sun::star::uno::Type * >(
280 }
281 
282 #if (defined(__SUNPRO_CC) && (__SUNPRO_CC == 0x500))
283 static typelib_TypeDescriptionReference * s_pType_com_sun_star_uno_Sequence_Char = 0;
284 #endif
285 
286 // char sequence
287 inline const ::com::sun::star::uno::Type &
289 {
290 #if !( defined(__SUNPRO_CC) && (__SUNPRO_CC == 0x500))
291  static typelib_TypeDescriptionReference * s_pType_com_sun_star_uno_Sequence_Char = 0;
292 #endif
293  if (! s_pType_com_sun_star_uno_Sequence_Char)
294  {
295  const ::com::sun::star::uno::Type & rElementType = ::getCharCppuType();
297  & s_pType_com_sun_star_uno_Sequence_Char,
298  rElementType.getTypeLibType() );
299  }
300  return * reinterpret_cast< const ::com::sun::star::uno::Type * >(
301  & s_pType_com_sun_star_uno_Sequence_Char );
302 }
303 
304 #endif
305 
306 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
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.
#define SAL_UNUSED_PARAMETER
Annotate unused but required C++ function parameters.
Definition: types.h:540
void(* uno_ReleaseFunc)(void *pInterface)
Generic function pointer declaration to release an interface.
Definition: data.h:52
~Sequence()
Destructor: Releases sequence handle.
Definition: Sequence.hxx:110
CPPU_DLLPUBLIC void typelib_static_sequence_type_init(typelib_TypeDescriptionReference **ppRef, typelib_TypeDescriptionReference *pElementType) SAL_THROW_EXTERN_C()
Inits static sequence type reference.
sal_Bool operator==(const Sequence< E > &rSeq) const
Equality operator: Compares two sequences.
Definition: Sequence.hxx:129
const ::com::sun::star::uno::Type & getCppuSequenceType(const ::com::sun::star::uno::Type &rElementType)
Gets the meta type of IDL sequence.
Definition: Sequence.hxx:269
const ::com::sun::star::uno::Type & getCharSequenceCppuType()
Gets the meta type of IDL sequence&lt; char &gt;.
Definition: Sequence.hxx:288
Sequence()
Default constructor: Creates an empty sequence.
Definition: Sequence.hxx:49
E * getArray()
Gets a pointer to elements array for reading and writing.
Definition: Sequence.hxx:152
void *(* uno_QueryInterfaceFunc)(void *pInterface, struct _typelib_TypeDescriptionReference *pType)
Generic function pointer declaration to query for an interface.
Definition: data.h:40
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:203
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.
Sequence< E > & operator=(const Sequence< E > &rSeq)
Assignment operator: Acquires given sequence handle and releases previously set handle.
Definition: Sequence.hxx:119
inline::com::sun::star::uno::Type const & getTypeFromTypeDescriptionReference(::typelib_TypeDescriptionReference *const *tdr)
Definition: unotype.hxx:94
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.
void * cpp_queryInterface(void *pCppI, typelib_TypeDescriptionReference *pType)
Function to query for a C++ interface.
Definition: genfunc.hxx:48
unsigned char sal_Bool
Definition: types.h:37
void cpp_acquire(void *pCppI)
Function to acquire a C++ interface.
Definition: genfunc.hxx:36
__sal_NoAcquire
Definition: types.h:370
This is the binary specification of a SAL sequence.
Definition: types.h:323
CPPU_DLLPUBLIC void uno_type_destructData(void *pValue, struct _typelib_TypeDescriptionReference *pType, uno_ReleaseFunc release) SAL_THROW_EXTERN_C()
Destructs a given value; does NOT free its memory!
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:369
Template C++ class representing an IDL sequence.
Definition: unotype.hxx:33
::com::sun::star::uno::Type const & getTypeFavourChar(SAL_UNUSED_PARAMETER::com::sun::star::uno::Sequence< T > const *)
Definition: Sequence.hxx:235
const ::com::sun::star::uno::Type & getCharCppuType()
Gets the meta type of IDL type char.
Definition: Type.hxx:130
void realloc(sal_Int32 nSize)
Reallocates sequence to new length.
Definition: Sequence.hxx:187
void cpp_release(void *pCppI)
Function to release a C++ interface.
Definition: genfunc.hxx:42
E & operator[](sal_Int32 nIndex)
Non-const index operator: Obtains a reference to element indexed at given position.
Definition: Sequence.hxx:170
typelib_TypeDescriptionReference * getTypeLibType() const
Gets the C typelib type description reference pointer.
Definition: Type.h:155
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.
Holds a weak reference to a type description.
Definition: typedescription.h:40
::com::sun::star::uno::Type const & getTypeFavourUnsigned(SAL_UNUSED_PARAMETER T const *)
A working replacement for getCppuType (see there).
Definition: unotype.hxx:287
#define SAL_THROW(exc)
Definition of function throw clause macros.
Definition: types.h:356
C++ class representing an IDL meta type.
Definition: Type.h:55
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.
#define sal_True
Definition: types.h:39
sal_Bool operator!=(const Sequence< E > &rSeq) const
Unequality operator: Compares two sequences.
Definition: Sequence.hxx:144
void(* uno_AcquireFunc)(void *pInterface)
Generic function pointer declaration to acquire an interface.
Definition: data.h:46