- let ResFilters and ResStore.h operate on ResObject (not Resolvable)
[platform/upstream/libzypp.git] / devel / devel.ma / Main.cc
1 #include <ctime>
2
3 #include <iostream>
4 #include <list>
5 #include <map>
6 #include <set>
7
8 #include <zypp/base/Logger.h>
9 #include <zypp/base/String.h>
10 #include <zypp/base/Exception.h>
11 #include <zypp/base/PtrTypes.h>
12 #include <zypp/base/Iterator.h>
13
14 #include <zypp/PathInfo.h>
15 #include <zypp/SourceFactory.h>
16 #include <zypp/source/Builtin.h>
17 #include <zypp/source/susetags/SuseTagsImpl.h>
18
19 #include <zypp/Resolvable.h>
20 #include <zypp/Package.h>
21 #include <zypp/detail/PackageImpl.h>
22 #include <zypp/Selection.h>
23 #include <zypp/detail/SelectionImpl.h>
24 #include <zypp/Patch.h>
25 #include <zypp/detail/PatchImpl.h>
26
27
28 #include <zypp/ResFilters.h>
29 #include <zypp/ResStatus.h>
30 #include <zypp/Bit.h>
31
32 using namespace std;
33 using namespace zypp;
34 using namespace zypp::functor;
35
36 ///////////////////////////////////////////////////////////////////
37 namespace zypp
38 {
39
40 }
41 ///////////////////////////////////////////////////////////////////
42
43 template<class _IntT>
44   struct Counter
45   {
46     Counter()
47     : _value( _IntT(0) )
48     {}
49
50     Counter( _IntT value_r )
51     : _value( _IntT( value_r ) )
52     {}
53
54     operator _IntT &()
55     { return _value; }
56
57     operator const _IntT &() const
58     { return _value; }
59
60     _IntT _value;
61   };
62
63
64 ///////////////////////////////////////////////////////////////////
65
66 template <class _Impl>
67   typename _Impl::ResType::Ptr fakeResKind( Resolvable::Ptr from_r )
68   {
69     // fake different kind based on NVRAD
70     NVRAD nvrad( from_r );
71     shared_ptr<_Impl> impl;
72     return detail::makeResolvableAndImpl( nvrad, impl );
73   }
74
75 ///////////////////////////////////////////////////////////////////
76
77 struct Rstats : public std::unary_function<ResObject::constPtr, void>
78 {
79   void operator()( ResObject::constPtr ptr )
80   {
81     ++_total;
82     ++_perKind[ptr->kind()];
83   }
84
85   typedef std::map<ResolvableTraits::KindType,Counter<unsigned> > KindMap;
86   Counter<unsigned> _total;
87   KindMap           _perKind;
88 };
89
90 std::ostream & operator<<( std::ostream & str, const Rstats & obj )
91 {
92   str << "Total: " << obj._total;
93   for( Rstats::KindMap::const_iterator it = obj._perKind.begin(); it != obj._perKind.end(); ++it )
94     {
95       str << endl << "  " << it->first << ":\t" << it->second;
96     }
97   return str;
98 }
99
100 ///////////////////////////////////////////////////////////////////
101
102 struct SelItem
103 {
104   SelItem( ResObject::Ptr ptr_r )
105   : _status( 0 )
106   , _ptr( ptr_r )
107   {}
108
109   unsigned       _status;
110   ResObject::Ptr _ptr;
111
112   operator ResObject::Ptr()
113   { return _ptr; }
114
115   operator ResObject::constPtr() const
116   { return _ptr; }
117
118   ResObject::Ptr operator->()
119   { return _ptr; }
120
121   ResObject::constPtr operator->() const
122   { return _ptr; }
123
124   bool operator<( const SelItem & rhs ) const
125   { return _ptr < rhs._ptr; }
126 };
127
128
129 struct FakeConv : public std::unary_function<Resolvable::Ptr, void>
130 {
131   void operator()( Resolvable::Ptr ptr )
132   {
133     if ( ptr->name()[0] == 's' )
134       {
135         ptr = fakeResKind<detail::SelectionImpl>( ptr );
136       }
137     else if ( ptr->name()[0] == 'p' )
138       {
139         ptr = fakeResKind<detail::PatchImpl>( ptr );
140       }
141     _store.insert( SelItem(asKind<ResObject>(ptr)) );
142   }
143
144
145   typedef SelItem           ValueT;
146   typedef std::set<ValueT>  ContainerT;
147
148   ContainerT _store;
149
150   typedef ContainerT::iterator       IteratorT;
151   typedef ContainerT::const_iterator ConstIteratorT;
152
153   ConstIteratorT begin() const
154   { return _store.begin(); }
155
156   ConstIteratorT end() const
157   { return _store.end(); }
158
159   template<class _Filter>
160     filter_iterator<_Filter, ConstIteratorT> begin() const
161     { return make_filter_iterator( _Filter(), begin(), end() ); }
162
163   template<class _Filter>
164     filter_iterator<_Filter, ConstIteratorT> begin( _Filter f ) const
165     { return make_filter_iterator( f, begin(), end() ); }
166
167   template<class _Filter>
168     filter_iterator<_Filter, ConstIteratorT> end() const
169     { return make_filter_iterator( _Filter(), end(), end() ); }
170
171   template<class _Filter>
172     filter_iterator<_Filter, ConstIteratorT> end( _Filter f ) const
173     { return make_filter_iterator( f, end(), end() ); }
174
175
176 };
177
178 ///////////////////////////////////////////////////////////////////
179
180 /******************************************************************
181 **
182 **      FUNCTION NAME : main
183 **      FUNCTION TYPE : int
184 */
185 int main( int argc, char * argv[] )
186 {
187   INT << "===[START]==========================================" << endl;
188   string infile( "p" );
189   if (argc >= 2 )
190     infile = argv[1];
191
192   Source src( SourceFactory().createFrom( new source::susetags::SuseTagsImpl(infile) ) );
193   MIL << src.resolvables().size() << endl;
194
195   FakeConv fakeconv;
196   for_each( src.resolvables().begin(), src.resolvables().end(), functorRef(fakeconv) );
197
198   Rstats rstats = Rstats();
199   for_each( fakeconv.begin( byKind<Package>() ),
200             fakeconv.end( byKind<Package>() ),
201             functorRef(rstats) );
202   MIL << rstats << endl;
203
204   SelItem it( *fakeconv.begin() );
205   SEC << it->kind() << it->name() << endl;
206
207
208   INT << "===[END]============================================" << endl;
209   return 0;
210 }
211