Imported Upstream version 14.45.0
[platform/upstream/libzypp.git] / zypp / base / Functional.h
index 9c8fc46..9d6de50 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
 { /////////////////////////////////////////////////////////////////
@@ -22,13 +23,12 @@ namespace zypp
   /* http://www.boost.org/libs/functional/mem_fun.html
 
    The header functional.hpp includes improved versions of
-   the full range of member function adapters from the the
+   the full range of member function adapters from the
    C++ Standard Library.
   */
   using boost::mem_fun;
   using boost::mem_fun_ref;
 
-
   ///////////////////////////////////////////////////////////////////
   namespace functor
   { /////////////////////////////////////////////////////////////////
@@ -205,7 +205,7 @@ namespace zypp
      *     \ref Cain functor.
      *
      * \code
-     *  struct Print; // functor priniting elements
+     *  struct Print; // functor printing elements
      *  struct Count; // functor counting number of elements
      *
      *  std::for_each( c.begin(), c.end(),
@@ -214,6 +214,29 @@ namespace zypp
     */
     //@{
 
+    /* functor that always returns a copied
+       value */
+    template<class T>
+    struct Constant
+    {
+      Constant( const T &value )
+        : _value(value)
+      {}
+
+      template<class _Tp>
+      T operator()( _Tp ) const
+      { return _value; }
+
+      T operator()() const
+      { return _value; }
+
+      T _value;
+    };
+
+    template<class T>
+    inline Constant<T> constant( const T &value )
+    { return Constant<T>(value); }
+
     /** Logical functor always \c true.
     */
     struct True
@@ -330,7 +353,90 @@ namespace zypp
     //@}
     ///////////////////////////////////////////////////////////////////
 
-    /////////////////////////////////////////////////////////////////
+    /** \defgroup ACTIONFUNCTOR
+     * \ingroup g_Functor
+     */
+    //@{
+
+    /** Strore the 1st result found in the variable passed to the ctor.
+     * \code
+     *   PoolItem result;
+     *   invokeOnEach( pool.byIdentBegin(installed), pool.byIdentEnd(installed),
+     *                 filter::SameItem( installed ),
+     *                 getFirst( result ) );
+     * \endcode
+     */
+    template<class _Tp>
+    struct GetFirst
+    {
+      GetFirst( _Tp & result_r )
+        : _result( &result_r )
+      {}
+      bool operator()( const _Tp & val_r )
+      { *_result = val_r; return false; }
+
+      private:
+        _Tp * _result;
+    };
+
+    /** Convenience function for creating \ref GetFirst. */
+    template<class _Tp>
+    GetFirst<_Tp> getFirst( _Tp & result_r )
+    { return GetFirst<_Tp>( result_r ); }
+
+
+    /** Strore the last result found in the variable passed to the ctor.
+     */
+    template<class _Tp>
+    struct GetLast
+    {
+      GetLast( _Tp & result_r )
+        : _result( &result_r )
+      {}
+      bool operator()( const _Tp & val_r )
+      { *_result = val_r; return true; }
+
+      private:
+        _Tp * _result;
+    };
+
+    /** Convenience function for creating \ref GetLast. */
+    template<class _Tp>
+    GetLast<_Tp> getLast( _Tp & result_r )
+    { return GetLast<_Tp>( result_r ); }
+
+
+    /** Store all results found to some output_iterator.
+     * \code
+     * std::vector<parser::ProductFileData> result;
+     * parser::ProductFileReader::scanDir( functor::getAll( std::back_inserter( result ) ),
+                                           sysRoot / "etc/products.d" );
+     * \endcode
+     */
+    template<class _OutputIterator>
+    struct GetAll
+    {
+      GetAll( _OutputIterator result_r )
+        : _result( result_r )
+      {}
+
+      template<class _Tp>
+      bool operator()(  const _Tp & val_r ) const
+      { *(_result++) = val_r; return true; }
+
+      private:
+        mutable _OutputIterator _result;
+    };
+
+    /** Convenience function for creating \ref GetAll. */
+    template<class _OutputIterator>
+    GetAll<_OutputIterator> getAll( _OutputIterator result_r )
+    { return GetAll<_OutputIterator>( result_r ); }
+
+    //@}
+    ///////////////////////////////////////////////////////////////////
+
+   /////////////////////////////////////////////////////////////////
   } // namespace functor
   ///////////////////////////////////////////////////////////////////
   /////////////////////////////////////////////////////////////////