Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r64575 - in sandbox/SOC/2010/bit_masks: boost/integer/detail/bitfield_vector lib/integer/test/bitfield_vector_testing
From: bbartmanboost_at_[hidden]
Date: 2010-08-03 12:42:03


Author: bbartman
Date: 2010-08-03 12:42:02 EDT (Tue, 03 Aug 2010)
New Revision: 64575
URL: http://svn.boost.org/trac/boost/changeset/64575

Log:
fixed my character based masking function's implementation
Text files modified:
   sandbox/SOC/2010/bit_masks/boost/integer/detail/bitfield_vector/bitfield_vector_member_impl.hpp | 153 +++++++++++++++++++++++++++++----------
   sandbox/SOC/2010/bit_masks/lib/integer/test/bitfield_vector_testing/proxy_reference_test.cpp | 120 +++++++++++++++++++++++++++++++
   2 files changed, 234 insertions(+), 39 deletions(-)

Modified: sandbox/SOC/2010/bit_masks/boost/integer/detail/bitfield_vector/bitfield_vector_member_impl.hpp
==============================================================================
--- sandbox/SOC/2010/bit_masks/boost/integer/detail/bitfield_vector/bitfield_vector_member_impl.hpp (original)
+++ sandbox/SOC/2010/bit_masks/boost/integer/detail/bitfield_vector/bitfield_vector_member_impl.hpp 2010-08-03 12:42:02 EDT (Tue, 03 Aug 2010)
@@ -10,6 +10,10 @@
 #include <boost/mpl/arithmetic.hpp>
 #include <boost/mpl/if.hpp>
 #include <boost/mpl/equal.hpp>
+#include <boost/type_traits/is_signed.hpp>
+#include <iostream>
+#include <iomanip>
+#include <cstring>
 
 namespace boost { namespace detail {
 
@@ -68,19 +72,74 @@
     }
 };
 
+
 /** This is the proxy reference type used by the iterator and by the
  * bitfield_tuple for accessing the storage and correctly returning
  * and setting the values within the bitfield_vector.
  */
-template <typename ReturnType,std::size_t Width>
-class proxy_reference_type {
- typedef proxy_reference_type<ReturnType,Width> _self;
+
+template < typename RetType,
+ std::size_t Width,
+ bool = is_signed<RetType>::type::value >
+class proxy_reference_type;
+
+template <typename RetType,std::size_t Width>
+class proxy_reference_type<RetType,Width,true> {
+ typedef proxy_reference_type<RetType,Width,true> _self;
     proxy_reference_type();
 public:
     /** Typedefs and integral static constant values. */
     //@{
     typedef unsigned char storage_type;
- typedef ReturnType value_type;
+ typedef RetType value_type;
+ typedef std::size_t offset_type;
+ BOOST_STATIC_CONSTANT( std::size_t, width = Width );
+ /** constructors and destructor for the proxy_reference_type type. */
+ //@{
+
+ /** Copy Constructor. */
+ proxy_reference_type(_self const& x)
+ :_ptr(x._ptr), _offset(x._offset)
+ { }
+
+ /** pointer, offset constructor. */
+ proxy_reference_type(storage_type* ptr, offset_type offset)
+ :_ptr(ptr), _offset(offset)
+ { }
+ //@}
+
+ /** Copy assignment. */
+ _self& operator=(_self const& x) {
+ _ptr = x._ptr;
+ _offset = x._offset;
+ return *this;
+ }
+
+ operator value_type() const;
+ /** value_type storage assignement operator.*/
+ _self& operator=(value_type x);
+
+ bool operator==(_self const& rhs);
+ bool operator!=(_self const& rhs);
+ bool operator<(_self const& rhs);
+
+
+ // private:
+ /** Member variables. */
+ storage_type* _ptr;
+ offset_type _offset;
+};
+
+
+template <typename RetType, std::size_t Width>
+class proxy_reference_type<RetType,Width,false> {
+ typedef proxy_reference_type<RetType,Width,false> _self;
+ proxy_reference_type();
+public:
+ /** Typedefs and integral static constant values. */
+ //@{
+ typedef unsigned char storage_type;
+ typedef RetType value_type;
     typedef std::size_t offset_type;
     BOOST_STATIC_CONSTANT( std::size_t, width = Width );
     //@}
@@ -108,53 +167,69 @@
 
     /** Implicit Conversion Operator*/
     operator value_type() const {
- value_type ret;
- // std::size_t bit_index = 0;
+ value_type ret = 0;
         storage_type* byte_ptr = _ptr;
- std::size_t remaining_bits = width;
+ // std::size_t remaining_bits = width;
+ storage_type mask = 0;
 
- // creating head mask.
- storage_type mask = ~(~storage_type(0) << (8 - _offset));
- // keep track of how many bits from the width have been extraced.
- remaining_bits -= (8 - _offset);
- // update return type.
- ret = value_type(mask & *byte_ptr) << (remaining_bits);
- // make sure to return if we are finished.
- if(remaining_bits == 0) {
- return ret;
+ // constructing mask inside of char array.
+ // TODO: Make this correctly deduced! as of right it is the largest
+ // possible size.
+ storage_type mask_array[9];
+ std::memset(mask_array, 0,9);
+
+
+ // this is so that the loop will work correctly the first time through.
+ storage_type* mask_ptr = mask_array;
+ --mask_ptr;
+ mask = 0x80;
+ mask >>= _offset;
+ std::size_t bit_copy_ammount = _offset + width;
+ std::size_t trailing_zeros = 8 - _offset;
+ std::size_t mask_byte_count = 0;
+ for(std::size_t bit_index = _offset;bit_index <= bit_copy_ammount; ++bit_index){
+ if( (bit_index%8) == 0) {
+ ++mask_byte_count;
+ trailing_zeros = 8;
+ mask >>= 1;
+ *mask_ptr |= mask;
+ ++mask_ptr;
+ mask = 0x80;
+ continue;
+ }
+ --trailing_zeros;
+ *mask_ptr |= mask;
+ mask >>= 1;
         }
- // next loop while the
- ++byte_ptr;
- mask = ~storage_type(0);
- while((remaining_bits / 8) > 0) {
- ret |= value_type(mask & *byte_ptr) << (remaining_bits);
-
- // increment values so that everything is
- // correctly retrieved.
- remaining_bits -= 8;
+
+ // mask_ptr = mask_array;
+ // storage_type* mask_array_end = (mask_array) + 9;
+
+ for( std::size_t mask_index = 0;
+ mask_index < mask_byte_count;
+ ++mask_index)
+ {
+ ret <<= 8;
+ ret += *byte_ptr & mask_array[mask_index];
             ++byte_ptr;
- }
- // because the field could have ended on a byte boundry then
- // I must then check before exiting.
- if(remaining_bits == 0) {
- return ret;
- }
- // if I reached this point it means that I must now deal with the
- // trailing bits of the field.
- mask = ~(~storage_type(0) >> remaining_bits);
- return ret | (value_type(mask & *byte_ptr)<<(remaining_bits));
+ }
+ return ret >> trailing_zeros;
     }
 
     /** value_type storage assignement operator.*/
- _self& operator=(value_type x);
+ _self& operator=(value_type x) {
+
+ }
 
     bool operator==(_self const& rhs);
     bool operator!=(_self const& rhs);
     bool operator<(_self const& rhs);
-private:
+
+
+// private:
     /** Member variables. */
- storage_type _ptr;
- offset_type _offset;
+ storage_type* _ptr;
+ offset_type _offset;
 };
 
 }} // end boost::detail

Modified: sandbox/SOC/2010/bit_masks/lib/integer/test/bitfield_vector_testing/proxy_reference_test.cpp
==============================================================================
--- sandbox/SOC/2010/bit_masks/lib/integer/test/bitfield_vector_testing/proxy_reference_test.cpp (original)
+++ sandbox/SOC/2010/bit_masks/lib/integer/test/bitfield_vector_testing/proxy_reference_test.cpp 2010-08-03 12:42:02 EDT (Tue, 03 Aug 2010)
@@ -6,11 +6,131 @@
 
 #include <boost/integer/bitfield_vector.hpp>
 #include <boost/detail/lightweight_test.hpp>
+#include <iostream>
+#include <iomanip>
+
+using namespace boost::detail;
 
 /** Proxy Reference Type.
  * Suite for proxy reference type.
  */
+
+// unsigned type testing.
+typedef proxy_reference_type<unsigned int, 3> test_type_1;
+typedef proxy_reference_type<unsigned char, 9> test_type_2;
+typedef proxy_reference_type<unsigned long, 7> test_type_3;
+typedef proxy_reference_type<unsigned long long, 17> test_type_4;
+typedef proxy_reference_type<unsigned long long, 50> test_type_5;
+
+// signed type testing.
+typedef proxy_reference_type<int, 3> test_type_6;
+typedef proxy_reference_type<char, 9> test_type_7;
+typedef proxy_reference_type<long, 7> test_type_8;
+typedef proxy_reference_type<long long, 17> test_type_9;
+typedef proxy_reference_type<long long, 50> test_type_10;
+
+
+/*
+test_type_1;
+test_type_2;
+test_type_3;
+test_type_4;
+test_type_5;
+test_type_6;
+test_type_7;
+test_type_8;
+test_type_9;
+test_type_10;
+*/
+
 int main() {
+ // Constructor tests.
+ {
+ unsigned char temp[20];
+#define TEST_MACRO_PTR_OFFSET_CTOR(NUMBER) \
+ test_type_##NUMBER t##NUMBER(temp,2); \
+ BOOST_TEST(t##NUMBER._offset == 2); \
+ BOOST_TEST(t##NUMBER._ptr == temp);
+
+ TEST_MACRO_PTR_OFFSET_CTOR(1);
+ TEST_MACRO_PTR_OFFSET_CTOR(2);
+ TEST_MACRO_PTR_OFFSET_CTOR(3);
+ TEST_MACRO_PTR_OFFSET_CTOR(4);
+ TEST_MACRO_PTR_OFFSET_CTOR(5);
+ TEST_MACRO_PTR_OFFSET_CTOR(6);
+ TEST_MACRO_PTR_OFFSET_CTOR(7);
+ TEST_MACRO_PTR_OFFSET_CTOR(8);
+ TEST_MACRO_PTR_OFFSET_CTOR(9);
+ TEST_MACRO_PTR_OFFSET_CTOR(10);
+
+#undef TEST_MACRO_PTR_OFFSET_CTOR
+ }
+ // testing copy constructor.
+ {
+ unsigned char temp[20];
+#define TEST_MACRO_COPY_CTOR(NUMBER) \
+ test_type_##NUMBER t##NUMBER(temp,2); \
+ test_type_##NUMBER u##NUMBER(t##NUMBER); \
+ BOOST_TEST(u##NUMBER._offset == 2); \
+ BOOST_TEST(u##NUMBER._ptr == temp);
+
+ TEST_MACRO_COPY_CTOR(1);
+ TEST_MACRO_COPY_CTOR(2);
+ TEST_MACRO_COPY_CTOR(3);
+ TEST_MACRO_COPY_CTOR(4);
+ TEST_MACRO_COPY_CTOR(5);
+ TEST_MACRO_COPY_CTOR(6);
+ TEST_MACRO_COPY_CTOR(7);
+ TEST_MACRO_COPY_CTOR(8);
+ TEST_MACRO_COPY_CTOR(9);
+ TEST_MACRO_COPY_CTOR(10);
+
+#undef TEST_MACRO_COPY_CTOR
+ }
+
+ // copy assignment operator.
+ {
+ unsigned char temp[20];
+ unsigned char temp2[30];
+#define TEST_MACRO_COPY_ASSIGNMENT_OPERATOR(NUMBER) \
+ test_type_##NUMBER t##NUMBER(temp,2); \
+ test_type_##NUMBER u##NUMBER(temp2,4); \
+ BOOST_TEST(u##NUMBER._offset == 4); \
+ BOOST_TEST(u##NUMBER._ptr == temp2); \
+ u##NUMBER = t##NUMBER; \
+ BOOST_TEST(u##NUMBER._offset == 2); \
+ BOOST_TEST(u##NUMBER._ptr == temp);
+
+ TEST_MACRO_COPY_ASSIGNMENT_OPERATOR(1);
+ TEST_MACRO_COPY_ASSIGNMENT_OPERATOR(2);
+ TEST_MACRO_COPY_ASSIGNMENT_OPERATOR(3);
+ TEST_MACRO_COPY_ASSIGNMENT_OPERATOR(4);
+ TEST_MACRO_COPY_ASSIGNMENT_OPERATOR(5);
+ TEST_MACRO_COPY_ASSIGNMENT_OPERATOR(6);
+ TEST_MACRO_COPY_ASSIGNMENT_OPERATOR(7);
+ TEST_MACRO_COPY_ASSIGNMENT_OPERATOR(8);
+ TEST_MACRO_COPY_ASSIGNMENT_OPERATOR(9);
+ TEST_MACRO_COPY_ASSIGNMENT_OPERATOR(10);
+
+#undef TEST_MACRO_COPY_ASSIGNMENT_OPERATOR
+ }
+ // encoding and decoding tests.
+ {
+ typedef unsigned char storage_type;
+ typedef storage_type* storage_ptr;
+ storage_type storage[20];
+ storage_ptr ptr = storage;
+ *ptr = storage_type(0x7) << 5;
+ std::cout << "Test type 1. First byte value in hex: " << std::endl;
+ std::cout << std::hex << std::size_t(*ptr) << std::endl;
+
+ // this will be access to the first element within the array.
+ test_type_1 t1(ptr,0);
+ test_type_1::value_type x = t1;
+ std::cout << "Value returned by t1: "<< std::hex << x << std::endl;
+ BOOST_TEST(x == 0x7);
+ test_type_1 t1(ptr,0);
+ }
     return boost::report_errors();
 }
 


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk