]> Dogcows Code - chaz/yoink/blob - src/stlplus/persistence/persistent_pointer.tpp
archiving support for releases
[chaz/yoink] / src / stlplus / persistence / persistent_pointer.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 // format: magic_key [ data ]
9
10 ////////////////////////////////////////////////////////////////////////////////
11 #include "persistent_int.hpp"
12
13 namespace stlplus
14 {
15
16 ////////////////////////////////////////////////////////////////////////////////
17
18 template<typename T, typename D>
19 void dump_pointer(dump_context& context, const T* const data, D dump_fn)
20 throw(persistent_dump_failed)
21 {
22 // register the address and get the magic key for it
23 std::pair<bool,unsigned> mapping = context.pointer_map(data);
24 dump_unsigned(context,mapping.second);
25 // if the address is null, then that is all that we need to do
26 // however, if it is non-null and this is the first sight of the address, dump the contents
27 // note that the address is mapped before it is dumped so that self-referential structures dump correctly
28 if (data && !mapping.first)
29 dump_fn(context,*data);
30 }
31
32 ////////////////////////////////////////////////////////////////////////////////
33
34 template<typename T, typename R>
35 void restore_pointer(restore_context& context, T*& data, R restore_fn)
36 throw(persistent_restore_failed)
37 {
38 if (data)
39 {
40 delete data;
41 data = 0;
42 }
43 // get the magic key
44 unsigned magic = 0;
45 restore_unsigned(context,magic);
46 // now lookup the magic key to see if this pointer has already been restored
47 // null pointers are always flagged as already restored
48 std::pair<bool,void*> address = context.pointer_map(magic);
49 if (address.first)
50 {
51 // seen before, so simply assign the old address
52 data = (T*)address.second;
53 }
54 else
55 {
56 // this pointer has never been seen before and is non-null
57 data = new T();
58 // add this pointer to the set of already seen objects
59 // do this before restoring the object so that self-referential structures restore correctly
60 context.pointer_add(magic,data);
61 // now restore it
62 restore_fn(context,*data);
63 }
64 }
65
66 ////////////////////////////////////////////////////////////////////////////////
67
68 } // end namespace stlplus
This page took 0.036603 seconds and 4 git commands to generate.