Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members

algo.h

00001 #ifndef s11n_ALGO_H_INCLUDED
00002 #define s11n_ALGO_H_INCLUDED 1
00003 
00004 /////////////////////////////////////////////////////////////////////////
00005 // algo.h: generic algorithms
00006 // Author: stephan beal <stephan@s11n.net>
00007 // License: Public Domain
00008 /////////////////////////////////////////////////////////////////////////
00009 
00010 #include <functional> // for_each()
00011 
00012 #include "functor.h" // object_deleter()
00013 
00014 namespace s11n {
00015 
00016 
00017         /**
00018            For each item in (begin,end] object_deleter()(*item) is called.
00019 
00020            After this call the container from which the iterators come
00021            still contains the items but they are invalid - deleted
00022            pointers. The client should call erase(begin,end) to delete
00023            the entries, or use convenience function
00024            free_list_entries(), which accepts a list-style
00025            container.
00026         */
00027         template <typename IterT>
00028         void delete_objects( IterT begin, IterT end )
00029         {
00030                 std::for_each( begin, end, object_deleter() );
00031         }
00032 
00033 
00034 
00035         /**
00036            Calls free_list_entries( c.begin(), c.end() )
00037            and then erases the entries from c using it's
00038            erase() member fuction.
00039 
00040            After this call, c will be empty.
00041         */
00042         template <typename ListType>
00043         void free_list_entries( ListType & c )
00044         {
00045                 object_reference_wrapper<ListType> wr(c); // in case ListType is a pointer type
00046                 if( ! wr.good() ) return;
00047                 delete_objects( wr().begin(), wr().end() );
00048                 wr().clear();
00049         }
00050 
00051 
00052        /**
00053            Deletes any pointers in map, and does nothing for reference
00054            types. This is used to treat arbitrary maps containing
00055            pointers or value types identically, by applying the same
00056            set of deallocation rules for both cases, simplifying some
00057            template implementations.
00058         */
00059         template <typename MapType>
00060         void free_map_entries( MapType & map )
00061         {
00062                 object_reference_wrapper<MapType> wr(map); // in case MapType is a pointer type
00063                 if( ! wr.good() ) return;
00064                 std::for_each( wr().begin(), wr().end(), pair_entry_deallocator() );
00065                 map.clear();
00066         }
00067 
00068 
00069 }
00070 
00071 #endif // s11n_ALGO_H_INCLUDED

Generated on Thu Jun 16 16:18:12 2005 for s11n by  doxygen 1.4.3-20050530