LibreOffice Module oox (master) 1
refmap.hxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#ifndef INCLUDED_OOX_HELPER_REFMAP_HXX
21#define INCLUDED_OOX_HELPER_REFMAP_HXX
22
23#include <algorithm>
24#include <functional>
25#include <map>
26#include <memory>
27#include <utility>
28
29namespace oox {
30
31
39template< typename KeyType, typename ObjType, typename CompType = std::less< KeyType > >
40class RefMap : public std::map< KeyType, std::shared_ptr< ObjType >, CompType >
41{
42public:
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;
48
49public:
52 bool has( key_type nKey ) const
53 {
54 const mapped_type* pxRef = getRef( nKey );
55 return pxRef && pxRef->get();
56 }
57
60 mapped_type get( key_type nKey ) const
61 {
62 if( const mapped_type* pxRef = getRef( nKey ) ) return *pxRef;
63 return mapped_type();
64 }
65
68 template< typename FunctorType >
69 void forEach( const FunctorType& rFunctor ) const
70 {
71 std::for_each( this->begin(), this->end(), ForEachFunctor< FunctorType >( rFunctor ) );
72 }
73
76 template< typename FuncType >
77 void forEachMem( FuncType pFunc ) const
78 {
79 forEach( ::std::bind( pFunc, std::placeholders::_1 ) );
80 }
81
84 template< typename FuncType, typename ParamType1, typename ParamType2 >
85 void forEachMem( FuncType pFunc, ParamType1 aParam1, ParamType2 aParam2 ) const
86 {
87 forEach( ::std::bind( pFunc, std::placeholders::_1, aParam1, aParam2 ) );
88 }
89
92 template< typename FuncType, typename ParamType1, typename ParamType2, typename ParamType3 >
93 void forEachMem( FuncType pFunc, ParamType1 aParam1, ParamType2 aParam2, ParamType3 aParam3 ) const
94 {
95 forEach( ::std::bind( pFunc, std::placeholders::_1, aParam1, aParam2, aParam3 ) );
96 }
97
98
101 template< typename FunctorType >
102 void forEachWithKey( const FunctorType& rFunctor ) const
103 {
104 std::for_each( this->begin(), this->end(), ForEachFunctorWithKey< FunctorType >( rFunctor ) );
105 }
106
109 template< typename FuncType >
110 void forEachMemWithKey( FuncType pFunc ) const
111 {
112 forEachWithKey( ::std::bind( pFunc, std::placeholders::_2, std::placeholders::_1 ) );
113 }
114
115
116private:
117 template< typename FunctorType >
119 {
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 ); }
123 };
124
125 template< typename FunctorType >
127 {
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 ); }
131 };
132
133 const mapped_type* getRef( key_type nKey ) const
134 {
135 typename container_type::const_iterator aIt = this->find( nKey );
136 return (aIt == this->end()) ? nullptr : &aIt->second;
137 }
138};
139
140
141} // namespace oox
142
143#endif
144
145/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Template for a map of ref-counted objects with additional accessor functions.
Definition: refmap.hxx:41
bool has(key_type nKey) const
Returns true, if the object associated to the passed key exists.
Definition: refmap.hxx:52
void forEachMem(FuncType pFunc, ParamType1 aParam1, ParamType2 aParam2, ParamType3 aParam3) const
Calls the passed member function of ObjType on every contained object, automatically skips all elemen...
Definition: refmap.hxx:93
container_type::key_compare key_compare
Definition: refmap.hxx:47
void forEachWithKey(const FunctorType &rFunctor) const
Calls the passed functor for every contained object.
Definition: refmap.hxx:102
container_type::value_type value_type
Definition: refmap.hxx:46
std::map< KeyType, std::shared_ptr< ObjType >, CompType > container_type
Definition: refmap.hxx:43
void forEachMem(FuncType pFunc, ParamType1 aParam1, ParamType2 aParam2) const
Calls the passed member function of ObjType on every contained object, automatically skips all elemen...
Definition: refmap.hxx:85
mapped_type get(key_type nKey) const
Returns a reference to the object associated to the passed key, or an empty reference on error.
Definition: refmap.hxx:60
void forEach(const FunctorType &rFunctor) const
Calls the passed functor for every contained object, automatically skips all elements that are empty ...
Definition: refmap.hxx:69
container_type::mapped_type mapped_type
Definition: refmap.hxx:45
container_type::key_type key_type
Definition: refmap.hxx:44
void forEachMemWithKey(FuncType pFunc) const
Calls the passed member function of ObjType on every contained object.
Definition: refmap.hxx:110
void forEachMem(FuncType pFunc) const
Calls the passed member function of ObjType on every contained object, automatically skips all elemen...
Definition: refmap.hxx:77
const mapped_type * getRef(key_type nKey) const
Definition: refmap.hxx:133
OSQLColumns::const_iterator find(const OSQLColumns::const_iterator &first, const OSQLColumns::const_iterator &last, std::u16string_view _rVal, const ::comphelper::UStringMixEqual &_rCase)
enumrange< T >::Iterator begin(enumrange< T >)
end
ForEachFunctorWithKey(FunctorType aFunctor)
Definition: refmap.hxx:129
void operator()(const value_type &rValue)
Definition: refmap.hxx:130
ForEachFunctor(FunctorType aFunctor)
Definition: refmap.hxx:121
void operator()(const value_type &rValue)
Definition: refmap.hxx:122