home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / IDIOMS.ZIP / ETRINGL.C < prev    next >
C/C++ Source or Header  |  1991-12-04  |  3KB  |  130 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 :    E T R I N G L . C                         //
  9. //                                                            //
  10. //         Interface for class Triangle                       //
  11. //                                                            //
  12. //************************************************************//
  13.  
  14. #include "etringl.h"
  15.  
  16. // class-specific variables
  17. ShapeRep *triangle = 0;
  18. char *Triangle::heap = 0;
  19. size_t Triangle::poolInitialized = 0;
  20.  
  21. void
  22. Triangle::init() {
  23.     // initialize Triangle-specific statics and globals.
  24.     // Some data structure initialization is done
  25.     // the first time ShapeRep::gcCommon is called
  26.     triangle = new Triangle(Exemplar(0));
  27. }
  28.  
  29. Shape
  30. Triangle::make()
  31. {
  32.     // make a default (degenerate) triangle
  33.     Triangle *retval = new Triangle;
  34.     retval->p1 = retval->p2 = retval->p3 = Coordinate(0,0);
  35.     retval->exemplarPointer = this;
  36.     return *retval;
  37. }
  38.  
  39. Shape
  40. Triangle::make(Coordinate pp1, Coordinate pp2, Coordinate pp3)
  41. {
  42.     // set up and return a new Triangle
  43.     Triangle *retval = new Triangle;
  44.     retval->p1 = pp1;
  45.     retval->p2 = pp2;
  46.     retval->p3 = pp3;
  47.     retval->exemplarPointer = this;
  48.     return *retval;
  49. }
  50.  
  51. void
  52. Triangle::gc(size_t nbytes) {
  53.     // pass Triangle memory data structures to common
  54.     // garbage collection (sweep) routine in base class
  55.     gcCommon(nbytes, poolInitialized, PoolSize, heap);
  56. }
  57.  
  58. void
  59. Triangle::draw() {
  60.     // for now, just print out a name corresponding
  61.     // to the triangle's position in the pool
  62.     int Sizeof = poolInitialized? poolInitialized:
  63.         Round(sizeof(Triangle));
  64.     printf("<Triangle object %c>",
  65.         'A' + (((char *)this-(char *)heap)/Sizeof));
  66. }
  67.  
  68. void
  69. Triangle::move(Coordinate) {
  70.     // . . .
  71. }
  72.  
  73. void
  74. Triangle::rotate(double) {
  75.     // . . .
  76. }
  77.  
  78. void *
  79. Triangle::operator new(size_t nbytes) {
  80.     // if pool has not yet been initialized, or if we just
  81.     // updated the Triangle class, give garbage collector
  82.     // control
  83.     if (poolInitialized - nbytes) {
  84.         gcCommon(nbytes, poolInitialized, PoolSize, heap);
  85.         poolInitialized = nbytes;
  86.     }
  87.  
  88.     // find a free one
  89.     Triangle *tp = (Triangle *)heap;
  90.     // need to add memory exhaustion test
  91.     while (tp->inUse) {
  92.         tp = (Triangle*)(((char*)tp) + Round(nbytes));
  93.     }
  94.  
  95.     // initialize memory bits appropriately
  96.     tp->gcmark = 0;
  97.     tp->inUse = 1;
  98.     return (void*) tp;
  99. }
  100.  
  101. void Triangle::operator delete(void *) {
  102.     // this should never be called, but C++ insists
  103.     // on its being here if new is
  104. }
  105.  
  106. Triangle::Triangle() { }  // size and vptr knowledge are in here
  107.  
  108. Triangle::Triangle(const Triangle& t) {
  109.     // copy constructor
  110.     p1 = t.p1;
  111.     p2 = t.p2;
  112.     p3 = t.p3;
  113. }
  114.  
  115. Triangle::Triangle(Exemplar e) : ShapeRep(e) {
  116.     // build a Triangle exemplar
  117. }
  118.  
  119. Shape
  120. Triangle::make(Coordinate) {
  121.     // dummy function--should never be called
  122.     return *aShape;
  123. }
  124.  
  125. Shape
  126. Triangle::make(Coordinate, Coordinate) {
  127.     // dummy function--should never be called
  128.     return *aShape;
  129. }
  130.