LibreOffice Module oox (master)  1
containerhelper.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 #ifndef INCLUDED_OOX_HELPER_CONTAINERHELPER_HXX
21 #define INCLUDED_OOX_HELPER_CONTAINERHELPER_HXX
22 
23 #include <cstddef>
24 #include <vector>
25 
26 #include <com/sun/star/uno/Reference.hxx>
27 #include <com/sun/star/uno/Sequence.hxx>
28 #include <oox/dllapi.h>
29 #include <rtl/ustring.hxx>
30 #include <sal/types.h>
31 
32 namespace com::sun::star {
33  namespace container { class XNameAccess; }
34  namespace container { class XNameContainer; }
35  namespace uno { class Any; }
36 }
37 
38 namespace oox {
39 
40 
42 struct ValueRange
43 {
44  sal_Int32 mnFirst;
45  sal_Int32 mnLast;
46 
47  explicit ValueRange( sal_Int32 nValue ) : mnFirst( nValue ), mnLast( nValue ) {}
48  explicit ValueRange( sal_Int32 nFirst, sal_Int32 nLast ) : mnFirst( nFirst ), mnLast( nLast ) {}
49 
50  bool operator==( const ValueRange& rRange ) const { return (mnFirst == rRange.mnFirst) && (mnLast == rRange.mnLast); }
51  bool operator!=( const ValueRange& rRange ) const { return !(*this == rRange); }
52  bool contains( const ValueRange& rRange ) const { return (mnFirst <= rRange.mnFirst) && (rRange.mnLast <= mnLast); }
53  bool intersects( const ValueRange& rRange ) const { return (mnFirst <= rRange.mnLast) && (rRange.mnFirst <= mnLast); }
54 };
55 
56 
57 typedef ::std::vector< ValueRange > ValueRangeVector;
58 
59 
64 {
65 public:
67 
69  void insert( const ValueRange& rRange );
70 
72  const ValueRangeVector& getRanges() const { return maRanges; }
73 
74 private:
75  ValueRangeVector maRanges;
76 };
77 
78 
84 template< typename Type >
85 class Matrix
86 {
87 public:
88  typedef ::std::vector< Type > container_type;
89  typedef typename container_type::value_type value_type;
90  typedef typename container_type::pointer pointer;
91  typedef typename container_type::reference reference;
92  typedef typename container_type::const_reference const_reference;
93  typedef typename container_type::size_type size_type;
94  typedef typename container_type::iterator iterator;
95  typedef typename container_type::const_iterator const_iterator;
96 
97  Matrix() : mnWidth( 0 ) {}
98  explicit Matrix( size_type nWidth, size_type nHeight ) { resize( nWidth, nHeight ); }
99  explicit Matrix( size_type nWidth, size_type nHeight, const_reference rData ) { resize( nWidth, nHeight, rData ); }
100 
101  bool empty() const { return maData.empty(); }
102  size_type size() const { return maData.size(); }
103  size_type width() const { return mnWidth; }
104  size_type height() const { return empty() ? 0 : (size() / width()); }
105 
106  void clear() { resize( 0, 0 ); }
107  void resize( size_type nWidth, size_type nHeight ) { mnWidth = nWidth; maData.resize( nWidth * nHeight ); }
108  void resize( size_type nWidth, size_type nHeight, const_reference rData ) { mnWidth = nWidth; maData.resize( nWidth * nHeight, rData ); }
109 
110  iterator at( size_type nX, size_type nY ) { return maData.begin() + mnWidth * nY + nX; }
111  const_iterator at( size_type nX, size_type nY ) const { return maData.begin() + mnWidth * nY + nX; }
112 
113  reference operator()( size_type nX, size_type nY ) { return *at( nX, nY ); }
114  const_reference operator()( size_type nX, size_type nY ) const { return *at( nX, nY ); }
115 
116  iterator begin() { return maData.begin(); }
117  const_iterator begin() const { return maData.begin(); }
118  iterator end() { return maData.end(); }
119  const_iterator end() const { return maData.end(); }
120 
121  iterator row_begin( size_type nY ) { return at( 0, nY ); }
122  const_iterator row_begin( size_type nY ) const { return at( 0, nY ); }
123  iterator row_end( size_type nY ) { return at( mnWidth, nY ); }
124  const_iterator row_end( size_type nY ) const { return at( mnWidth, nY ); }
125 
126  reference row_front( size_type nY ) { return (*this)( 0, nY ); }
127  const_reference row_front( size_type nY ) const { return (*this)( 0, nY ); }
128 
129 private:
130  container_type maData;
131  size_type mnWidth;
132 };
133 
134 
137 {
138 public:
139 
150  static OUString getUnusedName(
151  const css::uno::Reference< css::container::XNameAccess >& rxNameAccess,
152  const OUString& rSuggestedName,
153  sal_Unicode cSeparator );
154 
166  static bool insertByName(
167  const css::uno::Reference< css::container::XNameContainer >& rxNameContainer,
168  const OUString& rName,
169  const css::uno::Any& rObject );
170 
193  static OUString insertByUnusedName(
194  const css::uno::Reference< css::container::XNameContainer >& rxNameContainer,
195  const OUString& rSuggestedName,
196  sal_Unicode cSeparator,
197  const css::uno::Any& rObject );
198 
199  // std::vector and std::map element access --------------------------------
200 
203  template< typename VectorType >
204  static const typename VectorType::value_type*
205  getVectorElement( const VectorType& rVector, sal_Int32 nIndex );
206 
209  template< typename VectorType >
210  static typename VectorType::value_type*
211  getVectorElementAccess( VectorType& rVector, sal_Int32 nIndex );
212 
215  template< typename VectorType >
216  static const typename VectorType::value_type&
217  getVectorElement( const VectorType& rVector, sal_Int32 nIndex, const typename VectorType::value_type& rDefault );
218 
221  template< typename MapType >
222  static const typename MapType::mapped_type*
223  getMapElement( const MapType& rMap, const typename MapType::key_type& rKey );
224 
227  template< typename MapType >
228  static const typename MapType::mapped_type&
229  getMapElement( const MapType& rMap, const typename MapType::key_type& rKey, const typename MapType::mapped_type& rDefault );
230 
231  // vector/map/matrix to UNO sequence --------------------------------------
232 
240  template< typename VectorType >
241  static css::uno::Sequence< typename VectorType::value_type >
242  vectorToSequence( const VectorType& rVector );
243 
252  template< typename MatrixType >
253  static css::uno::Sequence< css::uno::Sequence< typename MatrixType::value_type > >
254  matrixToSequenceSequence( const MatrixType& rMatrix );
255 };
256 
257 
258 template< typename VectorType >
259 /*static*/ const typename VectorType::value_type* ContainerHelper::getVectorElement( const VectorType& rVector, sal_Int32 nIndex )
260 {
261  return ((0 <= nIndex) && (static_cast< size_t >( nIndex ) < rVector.size())) ? &rVector[ static_cast< size_t >( nIndex ) ] : 0;
262 }
263 
264 template< typename VectorType >
265 /*static*/ typename VectorType::value_type* ContainerHelper::getVectorElementAccess( VectorType& rVector, sal_Int32 nIndex )
266 {
267  return ((0 <= nIndex) && (static_cast< size_t >( nIndex ) < rVector.size())) ? &rVector[ static_cast< size_t >( nIndex ) ] : 0;
268 }
269 
270 template< typename VectorType >
271 /*static*/ const typename VectorType::value_type& ContainerHelper::getVectorElement( const VectorType& rVector, sal_Int32 nIndex, const typename VectorType::value_type& rDefault )
272 {
273  return ((0 <= nIndex) && (static_cast< size_t >( nIndex ) < rVector.size())) ? rVector[ static_cast< size_t >( nIndex ) ] : rDefault;
274 }
275 
276 template< typename MapType >
277 /*static*/ const typename MapType::mapped_type* ContainerHelper::getMapElement( const MapType& rMap, const typename MapType::key_type& rKey )
278 {
279  typename MapType::const_iterator aIt = rMap.find( rKey );
280  return (aIt == rMap.end()) ? 0 : &aIt->second;
281 }
282 
283 template< typename MapType >
284 /*static*/ const typename MapType::mapped_type& ContainerHelper::getMapElement( const MapType& rMap, const typename MapType::key_type& rKey, const typename MapType::mapped_type& rDefault )
285 {
286  typename MapType::const_iterator aIt = rMap.find( rKey );
287  return (aIt == rMap.end()) ? rDefault : aIt->second;
288 }
289 
290 template< typename VectorType >
291 /*static*/ css::uno::Sequence< typename VectorType::value_type > ContainerHelper::vectorToSequence( const VectorType& rVector )
292 {
293  typedef typename VectorType::value_type ValueType;
294  if( rVector.empty() )
295  return css::uno::Sequence< ValueType >();
296  return css::uno::Sequence<ValueType>(rVector.data(), static_cast<sal_Int32>(rVector.size()));
297 }
298 
299 template< typename MatrixType >
300 /*static*/ css::uno::Sequence< css::uno::Sequence< typename MatrixType::value_type > > ContainerHelper::matrixToSequenceSequence( const MatrixType& rMatrix )
301 {
302  typedef typename MatrixType::value_type ValueType;
303  css::uno::Sequence< css::uno::Sequence< ValueType > > aSeq;
304  if( !rMatrix.empty() )
305  {
306  aSeq.realloc( static_cast< sal_Int32 >( rMatrix.height() ) );
307  for( size_t nRow = 0, nHeight = rMatrix.height(); nRow < nHeight; ++nRow )
308  aSeq[ static_cast< sal_Int32 >( nRow ) ] =
309  css::uno::Sequence< ValueType >( &rMatrix.row_front( nRow ), static_cast< sal_Int32 >( rMatrix.width() ) );
310  }
311  return aSeq;
312 }
313 
314 
315 } // namespace oox
316 
317 #endif
318 
319 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Int32 nIndex
static const VectorType::value_type * getVectorElement(const VectorType &rVector, sal_Int32 nIndex)
Returns the pointer to an existing element of the passed vector, or a null pointer, if the passed index is out of bounds.
Template for a 2-dimensional array of objects.
bool operator!=(const ValueRange &rRange) const
bool contains(const ValueRange &rRange) const
exports com.sun.star. container
sal_uInt16 sal_Unicode
bool operator==(const ValueRange &rRange) const
::std::vector< ValueRange > ValueRangeVector
ValueRangeVector maRanges
ValueRange(sal_Int32 nFirst, sal_Int32 nLast)
static const MapType::mapped_type * getMapElement(const MapType &rMap, const typename MapType::key_type &rKey)
Returns the pointer to an existing element of the passed map, or a null pointer, if an element with t...
An ordered list of value ranges.
ValueType
bool intersects(const ValueRange &rRange) const
static css::uno::Sequence< css::uno::Sequence< typename MatrixType::value_type > > matrixToSequenceSequence(const MatrixType &rMatrix)
Creates a UNO sequence of sequences from a matrix with copies of all elements.
Sequence< sal_Int8 > aSeq
#define OOX_DLLPUBLIC
Definition: dllapi.h:28
static css::uno::Sequence< typename VectorType::value_type > vectorToSequence(const VectorType &rVector)
Creates a UNO sequence from a std::vector with copies of all elements.
const ValueRangeVector & getRanges() const
Returns the ordered list of all value ranges.
A range of signed 32-bit integer values.
Static helper functions for improved API container handling.
static VectorType::value_type * getVectorElementAccess(VectorType &rVector, sal_Int32 nIndex)
Returns the pointer to an existing element of the passed vector, or a null pointer, if the passed index is out of bounds.
ValueRange(sal_Int32 nValue)