- added zypp/base/LogTools.h (ostream operator<< for vector/set/list,
[platform/upstream/libzypp.git] / zypp / base / LogTools.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/base/LogTools.h
10  *
11 */
12 #ifndef ZYPP_BASE_LOGTOOLS_H
13 #define ZYPP_BASE_LOGTOOLS_H
14
15 #include <iostream>
16 #include <string>
17 #include <vector>
18 #include <list>
19 #include <set>
20 #include "zypp/base/Logger.h"
21
22 ///////////////////////////////////////////////////////////////////
23 namespace zypp
24 { /////////////////////////////////////////////////////////////////
25
26   /** Print range defined by iterators.
27    * \code
28    * intro [ pfx ITEM [ { sep ITEM }+ ] sfx ] extro
29    * \encode
30    * The defaults print the range enclosed in \c {}, one item per
31    * line indented by 2 spaces.
32    * \code
33    * {
34    *   item1
35    *   item2
36    * }
37    * {} // on empty rande
38    * \encode
39    * A comma separated list enclosed in \c () would be
40    * \code
41    * printRange( begin, end, stream, "(", "", ", ", "", ")" );
42    * \encode
43   */
44   template<class _Iterator>
45     std::ostream & printRange( _Iterator begin, _Iterator end,
46                                std::ostream & str,
47                                const std::string & intro = "{",
48                                const std::string & pfx   = "\n  ",
49                                const std::string & sep   = "\n  ",
50                                const std::string & sfx   = "\n",
51                                const std::string & extro = "}" )
52     {
53       str << intro;
54       if ( begin != end )
55         {
56           str << pfx << *begin;
57           for (  ++begin; begin != end; ++begin )
58             str << sep << *begin;
59           str << sfx;
60         }
61       return str << extro;
62     }
63
64   template<class _Tp>
65     std::ostream & operator<<( std::ostream & str, const std::vector<_Tp> & obj )
66     { return printRange( obj.begin(), obj.end(), str ); }
67
68   template<class _Tp>
69     std::ostream & operator<<( std::ostream & str, const std::set<_Tp> & obj )
70     { return printRange( obj.begin(), obj.end(), str ); }
71
72   template<class _Tp>
73     std::ostream & operator<<( std::ostream & str, const std::list<_Tp> & obj )
74     { return printRange( obj.begin(), obj.end(), str ); }
75
76   /////////////////////////////////////////////////////////////////
77 } // namespace zypp
78 ///////////////////////////////////////////////////////////////////
79 #endif // ZYPP_BASE_LOGTOOLS_H