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
25 * (c) Copyright 2007-2008 by Intra2net AG
28 #ifndef _I2N_CONTAINERFUNC_HPP_
29 #define _I2N_CONTAINERFUNC_HPP_
34 #include <boost/shared_ptr.hpp>
35 #include <boost/weak_ptr.hpp>
42 * convenience class to insert key-value pairs into a map.
44 template<typename K, typename V>
47 std::map<K,V> & MapRef;
50 MapFiller( std::map<K,V> & map_ref)
55 MapFiller& operator () (const K& key, const V& value)
61 }; // eo class MapFiller
65 * convenience class to fill values into a container (using push_back).
69 template< typename, typename> class C= std::list,
70 typename Alloc = std::allocator< T >
76 PushBackFiller( C<T, Alloc > & c )
80 PushBackFiller& operator () (const T& value)
82 CRef.push_back(value);
86 }; // eo class PushBackFiller
91 template< typename, typename > class C,
94 PushBackFiller< T, C, Alloc > get_push_back_filler( C< T, Alloc >& c)
96 return PushBackFiller< T, C, Alloc >(c);
97 } // eo get_push_back_filler(C< T, Alloc >&)
101 * convenience class for transient construction of a container including values.
105 template< typename, typename> class C= std::list,
106 typename Alloc = std::allocator< T >
108 class TransientPushBackFiller
112 typedef C< T, Alloc > CType;
114 TransientPushBackFiller()
117 TransientPushBackFiller& operator () (const T& value)
119 MyC.push_back(value);
123 operator CType () const { return MyC; }
124 }; // eo class TransientPushBackFiller
128 * convenience class for transient construction of a map including values.
130 template<typename K, typename V>
131 class TransientMapFiller
136 typedef std::map< K, V > CType;
139 TransientMapFiller( )
143 TransientMapFiller& operator () (const K& key, const V& value)
149 operator CType () const { return Map; }
150 }; // eo class MapFiller
156 * returns the keys of a map as a list.
157 * @param the_map the map.
158 * @param the_keys the list where the keys are added to.
161 template< typename K, typename V >
162 bool get_key_list( const std::map< K, V >& the_map, std::list< K >& the_keys )
164 for(typename std::map< K, V >::const_iterator it= the_map.begin();
168 the_keys.push_back( it->first );
171 } // eo get_key_list(const std::map< K,V >, std::list< K >&)
175 * returns the keys of a map as a list.
176 * @param the_map the map.
177 * @return the list of keys.
179 template< typename K, typename V >
180 std::list< K > get_key_list( const std::map< K, V>& the_map)
182 std::list< K > result;
183 get_key_list(the_map, result);
185 } // eo get_key_list(const std::map< K,V >)
189 * returns the keys of a map as a set.
190 * @param the_map the map.
191 * @param the_keys the set where the keys are added to.
194 template< typename K, typename V >
195 bool get_key_set( const std::map< K, V >& the_map, std::set< K >& the_keys )
197 for(typename std::map< K, V >::const_iterator it= the_map.begin();
201 the_keys.insert( it->first );
204 } // eo get_key_set(const std::map< K,V >, std::set< K >&)
208 * returns the keys of a map as a set.
209 * @param the_map the map.
210 * @return the set of keys.
212 template< typename K, typename V >
213 std::list< K > get_key_set( const std::map< K, V>& the_map)
215 std::set< K > result;
216 get_key_set(the_map, result);
218 } // eo get_key_set(const std::map< K,V >)
222 * functor version of new().
223 * can be used for deferred instantiation of objects.
230 typedef T* result_type;
232 result_type operator() ()
237 template<typename Arg1>
238 result_type operator() (Arg1 arg1)
243 template<typename Arg1, typename Arg2>
244 result_type operator() (Arg1 arg1,Arg2 arg2)
246 return new T(arg1,arg2);
249 template<typename Arg1, typename Arg2, typename Arg3>
250 result_type operator() (Arg1 arg1,Arg2 arg2,Arg3 arg3)
252 return new T(arg1,arg2,arg3);
255 template<typename Arg1, typename Arg2, typename Arg3, typename Arg4>
256 result_type operator() (Arg1 arg1,Arg2 arg2,Arg3 arg3,Arg4 arg4)
258 return new T(arg1,arg2,arg3,arg4);
261 template<typename Arg1, typename Arg2, typename Arg3, typename Arg4, typename Arg5>
262 result_type operator() (Arg1 arg1,Arg2 arg2,Arg3 arg3,Arg4 arg4,Arg5 arg5)
264 return new T(arg1,arg2,arg3,arg4,arg5);
273 * functor version of boost::shared_ptr = new().
274 * can be used for deferred instantiation of objects.
281 typedef boost::shared_ptr<T> result_type;
283 result_type operator() ()
285 return result_type(new T);
288 template<typename Arg1>
289 result_type operator() (Arg1 arg1)
291 return result_type(new T(arg1));
294 template<typename Arg1, typename Arg2>
295 result_type operator() (Arg1 arg1,Arg2 arg2)
297 return result_type(new T(arg1,arg2));
300 template<typename Arg1, typename Arg2, typename Arg3>
301 result_type operator() (Arg1 arg1,Arg2 arg2,Arg3 arg3)
303 return result_type(new T(arg1,arg2,arg3));
306 template<typename Arg1, typename Arg2, typename Arg3, typename Arg4>
307 result_type operator() (Arg1 arg1,Arg2 arg2,Arg3 arg3,Arg4 arg4)
309 return result_type(new T(arg1,arg2,arg3,arg4));
312 template<typename Arg1, typename Arg2, typename Arg3, typename Arg4, typename Arg5>
313 result_type operator() (Arg1 arg1,Arg2 arg2,Arg3 arg3,Arg4 arg4,Arg5 arg5)
315 return result_type(new T(arg1,arg2,arg3,arg4,arg5));
318 }; // eo struct SharedPtrNew
323 ** classes holding a shared or weak pointer.
324 ** designed to be used as policy classes...
325 ** (but may be used for other purposes, too)
331 class SharedOwnership
335 SharedOwnership( boost::shared_ptr< X > ptr)
340 boost::shared_ptr< X > get_ptr() const { return StoredPtr; }
344 boost::shared_ptr< X > StoredPtr;
345 }; // eo class SharedOwnership
356 WeakOwnership( boost::shared_ptr< X > ptr)
361 boost::shared_ptr< X > get_ptr() const { return StoredPtr.lock(); }
365 boost::weak_ptr< X > StoredPtr;
366 }; // eo class WeakOwnership
377 * @brief the ultimate class for doing nothing :-)
379 * Even it sounds odd to have a functor class which is doing nothing it makes
380 * sense to use this class in some situations.
382 * One example is the usage as Deleter for shared pointers which "own" a pointer to
383 * a static object (i.e. it is not allowed to be really deleted...). Using this class
384 * as Deleter in these pointers enables us to use interfaces which expect a shared pointer
385 * even if we want(/must) to pass pointers to static objects.
397 typename Arg1, typename Arg2
399 NoOperation(Arg1, Arg2) {}
402 typename Arg1, typename Arg2, typename Arg3
404 NoOperation(Arg1, Arg2, Arg3) {}
406 void operator() () const {}
411 void operator() (Arg1) const {}
414 typename Arg1, typename Arg2
416 void operator() (Arg1, Arg2) const {}
419 typename Arg1, typename Arg2, typename Arg3
421 void operator() (Arg1, Arg2, Arg3) const {}
423 }; // eo struct NoOperation
427 } // eo namespace I2n