2 The software in this package is distributed under the GNU General
3 Public License version 2 (with a special exception described below).
5 A copy of GNU General Public License (GPL) is included in this distribution,
6 in the file COPYING.GPL.
8 As a special exception, if other files instantiate templates or use macros
9 or inline functions from this file, or you compile this file and link it
10 with other works to produce a work based on this file, this file
11 does not by itself cause the resulting work to be covered
12 by the GNU General Public License.
14 However the source code for this file must still be made available
15 in accordance with section (3) of the GNU General Public License.
17 This exception does not invalidate any other reasons why a work based
18 on this file might be covered by the GNU General Public License.
21 * @brief some helper for handling (STL) data structures.
23 * @author Reinhard Pfau \<Reinhard.Pfau@intra2net.com\>
25 * (c) Copyright 2007-2008 by Intra2net AG
27 * opensource@intra2net.com
30 #ifndef _I2N_CONTAINERFUNC_HPP_
31 #define _I2N_CONTAINERFUNC_HPP_
36 #include <boost/shared_ptr.hpp>
37 #include <boost/weak_ptr.hpp>
44 * convenience class to insert key-value pairs into a map.
46 template<typename K, typename V>
49 std::map<K,V> & MapRef;
52 MapFiller( std::map<K,V> & map_ref)
57 MapFiller& operator () (const K& key, const V& value)
63 }; // eo class MapFiller
67 * convenience class to fill values into a container (using push_back).
71 template< typename, typename> class C= std::list,
72 typename Alloc = std::allocator< T >
78 PushBackFiller( C<T, Alloc > & c )
82 PushBackFiller& operator () (const T& value)
84 CRef.push_back(value);
88 }; // eo class PushBackFiller
93 template< typename, typename > class C,
96 PushBackFiller< T, C, Alloc > get_push_back_filler( C< T, Alloc >& c)
98 return PushBackFiller< T, C, Alloc >(c);
99 } // eo get_push_back_filler(C< T, Alloc >&)
103 * convenience class for transient construction of a container including values.
107 template< typename, typename> class C= std::list,
108 typename Alloc = std::allocator< T >
110 class TransientPushBackFiller
114 typedef C< T, Alloc > CType;
116 TransientPushBackFiller()
119 TransientPushBackFiller& operator () (const T& value)
121 MyC.push_back(value);
125 operator CType () const { return MyC; }
126 }; // eo class TransientPushBackFiller
130 * convenience class for transient construction of a map including values.
132 template<typename K, typename V>
133 class TransientMapFiller
138 typedef std::map< K, V > CType;
141 TransientMapFiller( )
145 TransientMapFiller& operator () (const K& key, const V& value)
151 operator CType () const { return Map; }
152 }; // eo class MapFiller
158 * returns the keys of a map as a list.
159 * @param the_map the map.
160 * @param the_keys the list where the keys are added to.
163 template< typename K, typename V >
164 bool get_key_list( const std::map< K, V >& the_map, std::list< K >& the_keys )
166 for(typename std::map< K, V >::const_iterator it= the_map.begin();
170 the_keys.push_back( it->first );
173 } // eo get_key_list(const std::map< K,V >, std::list< K >&)
177 * returns the keys of a map as a list.
178 * @param the_map the map.
179 * @return the list of keys.
181 template< typename K, typename V >
182 std::list< K > get_key_list( const std::map< K, V>& the_map)
184 std::list< K > result;
185 get_key_list(the_map, result);
187 } // eo get_key_list(const std::map< K,V >)
191 * returns the keys of a map as a set.
192 * @param the_map the map.
193 * @param the_keys the set where the keys are added to.
196 template< typename K, typename V >
197 bool get_key_set( const std::map< K, V >& the_map, std::set< K >& the_keys )
199 for(typename std::map< K, V >::const_iterator it= the_map.begin();
203 the_keys.insert( it->first );
206 } // eo get_key_set(const std::map< K,V >, std::set< K >&)
210 * returns the keys of a map as a set.
211 * @param the_map the map.
212 * @return the set of keys.
214 template< typename K, typename V >
215 std::list< K > get_key_set( const std::map< K, V>& the_map)
217 std::set< K > result;
218 get_key_set(the_map, result);
220 } // eo get_key_set(const std::map< K,V >)
224 * functor version of new().
225 * can be used for deferred instantiation of objects.
232 typedef T* result_type;
234 result_type operator() ()
239 template<typename Arg1>
240 result_type operator() (Arg1 arg1)
245 template<typename Arg1, typename Arg2>
246 result_type operator() (Arg1 arg1,Arg2 arg2)
248 return new T(arg1,arg2);
251 template<typename Arg1, typename Arg2, typename Arg3>
252 result_type operator() (Arg1 arg1,Arg2 arg2,Arg3 arg3)
254 return new T(arg1,arg2,arg3);
257 template<typename Arg1, typename Arg2, typename Arg3, typename Arg4>
258 result_type operator() (Arg1 arg1,Arg2 arg2,Arg3 arg3,Arg4 arg4)
260 return new T(arg1,arg2,arg3,arg4);
263 template<typename Arg1, typename Arg2, typename Arg3, typename Arg4, typename Arg5>
264 result_type operator() (Arg1 arg1,Arg2 arg2,Arg3 arg3,Arg4 arg4,Arg5 arg5)
266 return new T(arg1,arg2,arg3,arg4,arg5);
275 * functor version of boost::shared_ptr = new().
276 * can be used for deferred instantiation of objects.
283 typedef boost::shared_ptr<T> result_type;
285 result_type operator() ()
287 return result_type(new T);
290 template<typename Arg1>
291 result_type operator() (Arg1 arg1)
293 return result_type(new T(arg1));
296 template<typename Arg1, typename Arg2>
297 result_type operator() (Arg1 arg1,Arg2 arg2)
299 return result_type(new T(arg1,arg2));
302 template<typename Arg1, typename Arg2, typename Arg3>
303 result_type operator() (Arg1 arg1,Arg2 arg2,Arg3 arg3)
305 return result_type(new T(arg1,arg2,arg3));
308 template<typename Arg1, typename Arg2, typename Arg3, typename Arg4>
309 result_type operator() (Arg1 arg1,Arg2 arg2,Arg3 arg3,Arg4 arg4)
311 return result_type(new T(arg1,arg2,arg3,arg4));
314 template<typename Arg1, typename Arg2, typename Arg3, typename Arg4, typename Arg5>
315 result_type operator() (Arg1 arg1,Arg2 arg2,Arg3 arg3,Arg4 arg4,Arg5 arg5)
317 return result_type(new T(arg1,arg2,arg3,arg4,arg5));
320 }; // eo struct SharedPtrNew
325 ** classes holding a shared or weak pointer.
326 ** designed to be used as policy classes...
327 ** (but may be used for other purposes, too)
333 class SharedOwnership
337 SharedOwnership( boost::shared_ptr< X > ptr)
342 boost::shared_ptr< X > get_ptr() const { return StoredPtr; }
346 boost::shared_ptr< X > StoredPtr;
347 }; // eo class SharedOwnership
358 WeakOwnership( boost::shared_ptr< X > ptr)
363 boost::shared_ptr< X > get_ptr() const { return StoredPtr.lock(); }
367 boost::weak_ptr< X > StoredPtr;
368 }; // eo class WeakOwnership
379 * @brief the ultimate class for doing nothing :-)
381 * Even it sounds odd to have a functor class which is doing nothing it makes
382 * sense to use this class in some situations.
384 * One example is the usage as Deleter for shared pointers which "own" a pointer to
385 * a static object (i.e. it is not allowed to be really deleted...). Using this class
386 * as Deleter in these pointers enables us to use interfaces which expect a shared pointer
387 * even if we want(/must) to pass pointers to static objects.
399 typename Arg1, typename Arg2
401 NoOperation(Arg1, Arg2) {}
404 typename Arg1, typename Arg2, typename Arg3
406 NoOperation(Arg1, Arg2, Arg3) {}
408 void operator() () const {}
413 void operator() (Arg1) const {}
416 typename Arg1, typename Arg2
418 void operator() (Arg1, Arg2) const {}
421 typename Arg1, typename Arg2, typename Arg3
423 void operator() (Arg1, Arg2, Arg3) const {}
425 }; // eo struct NoOperation
429 } // eo namespace I2n