LogTools.h

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                          ____ _   __ __ ___                          |
00003 |                         |__  / \ / / . \ . \                         |
00004 |                           / / \ V /|  _/  _/                         |
00005 |                          / /__ | | | | | |                           |
00006 |                         /_____||_| |_| |_|                           |
00007 |                                                                      |
00008 \---------------------------------------------------------------------*/
00012 #ifndef ZYPP_BASE_LOGTOOLS_H
00013 #define ZYPP_BASE_LOGTOOLS_H
00014 
00015 #include <iostream>
00016 #include <string>
00017 #include <vector>
00018 #include <list>
00019 #include <set>
00020 #include <map>
00021 #include "zypp/base/Logger.h"
00022 #include "zypp/base/Iterator.h"
00023 #include "zypp/base/Deprecated.h"
00024 
00026 namespace zypp
00027 { 
00028 
00086   template<class _Iterator>
00087     std::ostream & dumpRange( std::ostream & str,
00088                               _Iterator begin, _Iterator end,
00089                               const std::string & intro = "{",
00090                               const std::string & pfx   = "\n  ",
00091                               const std::string & sep   = "\n  ",
00092                               const std::string & sfx   = "\n",
00093                               const std::string & extro = "}" )
00094     {
00095       str << intro;
00096       if ( begin != end )
00097         {
00098           str << pfx << *begin;
00099           for (  ++begin; begin != end; ++begin )
00100             str << sep << *begin;
00101           str << sfx;
00102         }
00103       return str << extro;
00104     }
00105 
00109   template<class _Iterator>
00110     std::ostream & dumpRangeLine( std::ostream & str,
00111                                   _Iterator begin, _Iterator end )
00112     { return dumpRange( str, begin, end, "(", "", ", ", "", ")" ); }
00113 
00114 
00115   template<class _Tp>
00116     std::ostream & operator<<( std::ostream & str, const std::vector<_Tp> & obj )
00117     { return dumpRange( str, obj.begin(), obj.end() ); }
00118 
00119   template<class _Tp>
00120     std::ostream & operator<<( std::ostream & str, const std::set<_Tp> & obj )
00121     { return dumpRange( str, obj.begin(), obj.end() ); }
00122 
00123   template<class _Tp>
00124     std::ostream & operator<<( std::ostream & str, const std::list<_Tp> & obj )
00125     { return dumpRange( str, obj.begin(), obj.end() ); }
00126 
00128   namespace _logtoolsdetail
00129   { 
00130 
00132     // mapEntry
00134 
00140     template<class _Pair>
00141       class MapEntry
00142       {
00143       public:
00144         MapEntry( const _Pair & pair_r )
00145         : _pair( &pair_r )
00146         {}
00147 
00148         const _Pair & pair() const
00149         { return *_pair; }
00150 
00151       private:
00152         const _Pair *const _pair;
00153       };
00154 
00156     template<class _Pair>
00157       std::ostream & operator<<( std::ostream & str, const MapEntry<_Pair> & obj )
00158       {
00159         return str << '[' << obj.pair().first << "] = " << obj.pair().second;
00160       }
00161 
00163     template<class _Pair>
00164       MapEntry<_Pair> mapEntry( const _Pair & pair_r )
00165       { return MapEntry<_Pair>( pair_r ); }
00166 
00168     // dumpMap
00170 
00175     template<class _Map>
00176       class DumpMap
00177       {
00178       public:
00179         typedef _Map                        MapType;
00180         typedef typename _Map::value_type   PairType;
00181         typedef MapEntry<PairType>          MapEntryType;
00182 
00183         struct Transformer : public std::unary_function<PairType, MapEntryType>
00184         {
00185           MapEntryType operator()( const PairType & pair_r ) const
00186           { return mapEntry( pair_r ); }
00187         };
00188 
00189         typedef transform_iterator<Transformer, typename MapType::const_iterator>
00190                 MapEntry_const_iterator;
00191 
00192       public:
00193         DumpMap( const _Map & map_r )
00194         : _map( &map_r )
00195         {}
00196 
00197         const _Map & map() const
00198         { return *_map; }
00199 
00200         MapEntry_const_iterator begin() const
00201         { return make_transform_iterator( map().begin(), Transformer() ); }
00202 
00203         MapEntry_const_iterator end() const
00204         { return make_transform_iterator( map().end(), Transformer() );}
00205 
00207         ZYPP_DEPRECATED MapEntry_const_iterator map_begin() const
00208         { return make_transform_iterator( map().begin(), Transformer() ); }
00209 
00211         ZYPP_DEPRECATED MapEntry_const_iterator map_end() const
00212         { return make_transform_iterator( map().end(), Transformer() );}
00213 
00214       private:
00215         const _Map *const _map;
00216       };
00217 
00219     template<class _Map>
00220       std::ostream & operator<<( std::ostream & str, const DumpMap<_Map> & obj )
00221       { return dumpRange( str, obj.begin(), obj.end() ); }
00222 
00224     template<class _Map>
00225       DumpMap<_Map> dumpMap( const _Map & map_r )
00226       { return DumpMap<_Map>( map_r ); }
00227 
00229     // dumpKeys
00231 
00239     template<class _Map>
00240       class DumpKeys
00241       {
00242       public:
00243         typedef typename MapKVIteratorTraits<_Map>::Key_const_iterator MapKey_const_iterator;
00244 
00245       public:
00246         DumpKeys( const _Map & map_r )
00247         : _map( &map_r )
00248         {}
00249 
00250         const _Map & map() const
00251         { return *_map; }
00252 
00253         MapKey_const_iterator begin() const
00254         { return make_map_key_begin( map() ); }
00255 
00256         MapKey_const_iterator end() const
00257         { return make_map_key_end( map() ); }
00258 
00259       private:
00260         const _Map *const _map;
00261       };
00262 
00264     template<class _Map>
00265       std::ostream & operator<<( std::ostream & str, const DumpKeys<_Map> & obj )
00266       { return dumpRange( str, obj.begin(), obj.end() ); }
00267 
00269     template<class _Map>
00270       DumpKeys<_Map> dumpKeys( const _Map & map_r )
00271       { return DumpKeys<_Map>( map_r ); }
00272 
00274     // dumpValues
00276 
00284     template<class _Map>
00285       class DumpValues
00286       {
00287       public:
00288         typedef typename MapKVIteratorTraits<_Map>::Value_const_iterator MapValue_const_iterator;
00289 
00290       public:
00291         DumpValues( const _Map & map_r )
00292         : _map( &map_r )
00293         {}
00294 
00295         const _Map & map() const
00296         { return *_map; }
00297 
00298         MapValue_const_iterator begin() const
00299         { return make_map_value_begin( map() ); }
00300 
00301         MapValue_const_iterator end() const
00302         { return make_map_value_end( map() ); }
00303 
00304       private:
00305         const _Map *const _map;
00306       };
00307 
00309     template<class _Map>
00310       std::ostream & operator<<( std::ostream & str, const DumpValues<_Map> & obj )
00311       { return dumpRange( str, obj.begin(), obj.end() ); }
00312 
00314     template<class _Map>
00315       DumpValues<_Map> dumpValues( const _Map & map_r )
00316       { return DumpValues<_Map>( map_r ); }
00317 
00319   } // namespace _logtoolsdetail
00321 
00322   // iomanipulator
00323   using _logtoolsdetail::mapEntry;   // std::pair as '[key] = value'
00324   using _logtoolsdetail::dumpMap;    // dumpRange '[key] = value'
00325   using _logtoolsdetail::dumpKeys;   // dumpRange keys
00326   using _logtoolsdetail::dumpValues; // dumpRange values
00327 
00328   template<class _Key, class _Tp>
00329     std::ostream & operator<<( std::ostream & str, const std::map<_Key, _Tp> & obj )
00330     { return str << dumpMap( obj ); }
00331 
00341   inline std::ostream & operator<<( std::ostream & str, const std::basic_ios<char> & obj )
00342   {
00343     std::string ret( "[" );
00344     ret += ( obj.good() ? 'g' : '_' );
00345     ret += ( obj.eof()  ? 'e' : '_' );
00346     ret += ( obj.fail() ? 'F' : '_' );
00347     ret += ( obj.bad()  ? 'B' : '_' );
00348     ret += "]";
00349     return str << ret;
00350   }
00351 
00353 } // namespace zypp
00355 #endif // ZYPP_BASE_LOGTOOLS_H

Generated on Tue Sep 25 19:22:58 2007 for libzypp by  doxygen 1.5.3