libzypp
10.5.0
|
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::multiset<_Tp> & obj ) 00133 { return dumpRange( str, obj.begin(), obj.end() ); } 00134 00135 template<class _Tp> 00136 std::ostream & operator<<( std::ostream & str, const std::list<_Tp> & obj ) 00137 { return dumpRange( str, obj.begin(), obj.end() ); } 00138 00140 namespace _logtoolsdetail 00141 { 00142 00144 // mapEntry 00146 00152 template<class _Pair> 00153 class MapEntry 00154 { 00155 public: 00156 MapEntry( const _Pair & pair_r ) 00157 : _pair( &pair_r ) 00158 {} 00159 00160 const _Pair & pair() const 00161 { return *_pair; } 00162 00163 private: 00164 const _Pair *const _pair; 00165 }; 00166 00168 template<class _Pair> 00169 std::ostream & operator<<( std::ostream & str, const MapEntry<_Pair> & obj ) 00170 { 00171 return str << '[' << obj.pair().first << "] = " << obj.pair().second; 00172 } 00173 00175 template<class _Pair> 00176 MapEntry<_Pair> mapEntry( const _Pair & pair_r ) 00177 { return MapEntry<_Pair>( pair_r ); } 00178 00180 // dumpMap 00182 00187 template<class _Map> 00188 class DumpMap 00189 { 00190 public: 00191 typedef _Map MapType; 00192 typedef typename _Map::value_type PairType; 00193 typedef MapEntry<PairType> MapEntryType; 00194 00195 struct Transformer : public std::unary_function<PairType, MapEntryType> 00196 { 00197 MapEntryType operator()( const PairType & pair_r ) const 00198 { return mapEntry( pair_r ); } 00199 }; 00200 00201 typedef transform_iterator<Transformer, typename MapType::const_iterator> 00202 MapEntry_const_iterator; 00203 00204 public: 00205 DumpMap( const _Map & map_r ) 00206 : _map( &map_r ) 00207 {} 00208 00209 const _Map & map() const 00210 { return *_map; } 00211 00212 MapEntry_const_iterator begin() const 00213 { return make_transform_iterator( map().begin(), Transformer() ); } 00214 00215 MapEntry_const_iterator end() const 00216 { return make_transform_iterator( map().end(), Transformer() );} 00217 00218 private: 00219 const _Map *const _map; 00220 }; 00221 00223 template<class _Map> 00224 std::ostream & operator<<( std::ostream & str, const DumpMap<_Map> & obj ) 00225 { return dumpRange( str, obj.begin(), obj.end() ); } 00226 00228 template<class _Map> 00229 DumpMap<_Map> dumpMap( const _Map & map_r ) 00230 { return DumpMap<_Map>( map_r ); } 00231 00233 // dumpKeys 00235 00243 template<class _Map> 00244 class DumpKeys 00245 { 00246 public: 00247 typedef typename MapKVIteratorTraits<_Map>::Key_const_iterator MapKey_const_iterator; 00248 00249 public: 00250 DumpKeys( const _Map & map_r ) 00251 : _map( &map_r ) 00252 {} 00253 00254 const _Map & map() const 00255 { return *_map; } 00256 00257 MapKey_const_iterator begin() const 00258 { return make_map_key_begin( map() ); } 00259 00260 MapKey_const_iterator end() const 00261 { return make_map_key_end( map() ); } 00262 00263 private: 00264 const _Map *const _map; 00265 }; 00266 00268 template<class _Map> 00269 std::ostream & operator<<( std::ostream & str, const DumpKeys<_Map> & obj ) 00270 { return dumpRange( str, obj.begin(), obj.end() ); } 00271 00273 template<class _Map> 00274 DumpKeys<_Map> dumpKeys( const _Map & map_r ) 00275 { return DumpKeys<_Map>( map_r ); } 00276 00278 // dumpValues 00280 00288 template<class _Map> 00289 class DumpValues 00290 { 00291 public: 00292 typedef typename MapKVIteratorTraits<_Map>::Value_const_iterator MapValue_const_iterator; 00293 00294 public: 00295 DumpValues( const _Map & map_r ) 00296 : _map( &map_r ) 00297 {} 00298 00299 const _Map & map() const 00300 { return *_map; } 00301 00302 MapValue_const_iterator begin() const 00303 { return make_map_value_begin( map() ); } 00304 00305 MapValue_const_iterator end() const 00306 { return make_map_value_end( map() ); } 00307 00308 private: 00309 const _Map *const _map; 00310 }; 00311 00313 template<class _Map> 00314 std::ostream & operator<<( std::ostream & str, const DumpValues<_Map> & obj ) 00315 { return dumpRange( str, obj.begin(), obj.end() ); } 00316 00318 template<class _Map> 00319 DumpValues<_Map> dumpValues( const _Map & map_r ) 00320 { return DumpValues<_Map>( map_r ); } 00321 00323 } // namespace _logtoolsdetail 00325 00326 // iomanipulator 00327 using _logtoolsdetail::mapEntry; // std::pair as '[key] = value' 00328 using _logtoolsdetail::dumpMap; // dumpRange '[key] = value' 00329 using _logtoolsdetail::dumpKeys; // dumpRange keys 00330 using _logtoolsdetail::dumpValues; // dumpRange values 00331 00332 template<class _Key, class _Tp> 00333 std::ostream & operator<<( std::ostream & str, const std::map<_Key, _Tp> & obj ) 00334 { return str << dumpMap( obj ); } 00335 00336 template<class _Key, class _Tp> 00337 std::ostream & operator<<( std::ostream & str, const std::tr1::unordered_map<_Key, _Tp> & obj ) 00338 { return str << dumpMap( obj ); } 00339 00340 template<class _Key, class _Tp> 00341 std::ostream & operator<<( std::ostream & str, const std::multimap<_Key, _Tp> & obj ) 00342 { return str << dumpMap( obj ); } 00343 00353 inline std::ostream & operator<<( std::ostream & str, const std::basic_ios<char> & obj ) 00354 { 00355 std::string ret( "[" ); 00356 ret += ( obj.good() ? 'g' : '_' ); 00357 ret += ( obj.eof() ? 'e' : '_' ); 00358 ret += ( obj.fail() ? 'F' : '_' ); 00359 ret += ( obj.bad() ? 'B' : '_' ); 00360 ret += "]"; 00361 return str << ret; 00362 } 00363 00365 // iomanipulator: str << dump(val) << ... 00366 // calls: std::ostream & dumpOn( std::ostream & str, const Type & obj ) 00368 00369 namespace detail 00370 { 00371 template<class _Tp> 00372 struct Dump 00373 { 00374 Dump( const _Tp & obj_r ) : _obj( obj_r ) {} 00375 const _Tp & _obj; 00376 }; 00377 00378 template<class _Tp> 00379 std::ostream & operator<<( std::ostream & str, const Dump<_Tp> & obj ) 00380 { return dumpOn( str, obj._obj ); } 00381 } 00382 00383 template<class _Tp> 00384 detail::Dump<_Tp> dump( const _Tp & obj_r ) 00385 { return detail::Dump<_Tp>(obj_r); } 00386 00387 00389 } // namespace zypp 00391 #endif // ZYPP_BASE_LOGTOOLS_H