]> Dogcows Code - chaz/yoink/blobdiff - src/stlplus/persistence/persistent_simple_ptr.tpp
testing new non-autotools build system
[chaz/yoink] / src / stlplus / persistence / persistent_simple_ptr.tpp
diff --git a/src/stlplus/persistence/persistent_simple_ptr.tpp b/src/stlplus/persistence/persistent_simple_ptr.tpp
new file mode 100644 (file)
index 0000000..36f88bd
--- /dev/null
@@ -0,0 +1,144 @@
+////////////////////////////////////////////////////////////////////////////////\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
+////////////////////////////////////////////////////////////////////////////////\r
+#include "persistent_int.hpp"\r
+#include "persistent_pointer.hpp"\r
+#include "persistent_callback.hpp"\r
+#include "persistent_interface.hpp"\r
+\r
+namespace stlplus\r
+{\r
+\r
+  ////////////////////////////////////////////////////////////////////////////////\r
+\r
+  template<typename  T, typename DE>\r
+  void dump_simple_ptr(dump_context& context, const simple_ptr<T>& data,\r
+                      DE dump_element)\r
+    throw(persistent_dump_failed)\r
+  {\r
+    // Many smart pointers can point to the same object.\r
+    // I could have used the address of the object to differentiate, \r
+    // but that would not have differentiated between different null smart pointers\r
+    // so I use the address of the count to differentiate between different objects.\r
+    // get a magic key for the substructure - this also returns a flag saying whether its been seen before\r
+    std::pair<bool,unsigned> mapping = context.pointer_map(data._count());\r
+    // dump the magic key for the count\r
+    dump_unsigned(context,mapping.second);\r
+    // dump the contents always - this is because I need to rely on the pointer routines dumping a second magic key\r
+    // use the existing routines for ordinary pointers to dump the contents\r
+    dump_pointer(context,data._pointer(),dump_element);\r
+  }\r
+\r
+  template<typename T, typename RE>\r
+  void restore_simple_ptr(restore_context& context, simple_ptr<T>& data,\r
+                         RE restore_element)\r
+    throw(persistent_restore_failed)\r
+  {\r
+    // get the old counter magic key\r
+    unsigned magic = 0;\r
+    restore_unsigned(context,magic);\r
+    // lookup this magic number to see if we have seen this already\r
+    std::pair<bool,void*> mapping = context.pointer_map(magic);\r
+    if (mapping.first)\r
+    {\r
+      // this holder has already been restored\r
+      // now restore the object and rely on the pointer routines to return the existing object\r
+      T* value = 0;\r
+      restore_pointer(context,value,restore_element);\r
+      // dealias the existing holder and replace it with the seen-before holder to make this object an alias of it\r
+      data._make_alias(value, (unsigned*)mapping.second);\r
+    }\r
+    else\r
+    {\r
+      // this is the first contact with this holder\r
+      // make sure this smart pointer is unique to prevent side-effects\r
+      data.clear_unique();\r
+      // map the magic key onto this structure's holder\r
+      // do this before restoring the object so that self-referential structures restore correctly\r
+      context.pointer_add(magic,data._count());\r
+      // now restore the object\r
+      T* value = 0;\r
+      restore_pointer(context,value,restore_element);\r
+      // and add it to the pointer\r
+      data.set(value);\r
+    }\r
+  }\r
+\r
+  ////////////////////////////////////////////////////////////////////////////////\r
+  // simple_ptr_clone using callbacks\r
+\r
+  template<typename T>\r
+  void dump_simple_ptr_clone_callback(dump_context& context, const simple_ptr_clone<T>& data)\r
+    throw(persistent_dump_failed)\r
+  {\r
+    std::pair<bool,unsigned> mapping = context.pointer_map(data._count());\r
+    dump_unsigned(context,mapping.second);\r
+    dump_callback(context,data._pointer());\r
+  }\r
+\r
+  template<typename T>\r
+  void restore_simple_ptr_clone_callback(restore_context& context, simple_ptr_clone<T>& data)\r
+    throw(persistent_restore_failed)\r
+  {\r
+    unsigned magic = 0;\r
+    restore_unsigned(context,magic);\r
+    std::pair<bool,void*> mapping = context.pointer_map(magic);\r
+    if (mapping.first)\r
+    {\r
+      T* value = 0;\r
+      restore_callback(context,value);\r
+      data._make_alias(value, (unsigned*)mapping.second);\r
+    }\r
+    else\r
+    {\r
+      data.clear_unique();\r
+      context.pointer_add(magic,data._count());\r
+      T* value = 0;\r
+      restore_callback(context,value);\r
+      data.set(value);\r
+    }\r
+  }\r
+\r
+  ////////////////////////////////////////////////////////////////////////////////\r
+  // simple_ptr_clone using interface\r
+\r
+  template<typename T>\r
+  void dump_simple_ptr_clone_interface(dump_context& context, const simple_ptr_clone<T>& data)\r
+    throw(persistent_dump_failed)\r
+  {\r
+    std::pair<bool,unsigned> mapping = context.pointer_map(data._count());\r
+    dump_unsigned(context,mapping.second);\r
+    dump_interface(context,data._pointer());\r
+  }\r
+\r
+  template<typename T>\r
+  void restore_simple_ptr_clone_interface(restore_context& context, simple_ptr_clone<T>& data)\r
+    throw(persistent_restore_failed)\r
+  {\r
+    unsigned magic = 0;\r
+    restore_unsigned(context,magic);\r
+    std::pair<bool,void*> mapping = context.pointer_map(magic);\r
+    if (mapping.first)\r
+    {\r
+      T* value = 0;\r
+      restore_interface(context,value);\r
+      data._make_alias(value, (unsigned*)mapping.second);\r
+    }\r
+    else\r
+    {\r
+      data.clear_unique();\r
+      context.pointer_add(magic,data._count());\r
+      T* value = 0;\r
+      restore_interface(context,value);\r
+      data.set(value);\r
+    }\r
+  }\r
+\r
+  ////////////////////////////////////////////////////////////////////////////////\r
+\r
+} // end namespace stlplus\r
This page took 0.02683 seconds and 4 git commands to generate.