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 // format: magic_key [ data ]
10 ////////////////////////////////////////////////////////////////////////////////
11 #include "persistent_int.hpp"
16 ////////////////////////////////////////////////////////////////////////////////
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)
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);
32 ////////////////////////////////////////////////////////////////////////////////
34 template<typename T, typename R>
35 void restore_pointer(restore_context& context, T*& data, R restore_fn)
36 throw(persistent_restore_failed)
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);
51 // seen before, so simply assign the old address
52 data = (T*)address.second;
56 // this pointer has never been seen before and is non-null
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);
62 restore_fn(context,*data);
66 ////////////////////////////////////////////////////////////////////////////////
68 } // end namespace stlplus