--- /dev/null
+////////////////////////////////////////////////////////////////////////////////\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
+// Polymorphous classes using the interface approach\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>\r
+ void dump_interface(dump_context& context, const T* const data)\r
+ throw(persistent_dump_failed)\r
+ {\r
+ try\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
+ if (data && !mapping.first)\r
+ {\r
+ // interface method\r
+ // the lookup just finds the magic key and the type has a dump method\r
+ // this will throw persistent_illegal_type if the type is not registered\r
+ unsigned key = context.lookup_interface(typeid(*data));\r
+ // dump the magic key for the type\r
+ dump_unsigned(context, key);\r
+ // now call the dump method defined by the interface\r
+ data->dump(context);\r
+ }\r
+ }\r
+ catch (const persistent_illegal_type& except)\r
+ {\r
+ // convert this to a simpler dump failed exception\r
+ throw persistent_dump_failed(except.what());\r
+ }\r
+ }\r
+\r
+ ////////////////////////////////////////////////////////////////////////////////\r
+\r
+ template<typename T>\r
+ void restore_interface(restore_context& context, T*& data)\r
+ throw(persistent_restore_failed)\r
+ {\r
+ try\r
+ {\r
+ // first delete any previous object pointed to since the restore creates the object of the right subclass\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 map it to the existing address\r
+ data = (T*)address.second;\r
+ }\r
+ else\r
+ {\r
+ // now restore the magic key that denotes the particular subclass\r
+ unsigned key = 0;\r
+ restore_unsigned(context, key);\r
+ // interface approach\r
+ // first clone the sample object stored in the map - lookup_interface can throw persistent_illegal_type\r
+ data = (T*)(context.lookup_interface(key)->clone());\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 the contents using the object's method\r
+ data->restore(context);\r
+ }\r
+ }\r
+ catch (const persistent_illegal_type& exception)\r
+ {\r
+ // convert this to a simpler dump failed exception\r
+ throw persistent_restore_failed(exception.what());\r
+ }\r
+ }\r
+\r
+ ////////////////////////////////////////////////////////////////////////////////\r
+\r
+} // end namespace stlplus\r