00001 #ifndef STORAGE_TMPL_H
00002 #define STORAGE_TMPL_H
00003
00004 #include <functional>
00005 #include <ostream>
00006 #include <sstream>
00007 #include <list>
00008 #include <map>
00009 #include <deque>
00010
00011 #include "y2storage/IterPair.h"
00012 #include "y2storage/FilterIterator.h"
00013 #include "y2storage/DerefIterator.h"
00014 #include "y2storage/AppUtil.h"
00015
00016 template< class Value >
00017 class CheckFnc
00018 {
00019 public:
00020 CheckFnc( bool (* ChkFnc)( Value& )=NULL ) : m_check(ChkFnc) {}
00021 bool operator()(Value& d) const
00022 { return(m_check==NULL || (*m_check)(d)); }
00023 private:
00024 bool (* m_check)( Value& d );
00025 };
00026
00027 template< class Pred, class Iter >
00028 class ContainerIter : public FilterIterator< Pred, Iter >
00029 {
00030 typedef FilterIterator< Pred, Iter > _bclass;
00031 public:
00032 ContainerIter() : _bclass() {}
00033 ContainerIter( const Iter& b, const Iter& e, const Pred& p,
00034 bool atend=false ) :
00035 _bclass(b, e, p, atend ) {}
00036 ContainerIter( const IterPair<Iter>& pair, const Pred& p,
00037 bool atend=false ) :
00038 _bclass(pair, p, atend ) {}
00039 ContainerIter( const ContainerIter& i) { *this=i;}
00040 };
00041
00042 template< class Pred, class Iter, class Value >
00043 class ContainerDerIter : public DerefIterator<Iter,Value>
00044 {
00045 typedef DerefIterator<Iter,Value> _bclass;
00046 public:
00047 ContainerDerIter() : _bclass() {}
00048 ContainerDerIter( const _bclass& i ) : _bclass(i) {}
00049 ContainerDerIter( const ContainerDerIter& i) { *this=i;}
00050 };
00051
00052 template< class Iter, class CastResult >
00053 class CastIterator : public Iter
00054 {
00055 public:
00056 typedef CastResult value_type;
00057 typedef CastResult& reference;
00058 typedef CastResult* pointer;
00059
00060 CastIterator() : Iter() {}
00061 CastIterator( const Iter& i ) : Iter( i ) {}
00062 CastIterator( const CastIterator& i ) { *this=i; }
00063 CastResult operator*() const
00064 {
00065 return( static_cast<CastResult>(Iter::operator*()) );
00066 }
00067 CastResult* operator->() const
00068 {
00069 return( static_cast<CastResult*>(Iter::operator->()) );
00070 }
00071 CastIterator& operator++()
00072 {
00073 Iter::operator++(); return(*this);
00074 }
00075 CastIterator operator++(int)
00076 {
00077 y2warning( "Expensive ++ CastIterator" );
00078 CastIterator tmp(*this);
00079 Iter::operator++();
00080 return(tmp);
00081 }
00082 CastIterator& operator--()
00083 {
00084 Iter::operator--(); return(*this);
00085 }
00086 CastIterator operator--(int)
00087 {
00088 y2warning( "Expensive -- CastIterator" );
00089 CastIterator tmp(*this);
00090 Iter::operator--();
00091 return(tmp);
00092 }
00093 };
00094
00095 template < class Checker, class ContIter, class Iter, class Value >
00096 class CheckerIterator : public Checker, public ContIter
00097 {
00098 public:
00099 CheckerIterator() {};
00100 CheckerIterator( const Iter& b, const Iter& e,
00101 bool (* CheckFnc)( const Value& )=NULL,
00102 bool atend=false ) :
00103 Checker( CheckFnc ),
00104 ContIter( b, e, *this, atend ) {}
00105 CheckerIterator( const IterPair<Iter>& p,
00106 bool (* CheckFnc)( const Value& )=NULL,
00107 bool atend=false ) :
00108 Checker( CheckFnc ),
00109 ContIter( p, *this, atend ) {}
00110 CheckerIterator( const CheckerIterator& i ) { *this=i; }
00111 };
00112
00113 template < class C >
00114 void pointerIntoSortedList( std::list<C*>& l, C* e )
00115 {
00116 typename std::list<C*>::iterator i = l.begin();
00117 while( i!=l.end() && **i < *e )
00118 i++;
00119 l.insert( i, e );
00120 }
00121
00122
00123 template<class Num> string decString(Num number)
00124 {
00125 std::ostringstream num_str;
00126 num_str << number;
00127 return( num_str.str() );
00128 }
00129
00130 template<class Num> string hexString(Num number)
00131 {
00132 std::ostringstream num_str;
00133 num_str << std::hex << number;
00134 return( num_str.str() );
00135 }
00136
00137 template<class Value> void operator>>( const string& d, Value& v)
00138 {
00139 std::istringstream Data( d );
00140 Data >> v;
00141 }
00142
00143 template<class Value> std::ostream& operator<<( std::ostream& s, const std::list<Value>& l )
00144 {
00145 s << "<";
00146 for( typename std::list<Value>::const_iterator i=l.begin(); i!=l.end(); i++ )
00147 {
00148 if( i!=l.begin() )
00149 s << " ";
00150 s << *i;
00151 }
00152 s << ">";
00153 return( s );
00154 }
00155
00156 template<class Value> std::ostream& operator<<( std::ostream& s, const std::deque<Value>& l )
00157 {
00158 s << "<";
00159 for( typename std::deque<Value>::const_iterator i=l.begin(); i!=l.end(); i++ )
00160 {
00161 if( i!=l.begin() )
00162 s << " ";
00163 s << *i;
00164 }
00165 s << ">";
00166 return( s );
00167 }
00168
00169 template<class F, class S> std::ostream& operator<<( std::ostream& s, const std::pair<F,S>& p )
00170 {
00171 s << "[" << p.first << ":" << p.second << "]";
00172 return( s );
00173 }
00174
00175 template<class Key, class Value> std::ostream& operator<<( std::ostream& s, const std::map<Key,Value>& m )
00176 {
00177 s << "<";
00178 for( typename std::map<Key,Value>::const_iterator i=m.begin(); i!=m.end(); i++ )
00179 {
00180 if( i!=m.begin() )
00181 s << " ";
00182 s << i->first << ":" << i->second;
00183 }
00184 s << ">";
00185 return( s );
00186 }
00187
00188 template< class Val >
00189 struct cont_less : public std::binary_function<Val*,Val*,bool>
00190 {
00191 bool operator()(const Val* __x, const Val* __y) const { return *__x < *__y; }
00192 };
00193
00194 template <class T, unsigned int sz>
00195 inline unsigned int lengthof (T (&)[sz]) { return sz; }
00196
00197 #endif