OSDN Git Service

bits.hpp: bit_ptr 実装中 (_bitの内部クラス追加)
authorMyun2 <myun2@nwhite.info>
Wed, 8 Sep 2010 18:45:42 +0000 (03:45 +0900)
committerMyun2 <myun2@nwhite.info>
Wed, 8 Sep 2010 18:47:12 +0000 (03:47 +0900)
roast/include/roast/memory/bits.hpp

index 53456a8..072fa55 100644 (file)
@@ -18,6 +18,26 @@ namespace roast
                T* m_p;
                size_t m_index;
        public:
+               class _bit
+               {
+               protected:
+                       bit_ptr& parent;
+               public:
+                       _bit(bit_ptr& in) : parent(in) {}
+                       
+                       _bit operator = (bool b){ parent.set(b); return *this; }
+                       operator bool(){ return parent.get(); }
+                       operator char(){ return parent.get(); }
+                       operator unsigned char(){ return parent.get(); }
+                       operator short(){ return parent.get(); }
+                       operator unsigned short(){ return parent.get(); }
+                       operator int(){ return parent.get(); }
+                       operator unsigned int(){ return parent.get(); }
+                       operator long(){ return parent.get(); }
+                       operator unsigned long(){ return parent.get(); }
+               };
+       
+       public:
                bit_ptr(T* p, size_t index=0) : m_p(p), m_index(index) {}
        
                bit_ptr& operator ++(int){ m_index++; return *this; }
@@ -28,6 +48,21 @@ namespace roast
                bit_ptr& operator -=(int n){ m_index-=n; return *this; }
                bit_ptr& operator +(int n){ bit_ptr work=*this; work+=n; return work; }
                bit_ptr& operator -(int n){ bit_ptr work=*this; work-=n; return work; }
+               
+               _bit operator *(){ return _bit(*this); }
+               
+               void set(bool b)
+               {
+                       size_t ary_index = m_index / TypeBits;
+                       size_t bit_index = m_index % TypeBits;
+                       m_p[ary_index] |= (b << bit_index);
+               }
+               bool get()
+               {
+                       size_t ary_index = m_index / TypeBits;
+                       size_t bit_index = m_index % TypeBits;
+                       return m_p[ary_index] & (1 << bit_index);
+               }
        };
 
        //template <unsigned int _BitCount, typename _BaseType = unsigned int>