Developer Documentation
SR_binary_vector_of_bool.inl
1 
2 template <> struct binary< std::vector<bool> >
3 {
4 
5  typedef std::vector< bool > value_type;
6  typedef value_type::value_type elem_type;
7 
8  static const bool is_streamable = true;
9 
10  static size_t size_of(void) { return UnknownSize; }
11  static size_t size_of(const value_type& _v)
12  {
13  return _v.size() / 8 + ((_v.size() % 8)!=0);
14  }
15 
16  static
17  size_t store( std::ostream& _ostr, const value_type& _v, bool )
18  {
19  size_t bytes = 0;
20 
21  size_t N = _v.size() / 8;
22  size_t R = _v.size() % 8;
23 
24  size_t idx; // element index
25  unsigned char bits; // bitset
26 
27  for (idx=0; idx < N; ++idx)
28  {
29  bits = static_cast<unsigned char>(_v[idx])
30  | (static_cast<unsigned char>(_v[idx+1]) << 1)
31  | (static_cast<unsigned char>(_v[idx+2]) << 2)
32  | (static_cast<unsigned char>(_v[idx+3]) << 3)
33  | (static_cast<unsigned char>(_v[idx+4]) << 4)
34  | (static_cast<unsigned char>(_v[idx+5]) << 5)
35  | (static_cast<unsigned char>(_v[idx+6]) << 6)
36  | (static_cast<unsigned char>(_v[idx+7]) << 7);
37  _ostr << bits;
38  }
39  bytes = N;
40 
41  if (R)
42  {
43  bits = 0;
44  switch(R)
45  {
46  case 7: bits |= (static_cast<unsigned char>(_v[idx+6]) << 6);
47  case 6: bits |= (static_cast<unsigned char>(_v[idx+5]) << 5);
48  case 5: bits |= (static_cast<unsigned char>(_v[idx+4]) << 4);
49  case 4: bits |= (static_cast<unsigned char>(_v[idx+3]) << 3);
50  case 3: bits |= (static_cast<unsigned char>(_v[idx+2]) << 2);
51  case 2: bits |= (static_cast<unsigned char>(_v[idx+1]) << 1);
52  case 1: bits |= static_cast<unsigned char>(_v[idx+0]);
53  }
54  _ostr << bits;
55  ++bytes;
56  }
57 
58  assert( bytes == size_of(_v) );
59 
60  return bytes;
61  }
62 
63  static
64  size_t restore( std::istream& _istr, value_type& _v, bool )
65  {
66  size_t bytes = 0;
67 
68  size_t N = _v.size() / 8;
69  size_t R = _v.size() % 8;
70 
71  size_t idx; // element index
72  unsigned char bits; // bitset
73 
74  for (idx=0; idx < N; ++idx)
75  {
76  _istr >> bits;
77  _v[idx+0] = ((bits & 0x01)!=0);
78  _v[idx+1] = ((bits & 0x02)!=0);
79  _v[idx+2] = ((bits & 0x04)!=0);
80  _v[idx+3] = ((bits & 0x08)!=0);
81  _v[idx+4] = ((bits & 0x10)!=0);
82  _v[idx+5] = ((bits & 0x20)!=0);
83  _v[idx+6] = ((bits & 0x40)!=0);
84  _v[idx+7] = ((bits & 0x80)!=0);
85  }
86  bytes = N;
87 
88  if (R)
89  {
90  _istr >> bits;
91  for(; idx < _v.size(); ++idx)
92  _v[idx] = (bits & (1 << (idx%8)))!=0;
93  ++bytes;
94  }
95 
96  return bytes;
97  }
98 };
STL namespace.