+////////////////////////////////////////////////////////////////////////////////\r
+\r
+// Author: Andy Rushton\r
+// Copyright: (c) Southampton University 1999-2004\r
+// (c) Andy Rushton 2004-2009\r
+// License: BSD License, see ../docs/license.html\r
+\r
+// format: magic_key [ data ]\r
+\r
+////////////////////////////////////////////////////////////////////////////////\r
+#include "persistent_int.hpp"\r
+\r
+namespace stlplus\r
+{\r
+\r
+ ////////////////////////////////////////////////////////////////////////////////\r
+\r
+ template<typename T, typename D>\r
+ void dump_pointer(dump_context& context, const T* const data, D dump_fn)\r
+ throw(persistent_dump_failed)\r
+ {\r
+ // register the address and get the magic key for it\r
+ std::pair<bool,unsigned> mapping = context.pointer_map(data);\r
+ dump_unsigned(context,mapping.second);\r
+ // if the address is null, then that is all that we need to do\r
+ // however, if it is non-null and this is the first sight of the address, dump the contents\r
+ // note that the address is mapped before it is dumped so that self-referential structures dump correctly\r
+ if (data && !mapping.first)\r
+ dump_fn(context,*data);\r
+ }\r
+\r
+ ////////////////////////////////////////////////////////////////////////////////\r
+\r
+ template<typename T, typename R>\r
+ void restore_pointer(restore_context& context, T*& data, R restore_fn)\r
+ throw(persistent_restore_failed)\r
+ {\r
+ if (data)\r
+ {\r
+ delete data;\r
+ data = 0;\r
+ }\r
+ // get the magic key\r
+ unsigned magic = 0;\r
+ restore_unsigned(context,magic);\r
+ // now lookup the magic key to see if this pointer has already been restored\r
+ // null pointers are always flagged as already restored\r
+ std::pair<bool,void*> address = context.pointer_map(magic);\r
+ if (address.first)\r
+ {\r
+ // seen before, so simply assign the old address\r
+ data = (T*)address.second;\r
+ }\r
+ else\r
+ {\r
+ // this pointer has never been seen before and is non-null\r
+ data = new T();\r
+ // add this pointer to the set of already seen objects\r
+ // do this before restoring the object so that self-referential structures restore correctly\r
+ context.pointer_add(magic,data);\r
+ // now restore it\r
+ restore_fn(context,*data);\r
+ }\r
+ }\r
+\r
+ ////////////////////////////////////////////////////////////////////////////////\r
+\r
+} // end namespace stlplus\r