Sophie

Sophie

distrib > Mandriva > 9.1 > ppc > media > contrib > by-pkgid > 263386785cefb9ae5d63b926d214d809 > files > 1353

mpqc-2.1.2-4mdk.ppc.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><meta name="robots" content="noindex">
<meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>The State Library</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body bgcolor="#ffffff">
<!-- Generated by Doxygen 1.2.5 on Mon Oct 14 14:18:08 2002 -->
<center>
<a class="qindex" href="index.html">Main Page</a> &nbsp; <a class="qindex" href="hierarchy.html">Class Hierarchy</a> &nbsp; <a class="qindex" href="annotated.html">Compound List</a> &nbsp; <a class="qindex" href="files.html">File List</a> &nbsp; <a class="qindex" href="functions.html">Compound Members</a> &nbsp; <a class="qindex" href="pages.html">Related Pages</a> &nbsp; </center>
<hr><a name="state"><h2>The State Library</h2></a>
 The state library provides means for objects to save and restore their state. Features include:
<p>
<ul>
 <li> Pointers to base types can be saved and restored. The exact types of the saved and restored objects will match. <li> If the pointer to an object is saved twice, only one copy of the object is saved. When these two pointers are restored they will point to the same object. <li> Virtual base classes are dealt with in a manner consistent with the way C++ treats virtual base classes. <li> The library is portable. Information about object layout for particular compiler implementations is not needed. </ul>

<p>
For objects of a class to be savable with this library the class must inherit SavableState which in turn inherits DescribedClass. SavableState must be inherited with the virtual qualifier. Also, a constructor taking a StateIn&amp; argument and a save_data_state(StateOut&amp;) member must be provided. If the class has virtual base classes other than SavableState, then a save_vbase_state(StateOut&amp;) member must also be provided.
<p>
<ul>
 <li> <a href="state.html#stateex">Simple Example</a> <li> <a href="state.html#stateexin">Example with Inheritance</a> <li> <a href="state.html#stateexvin">Example with Virtual and Nonvirtual Inheritance</a> <li> stateexvpoint <li> stateexvsmart <li> <a href="state.html#stateexdata">Example with Pointers to Data</a> </ul>

<p>
<a name="stateex"><h2>Simple Example</h2></a>

<p>
Here is a simple example of the specification of a client, C, of SavableState: <pre>
class C: virtual public SavableState {
  private:
    int i;
  public:
    C(StateIn&amp;);
    void save_data_state(StateOut&amp;);
};
</pre>
<p>
Here is the implementation for the above: <pre>
static ClassDesc C_cd(typeid(C),"C",1,"virtual public SavableState",
                      0, 0, create&lt;C&gt;);
void C::save_data_state(StateOut&amp;so) {
  so.put(i);
}
C::C(StateIn&amp;si): SavableState(si) {
  si.get(i);
}
</pre>
<p>
<a name="stateexin"><h2>Example with Inheritance</h2></a>

<p>
Here is an example of the specification of C, where C nonvirtually inherits from another SavableState derivative: <pre>
class C: public B {
  private:
    int i;
  public:
    C(StateIn&amp;);
    void save_data_state(StateOut&amp;);
};
</pre>
<p>
Here is the implementation for the above: <pre>
static ClassDesc C_cd(typeid(C),"C",1,"public B",
                      0, 0, create&lt;C&gt;);
void C::save_data_state(StateOut&amp;so) {
  B::save_data_state(so);
  so.put(i);
}
C::C(StateIn&amp;si): SavableState(si), B(si)  {
  si.get(i);
}
</pre>
<p>
Note that B (or one of its parents) virtually inherits from SavableState, so the StateIn constructor for SavableState is called explicitly from the class C constructor.
<p>
<a name="stateexvin"><h2>Example with Virtual and Nonvirtual Inheritance</h2></a>

<p>
Here is an example of the specification of C, where C nonvirtually inherits from another client of SavableState as well as virtually inherits from a client of SavableState: <pre>
class C: public B,
         virtual public E {
  private:
    int i;
  public:
    C(StateIn&amp;);
    void save_vbase_state(StateOut&amp;);
    void save_data_state(StateOut&amp;);
  };
</pre>
<p>
In this case a save_vbase_state member is required since virtual base classes besides SavableState exist. This member function must save the virtual base classes in the same order that virtual base classes are initialized in constructors. Virtual base classes are initialized before all other base classes in a depth first, left to right transversal of the directed acyclic graph of parent classes. In this example, B and E inherit virtually from SavableState. Here is the implementation: <pre>
static ClassDesc C_cd(typeid(C),"C",1,"public B, virtual public E",
                      0, 0, create&lt;C&gt;);
void C::save_vbase_state(StateOut&amp;sio) {
  SavableState::save_data_state(so);
  E::save_data_state(sio);
}
void C::save_data_state(StateOut&amp;so) {
  B::save_parent_state(so);
  so.put(i);
}
C::C(StateIn&amp;si): SavableState(si), B(si), E(si) {
  si.get(i);
}
</pre>
<p>
<a name="stateexpoint"><h2>Example with Pointers to SavableStates</h2></a>

<p>
Here is an example where C has data members which are pointers to derivatives of SavableState: <pre>
class C: virtual public SavableState {
  private:
    A* ap; // A is also a SavableState
  public:
    C(StateIn&amp;);
    void save_data_state(StateOut&amp;);
  };
</pre>
<p>
Here is the implementation for the above: <pre>
static ClassDesc C_cd(typeid(C),"C",1,"virtual public SavableState",
                      0, 0, create&lt;C&gt;);
void C::save_data_state(StateOut&amp;so) {
  SavableState::save_state(ap,so);
}
C::C(StateIn&amp;si): SavableState(si) {
  ap = dynamic_cast(SavableState::restore_state(si));
}
</pre>
<p>
<a name="stateexsmart"><h2>Example with Smart Pointers to SavableStates</h2></a>

<p>
Here is an example where C has data members which are smart pointers to derivatives of SavableState: <pre>
class C: virtual public SavableState {
  private:
    Ref a; // A is also a SavableState
  public:
    C(StateIn&amp;);
    void save_data_state(StateOut&amp;);
};
</pre>
<p>
Here is the implementation for the above: <pre>
static ClassDesc C_cd(typeid(C),"C",1,"virtual public SavableState",
                      0, 0, create&lt;C&gt;);
void C::save_data_state(StateOut&amp;so) {
  SavableState::save_state(a.pointer(),so);
}
C::C(StateIn&amp;si): SavableState(si) {
  a &lt;&lt; SavableState::restore_state(so);
}
</pre>
<p>
<a name="stateexdata"><h2>Example with Pointers to Data</h2></a>

<p>
Here is an example where C has data members which are pointers to data: <pre>
class C: virtual public SavableState {
  private:
    int vecsize;
    double *vec;
    int n1;
    int n2;
    double **array;
  public:
    C(StateIn&amp;);
    void save_data_state(StateOut&amp;);
};
</pre>
<p>
Here is the implementation for the above: <pre>
static ClassDesc C_cd(typeid(C),"C",1,"virtual public SavableState",
                      0, 0, create&lt;C&gt;);
void C::save_data_state(StateOut&amp;so) {
  so.put(vecsize);
  so.put_array_double(vec,vecsize);

  so.put(n1);
  so.put(n2);
  for (int i=0; i&lt;n1; i++) {
    so.put_array_double(array[i],n2);
  }
}
C::C(StateIn&amp;si): SavableState(si) {
  si.get(vecsize);
  vec = new double[vecsize];
  si.get_array_double(vec,vecsize);

  si.get(n1);
  si.get(n2);

  array = new double*[n1];
  for (int i=0; i&lt;n1; i++) {
    array[i] = new double[n2];
    si.get_array_double(array[i],n2);
  }
}
</pre>
<p>
<hr>
<address>
<small>

Generated at Mon Oct 14 14:18:08 2002 for <a
href="http://aros.ca.sandia.gov/~cljanss/mpqc">MPQC</a>
2.1.2 using the documentation package <a
href="http://www.stack.nl/~dimitri/doxygen/index.html">Doxygen</a>
1.2.5.

</small>
</address>
</body>
</html>