source: ThirdParty/mpqc_open/src/lib/util/state/state.dox@ 398fcd

Action_Thermostats Add_AtomRandomPerturbation Add_RotateAroundBondAction Add_SelectAtomByNameAction Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_StructOpt_integration_tests AutomationFragmentation_failures Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_ChronosMutex Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion GeometryObjects Gui_displays_atomic_force_velocity IndependentFragmentGrids_IntegrationTest JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks RotateToPrincipalAxisSystem_UndoRedo StoppableMakroAction Subpackage_levmar Subpackage_vmg ThirdParty_MPQC_rebuilt_buildsystem TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps Ubuntu_1604_changes stable
Last change on this file since 398fcd was 860145, checked in by Frederik Heber <heber@…>, 8 years ago

Merge commit '0b990dfaa8c6007a996d030163a25f7f5fc8a7e7' as 'ThirdParty/mpqc_open'

  • Property mode set to 100644
File size: 6.0 KB
Line 
1
2/** \page state The State Library
3
4The state library provides means for objects to save and restore their
5state. 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
23For objects of a class to be savable with this library the class must
24inherit SavableState which in turn inherits
25DescribedClass. SavableState must be inherited with the virtual qualifier.
26Also, a constructor taking a
27StateIn& argument and a
28save_data_state(StateOut&) member must be provided. If
29the class has virtual base classes other than SavableState, then a
30save_vbase_state(StateOut&) member must also be
31provided.
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
44Here is a simple example of the specification of a client, C,
45of SavableState:
46<pre>
47class C: virtual public SavableState {
48 private:
49 int i;
50 public:
51 C(StateIn&);
52 void save_data_state(StateOut&);
53};
54</pre>
55
56Here is the implementation for the above:
57<pre>
58static ClassDesc C_cd(typeid(C),"C",1,"virtual public SavableState",
59 0, 0, create<C>);
60void C::save_data_state(StateOut&so) {
61 so.put(i);
62}
63C::C(StateIn&si): SavableState(si) {
64 si.get(i);
65}
66</pre>
67
68\section stateexin Example with Inheritance
69
70Here is an example of the specification of C,
71where C nonvirtually inherits from another
72SavableState derivative:
73<pre>
74class C: public B {
75 private:
76 int i;
77 public:
78 C(StateIn&);
79 void save_data_state(StateOut&);
80};
81</pre>
82
83Here is the implementation for the above:
84<pre>
85static ClassDesc C_cd(typeid(C),"C",1,"public B",
86 0, 0, create<C>);
87void C::save_data_state(StateOut&so) {
88 B::save_data_state(so);
89 so.put(i);
90}
91C::C(StateIn&si): SavableState(si), B(si) {
92 si.get(i);
93}
94</pre>
95
96Note that B (or one of its parents) virtually inherits from
97SavableState, so the StateIn constructor for SavableState is
98called explicitly from the class C constructor.
99
100\section stateexvin Example with Virtual and Nonvirtual Inheritance
101
102Here is an example of the specification of C,
103where C nonvirtually inherits from another client of
104SavableState as well as virtually inherits from a client
105of SavableState:
106<pre>
107class 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
118In this case a save_vbase_state member is required since virtual
119base classes besides SavableState exist. This member function
120must save the virtual base classes in the same order that virtual
121base classes are initialized in constructors. Virtual base
122classes are initialized before all other base classes in a depth
123first, left to right transversal of the directed acyclic graph of
124parent classes. In this example, B and E inherit virtually
125from SavableState. Here is the implementation:
126<pre>
127static ClassDesc C_cd(typeid(C),"C",1,"public B, virtual public E",
128 0, 0, create<C>);
129void C::save_vbase_state(StateOut&sio) {
130 SavableState::save_data_state(so);
131 E::save_data_state(sio);
132}
133void C::save_data_state(StateOut&so) {
134 B::save_parent_state(so);
135 so.put(i);
136}
137C::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
144Here is an example where C has data members which are
145pointers to derivatives of SavableState:
146<pre>
147class 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
156Here is the implementation for the above:
157<pre>
158static ClassDesc C_cd(typeid(C),"C",1,"virtual public SavableState",
159 0, 0, create<C>);
160void C::save_data_state(StateOut&so) {
161 SavableState::save_state(ap,so);
162}
163C::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
170Here is an example where C has data members which are
171smart pointers to derivatives of SavableState:
172<pre>
173class 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
182Here is the implementation for the above:
183<pre>
184static ClassDesc C_cd(typeid(C),"C",1,"virtual public SavableState",
185 0, 0, create<C>);
186void C::save_data_state(StateOut&so) {
187 SavableState::save_state(a.pointer(),so);
188}
189C::C(StateIn&si): SavableState(si) {
190 a << SavableState::restore_state(so);
191}
192</pre>
193
194\section stateexdata Example with Pointers to Data
195
196Here is an example where C has data members which are
197pointers to data:
198<pre>
199class 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
212Here is the implementation for the above:
213<pre>
214static ClassDesc C_cd(typeid(C),"C",1,"virtual public SavableState",
215 0, 0, create<C>);
216void 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}
226C::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*/
Note: See TracBrowser for help on using the repository browser.