X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=src%2Fstlplus%2Fpersistence%2Fpersistent_pointer.tpp;fp=src%2Fstlplus%2Fpersistence%2Fpersistent_pointer.tpp;h=c760b17c36b34ac9a946ed5a6d38920267c39e45;hb=6b0a0d0efafe34d48ab344fca3b479553bd4e62c;hp=0000000000000000000000000000000000000000;hpb=85783316365181491a3e3c0c63659972477cebba;p=chaz%2Fyoink diff --git a/src/stlplus/persistence/persistent_pointer.tpp b/src/stlplus/persistence/persistent_pointer.tpp new file mode 100644 index 0000000..c760b17 --- /dev/null +++ b/src/stlplus/persistence/persistent_pointer.tpp @@ -0,0 +1,68 @@ +//////////////////////////////////////////////////////////////////////////////// + +// Author: Andy Rushton +// Copyright: (c) Southampton University 1999-2004 +// (c) Andy Rushton 2004-2009 +// License: BSD License, see ../docs/license.html + +// format: magic_key [ data ] + +//////////////////////////////////////////////////////////////////////////////// +#include "persistent_int.hpp" + +namespace stlplus +{ + + //////////////////////////////////////////////////////////////////////////////// + + template + void dump_pointer(dump_context& context, const T* const data, D dump_fn) + throw(persistent_dump_failed) + { + // register the address and get the magic key for it + std::pair mapping = context.pointer_map(data); + dump_unsigned(context,mapping.second); + // if the address is null, then that is all that we need to do + // however, if it is non-null and this is the first sight of the address, dump the contents + // note that the address is mapped before it is dumped so that self-referential structures dump correctly + if (data && !mapping.first) + dump_fn(context,*data); + } + + //////////////////////////////////////////////////////////////////////////////// + + template + void restore_pointer(restore_context& context, T*& data, R restore_fn) + throw(persistent_restore_failed) + { + if (data) + { + delete data; + data = 0; + } + // get the magic key + unsigned magic = 0; + restore_unsigned(context,magic); + // now lookup the magic key to see if this pointer has already been restored + // null pointers are always flagged as already restored + std::pair address = context.pointer_map(magic); + if (address.first) + { + // seen before, so simply assign the old address + data = (T*)address.second; + } + else + { + // this pointer has never been seen before and is non-null + data = new T(); + // add this pointer to the set of already seen objects + // do this before restoring the object so that self-referential structures restore correctly + context.pointer_add(magic,data); + // now restore it + restore_fn(context,*data); + } + } + + //////////////////////////////////////////////////////////////////////////////// + +} // end namespace stlplus