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
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 std::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
, std::shared_ptr
< ObjType
>, CompType
>
43 typedef std::map
< KeyType
, std::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 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 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 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 void forEachMem( FuncType pFunc
) const
79 forEach( ::std::bind( pFunc
, std::placeholders::_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 ParamType1
, typename ParamType2
>
85 void forEachMem( FuncType pFunc
, ParamType1 aParam1
, ParamType2 aParam2
) const
87 forEach( ::std::bind( pFunc
, std::placeholders::_1
, aParam1
, aParam2
) );
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
, typename ParamType3
>
93 void forEachMem( FuncType pFunc
, ParamType1 aParam1
, ParamType2 aParam2
, ParamType3 aParam3
) const
95 forEach( ::std::bind( pFunc
, std::placeholders::_1
, aParam1
, aParam2
, aParam3
) );
99 /** Calls the passed functor for every contained object. Passes the key as
100 first argument and the object reference as second argument to rFunctor. */
101 template< typename FunctorType
>
102 void forEachWithKey( const FunctorType
& rFunctor
) const
104 std::for_each( this->begin(), this->end(), ForEachFunctorWithKey
< FunctorType
>( rFunctor
) );
107 /** Calls the passed member function of ObjType on every contained object.
108 Passes the object key as argument to the member function. */
109 template< typename FuncType
>
110 void forEachMemWithKey( FuncType pFunc
) const
112 forEachWithKey( ::std::bind( pFunc
, std::placeholders::_2
, std::placeholders::_1
) );
117 template< typename FunctorType
>
118 struct ForEachFunctor
120 FunctorType maFunctor
;
121 explicit ForEachFunctor( FunctorType aFunctor
) : maFunctor(std::move( aFunctor
)) {}
122 void operator()( const value_type
& rValue
) { if( rValue
.second
.get() ) maFunctor( *rValue
.second
); }
125 template< typename FunctorType
>
126 struct ForEachFunctorWithKey
128 FunctorType maFunctor
;
129 explicit ForEachFunctorWithKey( FunctorType aFunctor
) : maFunctor(std::move( aFunctor
)) {}
130 void operator()( const value_type
& rValue
) { if( rValue
.second
.get() ) maFunctor( rValue
.first
, *rValue
.second
); }
133 const mapped_type
* getRef( key_type nKey
) const
135 typename
container_type::const_iterator aIt
= this->find( nKey
);
136 return (aIt
== this->end()) ? nullptr : &aIt
->second
;
145 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */