1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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_REFMAP_HXX
21 #define INCLUDED_OOX_HELPER_REFMAP_HXX
23 #include <sal/types.h>
27 #include <boost/bind.hpp>
33 /** Template for a map of ref-counted objects with additional accessor functions.
35 An instance of the class RefMap< Type > stores elements of the type
36 std::shared_ptr< Type >. The new accessor functions has() and get()
37 work correctly for nonexisting keys, there is no need to check the passed
40 template< typename KeyType
, typename ObjType
, typename CompType
= std::less
< KeyType
> >
41 class RefMap
: public std::map
< KeyType
, std::shared_ptr
< ObjType
>, CompType
>
44 typedef std::map
< KeyType
, std::shared_ptr
< ObjType
>, CompType
> container_type
;
45 typedef typename
container_type::key_type key_type
;
46 typedef typename
container_type::mapped_type mapped_type
;
47 typedef typename
container_type::value_type value_type
;
48 typedef typename
container_type::key_compare key_compare
;
51 /** Returns true, if the object associated to the passed key exists.
52 Returns false, if the key exists but points to an empty reference. */
53 bool has( key_type nKey
) const
55 const mapped_type
* pxRef
= getRef( nKey
);
56 return pxRef
&& pxRef
->get();
59 /** Returns a reference to the object associated to the passed key, or an
60 empty reference on error. */
61 mapped_type
get( key_type nKey
) const
63 if( const mapped_type
* pxRef
= getRef( nKey
) ) return *pxRef
;
67 /** Calls the passed functor for every contained object, automatically
68 skips all elements that are empty references. */
69 template< typename FunctorType
>
70 void forEach( const FunctorType
& rFunctor
) const
72 std::for_each( this->begin(), this->end(), ForEachFunctor
< FunctorType
>( rFunctor
) );
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 void forEachMem( FuncType pFunc
) const
80 forEach( ::boost::bind( pFunc
, _1
) );
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 void forEachMem( FuncType pFunc
, ParamType aParam
) const
88 forEach( ::boost::bind( pFunc
, _1
, aParam
) );
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 void forEachMem( FuncType pFunc
, ParamType1 aParam1
, ParamType2 aParam2
) const
96 forEach( ::boost::bind( pFunc
, _1
, aParam1
, aParam2
) );
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 void forEachMem( FuncType pFunc
, ParamType1 aParam1
, ParamType2 aParam2
, ParamType3 aParam3
) const
104 forEach( ::boost::bind( pFunc
, _1
, aParam1
, aParam2
, aParam3
) );
107 /** Calls the passed member function of ObjType on every contained object,
108 automatically skips all elements that are empty references. */
109 template< typename FuncType
, typename ParamType1
, typename ParamType2
, typename ParamType3
, typename ParamType4
>
110 void forEachMem( FuncType pFunc
, ParamType1 aParam1
, ParamType2 aParam2
, ParamType3 aParam3
, ParamType4 aParam4
) const
112 forEach( ::boost::bind( pFunc
, _1
, aParam1
, aParam2
, aParam3
, aParam4
) );
116 /** Calls the passed functor for every contained object. Passes the key as
117 first argument and the object reference as second argument to rFunctor. */
118 template< typename FunctorType
>
119 void forEachWithKey( const FunctorType
& rFunctor
) const
121 std::for_each( this->begin(), this->end(), ForEachFunctorWithKey
< FunctorType
>( rFunctor
) );
124 /** Calls the passed member function of ObjType on every contained object.
125 Passes the object key as argument to the member function. */
126 template< typename FuncType
>
127 void forEachMemWithKey( FuncType pFunc
) const
129 forEachWithKey( ::boost::bind( pFunc
, _2
, _1
) );
132 /** Calls the passed member function of ObjType on every contained object.
133 Passes the object key as first argument to the member function. */
134 template< typename FuncType
, typename ParamType
>
135 void forEachMemWithKey( FuncType pFunc
, ParamType aParam
) const
137 forEachWithKey( ::boost::bind( pFunc
, _2
, _1
, aParam
) );
140 /** Calls the passed member function of ObjType on every contained object.
141 Passes the object key as first argument to the member function. */
142 template< typename FuncType
, typename ParamType1
, typename ParamType2
>
143 void forEachMemWithKey( FuncType pFunc
, ParamType1 aParam1
, ParamType2 aParam2
) const
145 forEachWithKey( ::boost::bind( pFunc
, _2
, _1
, aParam1
, aParam2
) );
148 /** Calls the passed member function of ObjType on every contained object.
149 Passes the object key as first argument to the member function. */
150 template< typename FuncType
, typename ParamType1
, typename ParamType2
, typename ParamType3
>
151 void forEachMemWithKey( FuncType pFunc
, ParamType1 aParam1
, ParamType2 aParam2
, ParamType3 aParam3
) const
153 forEachWithKey( ::boost::bind( pFunc
, _2
, _1
, aParam1
, aParam2
, aParam3
) );
157 template< typename FunctorType
>
158 struct ForEachFunctor
160 FunctorType maFunctor
;
161 explicit ForEachFunctor( const FunctorType
& rFunctor
) : maFunctor( rFunctor
) {}
162 void operator()( const value_type
& rValue
) { if( rValue
.second
.get() ) maFunctor( *rValue
.second
); }
165 template< typename FunctorType
>
166 struct ForEachFunctorWithKey
168 FunctorType maFunctor
;
169 explicit ForEachFunctorWithKey( const FunctorType
& rFunctor
) : maFunctor( rFunctor
) {}
170 void operator()( const value_type
& rValue
) { if( rValue
.second
.get() ) maFunctor( rValue
.first
, *rValue
.second
); }
173 const mapped_type
* getRef( key_type nKey
) const
175 typename
container_type::const_iterator aIt
= this->find( nKey
);
176 return (aIt
== this->end()) ? 0 : &aIt
->second
;
186 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */