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 OOX_HELPER_REFMAP_HXX
21 #define OOX_HELPER_REFMAP_HXX
24 #include <boost/bind.hpp>
25 #include <boost/shared_ptr.hpp>
26 #include <sal/types.h>
30 // ============================================================================
32 /** Template for a map of ref-counted objects with additional accessor functions.
34 An instance of the class RefMap< Type > stores elements of the type
35 ::boost::shared_ptr< Type >. The new accessor functions has() and get()
36 work correctly for nonexisting keys, there is no need to check the passed
39 template< typename KeyType
, typename ObjType
, typename CompType
= ::std::less
< KeyType
> >
40 class RefMap
: public ::std::map
< KeyType
, ::boost::shared_ptr
< ObjType
>, CompType
>
43 typedef ::std::map
< KeyType
, ::boost::shared_ptr
< ObjType
>, CompType
> container_type
;
44 typedef typename
container_type::key_type key_type
;
45 typedef typename
container_type::mapped_type mapped_type
;
46 typedef typename
container_type::value_type value_type
;
47 typedef typename
container_type::key_compare key_compare
;
50 /** Returns true, if the object associated to the passed key exists.
51 Returns false, if the key exists but points to an empty reference. */
52 inline bool has( key_type nKey
) const
54 const mapped_type
* pxRef
= getRef( nKey
);
55 return pxRef
&& pxRef
->get();
58 /** Returns a reference to the object associated to the passed key, or an
59 empty reference on error. */
60 inline mapped_type
get( key_type nKey
) const
62 if( const mapped_type
* pxRef
= getRef( nKey
) ) return *pxRef
;
66 /** Calls the passed functor for every contained object, automatically
67 skips all elements that are empty references. */
68 template< typename FunctorType
>
69 inline void forEach( const FunctorType
& rFunctor
) const
71 ::std::for_each( this->begin(), this->end(), ForEachFunctor
< FunctorType
>( rFunctor
) );
74 /** Calls the passed member function of ObjType on every contained object,
75 automatically skips all elements that are empty references. */
76 template< typename FuncType
>
77 inline void forEachMem( FuncType pFunc
) const
79 forEach( ::boost::bind( pFunc
, _1
) );
82 /** Calls the passed member function of ObjType on every contained object,
83 automatically skips all elements that are empty references. */
84 template< typename FuncType
, typename ParamType
>
85 inline void forEachMem( FuncType pFunc
, ParamType aParam
) const
87 forEach( ::boost::bind( pFunc
, _1
, aParam
) );
90 /** Calls the passed member function of ObjType on every contained object,
91 automatically skips all elements that are empty references. */
92 template< typename FuncType
, typename ParamType1
, typename ParamType2
>
93 inline void forEachMem( FuncType pFunc
, ParamType1 aParam1
, ParamType2 aParam2
) const
95 forEach( ::boost::bind( pFunc
, _1
, aParam1
, aParam2
) );
98 /** Calls the passed member function of ObjType on every contained object,
99 automatically skips all elements that are empty references. */
100 template< typename FuncType
, typename ParamType1
, typename ParamType2
, typename ParamType3
>
101 inline void forEachMem( FuncType pFunc
, ParamType1 aParam1
, ParamType2 aParam2
, ParamType3 aParam3
) const
103 forEach( ::boost::bind( pFunc
, _1
, aParam1
, aParam2
, aParam3
) );
106 /** Calls the passed member function of ObjType on every contained object,
107 automatically skips all elements that are empty references. */
108 template< typename FuncType
, typename ParamType1
, typename ParamType2
, typename ParamType3
, typename ParamType4
>
109 inline void forEachMem( FuncType pFunc
, ParamType1 aParam1
, ParamType2 aParam2
, ParamType3 aParam3
, ParamType4 aParam4
) const
111 forEach( ::boost::bind( pFunc
, _1
, aParam1
, aParam2
, aParam3
, aParam4
) );
115 /** Calls the passed functor for every contained object. Passes the key as
116 first argument and the object reference as second argument to rFunctor. */
117 template< typename FunctorType
>
118 inline void forEachWithKey( const FunctorType
& rFunctor
) const
120 ::std::for_each( this->begin(), this->end(), ForEachFunctorWithKey
< FunctorType
>( rFunctor
) );
123 /** Calls the passed member function of ObjType on every contained object.
124 Passes the object key as argument to the member function. */
125 template< typename FuncType
>
126 inline void forEachMemWithKey( FuncType pFunc
) const
128 forEachWithKey( ::boost::bind( pFunc
, _2
, _1
) );
131 /** Calls the passed member function of ObjType on every contained object.
132 Passes the object key as first argument to the member function. */
133 template< typename FuncType
, typename ParamType
>
134 inline void forEachMemWithKey( FuncType pFunc
, ParamType aParam
) const
136 forEachWithKey( ::boost::bind( pFunc
, _2
, _1
, aParam
) );
139 /** Calls the passed member function of ObjType on every contained object.
140 Passes the object key as first argument to the member function. */
141 template< typename FuncType
, typename ParamType1
, typename ParamType2
>
142 inline void forEachMemWithKey( FuncType pFunc
, ParamType1 aParam1
, ParamType2 aParam2
) const
144 forEachWithKey( ::boost::bind( pFunc
, _2
, _1
, aParam1
, aParam2
) );
147 /** Calls the passed member function of ObjType on every contained object.
148 Passes the object key as first argument to the member function. */
149 template< typename FuncType
, typename ParamType1
, typename ParamType2
, typename ParamType3
>
150 inline void forEachMemWithKey( FuncType pFunc
, ParamType1 aParam1
, ParamType2 aParam2
, ParamType3 aParam3
) const
152 forEachWithKey( ::boost::bind( pFunc
, _2
, _1
, aParam1
, aParam2
, aParam3
) );
156 template< typename FunctorType
>
157 struct ForEachFunctor
159 FunctorType maFunctor
;
160 inline explicit ForEachFunctor( const FunctorType
& rFunctor
) : maFunctor( rFunctor
) {}
161 inline void operator()( const value_type
& rValue
) { if( rValue
.second
.get() ) maFunctor( *rValue
.second
); }
164 template< typename FunctorType
>
165 struct ForEachFunctorWithKey
167 FunctorType maFunctor
;
168 inline explicit ForEachFunctorWithKey( const FunctorType
& rFunctor
) : maFunctor( rFunctor
) {}
169 inline void operator()( const value_type
& rValue
) { if( rValue
.second
.get() ) maFunctor( rValue
.first
, *rValue
.second
); }
172 inline const mapped_type
* getRef( key_type nKey
) const
174 typename
container_type::const_iterator aIt
= this->find( nKey
);
175 return (aIt
== this->end()) ? 0 : &aIt
->second
;
179 // ============================================================================
185 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */