X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=src%2Fstlplus%2Fpersistence%2Fpersistent_xref.tpp;fp=src%2Fstlplus%2Fpersistence%2Fpersistent_xref.tpp;h=e12639d59e08ef89afda2e69a5b92146fbf7e935;hb=6b0a0d0efafe34d48ab344fca3b479553bd4e62c;hp=0000000000000000000000000000000000000000;hpb=85783316365181491a3e3c0c63659972477cebba;p=chaz%2Fyoink diff --git a/src/stlplus/persistence/persistent_xref.tpp b/src/stlplus/persistence/persistent_xref.tpp new file mode 100644 index 0000000..e12639d --- /dev/null +++ b/src/stlplus/persistence/persistent_xref.tpp @@ -0,0 +1,51 @@ +//////////////////////////////////////////////////////////////////////////////// + +// 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_xref(dump_context& context, const T* const data) + throw(persistent_dump_failed) + { + // register the address and get the magic key for it + std::pair mapping = context.pointer_map(data); + // if this is the first view of this pointer, simply throw an exception + if (!mapping.first) throw persistent_dump_failed("tried to dump a cross-reference not seen before"); + // otherwise, just dump the magic key + dump_unsigned(context,mapping.second); + } + + ////////////////////////////////////////////////////////////////////////////////> + + template + void restore_xref(restore_context& context, T*& data) + throw(persistent_restore_failed) + { + // Note: I do not try to delete the old data because this is a cross-reference + // 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 this is the first view of this pointer, simply throw an exception + if (!address.first) throw persistent_restore_failed("tried to restore a cross-reference not seen before"); + // seen before, so simply assign the old address + data = (T*)address.second; + } + + //////////////////////////////////////////////////////////////////////////////// + +} // end namespace stlplus