next up previous contents index
Next: Refcount Handling Up: Copying Instances Previous: Copying Instances

Copy Methods

 

First we discuss copying the instance itself. There are two copy methods which are implemented for most geometry classes.

   GRAPE(inst, "hardcopy")()
INSTANCE * obj
creates an identical copy of inst. All variables, lists and instance entries of inst are copied to a new instance which is returned. This method is also available in the interactive environment (see 4.3.5.3).

   GRAPE(inst, "softcopy")(copy)
INSTANCE * inst, * copy
creates a copy of inst in an existing instance copy, if copy is NULL a new instance is allocated. Differences to "hardcopy" are that storage available in copy is used, new storage is only allocated if necessary, and in case new storage has to be allocated only the exact amount of storage needed is allocated. In general both objects are therefore not identical, they are only identical from a geometric point of view.

We could use the superclass method to copy our Rot2d instances, this would copy the surface but not axis and curve. Therefore we have implemented these two method, too.

ROT2D *rot2d_hardcopy()
{
  ROT2D *self, *copy;

  self = (ROT2D *)START_METHOD(G_INSTANCE);
  ASSURE(self, "", END_METHOD(NULL));

  /*
   * First we call "hardcopy" on the superclass Triang2d to copy
   * the coordinate and connectivity lists.
   */
  ASSURE(copy = (ROT2D *)GRAPE(self, "^hardcopy")(),
         "rot2d_hardcopy: can't copy superclass", END_METHOD(NULL));

  /*
   * The superclass method has called "new-instance", therefore we
   * have these default curves, but we don't need them. We delete
   * them and reset the pointer to be sure it is valid.
   */
  GRAPE(copy->curve, "delete")();
  copy->curve = NULL;
  GRAPE(copy->axis, "delete")();
  copy->axis = NULL;

  /*
   * Now if there is a curve it is copied by calling "hardcopy"
   * and assigning the result to the curve of the copy. The
   * result is checked to see if we really got a copy.
   */
  if(self->curve)
    ASSURE(copy->curve = (TRIANG1D *)GRAPE(self->curve, "hardcopy")(),
           "rot2d_hardcopy: can't copy curve",
           { GRAPE(copy, "delete")(); END_METHOD(NULL); });

  /*
   * Same for the axis.
   */
  if(self->axis)
    ASSURE(copy->axis = (TRIANG1D *)GRAPE(self->axis, "hardcopy")(),
           "rot2d_hardcopy: can't copy axis",
           { GRAPE(copy, "delete")(); END_METHOD(NULL); });

  /*
   * The other values only have to be copied.
   */
  copy->angle = self->angle;
  copy->discr = self->discr;

  copy->current = self->current;

  END_METHOD(copy);
}


ROT2D *rot2d_softcopy(ROT2D *copy)
{
  ROT2D *self;

  self = (ROT2D *)START_METHOD(G_INSTANCE);
  ASSURE(self, "", END_METHOD(NULL));

  /*
   * First call "softcopy" on the superclass Triang2d. This will create
   * a new instance if necessary and copy the lists.
   */
  ASSURE(copy = (ROT2D *)GRAPE(self, "^softcopy")(copy),
         "rot2d_softcopy: can't copy superclass", END_METHOD(NULL));

  /*
   * Now we copy the curve. Since we might use the default curves memory
   * of the copy we delete it only if the original instance has no curve.
   */
  if(self->curve) {
    copy->curve = (TRIANG1D *)GRAPE(self->curve, "softcopy")(copy->curve);
    ASSURE(copy->curve, "rot2d_softcopy: can't copy curve",
           { GRAPE(copy, "delete")(); END_METHOD(NULL); });
  } else {
    GRAPE(copy->curve, "delete")();
    copy->curve = NULL;
  }

  /*
   * Same for the axis.
   */
  if(self->axis) {
    copy->axis = (TRIANG1D *)GRAPE(self->axis, "softcopy")(copy->axis);
    ASSURE(copy->axis, "rot2d_softcopy: can't copy axis",
           { GRAPE(copy, "delete")(); END_METHOD(NULL); });
  } else {
    GRAPE(copy->axis, "delete")();
    copy->axis = NULL;
  }

  /*
   * The other values only have to be copied.
   */
  copy->angle = self->angle;
  copy->discr = self->discr;

  copy->current = self->current;

  END_METHOD(copy);
}


SFB 256 Universität Bonn and IAM Universität Freiburg

Copyright © by the Sonderforschungsbereich 256 at the Institut für Angewandte Mathematik, Universität Bonn.