bump product version to 4.1.6.2
[LibreOffice.git] / include / oox / helper / containerhelper.hxx
blobf3b27f05b1e2183e4878abc71386e938ff2900c7
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef OOX_HELPER_CONTAINERHELPER_HXX
21 #define OOX_HELPER_CONTAINERHELPER_HXX
23 #include <map>
24 #include <vector>
25 #include <com/sun/star/uno/Reference.h>
26 #include <com/sun/star/uno/Sequence.h>
27 #include "oox/dllapi.h"
30 namespace com { namespace sun { namespace star {
31 namespace container { class XIndexAccess; }
32 namespace container { class XIndexContainer; }
33 namespace container { class XNameAccess; }
34 namespace container { class XNameContainer; }
35 namespace uno { class XComponentContext; }
36 } } }
38 namespace oox {
40 // ============================================================================
42 /** A range of signed 32-bit integer values. */
43 struct ValueRange
45 sal_Int32 mnFirst;
46 sal_Int32 mnLast;
48 inline explicit ValueRange( sal_Int32 nValue = 0 ) : mnFirst( nValue ), mnLast( nValue ) {}
49 inline explicit ValueRange( sal_Int32 nFirst, sal_Int32 nLast ) : mnFirst( nFirst ), mnLast( nLast ) {}
51 inline bool operator==( const ValueRange& rRange ) const { return (mnFirst == rRange.mnFirst) && (mnLast == rRange.mnLast); }
52 inline bool operator!=( const ValueRange& rRange ) const { return !(*this == rRange); }
53 inline bool contains( sal_Int32 nValue ) const { return (mnFirst <= nValue) && (nValue <= mnLast); }
54 inline bool contains( const ValueRange& rRange ) const { return (mnFirst <= rRange.mnFirst) && (rRange.mnLast <= mnLast); }
55 inline bool intersects( const ValueRange& rRange ) const { return (mnFirst <= rRange.mnLast) && (rRange.mnFirst <= mnLast); }
58 // ----------------------------------------------------------------------------
60 typedef ::std::vector< ValueRange > ValueRangeVector;
62 // ----------------------------------------------------------------------------
64 /** An ordered list of value ranges. The insertion operation will merge
65 consecutive value ranges.
67 class OOX_DLLPUBLIC ValueRangeSet
69 public:
70 inline explicit ValueRangeSet() {}
72 /** Inserts the passed value into the range list. */
73 inline void insert( sal_Int32 nValue ) { insert( ValueRange( nValue ) ); }
74 /** Inserts the passed value range into the range list. */
75 void insert( const ValueRange& rRange );
77 /** Returns the ordered list of all value ranges. */
78 inline const ValueRangeVector& getRanges() const { return maRanges; }
80 private:
81 ValueRangeVector maRanges;
84 // ============================================================================
86 /** Template for a 2-dimensional array of objects.
88 This class template provides a similar interface to the ::std::vector
89 template.
91 template< typename Type >
92 class Matrix
94 public:
95 typedef ::std::vector< Type > container_type;
96 typedef typename container_type::value_type value_type;
97 typedef typename container_type::pointer pointer;
98 typedef typename container_type::reference reference;
99 typedef typename container_type::const_reference const_reference;
100 typedef typename container_type::size_type size_type;
101 typedef typename container_type::iterator iterator;
102 typedef typename container_type::const_iterator const_iterator;
104 inline explicit Matrix() : mnWidth( 0 ) {}
105 inline explicit Matrix( size_type nWidth, size_type nHeight ) { this->resize( nWidth, nHeight ); }
106 inline explicit Matrix( size_type nWidth, size_type nHeight, const_reference rData ) { this->resize( nWidth, nHeight, rData ); }
108 inline size_type capacity() const { return maData.capacity(); }
109 inline bool empty() const { return maData.empty(); }
110 inline size_type size() const { return maData.size(); }
111 inline size_type width() const { return mnWidth; }
112 inline size_type height() const { return this->empty() ? 0 : (this->size() / this->width()); }
113 inline bool has( size_type nX, size_type nY ) const { return (nX < this->width()) && (nY < this->height()); }
115 inline void reserve( size_type nWidth, size_type nHeight ) { maData.reserve( nWidth * nHeight ); }
116 inline void clear() { this->resize( 0, 0 ); }
117 inline void resize( size_type nWidth, size_type nHeight ) { mnWidth = nWidth; maData.resize( nWidth * nHeight ); }
118 inline void resize( size_type nWidth, size_type nHeight, const_reference rData ) { mnWidth = nWidth; maData.resize( nWidth * nHeight, rData ); }
120 inline iterator at( size_type nX, size_type nY ) { return maData.begin() + mnWidth * nY + nX; }
121 inline const_iterator at( size_type nX, size_type nY ) const { return maData.begin() + mnWidth * nY + nX; }
123 inline reference operator()( size_type nX, size_type nY ) { return *this->at( nX, nY ); }
124 inline const_reference operator()( size_type nX, size_type nY ) const { return *this->at( nX, nY ); }
126 inline iterator begin() { return maData.begin(); }
127 inline const_iterator begin() const { return maData.begin(); }
128 inline iterator end() { return maData.end(); }
129 inline const_iterator end() const { return maData.end(); }
131 inline reference front() { return maData.front(); }
132 inline const_reference front() const { return maData.front(); }
133 inline reference back() { return maData.back(); }
134 inline const_reference back() const { return maData.back(); }
136 inline iterator row_begin( size_type nY ) { return this->at( 0, nY ); }
137 inline const_iterator row_begin( size_type nY ) const { return this->at( 0, nY ); }
138 inline iterator row_end( size_type nY ) { return this->at( mnWidth, nY ); }
139 inline const_iterator row_end( size_type nY ) const { return this->at( mnWidth, nY ); }
141 inline reference row_front( size_type nY ) { return (*this)( 0, nY ); }
142 inline const_reference row_front( size_type nY ) const { return (*this)( 0, nY ); }
143 inline reference row_back( size_type nY ) { return (*this)( mnWidth - 1, nY ); }
144 inline const_reference row_back( size_type nY ) const { return (*this)( mnWidth - 1, nY ); }
146 inline void swap( Matrix& rMatrix ) { maData.swap( rMatrix.maData ); }
148 private:
149 container_type maData;
150 size_type mnWidth;
153 // ============================================================================
155 /** Static helper functions for improved API container handling. */
156 class OOX_DLLPUBLIC ContainerHelper
158 public:
160 /** Returns a name that is not used in the passed name container.
162 @param rxNameAccess com.sun.star.container.XNameAccess interface of
163 the name container.
165 @param rSuggestedName Suggested name for the object.
167 @return An unused name. Will be equal to the suggested name, if not
168 contained, otherwise a numerical index will be appended.
170 static OUString getUnusedName(
171 const ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess >& rxNameAccess,
172 const OUString& rSuggestedName,
173 sal_Unicode cSeparator,
174 sal_Int32 nFirstIndexToAppend = 1 );
176 /** Inserts an object into a name container.
178 @param rxNameContainer com.sun.star.container.XNameContainer interface
179 of the name container.
181 @param rName Exact name for the object.
183 @param rObject The object to be inserted.
185 @return True = object successfully inserted.
187 static bool insertByName(
188 const ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameContainer >& rxNameContainer,
189 const OUString& rName,
190 const ::com::sun::star::uno::Any& rObject,
191 bool bReplaceOldExisting = true );
193 /** Inserts an object into a name container.
195 The function will use an unused name to insert the object, based on the
196 suggested object name. It is possible to specify whether the existing
197 object or the new inserted object will be renamed, if the container
198 already has an object with the name suggested for the new object.
200 @param rxNameContainer com.sun.star.container.XNameContainer interface
201 of the name container.
203 @param rSuggestedName Suggested name for the object.
205 @param rObject The object to be inserted.
207 @param bRenameOldExisting Specifies behaviour if an object with the
208 suggested name already exists. If false (default), the new object
209 will be inserted with a name not yet extant in the container (this
210 is done by appending a numerical index to the suggested name). If
211 true, the existing object will be removed and inserted with an
212 unused name, and the new object will be inserted with the suggested
213 name.
215 @return The final name the object is inserted with. Will always be
216 equal to the suggested name, if parameter bRenameOldExisting is
217 true.
219 static OUString insertByUnusedName(
220 const ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameContainer >& rxNameContainer,
221 const OUString& rSuggestedName,
222 sal_Unicode cSeparator,
223 const ::com::sun::star::uno::Any& rObject,
224 bool bRenameOldExisting = false );
226 // std::vector and std::map element access --------------------------------
228 /** Returns the pointer to an existing element of the passed vector, or a
229 null pointer, if the passed index is out of bounds. */
230 template< typename VectorType >
231 static const typename VectorType::value_type*
232 getVectorElement( const VectorType& rVector, sal_Int32 nIndex );
234 /** Returns the pointer to an existing element of the passed vector, or a
235 null pointer, if the passed index is out of bounds. */
236 template< typename VectorType >
237 static typename VectorType::value_type*
238 getVectorElementAccess( VectorType& rVector, sal_Int32 nIndex );
240 /** Returns the reference to an existing element of the passed vector, or
241 the passed default value, if the passed index is out of bounds. */
242 template< typename VectorType >
243 static const typename VectorType::value_type&
244 getVectorElement( const VectorType& rVector, sal_Int32 nIndex, const typename VectorType::value_type& rDefault );
246 /** Returns the reference to an existing element of the passed vector, or
247 the passed default value, if the passed index is out of bounds. */
248 template< typename VectorType >
249 static typename VectorType::value_type&
250 getVectorElementAccess( VectorType& rVector, sal_Int32 nIndex, typename VectorType::value_type& rDefault );
252 /** Returns the pointer to an existing element of the passed map, or a null
253 pointer, if an element with the passed key does not exist. */
254 template< typename MapType >
255 static const typename MapType::mapped_type*
256 getMapElement( const MapType& rMap, const typename MapType::key_type& rKey );
258 /** Returns the pointer to an existing element of the passed map, or a null
259 pointer, if an element with the passed key does not exist. */
260 template< typename MapType >
261 static typename MapType::mapped_type*
262 getMapElementAccess( MapType& rMap, const typename MapType::key_type& rKey );
264 /** Returns the reference to an existing element of the passed map, or the
265 passed default value, if an element with the passed key does not exist. */
266 template< typename MapType >
267 static const typename MapType::mapped_type&
268 getMapElement( const MapType& rMap, const typename MapType::key_type& rKey, const typename MapType::mapped_type& rDefault );
270 /** Returns the reference to an existing element of the passed map, or the
271 passed default value, if an element with the passed key does not exist. */
272 template< typename MapType >
273 static typename MapType::mapped_type&
274 getMapElementAccess( MapType& rMap, const typename MapType::key_type& rKey, typename MapType::mapped_type& rDefault );
276 // vector/map/matrix to UNO sequence --------------------------------------
278 /** Creates a UNO sequence from a std::vector with copies of all elements.
280 @param rVector The vector to be converted to a sequence.
282 @return A com.sun.star.uno.Sequence object with copies of all objects
283 contained in the passed vector.
285 template< typename VectorType >
286 static ::com::sun::star::uno::Sequence< typename VectorType::value_type >
287 vectorToSequence( const VectorType& rVector );
289 /** Creates a UNO sequence from a std::map with copies of all elements.
291 @param rMap The map to be converted to a sequence.
293 @return A com.sun.star.uno.Sequence object with copies of all objects
294 contained in the passed map.
296 template< typename MapType >
297 static ::com::sun::star::uno::Sequence< typename MapType::mapped_type >
298 mapToSequence( const MapType& rMap );
300 /** Creates a UNO sequence of sequences from a matrix with copies of all elements.
302 @param rMatrix The matrix to be converted to a sequence of sequences.
304 @return A com.sun.star.uno.Sequence object containing
305 com.sun.star.uno.Sequence objects with copies of all objects
306 contained in the passed matrix.
308 template< typename MatrixType >
309 static ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Sequence< typename MatrixType::value_type > >
310 matrixToSequenceSequence( const MatrixType& rMatrix );
313 // ----------------------------------------------------------------------------
315 template< typename VectorType >
316 /*static*/ const typename VectorType::value_type* ContainerHelper::getVectorElement( const VectorType& rVector, sal_Int32 nIndex )
318 return ((0 <= nIndex) && (static_cast< size_t >( nIndex ) < rVector.size())) ? &rVector[ static_cast< size_t >( nIndex ) ] : 0;
321 template< typename VectorType >
322 /*static*/ typename VectorType::value_type* ContainerHelper::getVectorElementAccess( VectorType& rVector, sal_Int32 nIndex )
324 return ((0 <= nIndex) && (static_cast< size_t >( nIndex ) < rVector.size())) ? &rVector[ static_cast< size_t >( nIndex ) ] : 0;
327 template< typename VectorType >
328 /*static*/ const typename VectorType::value_type& ContainerHelper::getVectorElement( const VectorType& rVector, sal_Int32 nIndex, const typename VectorType::value_type& rDefault )
330 return ((0 <= nIndex) && (static_cast< size_t >( nIndex ) < rVector.size())) ? rVector[ static_cast< size_t >( nIndex ) ] : rDefault;
333 template< typename VectorType >
334 /*static*/ typename VectorType::value_type& ContainerHelper::getVectorElementAccess( VectorType& rVector, sal_Int32 nIndex, typename VectorType::value_type& rDefault )
336 return ((0 <= nIndex) && (static_cast< size_t >( nIndex ) < rVector.size())) ? rVector[ static_cast< size_t >( nIndex ) ] : rDefault;
339 template< typename MapType >
340 /*static*/ const typename MapType::mapped_type* ContainerHelper::getMapElement( const MapType& rMap, const typename MapType::key_type& rKey )
342 typename MapType::const_iterator aIt = rMap.find( rKey );
343 return (aIt == rMap.end()) ? 0 : &aIt->second;
346 template< typename MapType >
347 /*static*/ typename MapType::mapped_type* ContainerHelper::getMapElementAccess( MapType& rMap, const typename MapType::key_type& rKey )
349 typename MapType::iterator aIt = rMap.find( rKey );
350 return (aIt == rMap.end()) ? 0 : &aIt->second;
353 template< typename MapType >
354 /*static*/ const typename MapType::mapped_type& ContainerHelper::getMapElement( const MapType& rMap, const typename MapType::key_type& rKey, const typename MapType::mapped_type& rDefault )
356 typename MapType::const_iterator aIt = rMap.find( rKey );
357 return (aIt == rMap.end()) ? rDefault : aIt->second;
360 template< typename MapType >
361 /*static*/ typename MapType::mapped_type& ContainerHelper::getMapElementAccess( MapType& rMap, const typename MapType::key_type& rKey, typename MapType::mapped_type& rDefault )
363 typename MapType::iterator aIt = rMap.find( rKey );
364 return (aIt == rMap.end()) ? rDefault : aIt->second;
367 template< typename VectorType >
368 /*static*/ ::com::sun::star::uno::Sequence< typename VectorType::value_type > ContainerHelper::vectorToSequence( const VectorType& rVector )
370 typedef typename VectorType::value_type ValueType;
371 if( rVector.empty() )
372 return ::com::sun::star::uno::Sequence< ValueType >();
373 return ::com::sun::star::uno::Sequence< ValueType >( &rVector.front(), static_cast< sal_Int32 >( rVector.size() ) );
376 template< typename MapType >
377 /*static*/ ::com::sun::star::uno::Sequence< typename MapType::mapped_type > ContainerHelper::mapToSequence( const MapType& rMap )
379 typedef typename MapType::mapped_type ValueType;
380 if( rMap.empty() )
381 return ::com::sun::star::uno::Sequence< ValueType >();
382 ::com::sun::star::uno::Sequence< ValueType > aSeq( static_cast< sal_Int32 >( rMap.size() ) );
383 sal_Int32 nIndex = 0;
384 for( typename MapType::const_iterator aIt = rMap.begin(), aEnd = rMap.end(); aIt != aEnd; ++aIt, ++nIndex )
385 aSeq[ nIndex ] = *aIt;
386 return aSeq;
389 template< typename MatrixType >
390 /*static*/ ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Sequence< typename MatrixType::value_type > > ContainerHelper::matrixToSequenceSequence( const MatrixType& rMatrix )
392 typedef typename MatrixType::value_type ValueType;
393 ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Sequence< ValueType > > aSeq;
394 if( !rMatrix.empty() )
396 aSeq.realloc( static_cast< sal_Int32 >( rMatrix.height() ) );
397 for( size_t nRow = 0, nHeight = rMatrix.height(); nRow < nHeight; ++nRow )
398 aSeq[ static_cast< sal_Int32 >( nRow ) ] =
399 ::com::sun::star::uno::Sequence< ValueType >( &rMatrix.row_front( nRow ), static_cast< sal_Int32 >( rMatrix.width() ) );
401 return aSeq;
404 // ============================================================================
406 } // namespace oox
408 #endif
410 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */