LibreOffice Module oox (master)  1
refvector.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_REFVECTOR_HXX
21 #define INCLUDED_OOX_HELPER_REFVECTOR_HXX
22 
23 #include <algorithm>
24 #include <functional>
25 #include <memory>
26 #include <vector>
27 
28 #include <sal/types.h>
29 
30 namespace oox {
31 
32 
40 template< typename ObjType >
41 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 
51  value_type get( sal_Int32 nIndex ) const
52  {
53  if( const value_type* pxRef = getRef( nIndex ) ) return *pxRef;
54  return value_type();
55  }
56 
59  template< typename FunctorType >
60  void forEach( FunctorType aFunctor ) const
61  {
62  ::std::for_each( this->begin(), this->end(), ForEachFunctor< FunctorType >( aFunctor ) );
63  }
64 
67  template< typename FuncType >
68  void forEachMem( FuncType pFunc ) const
69  {
70  forEach( ::std::bind( pFunc, std::placeholders::_1 ) );
71  }
72 
75  template< typename FuncType, typename ParamType >
76  void forEachMem( FuncType pFunc, ParamType aParam ) const
77  {
78  forEach( ::std::bind( pFunc, std::placeholders::_1, aParam ) );
79  }
80 
83  template< typename FuncType, typename ParamType1, typename ParamType2 >
84  void forEachMem( FuncType pFunc, ParamType1 aParam1, ParamType2 aParam2 ) const
85  {
86  forEach( ::std::bind( pFunc, std::placeholders::_1, aParam1, aParam2 ) );
87  }
88 
91  template< typename FuncType, typename ParamType1, typename ParamType2, typename ParamType3 >
92  void forEachMem( FuncType pFunc, ParamType1 aParam1, ParamType2 aParam2, ParamType3 aParam3 ) const
93  {
94  forEach( ::std::bind( pFunc, std::placeholders::_1, aParam1, aParam2, aParam3 ) );
95  }
96 
99  template< typename FunctorType >
100  void forEachWithIndex( const FunctorType& rFunctor ) const
101  {
102  ::std::for_each( this->begin(), this->end(), ForEachFunctorWithIndex< FunctorType >( rFunctor ) );
103  }
104 
107  template< typename FuncType, typename ParamType1, typename ParamType2 >
108  void forEachMemWithIndex( FuncType pFunc, ParamType1 aParam1, ParamType2 aParam2 ) const
109  {
110  forEachWithIndex( ::std::bind( pFunc, std::placeholders::_2, std::placeholders::_1, aParam1, aParam2 ) );
111  }
112 
115  template< typename FunctorType >
116  value_type findIf( const FunctorType& rFunctor ) const
117  {
118  typename container_type::const_iterator aIt = ::std::find_if( this->begin(), this->end(), FindFunctor< FunctorType >( rFunctor ) );
119  return (aIt == this->end()) ? value_type() : *aIt;
120  }
121 
122 private:
123  template< typename FunctorType >
125  {
126  FunctorType maFunctor;
127  explicit ForEachFunctor( const FunctorType& rFunctor ) : maFunctor( rFunctor ) {}
128  void operator()( const value_type& rxValue ) { if( rxValue.get() ) maFunctor( *rxValue ); }
129  };
130 
131  template< typename FunctorType >
133  {
134  FunctorType maFunctor;
135  sal_Int32 mnIndex;
136  explicit ForEachFunctorWithIndex( const FunctorType& rFunctor ) : maFunctor( rFunctor ), mnIndex( 0 ) {}
137  void operator()( const value_type& rxValue ) {
138  if( rxValue.get() ) maFunctor( mnIndex, *rxValue );
139  ++mnIndex;
140  }
141  };
142 
143  template< typename FunctorType >
144  struct FindFunctor
145  {
146  FunctorType maFunctor;
147  explicit FindFunctor( const FunctorType& rFunctor ) : maFunctor( rFunctor ) {}
148  bool operator()( const value_type& rxValue ) { return rxValue.get() && maFunctor( *rxValue ); }
149  };
150 
151  const value_type* getRef( sal_Int32 nIndex ) const
152  {
153  return ((0 <= nIndex) && (static_cast< size_type >( nIndex ) < this->size())) ?
154  &(*this)[ static_cast< size_type >( nIndex ) ] : 0;
155  }
156 };
157 
158 
159 } // namespace oox
160 
161 #endif
162 
163 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Int32 nIndex
container_type::size_type size_type
Definition: refvector.hxx:46
void forEachMem(FuncType pFunc) const
Calls the passed member function of ObjType on every contained object, automatically skips all elemen...
Definition: refvector.hxx:68
ForEachFunctorWithIndex(const FunctorType &rFunctor)
Definition: refvector.hxx:136
void forEach(FunctorType aFunctor) const
Calls the passed functor for every contained object, automatically skips all elements that are empty ...
Definition: refvector.hxx:60
enumrange< T >::Iterator begin(enumrange< T >)
void operator()(const value_type &rxValue)
Definition: refvector.hxx:128
void forEachMem(FuncType pFunc, ParamType aParam) const
Calls the passed member function of ObjType on every contained object, automatically skips all elemen...
Definition: refvector.hxx:76
value_type findIf(const FunctorType &rFunctor) const
Searches for an element by using the passed functor that takes a constant reference of the object typ...
Definition: refvector.hxx:116
container_type::value_type value_type
Definition: refvector.hxx:45
bool operator()(const value_type &rxValue)
Definition: refvector.hxx:148
void forEachMem(FuncType pFunc, ParamType1 aParam1, ParamType2 aParam2, ParamType3 aParam3) const
Calls the passed member function of ObjType on every contained object, automatically skips all elemen...
Definition: refvector.hxx:92
size
ParamType
void forEachWithIndex(const FunctorType &rFunctor) const
Calls the passed functor for every contained object.
Definition: refvector.hxx:100
FindFunctor(const FunctorType &rFunctor)
Definition: refvector.hxx:147
enumrange< T >::Iterator end(enumrange< T >)
void operator()(const value_type &rxValue)
Definition: refvector.hxx:137
void forEachMemWithIndex(FuncType pFunc, ParamType1 aParam1, ParamType2 aParam2) const
Calls the passed member function of ObjType on every contained object.
Definition: refvector.hxx:108
ForEachFunctor(const FunctorType &rFunctor)
Definition: refvector.hxx:127
::std::vector< std::shared_ptr< ObjType > > container_type
Definition: refvector.hxx:44
Template for a vector of ref-counted objects with additional accessor functions.
Definition: refvector.hxx:41
const value_type * getRef(sal_Int32 nIndex) const
Definition: refvector.hxx:151
void forEachMem(FuncType pFunc, ParamType1 aParam1, ParamType2 aParam2) const
Calls the passed member function of ObjType on every contained object, automatically skips all elemen...
Definition: refvector.hxx:84