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