Imported Upstream version 16.3.2
[platform/upstream/libzypp.git] / zypp / pool / PoolStats.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/pool/PoolStats.h
10  *
11 */
12 #ifndef ZYPP_POOL_POOLSTATS_H
13 #define ZYPP_POOL_POOLSTATS_H
14
15 #include <iosfwd>
16
17 #include "zypp/base/Iterator.h"
18 #include "zypp/base/Functional.h"
19 #include "zypp/base/Counter.h"
20 #include "zypp/ResObject.h"
21
22 ///////////////////////////////////////////////////////////////////
23 namespace zypp
24 { /////////////////////////////////////////////////////////////////
25   ///////////////////////////////////////////////////////////////////
26   namespace pool
27   { /////////////////////////////////////////////////////////////////
28
29     ///////////////////////////////////////////////////////////////////
30     //
31     //  CLASS NAME : PoolStats
32     //
33     /** Functor counting ResObjects per Kind.
34      * \see dumpPoolStats
35      * \code
36      * Total: 2830
37      *   language:     81
38      *   package:      2710
39      *   product:      2
40      *   selection:    36
41      *   system:       1
42      * \endcode
43     */
44     struct PoolStats : public std::unary_function<ResObject::constPtr, void>
45     {
46       void operator()( ResObject::constPtr ptr )
47       {
48         ++_total;
49         ++_perKind[ptr->kind()];
50       }
51     public:
52       typedef std::map<ResKind,Counter<unsigned> > KindMap;
53       Counter<unsigned> _total;
54       KindMap           _perKind;
55     };
56     ///////////////////////////////////////////////////////////////////
57
58     /** \relates PoolStats Stream output */
59     std::ostream & operator<<( std::ostream & str, const PoolStats & obj );
60
61     /////////////////////////////////////////////////////////////////
62   } // namespace pool
63   ///////////////////////////////////////////////////////////////////
64
65   /** \relates pool::PoolStats Convenience to count and print out the
66    *  number of ResObjects per Kind in a container.
67    * Fits container of ResObject::Ptr or PoolItem.
68   */
69   template <class TIterator>
70     std::ostream & dumpPoolStats( std::ostream & str,
71                                   TIterator begin_r, TIterator end_r )
72     {
73       pool::PoolStats stats;
74       std::for_each( begin_r, end_r,
75                      functor::functorRef<void,ResObject::constPtr>(stats) );
76       return str << stats;
77     }
78
79   /////////////////////////////////////////////////////////////////
80 } // namespace zypp
81 ///////////////////////////////////////////////////////////////////
82 #endif // ZYPP_POOL_POOLSTATS_H