1 ////////////////////////////////////////////////////////////////////////////////
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
8 ////////////////////////////////////////////////////////////////////////////////
9 #include "persistent_float.hpp"
11 ////////////////////////////////////////////////////////////////////////////////
12 // Macro for mapping either endian data onto little-endian addressing to make
13 // my life easier in writing this code! I think better in little-endian mode
14 // so the macro does nothing in that mode but maps little-endian onto
15 // big-endian addressing in big-endian mode
16 // TODO - make this compile-time configurable
18 #define INDEX(index) ((context.little_endian()) ? (index) : ((bytes) - (index) - 1))
20 /////////////////////////////////////////////////////////////////////
21 // floating point types
22 // format: {size}{byte}*size
23 // ordering is msB first
25 // this uses a similar mechanism to integer dumps. However, it is not clear how
26 // the big-endian and little-endian argument applies to multi-word data so
27 // this may need reworking by splitting into words and then bytes.
32 static void dump_float(stlplus::dump_context
& context
, unsigned bytes
, unsigned char* data
)
33 throw(stlplus::persistent_dump_failed
)
37 context
.put((unsigned char)i
);
40 context
.put(data
[INDEX(i
)]);
43 static void restore_float(stlplus::restore_context
& context
, unsigned bytes
, unsigned char* data
)
44 throw(stlplus::persistent_restore_failed
)
46 // get the dumped size from the file
47 unsigned dumped_bytes
= (unsigned)context
.get();
48 // get the bytes from the file
49 unsigned i
= dumped_bytes
;
52 int ch
= context
.get();
54 data
[INDEX(i
)] = (unsigned char)ch
;
56 // however, if the dumped size was different I don't know how to map the formats, so give an error
57 if (dumped_bytes
!= bytes
)
58 throw stlplus::persistent_restore_failed(std::string("size mismatch"));
61 } // end namespace stlplus
63 ////////////////////////////////////////////////////////////////////////////////
64 // exported functions which simply call the low-level byte-dump and byte-restore routines above
66 void stlplus::dump_float(stlplus::dump_context
& context
, const float& data
) throw(stlplus::persistent_dump_failed
)
68 stlplus::dump_float(context
, sizeof(float), (unsigned char*)&data
);
71 void stlplus::restore_float(restore_context
& context
, float& data
) throw(stlplus::persistent_restore_failed
)
73 stlplus::restore_float(context
, sizeof(float), (unsigned char*)&data
);
76 void stlplus::dump_double(stlplus::dump_context
& context
, const double& data
) throw(stlplus::persistent_dump_failed
)
78 stlplus::dump_float(context
, sizeof(double), (unsigned char*)&data
);
81 void stlplus::restore_double(restore_context
& context
, double& data
) throw(stlplus::persistent_restore_failed
)
83 stlplus::restore_float(context
, sizeof(double), (unsigned char*)&data
);
86 ////////////////////////////////////////////////////////////////////////////////