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