update emoji autocorrect entries from po-files
[LibreOffice.git] / include / oox / helper / refvector.hxx
blobc5b13d5c9a2e7c0fd86789481dd3a6e04a159f65
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 INCLUDED_OOX_HELPER_REFVECTOR_HXX
21 #define INCLUDED_OOX_HELPER_REFVECTOR_HXX
23 #include <sal/types.h>
24 #include <boost/bind.hpp>
25 #include <algorithm>
26 #include <memory>
27 #include <vector>
29 namespace oox {
33 /** Template for a vector of ref-counted objects with additional accessor functions.
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.
40 template< typename ObjType >
41 class RefVector : public ::std::vector< std::shared_ptr< ObjType > >
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;
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
53 const value_type* pxRef = getRef( nIndex );
54 return pxRef && pxRef->get();
57 /** Returns a reference to the object with the passed index, or 0 on error. */
58 value_type get( sal_Int32 nIndex ) const
60 if( const value_type* pxRef = getRef( nIndex ) ) return *pxRef;
61 return value_type();
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; }
68 /** Calls the passed functor for every contained object, automatically
69 skips all elements that are empty references. */
70 template< typename FunctorType >
71 void forEach( FunctorType aFunctor ) const
73 ::std::for_each( this->begin(), this->end(), ForEachFunctor< FunctorType >( aFunctor ) );
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 void forEachMem( FuncType pFunc ) const
81 forEach( ::boost::bind( pFunc, _1 ) );
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 void forEachMem( FuncType pFunc, ParamType aParam ) const
89 forEach( ::boost::bind( pFunc, _1, aParam ) );
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 void forEachMem( FuncType pFunc, ParamType1 aParam1, ParamType2 aParam2 ) const
97 forEach( ::boost::bind( pFunc, _1, aParam1, aParam2 ) );
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 void forEachMem( FuncType pFunc, ParamType1 aParam1, ParamType2 aParam2, ParamType3 aParam3 ) const
105 forEach( ::boost::bind( pFunc, _1, aParam1, aParam2, aParam3 ) );
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 void forEachWithIndex( const FunctorType& rFunctor ) const
113 ::std::for_each( this->begin(), this->end(), ForEachFunctorWithIndex< FunctorType >( rFunctor ) );
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
121 forEachWithIndex( ::boost::bind( pFunc, _2, _1 ) );
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
129 forEachWithIndex( ::boost::bind( pFunc, _2, _1, aParam ) );
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 void forEachMemWithIndex( FuncType pFunc, ParamType1 aParam1, ParamType2 aParam2 ) const
137 forEachWithIndex( ::boost::bind( pFunc, _2, _1, aParam1, aParam2 ) );
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
145 forEachWithIndex( ::boost::bind( pFunc, _2, _1, aParam1, aParam2, aParam3 ) );
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 value_type findIf( const FunctorType& rFunctor ) const
153 typename container_type::const_iterator aIt = ::std::find_if( this->begin(), this->end(), FindFunctor< FunctorType >( rFunctor ) );
154 return (aIt == this->end()) ? value_type() : *aIt;
157 private:
158 template< typename FunctorType >
159 struct ForEachFunctor
161 FunctorType maFunctor;
162 explicit ForEachFunctor( const FunctorType& rFunctor ) : maFunctor( rFunctor ) {}
163 void operator()( const value_type& rxValue ) { if( rxValue.get() ) maFunctor( *rxValue ); }
166 template< typename FunctorType >
167 struct ForEachFunctorWithIndex
169 FunctorType maFunctor;
170 sal_Int32 mnIndex;
171 explicit ForEachFunctorWithIndex( const FunctorType& rFunctor ) : maFunctor( rFunctor ), mnIndex( 0 ) {}
172 void operator()( const value_type& rxValue ) { if( rxValue.get() ) maFunctor( mnIndex, *rxValue ); ++mnIndex; }
175 template< typename FunctorType >
176 struct FindFunctor
178 FunctorType maFunctor;
179 explicit FindFunctor( const FunctorType& rFunctor ) : maFunctor( rFunctor ) {}
180 bool operator()( const value_type& rxValue ) { return rxValue.get() && maFunctor( *rxValue ); }
183 const value_type* getRef( sal_Int32 nIndex ) const
185 return ((0 <= nIndex) && (static_cast< size_type >( nIndex ) < this->size())) ?
186 &(*this)[ static_cast< size_type >( nIndex ) ] : 0;
192 } // namespace oox
194 #endif
196 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */