]> Dogcows Code - chaz/yoink/blob - src/stlplus/persistence/persistent_float.cpp
archiving support for releases
[chaz/yoink] / src / stlplus / persistence / persistent_float.cpp
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_float.hpp"
10
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
17
18 #define INDEX(index) ((context.little_endian()) ? (index) : ((bytes) - (index) - 1))
19
20 /////////////////////////////////////////////////////////////////////
21 // floating point types
22 // format: {size}{byte}*size
23 // ordering is msB first
24
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.
28
29 namespace stlplus
30 {
31
32 static void dump_float(stlplus::dump_context& context, unsigned bytes, unsigned char* data)
33 throw(stlplus::persistent_dump_failed)
34 {
35 unsigned i = bytes;
36 // put the size
37 context.put((unsigned char)i);
38 // and put the bytes
39 while(i--)
40 context.put(data[INDEX(i)]);
41 }
42
43 static void restore_float(stlplus::restore_context& context, unsigned bytes, unsigned char* data)
44 throw(stlplus::persistent_restore_failed)
45 {
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;
50 while(i--)
51 {
52 int ch = context.get();
53 if (i < bytes)
54 data[INDEX(i)] = (unsigned char)ch;
55 }
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"));
59 }
60
61 } // end namespace stlplus
62
63 ////////////////////////////////////////////////////////////////////////////////
64 // exported functions which simply call the low-level byte-dump and byte-restore routines above
65
66 void stlplus::dump_float(stlplus::dump_context& context, const float& data) throw(stlplus::persistent_dump_failed)
67 {
68 stlplus::dump_float(context, sizeof(float), (unsigned char*)&data);
69 }
70
71 void stlplus::restore_float(restore_context& context, float& data) throw(stlplus::persistent_restore_failed)
72 {
73 stlplus::restore_float(context, sizeof(float), (unsigned char*)&data);
74 }
75
76 void stlplus::dump_double(stlplus::dump_context& context, const double& data) throw(stlplus::persistent_dump_failed)
77 {
78 stlplus::dump_float(context, sizeof(double), (unsigned char*)&data);
79 }
80
81 void stlplus::restore_double(restore_context& context, double& data) throw(stlplus::persistent_restore_failed)
82 {
83 stlplus::restore_float(context, sizeof(double), (unsigned char*)&data);
84 }
85
86 ////////////////////////////////////////////////////////////////////////////////
This page took 0.031774 seconds and 4 git commands to generate.