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 <utility>
27#include <vector>
28
29#include <sal/types.h>
30
31namespace oox {
32
33
41template< typename ObjType >
42class RefVector : public ::std::vector< std::shared_ptr< ObjType > >
43{
44public:
45 typedef ::std::vector< std::shared_ptr< ObjType > > container_type;
46 typedef typename container_type::value_type value_type;
47 typedef typename container_type::size_type size_type;
48
49public:
50
52 value_type get( sal_Int32 nIndex ) const
53 {
54 if( const value_type* pxRef = getRef( nIndex ) ) return *pxRef;
55 return value_type();
56 }
57
60 template< typename FunctorType >
61 void forEach( FunctorType aFunctor ) const
62 {
63 ::std::for_each( this->begin(), this->end(), ForEachFunctor< FunctorType >( aFunctor ) );
64 }
65
68 template< typename FuncType >
69 void forEachMem( FuncType pFunc ) const
70 {
71 forEach( ::std::bind( pFunc, std::placeholders::_1 ) );
72 }
73
76 template< typename FuncType, typename ParamType >
77 void forEachMem( FuncType pFunc, ParamType aParam ) const
78 {
79 forEach( ::std::bind( pFunc, std::placeholders::_1, aParam ) );
80 }
81
84 template< typename FuncType, typename ParamType1, typename ParamType2 >
85 void forEachMem( FuncType pFunc, ParamType1 aParam1, ParamType2 aParam2 ) const
86 {
87 forEach( ::std::bind( pFunc, std::placeholders::_1, aParam1, aParam2 ) );
88 }
89
92 template< typename FuncType, typename ParamType1, typename ParamType2, typename ParamType3 >
93 void forEachMem( FuncType pFunc, ParamType1 aParam1, ParamType2 aParam2, ParamType3 aParam3 ) const
94 {
95 forEach( ::std::bind( pFunc, std::placeholders::_1, aParam1, aParam2, aParam3 ) );
96 }
97
100 template< typename FunctorType >
101 void forEachWithIndex( const FunctorType& rFunctor ) const
102 {
103 ::std::for_each( this->begin(), this->end(), ForEachFunctorWithIndex< FunctorType >( rFunctor ) );
104 }
105
108 template< typename FuncType, typename ParamType1, typename ParamType2 >
109 void forEachMemWithIndex( FuncType pFunc, ParamType1 aParam1, ParamType2 aParam2 ) const
110 {
111 forEachWithIndex( ::std::bind( pFunc, std::placeholders::_2, std::placeholders::_1, aParam1, aParam2 ) );
112 }
113
116 template< typename FunctorType >
117 value_type findIf( const FunctorType& rFunctor ) const
118 {
119 typename container_type::const_iterator aIt = ::std::find_if( this->begin(), this->end(), FindFunctor< FunctorType >( rFunctor ) );
120 return (aIt == this->end()) ? value_type() : *aIt;
121 }
122
123private:
124 template< typename FunctorType >
126 {
127 FunctorType maFunctor;
128 explicit ForEachFunctor( FunctorType aFunctor ) : maFunctor(std::move( aFunctor )) {}
129 void operator()( const value_type& rxValue ) { if( rxValue.get() ) maFunctor( *rxValue ); }
130 };
131
132 template< typename FunctorType >
134 {
135 FunctorType maFunctor;
136 sal_Int32 mnIndex;
137 explicit ForEachFunctorWithIndex( FunctorType aFunctor ) : maFunctor(std::move( aFunctor )), mnIndex( 0 ) {}
138 void operator()( const value_type& rxValue ) {
139 if( rxValue.get() ) maFunctor( mnIndex, *rxValue );
140 ++mnIndex;
141 }
142 };
143
144 template< typename FunctorType >
146 {
147 FunctorType maFunctor;
148 explicit FindFunctor( const FunctorType& rFunctor ) : maFunctor( rFunctor ) {}
149 bool operator()( const value_type& rxValue ) { return rxValue.get() && maFunctor( *rxValue ); }
150 };
151
152 const value_type* getRef( sal_Int32 nIndex ) const
153 {
154 return ((0 <= nIndex) && (static_cast< size_type >( nIndex ) < this->size())) ?
155 &(*this)[ static_cast< size_type >( nIndex ) ] : nullptr;
156 }
157};
158
159
160} // namespace oox
161
162#endif
163
164/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
ParamType
Template for a vector of ref-counted objects with additional accessor functions.
Definition: refvector.hxx:43
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:93
value_type get(sal_Int32 nIndex) const
Returns a reference to the object with the passed index, or 0 on error.
Definition: refvector.hxx:52
::std::vector< std::shared_ptr< ObjType > > container_type
Definition: refvector.hxx:45
void forEachWithIndex(const FunctorType &rFunctor) const
Calls the passed functor for every contained object.
Definition: refvector.hxx:101
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:117
void forEach(FunctorType aFunctor) const
Calls the passed functor for every contained object, automatically skips all elements that are empty ...
Definition: refvector.hxx:61
const value_type * getRef(sal_Int32 nIndex) const
Definition: refvector.hxx:152
void forEachMemWithIndex(FuncType pFunc, ParamType1 aParam1, ParamType2 aParam2) const
Calls the passed member function of ObjType on every contained object.
Definition: refvector.hxx:109
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:85
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:77
void forEachMem(FuncType pFunc) const
Calls the passed member function of ObjType on every contained object, automatically skips all elemen...
Definition: refvector.hxx:69
container_type::value_type value_type
Definition: refvector.hxx:46
container_type::size_type size_type
Definition: refvector.hxx:47
sal_Int32 nIndex
size
enumrange< T >::Iterator begin(enumrange< T >)
end
FindFunctor(const FunctorType &rFunctor)
Definition: refvector.hxx:148
bool operator()(const value_type &rxValue)
Definition: refvector.hxx:149
ForEachFunctorWithIndex(FunctorType aFunctor)
Definition: refvector.hxx:137
void operator()(const value_type &rxValue)
Definition: refvector.hxx:138
ForEachFunctor(FunctorType aFunctor)
Definition: refvector.hxx:128
void operator()(const value_type &rxValue)
Definition: refvector.hxx:129