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 
28 namespace oox {
29 
30 
38 template< typename KeyType, typename ObjType, typename CompType = std::less< KeyType > >
39 class RefMap : public std::map< KeyType, std::shared_ptr< ObjType >, CompType >
40 {
41 public:
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;
47 
48 public:
51  bool has( key_type nKey ) const
52  {
53  const mapped_type* pxRef = getRef( nKey );
54  return pxRef && pxRef->get();
55  }
56 
59  mapped_type get( key_type nKey ) const
60  {
61  if( const mapped_type* pxRef = getRef( nKey ) ) return *pxRef;
62  return mapped_type();
63  }
64 
67  template< typename FunctorType >
68  void forEach( const FunctorType& rFunctor ) const
69  {
70  std::for_each( this->begin(), this->end(), ForEachFunctor< FunctorType >( rFunctor ) );
71  }
72 
75  template< typename FuncType >
76  void forEachMem( FuncType pFunc ) const
77  {
78  forEach( ::std::bind( pFunc, std::placeholders::_1 ) );
79  }
80 
83  template< typename FuncType, typename ParamType1, typename ParamType2 >
84  void forEachMem( FuncType pFunc, ParamType1 aParam1, ParamType2 aParam2 ) const
85  {
86  forEach( ::std::bind( pFunc, std::placeholders::_1, aParam1, aParam2 ) );
87  }
88 
91  template< typename FuncType, typename ParamType1, typename ParamType2, typename ParamType3 >
92  void forEachMem( FuncType pFunc, ParamType1 aParam1, ParamType2 aParam2, ParamType3 aParam3 ) const
93  {
94  forEach( ::std::bind( pFunc, std::placeholders::_1, aParam1, aParam2, aParam3 ) );
95  }
96 
97 
100  template< typename FunctorType >
101  void forEachWithKey( const FunctorType& rFunctor ) const
102  {
103  std::for_each( this->begin(), this->end(), ForEachFunctorWithKey< FunctorType >( rFunctor ) );
104  }
105 
108  template< typename FuncType >
109  void forEachMemWithKey( FuncType pFunc ) const
110  {
111  forEachWithKey( ::std::bind( pFunc, std::placeholders::_2, std::placeholders::_1 ) );
112  }
113 
114 
115 private:
116  template< typename FunctorType >
118  {
119  FunctorType maFunctor;
120  explicit ForEachFunctor( const FunctorType& rFunctor ) : maFunctor( rFunctor ) {}
121  void operator()( const value_type& rValue ) { if( rValue.second.get() ) maFunctor( *rValue.second ); }
122  };
123 
124  template< typename FunctorType >
126  {
127  FunctorType maFunctor;
128  explicit ForEachFunctorWithKey( const FunctorType& rFunctor ) : maFunctor( rFunctor ) {}
129  void operator()( const value_type& rValue ) { if( rValue.second.get() ) maFunctor( rValue.first, *rValue.second ); }
130  };
131 
132  const mapped_type* getRef( key_type nKey ) const
133  {
134  typename container_type::const_iterator aIt = this->find( nKey );
135  return (aIt == this->end()) ? 0 : &aIt->second;
136  }
137 };
138 
139 
140 } // namespace oox
141 
142 #endif
143 
144 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
ForEachFunctorWithKey(const FunctorType &rFunctor)
Definition: refmap.hxx:128
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:92
void forEachMemWithKey(FuncType pFunc) const
Calls the passed member function of ObjType on every contained object.
Definition: refmap.hxx:109
std::map< KeyType, std::shared_ptr< ObjType >, CompType > container_type
Definition: refmap.hxx:42
void operator()(const value_type &rValue)
Definition: refmap.hxx:129
Template for a map of ref-counted objects with additional accessor functions.
Definition: refmap.hxx:39
OSQLColumns::const_iterator find(const OSQLColumns::const_iterator &first, const OSQLColumns::const_iterator &last, std::u16string_view _rVal, const ::comphelper::UStringMixEqual &_rCase)
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:84
enumrange< T >::Iterator begin(enumrange< T >)
bool has(key_type nKey) const
Returns true, if the object associated to the passed key exists.
Definition: refmap.hxx:51
container_type::mapped_type mapped_type
Definition: refmap.hxx:44
container_type::value_type value_type
Definition: refmap.hxx:45
enumrange< T >::Iterator end(enumrange< T >)
void operator()(const value_type &rValue)
Definition: refmap.hxx:121
container_type::key_compare key_compare
Definition: refmap.hxx:46
ForEachFunctor(const FunctorType &rFunctor)
Definition: refmap.hxx:120
void forEachWithKey(const FunctorType &rFunctor) const
Calls the passed functor for every contained object.
Definition: refmap.hxx:101
container_type::key_type key_type
Definition: refmap.hxx:43
const mapped_type * getRef(key_type nKey) const
Definition: refmap.hxx:132
void forEach(const FunctorType &rFunctor) const
Calls the passed functor for every contained object, automatically skips all elements that are empty ...
Definition: refmap.hxx:68
void forEachMem(FuncType pFunc) const
Calls the passed member function of ObjType on every contained object, automatically skips all elemen...
Definition: refmap.hxx:76