home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / IDIOMS.ZIP / ESHAPRP.C < prev    next >
C/C++ Source or Header  |  1991-12-04  |  6KB  |  219 lines

  1. /* Copyright (c) 1992 by AT&T Bell Laboratories. */
  2. /* Advanced C++ Programming Styles and Idioms */
  3. /* James O. Coplien */
  4. /* All rights reserved. */
  5.  
  6. //************************************************************//
  7. //                                                            //
  8. //     F I L E :    eshaprp.c                                 //
  9. //                                                            //
  10. //         Code for class ShapeRep                            //
  11. //                                                            //
  12. //************************************************************//
  13.  
  14. // ShapeRep is the base class for all letter classes that
  15. // use Shape as an envelope.  It is in the derived classes
  16. // of ShapeRep where all the shape intelligence is
  17.  
  18. #include "eshaprp.h"
  19. #ifndef _RECTANGLE_H
  20. #include "erect.h"
  21. #endif
  22. #ifndef _TRIANGLE_H
  23. #include "etringl.h"
  24. #endif
  25.  
  26. // "constant" used as a degenerate return value
  27. Shape *ShapeRep::aShape = 0;
  28.  
  29. // bits designating From space and To space for Baker's
  30. // algorithm;  are flipped on every cycle
  31. unsigned char ShapeRep::FromSpace = 0, ShapeRep::ToSpace = 1;
  32.  
  33. void ShapeRep::init() {
  34.     // initializes class data structures
  35.     aShape = new Shape;
  36. }
  37.  
  38. void *
  39. ShapeRep::operator new(size_t l) {
  40.     // overridden in derived classes
  41.     return ::operator new(l);
  42. }
  43.  
  44. void
  45. ShapeRep::operator delete(void *p) {
  46.     // overridden in derived classes
  47.     ::operator delete(p);
  48. }
  49.  
  50. Thing *
  51. ShapeRep::type() {
  52.     // type of any derived class of
  53.     // ShapeRep is kept in exemplarPointer
  54.     return (Thingp)exemplarPointer;
  55. }
  56.  
  57. Shape
  58. ShapeRep::make() {
  59.     // default ShapeRep manufacturer
  60.     return *aShape;
  61. }
  62.  
  63. Shape
  64. ShapeRep::make(Coordinate c1, Thingp) {
  65.     // one constraint: Point object
  66.     return point->make(c1);
  67. }
  68.  
  69. Shape
  70. ShapeRep::make(Coordinate c1, Coordinate c2, Thingp type) {
  71.     // exemplar constructor for Shape objects with two
  72.     // constraints:  Lines and Rectangles
  73.     return ((ShapeRep *)type)->make(c1, c2);
  74. }
  75.  
  76. Shape
  77. ShapeRep::make(Coordinate c1, Coordinate c2, Coordinate c3,
  78.            Thingp type) {
  79.     // exemplar constructor for Shape objects with three
  80.     // constraints:  Triangles, Arcs, Parallelograms
  81.     return ((ShapeRep*)type)->make(c1, c2, c3);
  82. }
  83.  
  84. Shape
  85. ShapeRep::make(Coordinate c1) {
  86.     // make a Shape from one coordinate
  87.     // default is to return a Point
  88.     return make(c1, (Thingp)&point);
  89. }
  90.  
  91. Shape
  92. ShapeRep::make(Coordinate c1, Coordinate c2) {
  93.     // make a Shape from two coordinates
  94.     // default is to return a Rectangle
  95.     return make(c1, c2, rectangle);
  96. }
  97.  
  98. Shape
  99. ShapeRep::make(Coordinate c1, Coordinate c2, Coordinate c3) {
  100.     // make a Shape from three coordinates
  101.     // default is to return a Triangle
  102.     return make(c1, c2, c3, triangle);
  103. }
  104.  
  105. void
  106. ShapeRep::gcCommon(size_t nbytes, const size_t poolInitialized,
  107.         const int PoolSize, Char_p &heap) {
  108.     // Garbage collection common to all ShapeReps.  This
  109.     // is where most of the "sweep" of the second half
  110.     // of Baker's algorithm happens.  It is called by
  111.     // the derived class gc routines, who call it
  112.     // directly with appropriate parameters.
  113.  
  114.     // calculate s to be the size of the object.  If
  115.     // nbytes is specified, use it:  it designates
  116.     // a change in size (or is given at initialization
  117.     // time to get things rolling).  Otherwise, use
  118.     // the value saved in poolInitialized.
  119.     size_t s = nbytes? nbytes: poolInitialized;
  120.  
  121.     // round up for alignment constraints
  122.     size_t Sizeof = Round(s);
  123.  
  124.     // if a non-zero parameter was given, it is the size
  125.     // of an object;  it indicates we are to discard the
  126.     // old pool and allocate a new one.  This is done at
  127.     // startup and after class update.
  128.     if (nbytes) heap = new char[PoolSize * Sizeof];
  129.  
  130.     ShapeRep *tp = (ShapeRep *)heap;
  131.  
  132.     // sweep through the pool
  133.     for (int i = 0; i < PoolSize; i++) {
  134.         switch (nbytes) {
  135.         case 0:   // normal garbage collection case
  136.             // If still earmarked, but not checked off, and
  137.             // in FromSpace, nuke it.
  138.             if (tp->inUse) {
  139.                 if (tp->gcmark || tp->space != FromSpace) {
  140.                     // don't sweep it away
  141.                     tp->space = ToSpace;
  142.                 } else if (tp != tp->type()) {
  143.                     // object needs to be reclaimed
  144.                     tp->ShapeRep::~ShapeRep();
  145.                     tp->inUse = 0;
  146.                     printf("ShapeRep::gcCommon ");
  147.                     printf("Reclaimed Shape object %c\n",
  148.                      'A' + (((char *)tp-(char *)heap)/Sizeof));
  149.                 }
  150.             }
  151.             break;
  152.         default:   // initialization of memory arena
  153.             tp->inUse = 0;
  154.             break;
  155.         }
  156.         tp->gcmark = 0;
  157.         tp = (ShapeRep*)(Char_p(tp) + Sizeof);
  158.     }
  159. }
  160.  
  161. ShapeRep::ShapeRep(Exemplar) {
  162.     // constructor to build ShapeRep's exemplar
  163.     // (not very important)
  164.     //
  165.     // Recall that Exemplar is a dummy type used
  166.     // to disambiguate this and the default constructor
  167.     exemplarPointer = this;
  168.     shape->Register(this);
  169. }
  170.  
  171. void
  172. ShapeRep::rotate(double) {
  173.     // acts like pure virtual function
  174.     printf("ShapeRep::rotate(double)\n");
  175. }
  176.  
  177. void
  178. ShapeRep::move(Coordinate) {
  179.     // acts like pure virtual function
  180.     printf("ShapeRep::move(Coordinate)\n");
  181. }
  182.  
  183. void
  184. ShapeRep::draw() {
  185.     // acts like pure virtual function.  For now,
  186.     // draw routines will just print the type's
  187.     // name
  188.     printf("<Shape>");
  189. }
  190.  
  191. void
  192. ShapeRep::erase() {
  193.     // acts like pure virtual function
  194.     printf("ShapeRep::erase()");
  195. }
  196.  
  197. void
  198. ShapeRep::gc(size_t) {
  199.     // acts like pure virtual function
  200. }
  201.  
  202. void
  203. ShapeRep::mark() {
  204.     // used by Shape in mark phase of Baker
  205.     gcmark = 1;
  206. }
  207.  
  208. ShapeRep::ShapeRep() {
  209.     // default constructor
  210.     exemplarPointer=this;
  211.     gcmark=0;
  212.     space=ToSpace;
  213.     inUse=1;
  214. }
  215.  
  216. ShapeRep::~ShapeRep() {
  217.     // destructor does nothing
  218. }
  219.