home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / IDIOMS.ZIP / ERECT.C < prev    next >
C/C++ Source or Header  |  1991-12-04  |  3KB  |  118 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 R E C T . C                             //
  9. //                                                            //
  10. //         Code for class Rectangle                           //
  11. //                                                            //
  12. //************************************************************//
  13.  
  14. #include "erect.h"
  15.  
  16. // This file contains the implementation for
  17. // Rectangle abstractions
  18.  
  19. ShapeRep *rectangle = 0;      // Rectangle exemplar
  20.  
  21. void
  22. Rectangle::init() {
  23.     // initializes global variables associated
  24.     // with Rectangle support, allowing main to
  25.     // initialize things in the proper order.
  26.     // Some data structure initialization is done
  27.     // the first time ShapeRep::gcCommon is called
  28.     rectangle = new Rectangle(Exemplar(0));
  29. }
  30.  
  31. Shape
  32. Rectangle::make()
  33. {
  34.     // create a default rectangle
  35.     Rectangle *retval = new Rectangle;
  36.     retval->p1 = retval->p2 = Coordinate(0,0);
  37.     retval->exemplarPointer = this;
  38.     return *retval;
  39. }
  40.  
  41. Shape
  42. Rectangle::make(Coordinate pp1, Coordinate pp2)
  43. {
  44.     // create a rectangle at a specified place
  45.     Rectangle *retval = new Rectangle;
  46.     retval->p1 = pp1;
  47.     retval->p2 = pp2;
  48.     retval->exemplarPointer = this;
  49.     return *retval;
  50. }
  51.  
  52. void
  53. Rectangle::draw() {
  54.     int Sizeof = poolInitialized? poolInitialized:
  55.         sizeof(Rectangle);
  56.     printf("<Rectangle object %c>",
  57.         'A' + (((char *)this-(char *)heap)/Sizeof));
  58. }
  59.  
  60. void
  61. Rectangle::move(Coordinate) {
  62.     // . . .
  63. }
  64.  
  65. void
  66. Rectangle::rotate(double) {
  67.     // . . .
  68. }
  69.  
  70. //************************************************************//
  71. //       M e m o r y    M a n a g e m e n t                   //
  72. //************************************************************//
  73.  
  74. char *Rectangle::heap = 0;             // pool for operator new
  75. size_t Rectangle::poolInitialized = 0; // size of a Rectangle
  76.  
  77. void
  78. Rectangle::gc(size_t nbytes) {
  79.     // do garbage collection on Rectangle instances
  80.     printf("Rectangle::gc(%d) entered\n", nbytes);
  81.     gcCommon(nbytes, poolInitialized, PoolSize, heap);
  82. }
  83.  
  84. void *
  85. Rectangle::operator new(size_t nbytes) {
  86.     // dynamically create memory for a new Rectangle
  87.     if (poolInitialized - nbytes) {
  88.         gcCommon(nbytes, poolInitialized, PoolSize, heap);
  89.         poolInitialized = nbytes;
  90.     }
  91.     Rectangle *tp = (Rectangle *)heap;
  92.     // good coder would add memory exhaustion test here
  93.     while (tp->inUse) {
  94.         tp = (Rectangle*)(((char*)tp) + Round(nbytes));
  95.     }
  96.     tp->gcmark = 0;
  97.     tp->inUse = 1;
  98.     return (void*) tp;
  99. }
  100.  
  101. void Rectangle::operator delete(void *) {
  102.     // does nothing--see Rectangle::gc, above, instead
  103. }
  104.  
  105. Rectangle::Rectangle() {
  106.     // size and vptr knowledge are in here
  107. }
  108.  
  109. Rectangle::Rectangle(const Rectangle& t) {
  110.     // copy constructor
  111.     p1 = t.p1;
  112.     p2 = t.p2;
  113. }
  114.  
  115. Rectangle::Rectangle(Exemplar e) : ShapeRep(e) {
  116.     // constructor to build Exemplar only
  117. }
  118.