update credits
[LibreOffice.git] / include / cosv / tpl / tpltools.hxx
blob487a74cf823701c33b27c6449fe635d1143ca7e6
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 CSV_TPLTOOLS_HXX
21 #define CSV_TPLTOOLS_HXX
23 #include <vector>
24 #include <map>
29 namespace csv
33 template <class COLLECTION>
34 inline void erase_container(
35 COLLECTION & o_rCollection );
37 /// Version for other containers than std::map, with non-pair value_type.
38 template <class COLLECTION>
39 void erase_container_of_heap_ptrs(
40 COLLECTION & o_rCollection );
43 template <class KEY, class VAL>
44 const VAL * find_in_map( /// Usable for all kinds of values
45 const std::map< KEY, VAL > &
46 i_rMap,
47 const KEY & i_rKey );
50 /** @return the value in the map, if it is in there, else 0.
51 @precond VAL has to be convertible to "0".
53 template <class KEY, class VAL>
54 VAL value_from_map(
55 const std::map< KEY, VAL > &
56 i_rMap,
57 const KEY & i_rKey );
59 /** @return the value in the map, if it is in there, else i_notFound.
61 template <class KEY, class VAL>
62 VAL value_from_map(
63 const std::map< KEY, VAL > &
64 i_rMap,
65 const KEY & i_rKey,
66 VAL i_notFound );
68 template <class COLLECTION, class VALUE>
69 bool contains(
70 const COLLECTION & i_collection,
71 const VALUE & i_value );
76 // IMPLEMENTATION
77 template <class COLLECTION>
78 inline void
79 erase_container( COLLECTION & o_rCollection )
81 o_rCollection.erase( o_rCollection.begin(),
82 o_rCollection.end() );
85 template <class COLLECTION>
86 void
87 erase_container_of_heap_ptrs( COLLECTION & o_rCollection )
89 typename COLLECTION::iterator itEnd = o_rCollection.end();
90 for ( typename COLLECTION::iterator it = o_rCollection.begin();
91 it != itEnd;
92 ++it )
94 delete *it;
97 o_rCollection.erase( o_rCollection.begin(),
98 o_rCollection.end() );
101 template <class KEY, class VAL>
102 const VAL *
103 find_in_map( const std::map< KEY, VAL > & i_rMap,
104 const KEY & i_rKey )
106 typename std::map< KEY, VAL >::const_iterator
107 ret = i_rMap.find(i_rKey);
108 return ret != i_rMap.end()
109 ? & (*ret).second
110 : (const VAL*)0;
113 template <class KEY, class VAL>
115 value_from_map( const std::map< KEY, VAL > & i_rMap,
116 const KEY & i_rKey )
118 typename std::map< KEY, VAL >::const_iterator
119 ret = i_rMap.find(i_rKey);
120 return ret != i_rMap.end()
121 ? (*ret).second
122 : VAL(0);
125 template <class KEY, class VAL>
127 value_from_map( const std::map< KEY, VAL > & i_rMap,
128 const KEY & i_rKey,
129 VAL i_notFound )
131 typename std::map< KEY, VAL >::const_iterator
132 ret = i_rMap.find(i_rKey);
133 return ret != i_rMap.end()
134 ? (*ret).second
135 : i_notFound;
138 template <class COLLECTION, class VALUE>
139 bool
140 contains( const COLLECTION & i_collection,
141 const VALUE & i_value )
143 return std::find(i_collection.begin(), i_collection.end(), i_value)
145 i_collection.end();
151 } // namespace csv
152 #endif
154 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */