1 #ifndef STLPLUS_PERSISTENT_CONTEXTS
2 #define STLPLUS_PERSISTENT_CONTEXTS
3 ////////////////////////////////////////////////////////////////////////////////
5 // Author: Andy Rushton
6 // Copyright: (c) Southampton University 1999-2004
7 // (c) Andy Rushton 2004-2009
8 // License: BSD License, see ../docs/license.html
10 // Core context classes used to control the persistent dump/restore operations
12 ////////////////////////////////////////////////////////////////////////////////
14 #include "persistence_fixes.hpp"
15 #include "persistent.hpp"
20 ////////////////////////////////////////////////////////////////////////////////
25 ////////////////////////////////////////////////////////////////////////////////
28 class dump_context_body
;
29 class restore_context_body
;
31 ////////////////////////////////////////////////////////////////////////////////
32 // The format version number currently supported
33 ////////////////////////////////////////////////////////////////////////////////
35 extern unsigned char PersistentVersion
;
37 ////////////////////////////////////////////////////////////////////////////////
38 // dump_context controls the formatting of a persistent dump
39 ////////////////////////////////////////////////////////////////////////////////
43 friend class persistent
;
45 //////////////////////////////////////////////////////////////////////////////
47 // device must be in binary mode
48 dump_context(std::ostream
& device
, unsigned char version
= PersistentVersion
) throw(persistent_dump_failed
);
51 // low level output used to dump a byte
52 void put(unsigned char data
) throw(persistent_dump_failed
);
54 // access the device, for example to check the error status
55 const std::ostream
& device(void) const;
57 // recover the version number of the dumped output
58 unsigned char version(void) const;
60 // test whether the current platform uses little-endian or big-endian addressing of bytes
61 // this is used in dump/restore of integers and is exported so that other routines can use it
62 bool little_endian(void) const;
64 // Assist functions for Pointers
65 // the return pair value is a flag saying whether this is a new pointer and the magic key to dump to file
66 std::pair
<bool,unsigned> pointer_map(const void* const pointer
);
68 // Assist functions for Polymorphous classes (i.e. subclasses) using callback approach
69 typedef void (*dump_callback
)(dump_context
&,const void*);
70 unsigned register_callback(const std::type_info
& info
, dump_callback
);
71 bool is_callback(const std::type_info
& info
) const;
72 typedef std::pair
<unsigned,dump_callback
> callback_data
;
73 callback_data
lookup_callback(const std::type_info
&) const throw(persistent_illegal_type
);
75 // Assist functions for Polymorphous classes (i.e. subclasses) using interface approach
76 unsigned register_interface(const std::type_info
& info
);
77 bool is_interface(const std::type_info
& info
) const;
78 unsigned lookup_interface(const std::type_info
&) const throw(persistent_illegal_type
);
80 // Register all Polymorphous classes using either approach by calling an installer callback
81 typedef void (*installer
)(dump_context
&);
82 void register_all(installer
);
85 friend class dump_context_body
;
86 dump_context_body
* m_body
;
88 // disallow copying by making assignment and copy constructor private
89 dump_context(const dump_context
&);
90 dump_context
& operator=(const dump_context
&);
93 ////////////////////////////////////////////////////////////////////////////////
94 // restore_context controls the reading of the persistent data during a restore
98 friend class persistent
;
100 //////////////////////////////////////////////////////////////////////////////
102 // device must be in binary mode
103 restore_context(std::istream
& device
) throw(persistent_restore_failed
);
104 ~restore_context(void);
106 // low level input used to restore a byte
107 int get(void) throw(persistent_restore_failed
);
109 // access the device, for example to check the error status
110 const std::istream
& device(void) const;
112 // access the version number of the input being restored
113 unsigned char version(void) const;
115 // test whether the current platform uses little-endian or big-endian addressing of bytes
116 // this is used in dump/restore of integers
117 bool little_endian(void) const;
119 // Assist functions for Pointers
120 std::pair
<bool,void*> pointer_map(unsigned magic
);
121 void pointer_add(unsigned magic
, void* new_pointer
);
123 // Assist functions for Polymorphous classes using the callback approach
124 typedef void* (*create_callback
)(void);
125 typedef void (*restore_callback
)(restore_context
&,void*);
126 unsigned register_callback(create_callback
,restore_callback
);
127 bool is_callback(unsigned) const;
128 typedef std::pair
<create_callback
, restore_callback
> callback_data
;
129 callback_data
lookup_callback(unsigned) const throw(persistent_illegal_type
);
131 // Assist functions for Polymorphous classes using the interface approach
132 unsigned register_interface(persistent
*);
133 bool is_interface(unsigned) const;
134 persistent
* lookup_interface(unsigned) const throw(persistent_illegal_type
);
136 // Register all Polymorphous classes using either approach by calling an installer callback
137 typedef void (*installer
)(restore_context
&);
138 void register_all(installer
);
141 friend class restore_context_body
;
142 restore_context_body
* m_body
;
144 typedef std::pair
<unsigned,persistent
*> interface_data
;
146 // disallow copying by making assignment and copy constructor private
147 restore_context(const restore_context
&);
148 restore_context
& operator=(const restore_context
&);
151 ////////////////////////////////////////////////////////////////////////////////
153 } // end namespace stlplus
155 ////////////////////////////////////////////////////////////////////////////////