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