LibreOffice
LibreOffice 4.3 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 
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 template< class E >
49 {
50  const Type & rType = ::cppu::getTypeFavourUnsigned( this );
52  &_pSequence, rType.getTypeLibType(),
54  // no bad_alloc, because empty sequence is statically allocated in cppu
55 }
56 
57 template< class E >
59 {
60  osl_atomic_increment( &rSeq._pSequence->nRefCount );
61  _pSequence = rSeq._pSequence;
62 }
63 
64 template< class E >
66  uno_Sequence * pSequence, __sal_NoAcquire ) SAL_THROW(())
67  : _pSequence( pSequence )
68 {
69 }
70 
71 template< class E >
72 inline Sequence< E >::Sequence( const E * pElements, sal_Int32 len )
73 {
74  const Type & rType = ::cppu::getTypeFavourUnsigned( this );
75  bool success =
77  &_pSequence, rType.getTypeLibType(),
78  const_cast< E * >( pElements ), len, (uno_AcquireFunc)cpp_acquire );
79  if (! success)
80  throw ::std::bad_alloc();
81 }
82 
83 template< class E >
84 inline Sequence< E >::Sequence( sal_Int32 len )
85 {
86  const Type & rType = ::cppu::getTypeFavourUnsigned( this );
87  bool success =
89  &_pSequence, rType.getTypeLibType(),
90  0, len, (uno_AcquireFunc)cpp_acquire );
91  if (! success)
92  throw ::std::bad_alloc();
93 }
94 
95 template< class E >
97 {
98  const Type & rType = ::cppu::getTypeFavourUnsigned( this );
100  this, rType.getTypeLibType(), (uno_ReleaseFunc)cpp_release );
101 }
102 
103 template< class E >
105 {
106  const Type & rType = ::cppu::getTypeFavourUnsigned( this );
108  &_pSequence, rSeq._pSequence, rType.getTypeLibType(), (uno_ReleaseFunc)cpp_release );
109  return *this;
110 }
111 
112 template< class E >
113 inline bool Sequence< E >::operator == ( const Sequence< E > & rSeq ) const
114  SAL_THROW(())
115 {
116  if (_pSequence == rSeq._pSequence)
117  return true;
118  const Type & rType = ::cppu::getTypeFavourUnsigned( this );
120  const_cast< Sequence< E > * >( this ), rType.getTypeLibType(),
121  const_cast< Sequence< E > * >( &rSeq ), rType.getTypeLibType(),
124 }
125 
126 template< class E >
127 inline bool Sequence< E >::operator != ( const Sequence< E > & rSeq ) const
128  SAL_THROW(())
129 {
130  return (! operator == ( rSeq ));
131 }
132 
133 template< class E >
135 {
136  const Type & rType = ::cppu::getTypeFavourUnsigned( this );
137  bool success =
139  &_pSequence, rType.getTypeLibType(),
141  if (! success)
142  throw ::std::bad_alloc();
143  return reinterpret_cast< E * >( _pSequence->elements );
144 }
145 
146 template<class E> E * Sequence<E>::begin() { return getArray(); }
147 
148 template<class E> E const * Sequence<E>::begin() const
149 { return getConstArray(); }
150 
151 template<class E> E * Sequence<E>::end() { return begin() + getLength(); }
152 
153 template<class E> E const * Sequence<E>::end() const
154 { return begin() + getLength(); }
155 
156 template< class E >
157 inline E & Sequence< E >::operator [] ( sal_Int32 nIndex )
158 {
159  // silence spurious -Werror=strict-overflow warnings from GCC 4.8.2
160  assert(nIndex >= 0 && static_cast<sal_uInt32>(nIndex) < static_cast<sal_uInt32>(getLength()));
161  return getArray()[ nIndex ];
162 }
163 
164 template< class E >
165 inline const E & Sequence< E >::operator [] ( sal_Int32 nIndex ) const
166  SAL_THROW(())
167 {
168  // silence spurious -Werror=strict-overflow warnings from GCC 4.8.2
169  assert(nIndex >= 0 && static_cast<sal_uInt32>(nIndex) < static_cast<sal_uInt32>(getLength()));
170  return reinterpret_cast< const E * >( _pSequence->elements )[ nIndex ];
171 }
172 
173 template< class E >
174 inline void Sequence< E >::realloc( sal_Int32 nSize )
175 {
176  const Type & rType = ::cppu::getTypeFavourUnsigned( this );
177  bool success =
179  &_pSequence, rType.getTypeLibType(), nSize,
181  if (!success)
182  throw ::std::bad_alloc();
183 }
184 
185 inline ::com::sun::star::uno::Sequence< sal_Int8 > SAL_CALL toUnoSequence(
186  const ::rtl::ByteSequence & rByteSequence ) SAL_THROW(())
187 {
188  return ::com::sun::star::uno::Sequence< sal_Int8 >(
189  * reinterpret_cast< const ::com::sun::star::uno::Sequence< sal_Int8 > * >( &rByteSequence ) );
190 }
191 
192 }
193 }
194 }
195 }
196 
197 namespace cppu {
198 
199 template< typename T > inline ::com::sun::star::uno::Type const &
201  SAL_UNUSED_PARAMETER ::com::sun::star::uno::Sequence< T > const *)
202 {
207  static_cast<
208  typename ::com::sun::star::uno::Sequence< T >::ElementType * >(
209  0)).
210  getTypeLibType()));
211  }
214 }
215 
216 template< typename T > inline ::com::sun::star::uno::Type const &
218  SAL_UNUSED_PARAMETER ::com::sun::star::uno::Sequence< T > const *)
219 {
220  //TODO On certain platforms with weak memory models, the following code can
221  // result in some threads observing that td points to garbage:
222  static typelib_TypeDescriptionReference * td = 0;
223  if (td == 0) {
225  &td,
227  static_cast<
228  typename ::com::sun::star::uno::Sequence< T >::ElementType * >(
229  0)).
230  getTypeLibType()));
231  }
233 }
234 
235 }
236 
237 // generic sequence template
238 template< class E >
239 inline const ::com::sun::star::uno::Type &
240 SAL_CALL getCppuType(
241  SAL_UNUSED_PARAMETER const ::com::sun::star::uno::Sequence< E > * )
242  SAL_THROW(())
243 {
245  static_cast< ::com::sun::star::uno::Sequence< E > * >(0));
246 }
247 
248 // generic sequence template for given element type (e.g. C++ arrays)
249 template< class E >
250 inline const ::com::sun::star::uno::Type &
251 SAL_CALL getCppuSequenceType( const ::com::sun::star::uno::Type & rElementType )
252  SAL_THROW(())
253 {
255  {
258  rElementType.getTypeLibType() );
259  }
260  return * reinterpret_cast< const ::com::sun::star::uno::Type * >(
262 }
263 
264 // char sequence
265 inline const ::com::sun::star::uno::Type &
267 {
268  static typelib_TypeDescriptionReference * s_pType_com_sun_star_uno_Sequence_Char = 0;
269  if (! s_pType_com_sun_star_uno_Sequence_Char)
270  {
271  const ::com::sun::star::uno::Type & rElementType = ::getCharCppuType();
273  & s_pType_com_sun_star_uno_Sequence_Char,
274  rElementType.getTypeLibType() );
275  }
276  return * reinterpret_cast< const ::com::sun::star::uno::Type * >(
277  & s_pType_com_sun_star_uno_Sequence_Char );
278 }
279 
280 #endif
281 
282 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
~Sequence() SAL_THROW(())
Destructor: Releases sequence handle.
Definition: Sequence.hxx:96
bool operator!=(const Sequence< E > &rSeq) const SAL_THROW(())
Unequality operator: Compares two sequences.
Definition: Sequence.hxx:127
typelib_TypeDescriptionReference * getTypeLibType() const SAL_THROW(())
Gets the C typelib type description reference pointer.
Definition: Type.h:155
void realloc(sal_Int32 nSize)
Reallocates sequence to new length.
Definition: Sequence.hxx:174
C++ class representing an IDL meta type.
Definition: Type.h:55
E * getArray()
Gets a pointer to elements array for reading and writing.
Definition: Sequence.hxx:134
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< E > & operator=(const Sequence< E > &rSeq) SAL_THROW(())
Assignment operator: Acquires given sequence handle and releases previously set handle.
Definition: Sequence.hxx:104
const ::com::sun::star::uno::Type & getCharSequenceCppuType() SAL_THROW(())
Gets the meta type of IDL sequence< char >.
Definition: Sequence.hxx:266
const ::com::sun::star::uno::Type & getCharCppuType() SAL_THROW(())
Gets the meta type of IDL type char.
Definition: Type.hxx:130
Template C++ class representing an IDL sequence.
Definition: unotype.hxx:33
__sal_NoAcquire
Definition: types.h:384
::com::sun::star::uno::Type const & getTypeFavourChar(SAL_UNUSED_PARAMETER::com::sun::star::uno::Sequence< T > const *)
Definition: Sequence.hxx:217
Holds a weak reference to a type description.
Definition: typedescription.h:40
E * end()
This function allows to use Sequence in standard algorightms, like std::find and others.
Definition: Sequence.hxx:151
This is the binary specification of a SAL sequence.
Definition: types.h:327
::com::sun::star::uno::Type const & getTypeFavourUnsigned(SAL_UNUSED_PARAMETER T const *)
A working replacement for getCppuType (see there).
Definition: unotype.hxx:287
void(* uno_AcquireFunc)(void *pInterface)
Generic function pointer declaration to acquire an interface.
Definition: data.h:46
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.
::com::sun::star::uno::Type const & getTypeFavourUnsigned(SAL_UNUSED_PARAMETER::com::sun::star::uno::Sequence< T > const *)
Definition: Sequence.hxx:200
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.
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) SAL_THROW(())
Creates a UNO byte sequence from a SAL byte sequence.
Definition: Sequence.hxx:185
Definition: Enterable.hxx:26
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.
Sequence() SAL_THROW(())
Default constructor: Creates an empty sequence.
Definition: Sequence.hxx:48
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.
#define SAL_THROW(x)
Exception specification documentation.
Definition: types.h:361
const ::com::sun::star::uno::Type & getCppuType(SAL_UNUSED_PARAMETER const ::com::sun::star::uno::Sequence< E > *)
Definition: Sequence.hxx:240
inline::com::sun::star::uno::Type const & getTypeFromTypeDescriptionReference(::typelib_TypeDescriptionReference *const *tdr)
Definition: unotype.hxx:94
void cpp_acquire(void *pCppI) SAL_THROW(())
Function to acquire a C++ interface.
Definition: genfunc.hxx:36
E * begin()
This function allows to use Sequence in standard algorightms, like std::find and others.
Definition: Sequence.hxx:146
#define SAL_UNUSED_PARAMETER
Annotate unused but required C++ function parameters.
Definition: types.h:582
E & operator[](sal_Int32 nIndex)
Non-const index operator: Obtains a reference to element indexed at given position.
Definition: Sequence.hxx:157
bool operator==(const Sequence< E > &rSeq) const SAL_THROW(())
Equality operator: Compares two sequences.
Definition: Sequence.hxx:113
void cpp_release(void *pCppI) SAL_THROW(())
Function to release a C++ interface.
Definition: genfunc.hxx:42
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!
void * cpp_queryInterface(void *pCppI, typelib_TypeDescriptionReference *pType) SAL_THROW(())
Function to query for a C++ interface.
Definition: genfunc.hxx:48
const ::com::sun::star::uno::Type & getCppuSequenceType(const ::com::sun::star::uno::Type &rElementType)
Gets the meta type of IDL sequence.
Definition: Sequence.hxx:251
void(* uno_ReleaseFunc)(void *pInterface)
Generic function pointer declaration to release an interface.
Definition: data.h:52
void *(* uno_QueryInterfaceFunc)(void *pInterface, struct _typelib_TypeDescriptionReference *pType)
Generic function pointer declaration to query for an interface.
Definition: data.h:40
Definition: types.h:391