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 INCLUDED_OOX_HELPER_REFVECTOR_HXX
21 : #define INCLUDED_OOX_HELPER_REFVECTOR_HXX
22 :
23 : #include <sal/types.h>
24 : #include <boost/bind.hpp>
25 : #include <algorithm>
26 : #include <memory>
27 : #include <vector>
28 :
29 : namespace oox {
30 :
31 :
32 :
33 : /** Template for a vector of ref-counted objects with additional accessor functions.
34 :
35 : An instance of the class RefVector< Type > stores elements of the type
36 : std::shared_ptr< Type >. The new accessor functions has() and get()
37 : work correctly for indexes out of the current range, there is no need to
38 : check the passed index before.
39 : */
40 : template< typename ObjType >
41 133078 : class RefVector : public ::std::vector< std::shared_ptr< ObjType > >
42 : {
43 : public:
44 : typedef ::std::vector< std::shared_ptr< ObjType > > container_type;
45 : typedef typename container_type::value_type value_type;
46 : typedef typename container_type::size_type size_type;
47 :
48 : public:
49 : /** Returns true, if the object with the passed index exists. Returns
50 : false, if the vector element exists but is an empty reference. */
51 : bool has( sal_Int32 nIndex ) const
52 : {
53 : const value_type* pxRef = getRef( nIndex );
54 : return pxRef && pxRef->get();
55 : }
56 :
57 : /** Returns a reference to the object with the passed index, or 0 on error. */
58 16416 : value_type get( sal_Int32 nIndex ) const
59 : {
60 16416 : if( const value_type* pxRef = getRef( nIndex ) ) return *pxRef;
61 462 : return value_type();
62 : }
63 :
64 : /** Returns the index of the last element, or -1, if the vector is empty.
65 : Does *not* check whether the last element is an empty reference. */
66 : sal_Int32 getLastIndex() const { return static_cast< sal_Int32 >( this->size() ) - 1; }
67 :
68 : /** Calls the passed functor for every contained object, automatically
69 : skips all elements that are empty references. */
70 : template< typename FunctorType >
71 3024 : void forEach( FunctorType aFunctor ) const
72 : {
73 3024 : ::std::for_each( this->begin(), this->end(), ForEachFunctor< FunctorType >( aFunctor ) );
74 3024 : }
75 :
76 : /** Calls the passed member function of ObjType on every contained object,
77 : automatically skips all elements that are empty references. */
78 : template< typename FuncType >
79 2871 : void forEachMem( FuncType pFunc ) const
80 : {
81 2871 : forEach( ::boost::bind( pFunc, _1 ) );
82 2871 : }
83 :
84 : /** Calls the passed member function of ObjType on every contained object,
85 : automatically skips all elements that are empty references. */
86 : template< typename FuncType, typename ParamType >
87 149 : void forEachMem( FuncType pFunc, ParamType aParam ) const
88 : {
89 149 : forEach( ::boost::bind( pFunc, _1, aParam ) );
90 149 : }
91 :
92 : /** Calls the passed member function of ObjType on every contained object,
93 : automatically skips all elements that are empty references. */
94 : template< typename FuncType, typename ParamType1, typename ParamType2 >
95 0 : void forEachMem( FuncType pFunc, ParamType1 aParam1, ParamType2 aParam2 ) const
96 : {
97 0 : forEach( ::boost::bind( pFunc, _1, aParam1, aParam2 ) );
98 0 : }
99 :
100 : /** Calls the passed member function of ObjType on every contained object,
101 : automatically skips all elements that are empty references. */
102 : template< typename FuncType, typename ParamType1, typename ParamType2, typename ParamType3 >
103 2 : void forEachMem( FuncType pFunc, ParamType1 aParam1, ParamType2 aParam2, ParamType3 aParam3 ) const
104 : {
105 2 : forEach( ::boost::bind( pFunc, _1, aParam1, aParam2, aParam3 ) );
106 2 : }
107 :
108 : /** Calls the passed functor for every contained object. Passes the index as
109 : first argument and the object reference as second argument to rFunctor. */
110 : template< typename FunctorType >
111 2 : void forEachWithIndex( const FunctorType& rFunctor ) const
112 : {
113 2 : ::std::for_each( this->begin(), this->end(), ForEachFunctorWithIndex< FunctorType >( rFunctor ) );
114 2 : }
115 :
116 : /** Calls the passed member function of ObjType on every contained object.
117 : Passes the vector index to the member function. */
118 : template< typename FuncType >
119 : void forEachMemWithIndex( FuncType pFunc ) const
120 : {
121 : forEachWithIndex( ::boost::bind( pFunc, _2, _1 ) );
122 : }
123 :
124 : /** Calls the passed member function of ObjType on every contained object.
125 : Passes the vector index as first argument to the member function. */
126 : template< typename FuncType, typename ParamType >
127 : void forEachMemWithIndex( FuncType pFunc, ParamType aParam ) const
128 : {
129 : forEachWithIndex( ::boost::bind( pFunc, _2, _1, aParam ) );
130 : }
131 :
132 : /** Calls the passed member function of ObjType on every contained object.
133 : Passes the vector index as first argument to the member function. */
134 : template< typename FuncType, typename ParamType1, typename ParamType2 >
135 2 : void forEachMemWithIndex( FuncType pFunc, ParamType1 aParam1, ParamType2 aParam2 ) const
136 : {
137 2 : forEachWithIndex( ::boost::bind( pFunc, _2, _1, aParam1, aParam2 ) );
138 2 : }
139 :
140 : /** Calls the passed member function of ObjType on every contained object.
141 : Passes the vector index as first argument to the member function. */
142 : template< typename FuncType, typename ParamType1, typename ParamType2, typename ParamType3 >
143 : void forEachMemWithIndex( FuncType pFunc, ParamType1 aParam1, ParamType2 aParam2, ParamType3 aParam3 ) const
144 : {
145 : forEachWithIndex( ::boost::bind( pFunc, _2, _1, aParam1, aParam2, aParam3 ) );
146 : }
147 :
148 : /** Searches for an element by using the passed functor that takes a
149 : constant reference of the object type (const ObjType&). */
150 : template< typename FunctorType >
151 3 : value_type findIf( const FunctorType& rFunctor ) const
152 : {
153 3 : typename container_type::const_iterator aIt = ::std::find_if( this->begin(), this->end(), FindFunctor< FunctorType >( rFunctor ) );
154 3 : return (aIt == this->end()) ? value_type() : *aIt;
155 : }
156 :
157 : private:
158 : template< typename FunctorType >
159 : struct ForEachFunctor
160 : {
161 : FunctorType maFunctor;
162 3024 : explicit ForEachFunctor( const FunctorType& rFunctor ) : maFunctor( rFunctor ) {}
163 5177 : void operator()( const value_type& rxValue ) { if( rxValue.get() ) maFunctor( *rxValue ); }
164 : };
165 :
166 : template< typename FunctorType >
167 : struct ForEachFunctorWithIndex
168 : {
169 : FunctorType maFunctor;
170 : sal_Int32 mnIndex;
171 2 : explicit ForEachFunctorWithIndex( const FunctorType& rFunctor ) : maFunctor( rFunctor ), mnIndex( 0 ) {}
172 6 : void operator()( const value_type& rxValue ) { if( rxValue.get() ) maFunctor( mnIndex, *rxValue ); ++mnIndex; }
173 : };
174 :
175 : template< typename FunctorType >
176 : struct FindFunctor
177 : {
178 : FunctorType maFunctor;
179 3 : explicit FindFunctor( const FunctorType& rFunctor ) : maFunctor( rFunctor ) {}
180 2 : bool operator()( const value_type& rxValue ) { return rxValue.get() && maFunctor( *rxValue ); }
181 : };
182 :
183 16416 : const value_type* getRef( sal_Int32 nIndex ) const
184 : {
185 15954 : return ((0 <= nIndex) && (static_cast< size_type >( nIndex ) < this->size())) ?
186 32370 : &(*this)[ static_cast< size_type >( nIndex ) ] : 0;
187 : }
188 : };
189 :
190 :
191 :
192 : } // namespace oox
193 :
194 : #endif
195 :
196 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|