libmoost
/home/mhx/git/github/libmoost/include/moost/kvds/kvds_pod_type.hpp
Go to the documentation of this file.
00001 /* vim:set ts=3 sw=3 sts=3 et: */
00028 
00029 
00030 #include <vector>
00031 
00032 #include <boost/type_traits.hpp>
00033 #include <boost/static_assert.hpp>
00034 
00035 #include "ikvds.hpp"
00036 
00037 #ifndef MOOST_KVDS_KVDS_POD_TYPE_HPP__
00038 #define MOOST_KVDS_KVDS_POD_TYPE_HPP__
00039 
00040 namespace moost { namespace kvds {
00041 
00055 
00057 
00058    template <typename T>
00059    class KvdsPodType 
00060    {
00061    public:
00062       typedef T kvds_type;
00063 
00065       BOOST_STATIC_ASSERT(boost::is_pod<T>::value);
00066 
00068       KvdsPodType(T & t) : t_(t), size_(sizeof(T)) {}
00069 
00071       KvdsPodType(KvdsPodType const & t) : t_(t.t_), size_(t.size_) {}
00072 
00074       KvdsPodType & operator = (KvdsPodType const & rhs)
00075       {
00076          t_ = rhs.t_;
00077          size_ = rhs.size_;
00078       }
00079 
00081       bool operator == (T const & t) const { return t_ == t; }
00082       bool operator != (T const & t) const { return !(t_ == t); }
00083       bool operator == (KvdsPodType<T> const & t) const { return t_ == t.t_; }
00084       bool operator != (KvdsPodType<T> const & t) const { return !(*this == t); }
00085 
00092 
00094       size_t size() const { return size_; }
00095 
00097       kvds_type const * operator & () const { return &t_; }
00098 
00100       kvds_type & operator * () const { return t_; }
00101 
00112 
00114       size_t & size() { return size_; }
00115 
00117       kvds_type * operator & () { return &t_; }
00118 
00131       kvds_type & operator * () { return t_; }
00132 
00134       class vector_type
00135       {
00136       public:
00139          template <typename U>
00140          struct kvds_vector
00141          {
00143             typedef std::vector<U> type;
00144             private: kvds_vector();
00145          };
00146 
00147          template <typename U>
00148          struct kvds_vector<U const>
00149          {
00151             typedef std::vector<typename boost::remove_const<U>::type> const type;
00152             private: kvds_vector();
00153          };
00154 
00156          typedef typename kvds_vector<kvds_type>::type kvds_vector_type;
00157 
00162          vector_type(kvds_vector_type & kvt) : kvt_(kvt), kvt_size_(kvt.size() * sizeof(kvds_type)) {}
00163 
00169          void resize(size_t const size)
00170          {
00171             if(0 != (size % sizeof(kvds_type)))
00172             {
00173                throw std::runtime_error("size is invalid for kvds_type");
00174             }
00175 
00176             kvt_size_ = size;
00177             kvt_.resize(kvt_size_ / sizeof(kvds_type));
00178          }
00179 
00181          size_t size() const { return kvt_size_; }
00182 
00184          kvds_type * operator & () { return &kvt_[0]; }
00185 
00194          kvds_vector_type & operator * () { return kvt_; }
00195 
00196       private:
00197          kvds_vector_type & kvt_;
00198          size_t kvt_size_;
00199       };
00200 
00201    private:
00202       kvds_type & t_;
00203       size_t size_;
00204    };
00205 
00206 }}
00207 
00208 #endif // MOOST_KVDS_KVDS_POD_TYPE_HPP__