+++ /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 callback 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_callback(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
- // callback method - get the callback data and perform the dump\r
- // this will throw persistent_illegal_type if not recognised, thus the try block\r
- dump_context::callback_data callback = context.lookup_callback(typeid(*data));\r
- // dump the magic key for the type\r
- dump_unsigned(context, callback.first);\r
- // now call the callback that dumps the subclass\r
- callback.second(context,data);\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_callback(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
- // callback approach\r
- // call the create callback to create an object of the right type\r
- // then call the restore callback to get the contents\r
- // this will throw persistent_illegal_type if not recognised - this is caught below\r
- restore_context::callback_data callbacks = context.lookup_callback(key);\r
- data = (T*)callbacks.first();\r
- // add this pointer to the set of already seen objects\r
- // note that the address is mapped before it is dumped so that self-referential structures dump correctly\r
- context.pointer_add(magic,data);\r
- callbacks.second(context,data);\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