00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef QOLYESTER_UTL_STAMPABLE_HH
00020 # define QOLYESTER_UTL_STAMPABLE_HH 1
00021
00022 # include <map>
00023 # include "comparator.hh"
00024 # include "timeval.hh"
00025
00026 namespace olsr {
00027
00028 namespace utl {
00029
00030 class Stampable {
00031 protected:
00032 inline Stampable();
00033 public:
00034 void set_stamp(const TimeVal& now = TimeVal::now()) {
00035 _stamp = now;
00036 }
00037 const TimeVal stamp() const { return _stamp; }
00038 inline bool expired(const TimeVal& period,
00039 const TimeVal& now = TimeVal::now()) const;
00040
00041 bool operator<(const Stampable& rhs) const {
00042 return _stamp < rhs._stamp;
00043 }
00044 private:
00045 TimeVal _stamp;
00046 };
00047
00048 template <unsigned Dim, class Key, class Compare = pless<Key> >
00049 class MultiStampable {
00050 typedef MultiStampable<Dim, Key, Compare> This;
00051 typedef std::map<Key, TimeVal> stampset_t;
00052 protected:
00053 inline MultiStampable();
00054 public:
00055 inline void set_stamp(unsigned d, const Key& k,
00056 const TimeVal& now = TimeVal::now());
00057 inline void remove_stamp(unsigned d, const Key& k);
00058 inline const TimeVal stamp(unsigned d, const Key& k) const;
00059 inline bool expired(unsigned d,
00060 const Key& k,
00061 const TimeVal& period,
00062 const TimeVal& now = TimeVal::now()) const;
00063 inline bool less(unsigned d, const Key& k, const This& rhs) const;
00064 private:
00065 stampset_t _stampset[Dim];
00066 };
00067
00068 template <class Key, class Compare>
00069 class MultiStampable<0, Key, Compare>;
00070
00071 template <class Key, class Compare>
00072 class MultiStampable<1, Key, Compare> {
00073 typedef MultiStampable<1, Key, Compare> This;
00074 typedef std::map<Key, TimeVal> stampset_t;
00075 protected:
00076 inline MultiStampable();
00077 public:
00078 inline void set_stamp(const Key& k,
00079 const TimeVal& now = TimeVal::now());
00080 void set_stamp(unsigned d, const Key& k,
00081 const TimeVal& now = TimeVal::now()) {
00082 assert(d == 0);
00083 (void) d;
00084 return set_stamp(k, now);
00085 }
00086
00087 inline void remove_stamp(const Key& k);
00088 void remove_stamp(unsigned d, const Key& k) {
00089 assert(d == 0);
00090 (void) d;
00091 return remove_stamp(k);
00092 }
00093 inline const TimeVal stamp(const Key& k) const;
00094 const TimeVal stamp(unsigned d, const Key& k) const {
00095 assert(d == 0);
00096 (void) d;
00097 return stamp(k);
00098 }
00099 inline bool expired(const Key& k,
00100 const TimeVal& period,
00101 const TimeVal& now = TimeVal::now()) const;
00102 bool expired(unsigned d, const Key& k,
00103 const TimeVal& period,
00104 const TimeVal& now = TimeVal::now()) const {
00105 assert(d == 0);
00106 (void) d;
00107 return expired(k, period, now);
00108 }
00109 inline bool less(const Key& k, const This& rhs) const;
00110 bool less(unsigned d, const Key& k, const This& rhs) const {
00111 assert(d == 0);
00112 (void) d;
00113 return less(k, rhs);
00114 }
00115 private:
00116 stampset_t _stampset;
00117 };
00118
00119 template <class Key, class Iter>
00120 struct imultistampable_less {
00121 private:
00122 typedef imultistampable_less<Key, Iter> This;
00123 public:
00124 imultistampable_less(unsigned d, const Key& k)
00125 : _d(d),
00126 _k(k)
00127 {}
00128
00129 bool operator()(const Iter& a, const Iter& b) const {
00130 return a->less(_d, _k, *b);
00131 }
00132 private:
00133 const unsigned _d;
00134 const Key _k;
00135 };
00136
00137 template <unsigned Dim, class Key, class Compare = pless<Key> >
00138 struct multistampable_less {
00139 private:
00140 typedef multistampable_less<Dim, Key, Compare> This;
00141 public:
00142 multistampable_less(unsigned d, const Key& k)
00143 : _d(d),
00144 _k(k)
00145 {}
00146
00147 bool operator()(const MultiStampable<Dim, Key, Compare>& a,
00148 const MultiStampable<Dim, Key, Compare>& b) const {
00149 return a.less(_d, _k, b);
00150 }
00151
00152 bool operator()(const MultiStampable<Dim, Key, Compare>* a,
00153 const MultiStampable<Dim, Key, Compare>* b) const {
00154 return a->less(_d, _k, *b);
00155 }
00156 private:
00157 const unsigned _d;
00158 const Key _k;
00159 };
00160
00161 template <class Key, class Compare>
00162 struct multistampable_less<0, Key, Compare> {};
00163
00164 template <class Key, class Compare>
00165 struct multistampable_less<1, Key, Compare> {
00166 private:
00167 typedef multistampable_less<1, Key, Compare> This;
00168 public:
00169 multistampable_less(const Key& k)
00170 : _k(k)
00171 {}
00172
00173 bool operator()(const MultiStampable<1, Key, Compare>& a,
00174 const MultiStampable<1, Key, Compare>& b) const {
00175 return a.less(_k, b);
00176 }
00177
00178 bool operator()(const MultiStampable<1, Key, Compare>* a,
00179 const MultiStampable<1, Key, Compare>* b) const {
00180 return a->less(_k, *b);
00181 }
00182 private:
00183 const Key _k;
00184 };
00185
00186 }
00187
00188 }
00189
00190 # include "stampable.hxx"
00191
00192 #endif // ! QOLYESTER_UTL_STAMPABLE_HH