bump product version to 4.1.6.2
[LibreOffice.git] / include / oox / helper / refvector.hxx
blobc0602b571c2347a462a0c5a42582c9e720d23013
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_REFVECTOR_HXX
21 #define OOX_HELPER_REFVECTOR_HXX
23 #include <vector>
24 #include <boost/bind.hpp>
25 #include <boost/shared_ptr.hpp>
26 #include <sal/types.h>
28 namespace oox {
30 // ============================================================================
32 /** Template for a vector of ref-counted objects with additional accessor functions.
34 An instance of the class RefVector< Type > stores elements of the type
35 ::boost::shared_ptr< Type >. The new accessor functions has() and get()
36 work correctly for indexes out of the current range, there is no need to
37 check the passed index before.
39 template< typename ObjType >
40 class RefVector : public ::std::vector< ::boost::shared_ptr< ObjType > >
42 public:
43 typedef ::std::vector< ::boost::shared_ptr< ObjType > > container_type;
44 typedef typename container_type::value_type value_type;
45 typedef typename container_type::size_type size_type;
47 public:
48 /** Returns true, if the object with the passed index exists. Returns
49 false, if the vector element exists but is an empty reference. */
50 inline bool has( sal_Int32 nIndex ) const
52 const value_type* pxRef = getRef( nIndex );
53 return pxRef && pxRef->get();
56 /** Returns a reference to the object with the passed index, or 0 on error. */
57 inline value_type get( sal_Int32 nIndex ) const
59 if( const value_type* pxRef = getRef( nIndex ) ) return *pxRef;
60 return value_type();
63 /** Returns the index of the last element, or -1, if the vector is empty.
64 Does *not* check whether the last element is an empty reference. */
65 inline sal_Int32 getLastIndex() const { return static_cast< sal_Int32 >( this->size() ) - 1; }
67 /** Calls the passed functor for every contained object, automatically
68 skips all elements that are empty references. */
69 template< typename FunctorType >
70 inline void forEach( FunctorType aFunctor ) const
72 ::std::for_each( this->begin(), this->end(), ForEachFunctor< FunctorType >( aFunctor ) );
75 /** Calls the passed member function of ObjType on every contained object,
76 automatically skips all elements that are empty references. */
77 template< typename FuncType >
78 inline void forEachMem( FuncType pFunc ) const
80 forEach( ::boost::bind( pFunc, _1 ) );
83 /** Calls the passed member function of ObjType on every contained object,
84 automatically skips all elements that are empty references. */
85 template< typename FuncType, typename ParamType >
86 inline void forEachMem( FuncType pFunc, ParamType aParam ) const
88 forEach( ::boost::bind( pFunc, _1, aParam ) );
91 /** Calls the passed member function of ObjType on every contained object,
92 automatically skips all elements that are empty references. */
93 template< typename FuncType, typename ParamType1, typename ParamType2 >
94 inline void forEachMem( FuncType pFunc, ParamType1 aParam1, ParamType2 aParam2 ) const
96 forEach( ::boost::bind( pFunc, _1, aParam1, aParam2 ) );
99 /** Calls the passed member function of ObjType on every contained object,
100 automatically skips all elements that are empty references. */
101 template< typename FuncType, typename ParamType1, typename ParamType2, typename ParamType3 >
102 inline void forEachMem( FuncType pFunc, ParamType1 aParam1, ParamType2 aParam2, ParamType3 aParam3 ) const
104 forEach( ::boost::bind( pFunc, _1, aParam1, aParam2, aParam3 ) );
107 /** Calls the passed functor for every contained object. Passes the index as
108 first argument and the object reference as second argument to rFunctor. */
109 template< typename FunctorType >
110 inline void forEachWithIndex( const FunctorType& rFunctor ) const
112 ::std::for_each( this->begin(), this->end(), ForEachFunctorWithIndex< FunctorType >( rFunctor ) );
115 /** Calls the passed member function of ObjType on every contained object.
116 Passes the vector index to the member function. */
117 template< typename FuncType >
118 inline void forEachMemWithIndex( FuncType pFunc ) const
120 forEachWithIndex( ::boost::bind( pFunc, _2, _1 ) );
123 /** Calls the passed member function of ObjType on every contained object.
124 Passes the vector index as first argument to the member function. */
125 template< typename FuncType, typename ParamType >
126 inline void forEachMemWithIndex( FuncType pFunc, ParamType aParam ) const
128 forEachWithIndex( ::boost::bind( pFunc, _2, _1, aParam ) );
131 /** Calls the passed member function of ObjType on every contained object.
132 Passes the vector index as first argument to the member function. */
133 template< typename FuncType, typename ParamType1, typename ParamType2 >
134 inline void forEachMemWithIndex( FuncType pFunc, ParamType1 aParam1, ParamType2 aParam2 ) const
136 forEachWithIndex( ::boost::bind( pFunc, _2, _1, aParam1, aParam2 ) );
139 /** Calls the passed member function of ObjType on every contained object.
140 Passes the vector index as first argument to the member function. */
141 template< typename FuncType, typename ParamType1, typename ParamType2, typename ParamType3 >
142 inline void forEachMemWithIndex( FuncType pFunc, ParamType1 aParam1, ParamType2 aParam2, ParamType3 aParam3 ) const
144 forEachWithIndex( ::boost::bind( pFunc, _2, _1, aParam1, aParam2, aParam3 ) );
147 /** Searches for an element by using the passed functor that takes a
148 constant reference of the object type (const ObjType&). */
149 template< typename FunctorType >
150 inline value_type findIf( const FunctorType& rFunctor ) const
152 typename container_type::const_iterator aIt = ::std::find_if( this->begin(), this->end(), FindFunctor< FunctorType >( rFunctor ) );
153 return (aIt == this->end()) ? value_type() : *aIt;
156 private:
157 template< typename FunctorType >
158 struct ForEachFunctor
160 FunctorType maFunctor;
161 inline explicit ForEachFunctor( const FunctorType& rFunctor ) : maFunctor( rFunctor ) {}
162 inline void operator()( const value_type& rxValue ) { if( rxValue.get() ) maFunctor( *rxValue ); }
165 template< typename FunctorType >
166 struct ForEachFunctorWithIndex
168 FunctorType maFunctor;
169 sal_Int32 mnIndex;
170 inline explicit ForEachFunctorWithIndex( const FunctorType& rFunctor ) : maFunctor( rFunctor ), mnIndex( 0 ) {}
171 inline void operator()( const value_type& rxValue ) { if( rxValue.get() ) maFunctor( mnIndex, *rxValue ); ++mnIndex; }
174 template< typename FunctorType >
175 struct FindFunctor
177 FunctorType maFunctor;
178 inline explicit FindFunctor( const FunctorType& rFunctor ) : maFunctor( rFunctor ) {}
179 inline bool operator()( const value_type& rxValue ) { return rxValue.get() && maFunctor( *rxValue ); }
182 inline const value_type* getRef( sal_Int32 nIndex ) const
184 return ((0 <= nIndex) && (static_cast< size_type >( nIndex ) < this->size())) ?
185 &(*this)[ static_cast< size_type >( nIndex ) ] : 0;
189 // ============================================================================
191 } // namespace oox
193 #endif
195 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */