backup
[platform/upstream/libzypp.git] / devel / devel.ma / Tools.h
1 #ifndef Tools_h
2 #define Tools_h
3
4 #include <iostream>
5
6 #include "Measure.h"
7 #include "Printing.h"
8
9 #include <zypp/ResObject.h>
10
11 #include <zypp/SourceFactory.h>
12 #include <zypp/source/susetags/SuseTagsImpl.h>
13
14 using namespace zypp;
15 using std::endl;
16
17 ///////////////////////////////////////////////////////////////////
18 // rstats
19 template<class _IntT>
20   struct Counter
21   {
22     Counter()                : _value( _IntT(0) )         {}
23     Counter( _IntT value_r ) : _value( _IntT( value_r ) ) {}
24     operator       _IntT &()       { return _value; }
25     operator const _IntT &() const { return _value; }
26
27     _IntT _value;
28   };
29
30 struct Rstats : public std::unary_function<ResObject::constPtr, void>
31 {
32   void operator()( ResObject::constPtr ptr )
33   {
34     ++_total;
35     ++_perKind[ptr->kind()];
36   }
37
38   typedef std::map<ResolvableTraits::KindType,Counter<unsigned> > KindMap;
39   Counter<unsigned> _total;
40   KindMap           _perKind;
41 };
42
43 std::ostream & operator<<( std::ostream & str, const Rstats & obj )
44 {
45   str << "Total: " << obj._total;
46   for( Rstats::KindMap::const_iterator it = obj._perKind.begin(); it != obj._perKind.end(); ++it )
47     {
48       str << endl << "  " << it->first << ":\t" << it->second;
49     }
50   return str;
51 }
52
53 template<class _Iterator>
54   void rstats( _Iterator begin, _Iterator end )
55   {
56     DBG << __PRETTY_FUNCTION__ << endl;
57     Rstats stats;
58     for_each( begin, end, functor::functorRef<void,ResObject::constPtr>(stats) );
59     MIL << stats << endl;
60   }
61
62 template<class _Container>
63   void rstats( const _Container & c )
64   {
65     rstats( c.begin(), c.end() );
66   }
67
68 ///////////////////////////////////////////////////////////////////
69 inline Source_Ref createSource( const Url & url_r )
70 {
71   Source_Ref ret;
72   Measure x( "createSource: " + url_r.asString() );
73   try
74     {
75       ret = SourceFactory().createFrom( url_r );
76     }
77   catch ( const Exception & )
78     {
79       return Source_Ref::noSource;
80     }
81   x.start( "parseSource: " + url_r.asString() );
82   ret.resolvables();
83   x.stop();
84   MIL << "Content " << ret << "{" << endl;
85   rstats( ret.resolvables() );
86   MIL << "}" << endl;
87
88   return ret;
89 }
90 inline Source_Ref createSource( const std::string & url_r )
91 {
92   try
93     {
94       return createSource( Url(url_r) );
95     }
96   catch ( const Exception & )
97     {
98       return Source_Ref::noSource;
99     }
100 }
101
102 #endif // Tools_h