]> Dogcows Code - chaz/yoink/blob - src/stlplus/persistence/persistent_bitset.tpp
testing new non-autotools build system
[chaz/yoink] / src / stlplus / persistence / persistent_bitset.tpp
1 ////////////////////////////////////////////////////////////////////////////////
2
3 // Author: Andy Rushton
4 // Copyright: (c) Southampton University 1999-2004
5 // (c) Andy Rushton 2004-2009
6 // License: BSD License, see ../docs/license.html
7
8 ////////////////////////////////////////////////////////////////////////////////
9 #include "persistent_int.hpp"
10
11 namespace stlplus
12 {
13
14 ////////////////////////////////////////////////////////////////////////////////
15 // format: data msB first, packed into bytes with lowest index at the byte's lsb
16
17 // Note: the interface does not provide access to the internal storage and yet
18 // to be efficient the std::bitset must be packed as bytes. Thus I have to do it the
19 // hard way.
20
21 template<size_t N>
22 void dump_bitset(dump_context& context, const std::bitset<N>& data)
23 throw(persistent_dump_failed)
24 {
25 size_t bits = data.size();
26 size_t bytes = (bits+7)/8;
27 for (size_t B = bytes; B--; )
28 {
29 unsigned char ch = 0;
30 for (size_t b = 0; b < 8; b++)
31 {
32 size_t bit = B*8+b;
33 if (bit < bits && data.test(bit))
34 ch |= (0x01 << b);
35 }
36 dump_unsigned_char(context,ch);
37 }
38 }
39
40 template<size_t N>
41 void restore_bitset(restore_context& context, std::bitset<N>& data)
42 throw(persistent_restore_failed)
43 {
44 size_t bits = data.size();
45 size_t bytes = (bits+7)/8;
46 for (size_t B = bytes; B--; )
47 {
48 unsigned char ch = 0;
49 restore_unsigned_char(context,ch);
50 for (size_t b = 0; b < 8; b++)
51 {
52 size_t bit = B*8+b;
53 if (bit >= bits) break;
54 data.set(bit, ch & (0x01 << b) ? true : false);
55 }
56 }
57 }
58
59 ////////////////////////////////////////////////////////////////////////////////
60
61 } // end namespace stlplus
This page took 0.032539 seconds and 4 git commands to generate.