xref: /trunk/main/autodoc/inc/cosv/tpl/tpltools.hxx (revision 11c03c6d)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef CSV_TPLTOOLS_HXX
25 #define CSV_TPLTOOLS_HXX
26 
27 #include <vector>
28 #include <map>
29 
30 
31 
32 
33 namespace csv
34 {
35 
36 
37 template <class COLLECTION>
38 inline void		    erase_container(
39 						COLLECTION &		o_rCollection );
40 
41 /// Version for std::map
42 template <class COLLECTION>
43 void		  		erase_map_of_heap_ptrs(
44 						COLLECTION &		o_rCollection );
45 
46 /// Version for other containers than std::map, with non-pair value_type.
47 template <class COLLECTION>
48 void		  		erase_container_of_heap_ptrs(
49 						COLLECTION &		o_rCollection );
50 
51 template <class VECTOR_ELEM>
52 void                adjust_vector_size(
53                         std::vector<VECTOR_ELEM> &
54                                             io_rVector,
55                         uintt               i_nSize,
56                         const VECTOR_ELEM & i_nFill );
57 
58 
59 template <class KEY, class VAL>
60 const VAL *	        find_in_map(            /// Usable for all kinds of values
61 						const std::map< KEY, VAL > &
62 											i_rMap,
63 						const KEY &			i_rKey );
64 
65 
66 /** @return the value in the map, if it is in there, else 0.
67     @precond VAL has to be convertable to "0".
68 */
69 template <class KEY, class VAL>
70 VAL					value_from_map(
71 						const std::map< KEY, VAL > &
72 											i_rMap,
73 						const KEY &			i_rKey );
74 
75 /** @return the value in the map, if it is in there, else i_notFound.
76 */
77 template <class KEY, class VAL>
78 VAL					value_from_map(
79 						const std::map< KEY, VAL > &
80 											i_rMap,
81 						const KEY &			i_rKey,
82 						VAL                 i_notFound );
83 
84 template <class COLLECTION, class VALUE>
85 bool		  		contains(
86 						const COLLECTION &	i_collection,
87 						const VALUE &       i_value );
88 
89 // Object oriented for_each:
90 template <class COLLECTION, class CLASS, class MEMFUNC>
91 void                call_for_each(
92                         const COLLECTION &  i_rList,
93                         CLASS *             io_pThis,
94                         MEMFUNC             i_fMethod );
95 
96 
97 
98 
99 // IMPLEMENTATION
100 template <class COLLECTION>
101 inline void
erase_container(COLLECTION & o_rCollection)102 erase_container( COLLECTION & o_rCollection )
103 {
104 	o_rCollection.erase( o_rCollection.begin(),
105 						 o_rCollection.end() );
106 }
107 
108 template <class COLLECTION>
109 void
erase_map_of_heap_ptrs(COLLECTION & o_rCollection)110 erase_map_of_heap_ptrs( COLLECTION & o_rCollection )
111 {
112     typename COLLECTION::iterator itEnd = o_rCollection.end();
113     for ( typename COLLECTION::iterator it = o_rCollection.begin();
114           it != itEnd;
115           ++it )
116     {
117         delete (*it).second;
118     }
119 
120 	o_rCollection.erase( o_rCollection.begin(),
121 						 o_rCollection.end() );
122 }
123 
124 template <class COLLECTION>
125 void
erase_container_of_heap_ptrs(COLLECTION & o_rCollection)126 erase_container_of_heap_ptrs( COLLECTION & o_rCollection )
127 {
128 	typename COLLECTION::iterator itEnd = o_rCollection.end();
129 	for ( typename COLLECTION::iterator it = o_rCollection.begin();
130 		  it != itEnd;
131 		  ++it )
132 	{
133 		delete *it;
134 	}
135 
136 	o_rCollection.erase( o_rCollection.begin(),
137 						 o_rCollection.end() );
138 }
139 
140 template <class VECTOR_ELEM>
141 void
adjust_vector_size(std::vector<VECTOR_ELEM> & io_rVector,uintt i_nSize,const VECTOR_ELEM & i_nFill)142 adjust_vector_size( std::vector<VECTOR_ELEM> & io_rVector,
143                     uintt                      i_nSize,
144                     const VECTOR_ELEM &        i_nFill )
145 {
146     if ( io_rVector.size() > i_nSize )
147     {
148         io_rVector.erase( io_rVector.begin() + i_nSize, io_rVector.end() );
149     }
150     else
151     {
152         io_rVector.reserve(i_nSize);
153         while ( io_rVector.size() <  i_nSize )
154             io_rVector.push_back(i_nFill);
155     }
156 }
157 
158 
159 template <class KEY, class VAL>
160 const VAL *
find_in_map(const std::map<KEY,VAL> & i_rMap,const KEY & i_rKey)161 find_in_map( const std::map< KEY, VAL > & i_rMap,
162 			 const KEY &			      i_rKey )
163 {
164 	typename std::map< KEY, VAL >::const_iterator
165 			ret =  i_rMap.find(i_rKey);
166 	return ret != i_rMap.end()
167 				?	& (*ret).second
168 				:	(const VAL*)0;
169 }
170 
171 template <class KEY, class VAL>
172 VAL
value_from_map(const std::map<KEY,VAL> & i_rMap,const KEY & i_rKey)173 value_from_map( const std::map< KEY, VAL > & i_rMap,
174 			    const KEY &			         i_rKey )
175 {
176 	typename std::map< KEY, VAL >::const_iterator
177 			ret =  i_rMap.find(i_rKey);
178 	return ret != i_rMap.end()
179 				?	(*ret).second
180 				:	VAL(0);
181 }
182 
183 template <class KEY, class VAL>
184 VAL
value_from_map(const std::map<KEY,VAL> & i_rMap,const KEY & i_rKey,VAL i_notFound)185 value_from_map( const std::map< KEY, VAL > & i_rMap,
186 			    const KEY &			         i_rKey,
187 			    VAL                          i_notFound )
188 {
189 	typename std::map< KEY, VAL >::const_iterator
190 			ret =  i_rMap.find(i_rKey);
191 	return ret != i_rMap.end()
192 				?	(*ret).second
193 				:	i_notFound;
194 }
195 
196 template <class COLLECTION, class VALUE>
197 bool
contains(const COLLECTION & i_collection,const VALUE & i_value)198 contains( const COLLECTION &  i_collection,
199 		  const VALUE &       i_value )
200 {
201     return std::find(i_collection.begin(), i_collection.end(), i_value)
202            !=
203            i_collection.end();
204 }
205 
206 template <class COLLECTION, class CLASS, class MEMFUNC>
207 void
call_for_each(const COLLECTION & i_rList,CLASS * io_pThis,MEMFUNC i_fMethod)208 call_for_each( const COLLECTION &  i_rList,
209                CLASS *             io_pThis,
210                MEMFUNC             i_fMethod )
211 {
212     typename COLLECTION::const_iterator it = i_rList.begin();
213     typename COLLECTION::const_iterator itEnd = i_rList.end();
214     for ( ; it != itEnd; ++it )
215     {
216         (io_pThis->*i_fMethod)(*it);
217     }
218 }
219 
220 
221 
222 
223 }   // namespace csv
224 #endif
225