Line data Source code
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 OOX_HELPER_REFVECTOR_HXX
21 : #define OOX_HELPER_REFVECTOR_HXX
22 :
23 : #include <vector>
24 : #include <boost/bind.hpp>
25 : #include <boost/shared_ptr.hpp>
26 : #include <sal/types.h>
27 :
28 : namespace oox {
29 :
30 : // ============================================================================
31 :
32 : /** Template for a vector of ref-counted objects with additional accessor functions.
33 :
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.
38 : */
39 : template< typename ObjType >
40 3812 : class RefVector : public ::std::vector< ::boost::shared_ptr< ObjType > >
41 : {
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;
46 :
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
51 : {
52 : const value_type* pxRef = getRef( nIndex );
53 : return pxRef && pxRef->get();
54 : }
55 :
56 : /** Returns a reference to the object with the passed index, or 0 on error. */
57 1537 : inline value_type get( sal_Int32 nIndex ) const
58 : {
59 1537 : if( const value_type* pxRef = getRef( nIndex ) ) return *pxRef;
60 46 : return value_type();
61 : }
62 :
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; }
66 :
67 : /** Calls the passed functor for every contained object, automatically
68 : skips all elements that are empty references. */
69 : template< typename FunctorType >
70 249 : inline void forEach( FunctorType aFunctor ) const
71 : {
72 249 : ::std::for_each( this->begin(), this->end(), ForEachFunctor< FunctorType >( aFunctor ) );
73 249 : }
74 :
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 249 : inline void forEachMem( FuncType pFunc ) const
79 : {
80 249 : forEach( ::boost::bind( pFunc, _1 ) );
81 249 : }
82 :
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 0 : inline void forEachMem( FuncType pFunc, ParamType aParam ) const
87 : {
88 0 : forEach( ::boost::bind( pFunc, _1, aParam ) );
89 0 : }
90 :
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 0 : inline void forEachMem( FuncType pFunc, ParamType1 aParam1, ParamType2 aParam2 ) const
95 : {
96 0 : forEach( ::boost::bind( pFunc, _1, aParam1, aParam2 ) );
97 0 : }
98 :
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 0 : inline void forEachMem( FuncType pFunc, ParamType1 aParam1, ParamType2 aParam2, ParamType3 aParam3 ) const
103 : {
104 0 : forEach( ::boost::bind( pFunc, _1, aParam1, aParam2, aParam3 ) );
105 0 : }
106 :
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 0 : inline void forEachWithIndex( const FunctorType& rFunctor ) const
111 : {
112 0 : ::std::for_each( this->begin(), this->end(), ForEachFunctorWithIndex< FunctorType >( rFunctor ) );
113 0 : }
114 :
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
119 : {
120 : forEachWithIndex( ::boost::bind( pFunc, _2, _1 ) );
121 : }
122 :
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
127 : {
128 : forEachWithIndex( ::boost::bind( pFunc, _2, _1, aParam ) );
129 : }
130 :
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 0 : inline void forEachMemWithIndex( FuncType pFunc, ParamType1 aParam1, ParamType2 aParam2 ) const
135 : {
136 0 : forEachWithIndex( ::boost::bind( pFunc, _2, _1, aParam1, aParam2 ) );
137 0 : }
138 :
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
143 : {
144 : forEachWithIndex( ::boost::bind( pFunc, _2, _1, aParam1, aParam2, aParam3 ) );
145 : }
146 :
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 1 : inline value_type findIf( const FunctorType& rFunctor ) const
151 : {
152 1 : typename container_type::const_iterator aIt = ::std::find_if( this->begin(), this->end(), FindFunctor< FunctorType >( rFunctor ) );
153 1 : return (aIt == this->end()) ? value_type() : *aIt;
154 : }
155 :
156 : private:
157 : template< typename FunctorType >
158 : struct ForEachFunctor
159 : {
160 : FunctorType maFunctor;
161 249 : inline explicit ForEachFunctor( const FunctorType& rFunctor ) : maFunctor( rFunctor ) {}
162 524 : inline void operator()( const value_type& rxValue ) { if( rxValue.get() ) maFunctor( *rxValue ); }
163 : };
164 :
165 : template< typename FunctorType >
166 : struct ForEachFunctorWithIndex
167 : {
168 : FunctorType maFunctor;
169 : sal_Int32 mnIndex;
170 0 : inline explicit ForEachFunctorWithIndex( const FunctorType& rFunctor ) : maFunctor( rFunctor ), mnIndex( 0 ) {}
171 0 : inline void operator()( const value_type& rxValue ) { if( rxValue.get() ) maFunctor( mnIndex, *rxValue ); ++mnIndex; }
172 : };
173 :
174 : template< typename FunctorType >
175 : struct FindFunctor
176 : {
177 : FunctorType maFunctor;
178 1 : inline explicit FindFunctor( const FunctorType& rFunctor ) : maFunctor( rFunctor ) {}
179 1 : inline bool operator()( const value_type& rxValue ) { return rxValue.get() && maFunctor( *rxValue ); }
180 : };
181 :
182 1537 : inline const value_type* getRef( sal_Int32 nIndex ) const
183 : {
184 : return ((0 <= nIndex) && (static_cast< size_type >( nIndex ) < this->size())) ?
185 1537 : &(*this)[ static_cast< size_type >( nIndex ) ] : 0;
186 : }
187 : };
188 :
189 : // ============================================================================
190 :
191 : } // namespace oox
192 :
193 : #endif
194 :
195 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|