libmoost
|
00001 /* vim:set ts=3 sw=3 sts=3 et: */ 00028 00029 #include <cassert> 00030 #include <stdexcept> 00031 #include <vector> 00032 00033 #include <boost/shared_ptr.hpp> 00034 #include <boost/type_traits.hpp> 00035 #include <boost/static_assert.hpp> 00036 00037 #ifndef MOOST_KVDS_KVDS_ITERATOR_HPP__ 00038 #define MOOST_KVDS_KVDS_ITERATOR_HPP__ 00039 00040 namespace moost { namespace kvds { 00041 00057 00060 00062 00063 template <typename kvdsT> 00064 class KvdsKeyIterator 00065 { 00066 public: 00067 typedef typename kvdsT::key_type key_type; 00068 00069 private: 00070 typedef typename kvdsT::kvds_key_t kvds_key_t; 00071 00072 void get_next_key() 00073 { 00074 assert(ikvds_ptr_); 00075 00076 if(ikvds_ptr_) 00077 { 00078 if(ikvds_ptr_->nxt(&key_, key_.size())) 00079 { 00080 *key_; 00081 ++pos_; 00082 } 00083 else 00084 { 00085 if(key_.size() != 0) 00086 { 00087 throw std::runtime_error("unexpected key size"); 00088 } 00089 00090 ikvds_ptr_.reset(); 00091 } 00092 } 00093 } 00094 00095 void init() 00096 { 00097 assert(ikvds_ptr_); 00098 00099 if(!ikvds_ptr_ || !ikvds_ptr_->beg()) 00100 { 00101 throw std::runtime_error("failed to initialise iterator"); 00102 } 00103 00104 get_next_key(); 00105 } 00106 00107 public: 00108 KvdsKeyIterator() : key__(0), key_(key__), pos_(0) {} 00109 00110 KvdsKeyIterator(kvdsT const & kvds) : 00111 key__(0), key_(key__), ikvds_ptr_(kvds.get_ikvds_ptr()) 00112 { 00113 init(); 00114 } 00115 00116 KvdsKeyIterator(ikvds_ptr_t ikvds_ptr) : 00117 key__(0), key_(key__), pos_(0), ikvds_ptr_(ikvds_ptr) 00118 { 00119 init(); 00120 } 00121 00122 KvdsKeyIterator(KvdsKeyIterator const & rhs) : 00123 key__(rhs.key__), key_(key__), pos_(rhs.pos_), ikvds_ptr_(rhs.ikvds_ptr_) 00124 { 00125 } 00126 00127 KvdsKeyIterator & operator = (KvdsKeyIterator const & rhs) 00128 { 00129 if(this != &rhs) 00130 { 00131 ikvds_ptr_ = rhs.ikvds_ptr_; 00132 key__ = rhs.key__; 00133 pos_ = rhs.pos_; 00134 } 00135 00136 return *this; 00137 } 00138 00139 KvdsKeyIterator & operator ++ () // prefix 00140 { 00141 if(!ikvds_ptr_) 00142 { 00143 assert(false); 00144 throw std::runtime_error("invalid iterator"); 00145 } 00146 00147 get_next_key(); 00148 00149 return *this; 00150 } 00151 00152 KvdsKeyIterator operator ++ (int) // postfix 00153 { 00154 KvdsKeyIterator tmp = *this; 00155 00156 if(!ikvds_ptr_) 00157 { 00158 assert(false); 00159 throw std::runtime_error("invalid iterator"); 00160 } 00161 00162 get_next_key(); 00163 00164 return tmp; 00165 } 00166 00167 bool operator == (KvdsKeyIterator const & rhs) const 00168 { 00169 if(!ikvds_ptr_ && !rhs.ikvds_ptr_) { return true; } 00170 if(ikvds_ptr_ && rhs.ikvds_ptr_) { return pos_ == rhs.pos_; } 00171 return false; 00172 } 00173 00174 bool operator != (KvdsKeyIterator const & rhs) const 00175 { 00176 return !(*this == rhs); 00177 } 00178 00179 key_type const & operator * () const 00180 { 00181 if(!ikvds_ptr_) 00182 { 00183 assert(false); 00184 throw std::runtime_error("invalid iterator"); 00185 } 00186 00187 return *key_; 00188 } 00189 00190 private: 00191 key_type key__; 00192 kvds_key_t key_; 00193 size_t pos_; 00194 ikvds_ptr_t ikvds_ptr_; 00195 }; 00196 00197 00198 }} 00199 00200 #endif // MOOST_KVDS_KVDS_ITERATOR_HPP__