added Collector: functor writing elements to an output iterator.
authorMichael Andres <ma@suse.de>
Tue, 19 Feb 2008 15:08:17 +0000 (15:08 +0000)
committerMichael Andres <ma@suse.de>
Tue, 19 Feb 2008 15:08:17 +0000 (15:08 +0000)
zypp/CMakeLists.txt
zypp/base/Collector.h [new file with mode: 0644]
zypp/base/Functional.h
zypp/base/Iterator.h
zypp/sat/Pool.h
zypp/sat/Solvable.cc

index 3484d8c..1f1a326 100644 (file)
@@ -205,6 +205,7 @@ SET( zypp_base_SRCS
 )
 
 SET( zypp_base_HEADERS
+  base/Collector.h
   base/SerialNumber.h
   base/Easy.h
   base/Random.h
diff --git a/zypp/base/Collector.h b/zypp/base/Collector.h
new file mode 100644 (file)
index 0000000..605e7b4
--- /dev/null
@@ -0,0 +1,66 @@
+/*---------------------------------------------------------------------\
+|                          ____ _   __ __ ___                          |
+|                         |__  / \ / / . \ . \                         |
+|                           / / \ V /|  _/  _/                         |
+|                          / /__ | | | | | |                           |
+|                         /_____||_| |_| |_|                           |
+|                                                                      |
+\---------------------------------------------------------------------*/
+/** \file      zypp/base/Collector.h
+ *
+*/
+#ifndef ZYPP_BASE_COLLECTOR_H
+#define ZYPP_BASE_COLLECTOR_H
+
+///////////////////////////////////////////////////////////////////
+namespace zypp
+{ /////////////////////////////////////////////////////////////////
+///////////////////////////////////////////////////////////////////
+namespace functor
+{ /////////////////////////////////////////////////////////////////
+
+  ///////////////////////////////////////////////////////////////////
+  //
+  //   CLASS NAME : _Collector<_OutputIterator>
+  //
+  /** Functor feeding values to an output_iterator.
+   *
+   * \code
+   * LocaleSet locales;
+   * for_each( begin(), end(),
+   *           Collector( std::inserter( locales_r, locales_r.begin() ) ) );
+   * \endcode
+   *
+   * \see Convenience constructor \ref Collector.
+   */
+  template<class _OutputIterator>
+  struct _Collector
+  {
+    _Collector( _OutputIterator iter_r ) : _iter( iter_r ) {}
+
+    template<class _Tp>
+    bool operator()( const _Tp & value_r ) const
+    {
+      *_iter++ = value_r;
+      return true;
+    }
+
+    private:
+      mutable _OutputIterator _iter;
+  };
+  ///////////////////////////////////////////////////////////////////
+
+  /** \relates _Collector Convenience constructor. */
+  template<class _OutputIterator>
+  inline _Collector<_OutputIterator> Collector( _OutputIterator iter_r )
+  { return _Collector<_OutputIterator>( iter_r ); }
+
+  ///////////////////////////////////////////////////////////////////
+
+  /////////////////////////////////////////////////////////////////
+} // namespace functor
+///////////////////////////////////////////////////////////////////
+  /////////////////////////////////////////////////////////////////
+} // namespace zypp
+///////////////////////////////////////////////////////////////////
+#endif // ZYPP_BASE_COLLECTOR_H
index 1c1a30d..26e8208 100644 (file)
 #ifndef ZYPP_BASE_FUNCTIONAL_H
 #define ZYPP_BASE_FUNCTIONAL_H
 
-//#include <functional>
 #include <boost/functional.hpp>
 
+#include "zypp/base/Function.h"
+
 ///////////////////////////////////////////////////////////////////
 namespace zypp
 { /////////////////////////////////////////////////////////////////
@@ -28,7 +29,6 @@ namespace zypp
   using boost::mem_fun;
   using boost::mem_fun_ref;
 
-
   ///////////////////////////////////////////////////////////////////
   namespace functor
   { /////////////////////////////////////////////////////////////////
index a9385a7..3f28b97 100644 (file)
@@ -13,9 +13,9 @@
 #define ZYPP_BASE_ITERATOR_H
 
 #include <iterator>
-#include <functional>
 #include <utility>
 
+#include <boost/functional.hpp>
 #include <boost/iterator/filter_iterator.hpp>
 #include <boost/iterator/transform_iterator.hpp>
 #include <boost/function_output_iterator.hpp>
index 1016ae9..4bec539 100644 (file)
@@ -130,7 +130,18 @@ namespace zypp
         /** Iterator behind the last \ref Solvable. */
         SolvableIterator solvablesEnd() const;
 
-      public:
+        /** \name Iterate all Solvables matching a \c _Filter. */
+        //@{
+        template<class _Filter>
+        filter_iterator<_Filter,SolvableIterator> filterBegin( const _Filter & filter_r ) const
+        { return make_filter_iterator( filter_r, solvablesBegin(), solvablesEnd() ); }
+
+        template<class _Filter>
+        filter_iterator<_Filter,SolvableIterator> filterEnd( const _Filter & filter_r ) const
+        { return make_filter_iterator( filter_r, solvablesEnd(), solvablesEnd() ); }
+        //@}
+
+     public:
         /** Conainer of all \ref Solvable providing \c cap_r.  */
         WhatProvides whatProvides( Capability cap_r ) const
         { return WhatProvides( cap_r ); }
index 299d7c7..89f079e 100644 (file)
@@ -14,8 +14,8 @@
 #include "zypp/base/Logger.h"
 #include "zypp/base/Gettext.h"
 #include "zypp/base/Exception.h"
-#include "zypp/base/Function.h"
 #include "zypp/base/Functional.h"
+#include "zypp/base/Collector.h"
 
 #include "zypp/sat/detail/PoolImpl.h"
 #include "zypp/sat/Solvable.h"
@@ -361,6 +361,10 @@ namespace zypp
     ///////////////////////////////////////////////////////////////////
     namespace
     { /////////////////////////////////////////////////////////////////
+      /** Expand \ref Capability and call \c fnc_r for each namescpace:language
+       * dependency. Return #invocations of fnc_r, negative if fnc_r returned
+       * false to indicate abort.
+       */
       int invokeOnEachSupportedLocale( Capability cap_r, function<bool (const Locale &)> fnc_r )
       {
         CapDetail detail( cap_r );
@@ -397,7 +401,10 @@ namespace zypp
         return 0;
       }
 
-      // return #invocations of fnc_r, negative if fnc_r returned false to indicate abort.
+       /** Expand \ref Capability and call \c fnc_r for each namescpace:language
+       * dependency. Return #invocations of fnc_r, negative if fnc_r returned
+       * false to indicate abort.
+       */
       inline int invokeOnEachSupportedLocale( Capabilities cap_r, function<bool (const Locale &)> fnc_r )
       {
         int cnt = 0;
@@ -410,6 +417,7 @@ namespace zypp
         }
         return cnt;
       }
+      //@}
 
       // Functor returning false if a Locale is in the set.
       struct NoMatchIn
@@ -424,25 +432,6 @@ namespace zypp
         const LocaleSet & _locales;
       };
 
-      template<class _OutputIterator>
-      struct _Collector
-      {
-        _Collector( _OutputIterator iter_r ) : _iter( iter_r ) {}
-
-        template<class _Tp>
-        bool operator()( const _Tp & value_r ) const
-        {
-          *_iter++ = value_r;
-          return true;
-        }
-
-        mutable _OutputIterator _iter;
-      };
-
-      template<class _OutputIterator>
-      inline _Collector<_OutputIterator> Collector( _OutputIterator iter_r )
-      { return _Collector<_OutputIterator>( iter_r ); }
-
     } /////////////////////////////////////////////////////////////////
 
     bool Solvable::supportsLocales() const
@@ -471,7 +460,7 @@ namespace zypp
     void Solvable::getSupportedLocales( LocaleSet & locales_r ) const
     {
       invokeOnEachSupportedLocale( supplements(),
-                                   Collector( std::inserter( locales_r, locales_r.begin() ) ) );
+                                   functor::Collector( std::inserter( locales_r, locales_r.begin() ) ) );
     }
 
     /******************************************************************