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
31 /** Template for a map of ref-counted objects with additional accessor functions.
33 An instance of the class RefMap< Type > stores elements of the type
34 std::shared_ptr< Type >. The new accessor functions has() and get()
35 work correctly for nonexisting keys, there is no need to check the passed
38 template< typename KeyType
, typename ObjType
, typename CompType
= std::less
< KeyType
> >
39 class RefMap
: public std::map
< KeyType
, std::shared_ptr
< ObjType
>, CompType
>
42 typedef std::map
< KeyType
, std::shared_ptr
< ObjType
>, CompType
> container_type
;
43 typedef typename
container_type::key_type key_type
;
44 typedef typename
container_type::mapped_type mapped_type
;
45 typedef typename
container_type::value_type value_type
;
46 typedef typename
container_type::key_compare key_compare
;
49 /** Returns true, if the object associated to the passed key exists.
50 Returns false, if the key exists but points to an empty reference. */
51 bool has(const key_type
& rKey
) const
53 const mapped_type
* pxRef
= getRef(rKey
);
54 return pxRef
&& pxRef
->get();
57 /** Returns a reference to the object associated to the passed key, or an
58 empty reference on error. */
59 mapped_type
get(const key_type
& rKey
) const
61 if( const mapped_type
* pxRef
= getRef(rKey
) ) return *pxRef
;
65 /** Calls the passed functor for every contained object, automatically
66 skips all elements that are empty references. */
67 template< typename FunctorType
>
68 void forEach( const FunctorType
& rFunctor
) const
70 std::for_each( this->begin(), this->end(), ForEachFunctor
< FunctorType
>( rFunctor
) );
73 /** Calls the passed member function of ObjType on every contained object,
74 automatically skips all elements that are empty references. */
75 template< typename FuncType
>
76 void forEachMem( FuncType pFunc
) const
78 forEach( ::std::bind( pFunc
, std::placeholders::_1
) );
81 /** Calls the passed member function of ObjType on every contained object,
82 automatically skips all elements that are empty references. */
83 template< typename FuncType
, typename ParamType1
, typename ParamType2
>
84 void forEachMem( FuncType pFunc
, ParamType1 aParam1
, ParamType2 aParam2
) const
86 forEach( ::std::bind( pFunc
, std::placeholders::_1
, aParam1
, aParam2
) );
89 /** Calls the passed member function of ObjType on every contained object,
90 automatically skips all elements that are empty references. */
91 template< typename FuncType
, typename ParamType1
, typename ParamType2
, typename ParamType3
>
92 void forEachMem( FuncType pFunc
, ParamType1 aParam1
, ParamType2 aParam2
, ParamType3 aParam3
) const
94 forEach( ::std::bind( pFunc
, std::placeholders::_1
, aParam1
, aParam2
, aParam3
) );
98 /** Calls the passed functor for every contained object. Passes the key as
99 first argument and the object reference as second argument to rFunctor. */
100 template< typename FunctorType
>
101 void forEachWithKey( const FunctorType
& rFunctor
) const
103 std::for_each( this->begin(), this->end(), ForEachFunctorWithKey
< FunctorType
>( rFunctor
) );
106 /** Calls the passed member function of ObjType on every contained object.
107 Passes the object key as argument to the member function. */
108 template< typename FuncType
>
109 void forEachMemWithKey( FuncType pFunc
) const
111 forEachWithKey( ::std::bind( pFunc
, std::placeholders::_2
, std::placeholders::_1
) );
116 template< typename FunctorType
>
117 struct ForEachFunctor
119 FunctorType maFunctor
;
120 explicit ForEachFunctor( FunctorType aFunctor
) : maFunctor(std::move( aFunctor
)) {}
121 void operator()( const value_type
& rValue
) { if( rValue
.second
.get() ) maFunctor( *rValue
.second
); }
124 template< typename FunctorType
>
125 struct ForEachFunctorWithKey
127 FunctorType maFunctor
;
128 explicit ForEachFunctorWithKey( FunctorType aFunctor
) : maFunctor(std::move( aFunctor
)) {}
129 void operator()( const value_type
& rValue
) { if( rValue
.second
.get() ) maFunctor( rValue
.first
, *rValue
.second
); }
132 const mapped_type
* getRef(const key_type
& rKey
) const
134 typename
container_type::const_iterator aIt
= this->find(rKey
);
135 return (aIt
== this->end()) ? nullptr : &aIt
->second
;
144 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */