libzypp 8.13.6
|
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 00022 #include "zypp/base/Tr1hash.h" 00023 #include "zypp/base/Logger.h" 00024 #include "zypp/base/Iterator.h" 00025 #include "zypp/base/Deprecated.h" 00026 00028 namespace zypp 00029 { 00030 00031 using std::endl; 00032 00090 template<class _Iterator> 00091 std::ostream & dumpRange( std::ostream & str, 00092 _Iterator begin, _Iterator end, 00093 const std::string & intro = "{", 00094 const std::string & pfx = "\n ", 00095 const std::string & sep = "\n ", 00096 const std::string & sfx = "\n", 00097 const std::string & extro = "}" ) 00098 { 00099 str << intro; 00100 if ( begin != end ) 00101 { 00102 str << pfx << *begin; 00103 for ( ++begin; begin != end; ++begin ) 00104 str << sep << *begin; 00105 str << sfx; 00106 } 00107 return str << extro; 00108 } 00109 00113 template<class _Iterator> 00114 std::ostream & dumpRangeLine( std::ostream & str, 00115 _Iterator begin, _Iterator end ) 00116 { return dumpRange( str, begin, end, "(", "", ", ", "", ")" ); } 00117 00118 00119 template<class _Tp> 00120 std::ostream & operator<<( std::ostream & str, const std::vector<_Tp> & obj ) 00121 { return dumpRange( str, obj.begin(), obj.end() ); } 00122 00123 template<class _Tp> 00124 std::ostream & operator<<( std::ostream & str, const std::set<_Tp> & obj ) 00125 { return dumpRange( str, obj.begin(), obj.end() ); } 00126 00127 template<class _Tp> 00128 std::ostream & operator<<( std::ostream & str, const std::tr1::unordered_set<_Tp> & obj ) 00129 { return dumpRange( str, obj.begin(), obj.end() ); } 00130 00131 template<class _Tp> 00132 std::ostream & operator<<( std::ostream & str, const std::list<_Tp> & obj ) 00133 { return dumpRange( str, obj.begin(), obj.end() ); } 00134 00136 namespace _logtoolsdetail 00137 { 00138 00140 // mapEntry 00142 00148 template<class _Pair> 00149 class MapEntry 00150 { 00151 public: 00152 MapEntry( const _Pair & pair_r ) 00153 : _pair( &pair_r ) 00154 {} 00155 00156 const _Pair & pair() const 00157 { return *_pair; } 00158 00159 private: 00160 const _Pair *const _pair; 00161 }; 00162 00164 template<class _Pair> 00165 std::ostream & operator<<( std::ostream & str, const MapEntry<_Pair> & obj ) 00166 { 00167 return str << '[' << obj.pair().first << "] = " << obj.pair().second; 00168 } 00169 00171 template<class _Pair> 00172 MapEntry<_Pair> mapEntry( const _Pair & pair_r ) 00173 { return MapEntry<_Pair>( pair_r ); } 00174 00176 // dumpMap 00178 00183 template<class _Map> 00184 class DumpMap 00185 { 00186 public: 00187 typedef _Map MapType; 00188 typedef typename _Map::value_type PairType; 00189 typedef MapEntry<PairType> MapEntryType; 00190 00191 struct Transformer : public std::unary_function<PairType, MapEntryType> 00192 { 00193 MapEntryType operator()( const PairType & pair_r ) const 00194 { return mapEntry( pair_r ); } 00195 }; 00196 00197 typedef transform_iterator<Transformer, typename MapType::const_iterator> 00198 MapEntry_const_iterator; 00199 00200 public: 00201 DumpMap( const _Map & map_r ) 00202 : _map( &map_r ) 00203 {} 00204 00205 const _Map & map() const 00206 { return *_map; } 00207 00208 MapEntry_const_iterator begin() const 00209 { return make_transform_iterator( map().begin(), Transformer() ); } 00210 00211 MapEntry_const_iterator 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 00332 template<class _Key, class _Tp> 00333 std::ostream & operator<<( std::ostream & str, const std::tr1::unordered_map<_Key, _Tp> & obj ) 00334 { return str << dumpMap( obj ); } 00335 00345 inline std::ostream & operator<<( std::ostream & str, const std::basic_ios<char> & obj ) 00346 { 00347 std::string ret( "[" ); 00348 ret += ( obj.good() ? 'g' : '_' ); 00349 ret += ( obj.eof() ? 'e' : '_' ); 00350 ret += ( obj.fail() ? 'F' : '_' ); 00351 ret += ( obj.bad() ? 'B' : '_' ); 00352 ret += "]"; 00353 return str << ret; 00354 } 00355 00357 // iomanipulator: str << dump(val) << ... 00358 // calls: std::ostream & dumpOn( std::ostream & str, const Type & obj ) 00360 00361 namespace detail 00362 { 00363 template<class _Tp> 00364 struct Dump 00365 { 00366 Dump( const _Tp & obj_r ) : _obj( obj_r ) {} 00367 const _Tp & _obj; 00368 }; 00369 00370 template<class _Tp> 00371 std::ostream & operator<<( std::ostream & str, const Dump<_Tp> & obj ) 00372 { return dumpOn( str, obj._obj ); } 00373 } 00374 00375 template<class _Tp> 00376 detail::Dump<_Tp> dump( const _Tp & obj_r ) 00377 { return detail::Dump<_Tp>(obj_r); } 00378 00379 00381 } // namespace zypp 00383 #endif // ZYPP_BASE_LOGTOOLS_H