transform_width.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #ifndef BOOST_ARCHIVE_ITERATORS_TRANSFORM_WIDTH_HPP
  2. #define BOOST_ARCHIVE_ITERATORS_TRANSFORM_WIDTH_HPP
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER) && (_MSC_VER >= 1020)
  5. # pragma once
  6. #endif
  7. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  8. // transform_width.hpp
  9. // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
  10. // Use, modification and distribution is subject to the Boost Software
  11. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  12. // http://www.boost.org/LICENSE_1_0.txt)
  13. // See http://www.boost.org for updates, documentation, and revision history.
  14. // iterator which takes elements of x bits and returns elements of y bits.
  15. // used to change streams of 8 bit characters into streams of 6 bit characters.
  16. // and vice-versa for implementing base64 encodeing/decoding. Be very careful
  17. // when using and end iterator. end is only reliable detected when the input
  18. // stream length is some common multiple of x and y. E.G. Base64 6 bit
  19. // character and 8 bit bytes. Lowest common multiple is 24 => 4 6 bit characters
  20. // or 3 8 bit characters
  21. #include <boost/config.hpp> // for BOOST_DEDUCED_TYPENAME & PTFO
  22. #include <boost/serialization/pfto.hpp>
  23. #include <boost/iterator/iterator_adaptor.hpp>
  24. #include <boost/iterator/iterator_traits.hpp>
  25. namespace boost {
  26. namespace archive {
  27. namespace iterators {
  28. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  29. // class used by text archives to translate char strings to wchar_t
  30. // strings of the currently selected locale
  31. template<
  32. class Base,
  33. int BitsOut,
  34. int BitsIn,
  35. class CharType = BOOST_DEDUCED_TYPENAME boost::iterator_value<Base>::type // output character
  36. >
  37. class transform_width :
  38. public boost::iterator_adaptor<
  39. transform_width<Base, BitsOut, BitsIn, CharType>,
  40. Base,
  41. CharType,
  42. single_pass_traversal_tag,
  43. CharType
  44. >
  45. {
  46. friend class boost::iterator_core_access;
  47. typedef BOOST_DEDUCED_TYPENAME boost::iterator_adaptor<
  48. transform_width<Base, BitsOut, BitsIn, CharType>,
  49. Base,
  50. CharType,
  51. single_pass_traversal_tag,
  52. CharType
  53. > super_t;
  54. typedef transform_width<Base, BitsOut, BitsIn, CharType> this_t;
  55. typedef BOOST_DEDUCED_TYPENAME iterator_value<Base>::type base_value_type;
  56. void fill();
  57. CharType dereference() const {
  58. if(!m_buffer_out_full)
  59. const_cast<this_t *>(this)->fill();
  60. return m_buffer_out;
  61. }
  62. bool equal_impl(const this_t & rhs){
  63. if(BitsIn < BitsOut) // discard any left over bits
  64. return this->base_reference() == rhs.base_reference();
  65. else{
  66. // BitsIn > BitsOut // zero fill
  67. if(this->base_reference() == rhs.base_reference()){
  68. m_end_of_sequence = true;
  69. return 0 == m_remaining_bits;
  70. }
  71. return false;
  72. }
  73. }
  74. // standard iterator interface
  75. bool equal(const this_t & rhs) const {
  76. return const_cast<this_t *>(this)->equal_impl(rhs);
  77. }
  78. void increment(){
  79. m_buffer_out_full = false;
  80. }
  81. bool m_buffer_out_full;
  82. CharType m_buffer_out;
  83. // last read element from input
  84. base_value_type m_buffer_in;
  85. // number of bits to left in the input buffer.
  86. unsigned int m_remaining_bits;
  87. // flag to indicate we've reached end of data.
  88. bool m_end_of_sequence;
  89. public:
  90. // make composible buy using templated constructor
  91. template<class T>
  92. transform_width(BOOST_PFTO_WRAPPER(T) start) :
  93. super_t(Base(BOOST_MAKE_PFTO_WRAPPER(static_cast< T >(start)))),
  94. m_buffer_out_full(false),
  95. m_remaining_bits(0),
  96. m_end_of_sequence(false)
  97. {}
  98. // intel 7.1 doesn't like default copy constructor
  99. transform_width(const transform_width & rhs) :
  100. super_t(rhs.base_reference()),
  101. m_buffer_out_full(rhs.m_buffer_out_full),
  102. m_remaining_bits(rhs.m_remaining_bits),
  103. m_buffer_in(rhs.m_buffer_in),
  104. m_end_of_sequence(false)
  105. {}
  106. };
  107. template<
  108. class Base,
  109. int BitsOut,
  110. int BitsIn,
  111. class CharType
  112. >
  113. void transform_width<Base, BitsOut, BitsIn, CharType>::fill() {
  114. unsigned int missing_bits = BitsOut;
  115. m_buffer_out = 0;
  116. do{
  117. if(0 == m_remaining_bits){
  118. if(m_end_of_sequence){
  119. m_buffer_in = 0;
  120. m_remaining_bits = missing_bits;
  121. }
  122. else{
  123. m_buffer_in = * this->base_reference()++;
  124. m_remaining_bits = BitsIn;
  125. }
  126. }
  127. // append these bits to the next output
  128. // up to the size of the output
  129. unsigned int i = std::min(missing_bits, m_remaining_bits);
  130. // shift interesting bits to least significant position
  131. base_value_type j = m_buffer_in >> (m_remaining_bits - i);
  132. // and mask off the un interesting higher bits
  133. // note presumption of twos complement notation
  134. j &= (1 << i) - 1;
  135. // append then interesting bits to the output value
  136. m_buffer_out <<= i;
  137. m_buffer_out |= j;
  138. // and update counters
  139. missing_bits -= i;
  140. m_remaining_bits -= i;
  141. }while(0 < missing_bits);
  142. m_buffer_out_full = true;
  143. }
  144. } // namespace iterators
  145. } // namespace archive
  146. } // namespace boost
  147. #endif // BOOST_ARCHIVE_ITERATORS_TRANSFORM_WIDTH_HPP