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