| [5d30c1] | 1 |  | 
|---|
|  | 2 | /** \page state The State Library | 
|---|
|  | 3 |  | 
|---|
|  | 4 | The state library provides means for objects to save and restore their | 
|---|
|  | 5 | state.  Features include: | 
|---|
|  | 6 |  | 
|---|
|  | 7 | <ul> | 
|---|
|  | 8 | <li> | 
|---|
|  | 9 | Pointers to base types can be saved and restored. | 
|---|
|  | 10 | The exact types of the saved and restored objects will match. | 
|---|
|  | 11 | <li> | 
|---|
|  | 12 | If the pointer to an object is saved twice, only one copy of the | 
|---|
|  | 13 | object is saved.  When these two pointers are restored they will | 
|---|
|  | 14 | point to the same object. | 
|---|
|  | 15 | <li> | 
|---|
|  | 16 | Virtual base classes are dealt with in a manner consistent with | 
|---|
|  | 17 | the way C++ treats virtual base classes. | 
|---|
|  | 18 | <li> | 
|---|
|  | 19 | The library is portable.  Information about object layout for | 
|---|
|  | 20 | particular compiler implementations is not needed. | 
|---|
|  | 21 | </ul> | 
|---|
|  | 22 |  | 
|---|
|  | 23 | For objects of a class to be savable with this library the class must | 
|---|
|  | 24 | inherit SavableState which in turn inherits | 
|---|
|  | 25 | DescribedClass.  SavableState must be inherited with the virtual qualifier. | 
|---|
|  | 26 | Also, a constructor taking a | 
|---|
|  | 27 | StateIn& argument and a | 
|---|
|  | 28 | save_data_state(StateOut&) member must be provided.  If | 
|---|
|  | 29 | the class has virtual base classes other than SavableState, then a | 
|---|
|  | 30 | save_vbase_state(StateOut&) member must also be | 
|---|
|  | 31 | provided. | 
|---|
|  | 32 |  | 
|---|
|  | 33 | <ul> | 
|---|
|  | 34 | <li> \ref stateex | 
|---|
|  | 35 | <li> \ref stateexin | 
|---|
|  | 36 | <li> \ref stateexvin | 
|---|
|  | 37 | <li> \ref stateexpoint | 
|---|
|  | 38 | <li> \ref stateexsmart | 
|---|
|  | 39 | <li> \ref stateexdata | 
|---|
|  | 40 | </ul> | 
|---|
|  | 41 |  | 
|---|
|  | 42 | \section stateex Simple Example | 
|---|
|  | 43 |  | 
|---|
|  | 44 | Here is a simple example of the specification of a client, C, | 
|---|
|  | 45 | of SavableState: | 
|---|
|  | 46 | <pre> | 
|---|
|  | 47 | class C: virtual public SavableState { | 
|---|
|  | 48 | private: | 
|---|
|  | 49 | int i; | 
|---|
|  | 50 | public: | 
|---|
|  | 51 | C(StateIn&); | 
|---|
|  | 52 | void save_data_state(StateOut&); | 
|---|
|  | 53 | }; | 
|---|
|  | 54 | </pre> | 
|---|
|  | 55 |  | 
|---|
|  | 56 | Here is the implementation for the above: | 
|---|
|  | 57 | <pre> | 
|---|
|  | 58 | static ClassDesc C_cd(typeid(C),"C",1,"virtual public SavableState", | 
|---|
|  | 59 | 0, 0, create<C>); | 
|---|
|  | 60 | void C::save_data_state(StateOut&so) { | 
|---|
|  | 61 | so.put(i); | 
|---|
|  | 62 | } | 
|---|
|  | 63 | C::C(StateIn&si): SavableState(si) { | 
|---|
|  | 64 | si.get(i); | 
|---|
|  | 65 | } | 
|---|
|  | 66 | </pre> | 
|---|
|  | 67 |  | 
|---|
|  | 68 | \section stateexin Example with Inheritance | 
|---|
|  | 69 |  | 
|---|
|  | 70 | Here is an example of the specification of C, | 
|---|
|  | 71 | where C nonvirtually inherits from another | 
|---|
|  | 72 | SavableState derivative: | 
|---|
|  | 73 | <pre> | 
|---|
|  | 74 | class C: public B { | 
|---|
|  | 75 | private: | 
|---|
|  | 76 | int i; | 
|---|
|  | 77 | public: | 
|---|
|  | 78 | C(StateIn&); | 
|---|
|  | 79 | void save_data_state(StateOut&); | 
|---|
|  | 80 | }; | 
|---|
|  | 81 | </pre> | 
|---|
|  | 82 |  | 
|---|
|  | 83 | Here is the implementation for the above: | 
|---|
|  | 84 | <pre> | 
|---|
|  | 85 | static ClassDesc C_cd(typeid(C),"C",1,"public B", | 
|---|
|  | 86 | 0, 0, create<C>); | 
|---|
|  | 87 | void C::save_data_state(StateOut&so) { | 
|---|
|  | 88 | B::save_data_state(so); | 
|---|
|  | 89 | so.put(i); | 
|---|
|  | 90 | } | 
|---|
|  | 91 | C::C(StateIn&si): SavableState(si), B(si)  { | 
|---|
|  | 92 | si.get(i); | 
|---|
|  | 93 | } | 
|---|
|  | 94 | </pre> | 
|---|
|  | 95 |  | 
|---|
|  | 96 | Note that B (or one of its parents) virtually inherits from | 
|---|
|  | 97 | SavableState, so the StateIn constructor for SavableState is | 
|---|
|  | 98 | called explicitly from the class C constructor. | 
|---|
|  | 99 |  | 
|---|
|  | 100 | \section stateexvin Example with Virtual and Nonvirtual Inheritance | 
|---|
|  | 101 |  | 
|---|
|  | 102 | Here is an example of the specification of C, | 
|---|
|  | 103 | where C nonvirtually inherits from another client of | 
|---|
|  | 104 | SavableState as well as virtually inherits from a client | 
|---|
|  | 105 | of SavableState: | 
|---|
|  | 106 | <pre> | 
|---|
|  | 107 | class C: public B, | 
|---|
|  | 108 | virtual public E { | 
|---|
|  | 109 | private: | 
|---|
|  | 110 | int i; | 
|---|
|  | 111 | public: | 
|---|
|  | 112 | C(StateIn&); | 
|---|
|  | 113 | void save_vbase_state(StateOut&); | 
|---|
|  | 114 | void save_data_state(StateOut&); | 
|---|
|  | 115 | }; | 
|---|
|  | 116 | </pre> | 
|---|
|  | 117 |  | 
|---|
|  | 118 | In this case a save_vbase_state member is required since virtual | 
|---|
|  | 119 | base classes besides SavableState exist.  This member function | 
|---|
|  | 120 | must save the virtual base classes in the same order that virtual | 
|---|
|  | 121 | base classes are initialized in constructors.  Virtual base | 
|---|
|  | 122 | classes are initialized before all other base classes in a depth | 
|---|
|  | 123 | first, left to right transversal of the directed acyclic graph of | 
|---|
|  | 124 | parent classes.  In this example, B and E inherit virtually | 
|---|
|  | 125 | from SavableState.  Here is the implementation: | 
|---|
|  | 126 | <pre> | 
|---|
|  | 127 | static ClassDesc C_cd(typeid(C),"C",1,"public B, virtual public E", | 
|---|
|  | 128 | 0, 0, create<C>); | 
|---|
|  | 129 | void C::save_vbase_state(StateOut&sio) { | 
|---|
|  | 130 | SavableState::save_data_state(so); | 
|---|
|  | 131 | E::save_data_state(sio); | 
|---|
|  | 132 | } | 
|---|
|  | 133 | void C::save_data_state(StateOut&so) { | 
|---|
|  | 134 | B::save_parent_state(so); | 
|---|
|  | 135 | so.put(i); | 
|---|
|  | 136 | } | 
|---|
|  | 137 | C::C(StateIn&si): SavableState(si), B(si), E(si) { | 
|---|
|  | 138 | si.get(i); | 
|---|
|  | 139 | } | 
|---|
|  | 140 | </pre> | 
|---|
|  | 141 |  | 
|---|
|  | 142 | \section stateexpoint Example with Pointers to SavableStates | 
|---|
|  | 143 |  | 
|---|
|  | 144 | Here is an example where C has data members which are | 
|---|
|  | 145 | pointers to derivatives of SavableState: | 
|---|
|  | 146 | <pre> | 
|---|
|  | 147 | class C: virtual public SavableState { | 
|---|
|  | 148 | private: | 
|---|
|  | 149 | A* ap; // A is also a SavableState | 
|---|
|  | 150 | public: | 
|---|
|  | 151 | C(StateIn&); | 
|---|
|  | 152 | void save_data_state(StateOut&); | 
|---|
|  | 153 | }; | 
|---|
|  | 154 | </pre> | 
|---|
|  | 155 |  | 
|---|
|  | 156 | Here is the implementation for the above: | 
|---|
|  | 157 | <pre> | 
|---|
|  | 158 | static ClassDesc C_cd(typeid(C),"C",1,"virtual public SavableState", | 
|---|
|  | 159 | 0, 0, create<C>); | 
|---|
|  | 160 | void C::save_data_state(StateOut&so) { | 
|---|
|  | 161 | SavableState::save_state(ap,so); | 
|---|
|  | 162 | } | 
|---|
|  | 163 | C::C(StateIn&si): SavableState(si) { | 
|---|
|  | 164 | ap = dynamic_cast<A>(SavableState::restore_state(si)); | 
|---|
|  | 165 | } | 
|---|
|  | 166 | </pre> | 
|---|
|  | 167 |  | 
|---|
|  | 168 | \section stateexsmart Example with Smart Pointers to SavableStates | 
|---|
|  | 169 |  | 
|---|
|  | 170 | Here is an example where C has data members which are | 
|---|
|  | 171 | smart pointers to derivatives of SavableState: | 
|---|
|  | 172 | <pre> | 
|---|
|  | 173 | class C: virtual public SavableState { | 
|---|
|  | 174 | private: | 
|---|
|  | 175 | Ref<A> a; // A is also a SavableState | 
|---|
|  | 176 | public: | 
|---|
|  | 177 | C(StateIn&); | 
|---|
|  | 178 | void save_data_state(StateOut&); | 
|---|
|  | 179 | }; | 
|---|
|  | 180 | </pre> | 
|---|
|  | 181 |  | 
|---|
|  | 182 | Here is the implementation for the above: | 
|---|
|  | 183 | <pre> | 
|---|
|  | 184 | static ClassDesc C_cd(typeid(C),"C",1,"virtual public SavableState", | 
|---|
|  | 185 | 0, 0, create<C>); | 
|---|
|  | 186 | void C::save_data_state(StateOut&so) { | 
|---|
|  | 187 | SavableState::save_state(a.pointer(),so); | 
|---|
|  | 188 | } | 
|---|
|  | 189 | C::C(StateIn&si): SavableState(si) { | 
|---|
|  | 190 | a << SavableState::restore_state(so); | 
|---|
|  | 191 | } | 
|---|
|  | 192 | </pre> | 
|---|
|  | 193 |  | 
|---|
|  | 194 | \section stateexdata Example with Pointers to Data | 
|---|
|  | 195 |  | 
|---|
|  | 196 | Here is an example where C has data members which are | 
|---|
|  | 197 | pointers to data: | 
|---|
|  | 198 | <pre> | 
|---|
|  | 199 | class C: virtual public SavableState { | 
|---|
|  | 200 | private: | 
|---|
|  | 201 | int vecsize; | 
|---|
|  | 202 | double *vec; | 
|---|
|  | 203 | int n1; | 
|---|
|  | 204 | int n2; | 
|---|
|  | 205 | double **array; | 
|---|
|  | 206 | public: | 
|---|
|  | 207 | C(StateIn&); | 
|---|
|  | 208 | void save_data_state(StateOut&); | 
|---|
|  | 209 | }; | 
|---|
|  | 210 | </pre> | 
|---|
|  | 211 |  | 
|---|
|  | 212 | Here is the implementation for the above: | 
|---|
|  | 213 | <pre> | 
|---|
|  | 214 | static ClassDesc C_cd(typeid(C),"C",1,"virtual public SavableState", | 
|---|
|  | 215 | 0, 0, create<C>); | 
|---|
|  | 216 | void C::save_data_state(StateOut&so) { | 
|---|
|  | 217 | so.put(vecsize); | 
|---|
|  | 218 | so.put_array_double(vec,vecsize); | 
|---|
|  | 219 |  | 
|---|
|  | 220 | so.put(n1); | 
|---|
|  | 221 | so.put(n2); | 
|---|
|  | 222 | for (int i=0; i<n1; i++) { | 
|---|
|  | 223 | so.put_array_double(array[i],n2); | 
|---|
|  | 224 | } | 
|---|
|  | 225 | } | 
|---|
|  | 226 | C::C(StateIn&si): SavableState(si) { | 
|---|
|  | 227 | si.get(vecsize); | 
|---|
|  | 228 | vec = new double[vecsize]; | 
|---|
|  | 229 | si.get_array_double(vec,vecsize); | 
|---|
|  | 230 |  | 
|---|
|  | 231 | si.get(n1); | 
|---|
|  | 232 | si.get(n2); | 
|---|
|  | 233 |  | 
|---|
|  | 234 | array = new double*[n1]; | 
|---|
|  | 235 | for (int i=0; i<n1; i++) { | 
|---|
|  | 236 | array[i] = new double[n2]; | 
|---|
|  | 237 | si.get_array_double(array[i],n2); | 
|---|
|  | 238 | } | 
|---|
|  | 239 | } | 
|---|
|  | 240 | </pre> | 
|---|
|  | 241 |  | 
|---|
|  | 242 | */ | 
|---|