Merged revisions 4705-4906 via svnmerge from
[platform/upstream/libzypp.git] / devel / devel.ma / Tools.h
1 #ifndef Tools_h
2 #define Tools_h
3
4 #include <iostream>
5
6 #include "Printing.h"
7
8 #include <zypp/base/Counter.h>
9 #include <zypp/base/Measure.h>
10
11 #include <zypp/Date.h>
12 #include <zypp/ResObject.h>
13 #include <zypp/pool/PoolStats.h>
14
15 #include <zypp/SourceFactory.h>
16 #include <zypp/source/susetags/SuseTagsImpl.h>
17
18 using namespace zypp;
19 using zypp::debug::Measure;
20 using std::endl;
21
22 ///////////////////////////////////////////////////////////////////
23 //
24
25 template<class _Condition>
26   struct SetTrue
27   {
28     SetTrue( _Condition cond_r )
29     : _cond( cond_r )
30     {}
31
32     template<class _Tp>
33       bool operator()( _Tp t ) const
34       {
35         _cond( t );
36         return true;
37       }
38
39     _Condition _cond;
40   };
41
42 template<class _Condition>
43   inline SetTrue<_Condition> setTrue_c( _Condition cond_r )
44   {
45     return SetTrue<_Condition>( cond_r );
46   }
47
48 struct PrintPoolItem
49 {
50   void operator()( const PoolItem & pi ) const
51   { USR << pi << " (" << pi.resolvable().get() << ")" <<endl; }
52 };
53
54 template <class _Iterator>
55   std::ostream & vdumpPoolStats( std::ostream & str,
56                                  _Iterator begin_r, _Iterator end_r )
57   {
58     pool::PoolStats stats;
59     std::for_each( begin_r, end_r,
60
61                    functor::chain( setTrue_c(PrintPoolItem()),
62                                    setTrue_c(functor::functorRef<void,ResObject::constPtr>(stats)) )
63
64                  );
65     return str << stats;
66   }
67
68 ///////////////////////////////////////////////////////////////////
69 // rstats
70
71 typedef zypp::pool::PoolStats Rstats;
72
73 template<class _Iterator>
74   void rstats( _Iterator begin, _Iterator end )
75   {
76     DBG << __PRETTY_FUNCTION__ << endl;
77     Rstats stats;
78     for_each( begin, end, functor::functorRef<void,ResObject::constPtr>(stats) );
79     MIL << stats << endl;
80   }
81
82 template<class _Container>
83   void rstats( const _Container & c )
84   {
85     rstats( c.begin(), c.end() );
86   }
87
88 ///////////////////////////////////////////////////////////////////
89 inline Source_Ref createSource( const Url & url_r, const std::string & alias_r = std::string() )
90 {
91   Source_Ref ret;
92   Measure x( "createSource: " + url_r.asString() );
93   try
94     {
95       std::string alias( alias_r.empty() ? Date::now().asSeconds() : alias_r );
96       try
97         {
98           ret = SourceFactory().createFrom( url_r, "/", alias );
99         }
100       catch ( const source::SourceUnknownTypeException & )
101         {
102           ret = SourceFactory().createFrom( "Plaindir", url_r, "/", alias, "", false, true );
103         }
104     }
105   catch ( const Exception & )
106     {
107       return Source_Ref::noSource;
108     }
109   x.start( "parseSource: " + url_r.asString() );
110   {
111     //zypp::base::LogControl::TmpLineWriter shutUp;
112     ret.resolvables();
113   }
114   x.stop();
115   MIL << "Content " << ret << "{" << endl;
116   rstats( ret.resolvables() );
117   MIL << "}" << endl;
118
119   return ret;
120 }
121 inline Source_Ref createSource( const std::string & url_r, const std::string & alias_r = std::string() )
122 {
123   try
124     {
125       return createSource( Url(url_r), alias_r );
126     }
127   catch ( const Exception & )
128     {
129       return Source_Ref::noSource;
130     }
131 }
132
133 #endif // Tools_h