Helper.cc

Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
00002 /* Helper.cc
00003  *
00004  * Static helpers
00005  *
00006  * Copyright (C) 2000-2002 Ximian, Inc.
00007  * Copyright (C) 2005 SUSE Linux Products GmbH
00008  *
00009  * This program is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU General Public License,
00011  * version 2, as published by the Free Software Foundation.
00012  *
00013  * This program is distributed in the hope that it will be useful, but
00014  * WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00021  * 02111-1307, USA.
00022  */
00023 
00024 #include "zypp/solver/detail/Helper.h"
00025 
00026 #include "zypp/CapSet.h"
00027 #include "zypp/base/Logger.h"
00028 #include "zypp/base/String.h"
00029 #include "zypp/base/Gettext.h"
00030 
00031 #include "zypp/base/Algorithm.h"
00032 #include "zypp/ResPool.h"
00033 #include "zypp/ResFilters.h"
00034 #include "zypp/CapFilters.h"
00035 
00036 using namespace std;
00037 
00039 namespace zypp
00040 { 
00041 
00042   namespace solver
00043   { 
00044 
00045     namespace detail
00046     { 
00047 
00048 ostream &
00049 operator<< (ostream & os, const PoolItemList & itemlist)
00050 {
00051     for (PoolItemList::const_iterator iter = itemlist.begin(); iter != itemlist.end(); ++iter) {
00052         if (iter != itemlist.begin())
00053             os << ", ";
00054         os << *iter;
00055     }
00056     return os;
00057 }
00058 
00059 
00060 class LookFor : public resfilter::PoolItemFilterFunctor
00061 {
00062   public:
00063     PoolItem_Ref item;
00064 
00065     bool operator()( PoolItem_Ref provider )
00066     {
00067         item = provider;
00068         return false;                           // stop here, we found it
00069     }
00070 };
00071 
00072 
00073 // just find installed item with same kind/name as item
00074 
00075 PoolItem_Ref
00076 Helper::findInstalledByNameAndKind (const ResPool & pool, const string & name, const Resolvable::Kind & kind)
00077 {
00078     LookFor info;
00079 
00080     invokeOnEach( pool.byNameBegin( name ),
00081                   pool.byNameEnd( name ),
00082                   functor::chain (resfilter::ByInstalled (),                    // ByInstalled
00083                                   resfilter::ByKind( kind ) ),                  // equal kind
00084                   functor::functorRef<bool,PoolItem> (info) );
00085 
00086     _XDEBUG("Helper::findInstalledByNameAndKind (" << name << ", " << kind << ") => " << info.item);
00087     return info.item;
00088 }
00089 
00090 
00091 // just find uninstalled item with same kind/name as item
00092 
00093 PoolItem_Ref
00094 Helper::findUninstalledByNameAndKind (const ResPool & pool, const string & name, const Resolvable::Kind & kind)
00095 {
00096     LookFor info;
00097 
00098     invokeOnEach( pool.byNameBegin( name ),
00099                   pool.byNameEnd( name ),
00100                   functor::chain (resfilter::ByUninstalled (),                  // ByUninstalled
00101                                   resfilter::ByKind( kind ) ),                  // equal kind
00102                   functor::functorRef<bool,PoolItem> (info) );
00103 
00104     _XDEBUG("Helper::findUninstalledByNameAndKind (" << name << ", " << kind << ") => " << info.item);
00105     return info.item;
00106 }
00107 
00108 
00109 // just find installed item with same kind/name as item
00110 // does *NOT* check edition
00111 
00112 PoolItem_Ref
00113 Helper::findInstalledItem (const ResPool & pool, PoolItem_Ref item)
00114 {
00115     return findInstalledByNameAndKind (pool, item->name(), item->kind() );
00116 }
00117 
00118 //----------------------------------------------------------------------------
00119 
00120 class LookForUpdate : public resfilter::PoolItemFilterFunctor
00121 {
00122   public:
00123     PoolItem_Ref uninstalled;
00124 
00125     bool operator()( PoolItem_Ref provider )
00126     {
00127         if ((!uninstalled                                                       // none yet
00128             || (uninstalled->edition().compare( provider->edition() ) < 0)      // or a better edition
00129             || (uninstalled->arch().compare( provider->arch() ) < 0) )          // or a better architecture
00130             && !provider.status().isLocked())                                   // is not locked
00131         {
00132             uninstalled = provider;                                             // store 
00133         }
00134         return true;
00135     }
00136 };
00137 
00138 
00139 // just find best (according to edition) uninstalled item with same kind/name as item
00140 // *DOES* check edition
00141 
00142 PoolItem_Ref
00143 Helper::findUpdateItem (const ResPool & pool, PoolItem_Ref item)
00144 {
00145     LookForUpdate info;
00146 
00147     invokeOnEach( pool.byNameBegin( item->name() ),
00148                   pool.byNameEnd( item->name() ),
00149                   functor::chain (functor::chain (resfilter::ByUninstalled (),                  // ByUninstalled
00150                                                   resfilter::ByKind( item->kind() ) ),          // equal kind
00151                                   resfilter::byEdition<CompareByGT<Edition> >( item->edition() )),      // only look at better editions
00152                   functor::functorRef<bool,PoolItem> (info) );
00153 
00154     _XDEBUG("Helper::findUpdateItem(" << item << ") => " << info.uninstalled);
00155     return info.uninstalled;
00156 }
00157 
00158 
00159 //----------------------------------------------------------------------------
00160 
00161 class LookForReinstall : public resfilter::PoolItemFilterFunctor
00162 {
00163   public:
00164     PoolItem_Ref uninstalled;
00165 
00166     bool operator()( PoolItem_Ref provider )
00167     {
00168         if (provider.status().isLocked()) {
00169             return true; // search next
00170         } else {
00171             uninstalled = provider;
00172             return false;                               // stop here, we found it
00173         }
00174     }
00175 };
00176 
00177 
00178 PoolItem_Ref
00179 Helper::findReinstallItem (const ResPool & pool, PoolItem_Ref item)
00180 {
00181     LookForReinstall info;
00182 
00183     invokeOnEach( pool.byNameBegin( item->name() ),
00184                   pool.byNameEnd( item->name() ),
00185                   functor::chain (functor::chain (resfilter::ByUninstalled (),                  // ByUninstalled
00186                                                   resfilter::ByKind( item->kind() ) ),          // equal kind
00187                                   resfilter::byEdition<CompareByEQ<Edition> >( item->edition() )),
00188                   functor::functorRef<bool,PoolItem> (info) );
00189 
00190     _XDEBUG("Helper::findReinstallItem(" << item << ") => " << info.uninstalled);
00191     return info.uninstalled;
00192 }
00193 
00195     };// namespace detail
00198   };// namespace solver
00201 };// namespace zypp
00203 

Generated on Thu Jul 6 00:07:22 2006 for zypp by  doxygen 1.4.6