LibreOffice
LibreOffice 7.1 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#if defined LIBO_INTERNAL_ONLY
27# include <type_traits>
28# include <ostream>
29#endif
30
31#include "osl/interlck.h"
34#include "uno/data.h"
36#include "cppu/unotype.hxx"
37
38namespace com
39{
40namespace sun
41{
42namespace star
43{
44namespace uno
45{
46
48template< class E >
49typelib_TypeDescriptionReference * Sequence< E >::s_pType = NULL;
51
52template< class E >
54{
55 const Type & rType = ::cppu::getTypeFavourUnsigned( this );
57 &_pSequence, rType.getTypeLibType(),
58 NULL, 0, cpp_acquire );
59 // no bad_alloc, because empty sequence is statically allocated in cppu
60}
61
62template< class E >
63inline Sequence< E >::Sequence( const Sequence & rSeq )
64{
65 osl_atomic_increment( &rSeq._pSequence->nRefCount );
66 _pSequence = rSeq._pSequence;
67}
68
69template< class E >
71 uno_Sequence * pSequence, __sal_NoAcquire )
72 : _pSequence( pSequence )
73{
74}
75
76template< class E >
77inline Sequence< E >::Sequence( const E * pElements, sal_Int32 len )
78{
79 const Type & rType = ::cppu::getTypeFavourUnsigned( this );
80
81 bool success =
83 &_pSequence, rType.getTypeLibType(),
84 const_cast< E * >( pElements ), len, cpp_acquire );
85 if (! success)
86 throw ::std::bad_alloc();
87}
88
89template< class E >
90inline Sequence< E >::Sequence( sal_Int32 len )
91{
92 const Type & rType = ::cppu::getTypeFavourUnsigned( this );
93 bool success =
95 &_pSequence, rType.getTypeLibType(),
96 NULL, len, cpp_acquire );
97 if (! success)
98 throw ::std::bad_alloc();
99}
100
101#if defined LIBO_INTERNAL_ONLY
102template<typename E> Sequence<E>::Sequence(std::initializer_list<E> init) {
104 &_pSequence, cppu::getTypeFavourUnsigned(this).getTypeLibType(),
105 const_cast<E *>(init.begin()), init.size(), cpp_acquire))
106 {
107 throw std::bad_alloc();
108 }
109}
110#endif
111
112template< class E >
114{
115 if (osl_atomic_decrement( &_pSequence->nRefCount ) == 0)
116 {
117 const Type & rType = ::cppu::getTypeFavourUnsigned( this );
119 _pSequence, rType.getTypeLibType(), cpp_release );
120 }
121}
122
123template< class E >
125{
126 const Type & rType = ::cppu::getTypeFavourUnsigned( this );
128 &_pSequence, rSeq._pSequence, rType.getTypeLibType(), cpp_release );
129 return *this;
130}
131
132template< class E >
133inline bool Sequence< E >::operator == ( const Sequence & rSeq ) const
134{
135 if (_pSequence == rSeq._pSequence)
136 return true;
137 const Type & rType = ::cppu::getTypeFavourUnsigned( this );
139 const_cast< Sequence * >( this ), rType.getTypeLibType(),
140 const_cast< Sequence * >( &rSeq ), rType.getTypeLibType(),
142 cpp_release );
143}
144
145template< class E >
146inline bool Sequence< E >::operator != ( const Sequence & rSeq ) const
147{
148 return (! operator == ( rSeq ));
149}
150
151template< class E >
153{
154 const Type & rType = ::cppu::getTypeFavourUnsigned( this );
155 bool success =
157 &_pSequence, rType.getTypeLibType(),
159 if (! success)
160 throw ::std::bad_alloc();
161 return reinterpret_cast< E * >( _pSequence->elements );
162}
163
164template<class E> E * Sequence<E>::begin() { return getArray(); }
165
166template<class E> E const * Sequence<E>::begin() const
167{ return getConstArray(); }
168
169template<class E> E * Sequence<E>::end() { return begin() + getLength(); }
170
171template<class E> E const * Sequence<E>::end() const
172{ return begin() + getLength(); }
173
174template< class E >
175inline E & Sequence< E >::operator [] ( sal_Int32 nIndex )
176{
177 // silence spurious -Werror=strict-overflow warnings from GCC 4.8.2
178 assert(nIndex >= 0 && static_cast<sal_uInt32>(nIndex) < static_cast<sal_uInt32>(getLength()));
179 return getArray()[ nIndex ];
180}
181
182template< class E >
183inline 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
190template< class E >
191inline 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
202inline ::com::sun::star::uno::Sequence< sal_Int8 > SAL_CALL toUnoSequence(
203 const ::rtl::ByteSequence & rByteSequence )
204{
205 return * reinterpret_cast< const ::com::sun::star::uno::Sequence< sal_Int8 > * >( &rByteSequence );
206}
207
208#if defined LIBO_INTERNAL_ONLY
209
211
212namespace uno_detail {
213
214template< typename value_t, typename charT, typename traits >
215void sequence_output_elems( std::basic_ostream<charT, traits> &os, const value_t *pAry, sal_Int32 nLen, std::true_type )
216{
217 // for integral types, use hex notation
218 auto const flags = os.setf(std::ios_base::hex);
219 for(sal_Int32 i=0; i<nLen-1; ++i)
220 os << "0x" << *pAry++ << ", ";
221 if( nLen > 1 )
222 os << "0x" << *pAry++;
223 os.setf(flags);
224}
225
226template< typename value_t, typename charT, typename traits >
227void sequence_output_elems( std::basic_ostream<charT, traits> &os, const value_t *pAry, sal_Int32 nLen, std::false_type )
228{
229 // every other type: rely on their own ostream operator<<
230 for(sal_Int32 i=0; i<nLen-1; ++i)
231 os << *pAry++ << ", ";
232 if( nLen > 1 )
233 os << *pAry++;
234}
235
236template< typename value_t, typename charT, typename traits >
237void sequence_output_bytes( std::basic_ostream<charT, traits> &os, const value_t *pAry, sal_Int32 nLen )
238{
239 // special case bytes - ostream operator<< outputs those as char
240 // values, but we need raw ints here
241 auto const flags = os.setf(std::ios_base::hex);
242 for(sal_Int32 i=0; i<nLen-1; ++i)
243 os << "0x" << (0xFF & +*pAry++) << ", ";
244 if( nLen > 1 )
245 os << "0x" << (0xFF & +*pAry++);
246 os.setf(flags);
247}
248
249}
250
257template< typename value_t, typename charT, typename traits >
258inline std::basic_ostream<charT, traits> &operator<<(std::basic_ostream<charT, traits> &os, css::uno::Sequence<value_t> const& v)
259{
260 const value_t *pAry = v.getConstArray();
261 sal_Int32 nLen = v.getLength();
262 if constexpr (std::is_same<sal_Int8, value_t>::value) {
263 uno_detail::sequence_output_bytes(os, pAry, nLen);
264 } else {
265 uno_detail::sequence_output_elems(os, pAry, nLen, std::is_integral<value_t>());
266 }
267 return os;
268}
269
271
272#endif
273
274}
275}
276}
277}
278
279namespace cppu {
280
281template< typename T > inline ::com::sun::star::uno::Type const &
283 SAL_UNUSED_PARAMETER ::com::sun::star::uno::Sequence< T > const *)
284{
289 static_cast<
290 typename ::com::sun::star::uno::Sequence< T >::ElementType * >(
291 0)).
292 getTypeLibType()));
293 }
296}
297
298template< typename T > inline ::com::sun::star::uno::Type const &
300 SAL_UNUSED_PARAMETER ::com::sun::star::uno::Sequence< T > const *)
301{
302 //TODO On certain platforms with weak memory models, the following code can
303 // result in some threads observing that td points to garbage:
304 static typelib_TypeDescriptionReference * td = NULL;
305 if (td == NULL) {
307 &td,
309 static_cast<
310 typename ::com::sun::star::uno::Sequence< T >::ElementType * >(
311 0)).
312 getTypeLibType()));
313 }
315}
316
317}
318
319// generic sequence template
320template< class E >
321inline const ::com::sun::star::uno::Type &
322SAL_CALL getCppuType(
323 SAL_UNUSED_PARAMETER const ::com::sun::star::uno::Sequence< E > * )
324{
326 static_cast< ::com::sun::star::uno::Sequence< E > * >(0));
327}
328
329// generic sequence template for given element type (e.g. C++ arrays)
330template< class E >
331inline const ::com::sun::star::uno::Type &
332SAL_CALL getCppuSequenceType( const ::com::sun::star::uno::Type & rElementType )
333{
335 {
338 rElementType.getTypeLibType() );
339 }
340 return * reinterpret_cast< const ::com::sun::star::uno::Type * >(
342}
343
344// char sequence
345inline const ::com::sun::star::uno::Type &
347{
348 static typelib_TypeDescriptionReference * s_pType_com_sun_star_uno_Sequence_Char = NULL;
349 if (! s_pType_com_sun_star_uno_Sequence_Char)
350 {
351 const ::com::sun::star::uno::Type & rElementType = cppu::UnoType<cppu::UnoCharType>::get();
353 & s_pType_com_sun_star_uno_Sequence_Char,
354 rElementType.getTypeLibType() );
355 }
356 return * reinterpret_cast< const ::com::sun::star::uno::Type * >(
357 & s_pType_com_sun_star_uno_Sequence_Char );
358}
359
360#endif
361
362/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
__sal_NoAcquire
Definition: types.h:349
#define SAL_UNUSED_PARAMETER
Annotate unused but required C++ function parameters.
Definition: types.h:539
CPPU_DLLPUBLIC void typelib_static_sequence_type_init(typelib_TypeDescriptionReference **ppRef, typelib_TypeDescriptionReference *pElementType) SAL_THROW_EXTERN_C()
Inits static sequence type reference.
struct SAL_DLLPUBLIC_RTTI _typelib_TypeDescriptionReference typelib_TypeDescriptionReference
Holds a weak reference to a type description.
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 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.
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.
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.
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 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.
const ::com::sun::star::uno::Type & getCharSequenceCppuType()
Gets the meta type of IDL sequence< char >.
Definition: Sequence.hxx:346
const ::com::sun::star::uno::Type & getCppuSequenceType(const ::com::sun::star::uno::Type &rElementType)
Gets the meta type of IDL sequence.
Definition: Sequence.hxx:332
const ::com::sun::star::uno::Type & getCppuType(SAL_UNUSED_PARAMETER const ::com::sun::star::uno::Sequence< E > *)
Definition: Sequence.hxx:322
Definition: Enterable.hxx:27
css::uno::Type const & getTypeFavourUnsigned(SAL_UNUSED_PARAMETER T const *)
A working replacement for getCppuType (see there).
Definition: unotype.hxx:320
::com::sun::star::uno::Type const & getTypeFavourChar(SAL_UNUSED_PARAMETER ::com::sun::star::uno::Sequence< T > const *)
Definition: Sequence.hxx:299
::com::sun::star::uno::Type const & getTypeFavourUnsigned(SAL_UNUSED_PARAMETER ::com::sun::star::uno::Sequence< T > const *)
Definition: Sequence.hxx:282
Definition: unotype.hxx:35
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:202
std::basic_ostream< charT, traits > & operator<<(std::basic_ostream< charT, traits > &o, Any const &any)
Support for Any in std::ostream (and thus in CPPUNIT_ASSERT or SAL_INFO macros, for example).
Definition: Any.hxx:695
void cpp_release(void *pCppI)
Function to release a C++ interface.
Definition: genfunc.hxx:46
void * cpp_queryInterface(void *pCppI, typelib_TypeDescriptionReference *pType)
Function to query for a C++ interface.
Definition: genfunc.hxx:51
void cpp_acquire(void *pCppI)
Function to acquire a C++ interface.
Definition: genfunc.hxx:41
css::uno::Type const & getTypeFromTypeDescriptionReference(::typelib_TypeDescriptionReference *const *tdr)
Definition: unotype.hxx:101
Template C++ class representing an IDL sequence.
Definition: Sequence.h:57
~Sequence()
Destructor: Releases sequence handle.
Definition: Sequence.hxx:113
bool operator==(const Sequence &rSeq) const
Equality operator: Compares two sequences.
Definition: Sequence.hxx:133
E * begin()
This function allows to use Sequence in standard algorithms, like std::find and others.
Definition: Sequence.hxx:164
E & operator[](sal_Int32 nIndex)
Non-const index operator: Obtains a reference to element indexed at given position.
Definition: Sequence.hxx:175
E * end()
This function allows to use Sequence in standard algorithms, like std::find and others.
Definition: Sequence.hxx:169
E * getArray()
Gets a pointer to elements array for reading and writing.
Definition: Sequence.hxx:152
bool operator!=(const Sequence &rSeq) const
Inequality operator: Compares two sequences.
Definition: Sequence.hxx:146
Sequence()
Default constructor: Creates an empty sequence.
Definition: Sequence.hxx:53
Sequence & operator=(const Sequence &rSeq)
Assignment operator: Acquires given sequence handle and releases previously set handle.
Definition: Sequence.hxx:124
void realloc(sal_Int32 nSize)
Reallocates sequence to new length.
Definition: Sequence.hxx:191
static css::uno::Type const & get()
Definition: unotype.hxx:288
This is the binary specification of a SAL sequence.
Definition: types.h:300
sal_Int32 nRefCount
reference count of sequence
Definition: types.h:303
C++ class representing an IDL meta type.
Definition: Type.h:55
typelib_TypeDescriptionReference * getTypeLibType() const
Gets the C typelib type description reference pointer.
Definition: Type.h:154