home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / IDIOMS.ZIP / EV3TRIC.C < prev    next >
C/C++ Source or Header  |  1991-12-04  |  3KB  |  91 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 V 3 T R I C . C                         //
  9. //                                                            //
  10. //         Cutover control code for version 3 Triangle code   //
  11. //                                                            //
  12. //************************************************************//
  13.  
  14. #include "ev3tri.h"
  15. #include "Map.h"
  16.  
  17. // --------------------------------------------------------------
  18.  
  19. class v2Triangle: public ShapeRep {
  20. public:
  21.     Shape make();
  22.     Shape make(Coordinate, Coordinate, Coordinate);
  23.     v2Triangle();
  24.     void move(Coordinate);
  25.     void *operator new(size_t);
  26.     void operator delete(void *);
  27.     void gc(size_t = 0);
  28.     void draw();
  29.     v2Triangle(Exemplar);
  30.     static void init();
  31. private:
  32.     friend Thing *Triangle::cutover();  // put in for conversion
  33.     static void poolInit(size_t);
  34.     Shape make(Coordinate) { return *aShape; }
  35.     Shape make(Coordinate, Coordinate) { return *aShape; }
  36.     Coordinate p1, p2, p3;
  37. };
  38.  
  39. // --------------------------------------------------------------
  40.  
  41. // Use this map to keep track of all old objects we are
  42. // asked to convert, and of the new ones they were converted to.
  43. // That way, if we are asked to convert the same object
  44. // several times, we map all requests onto the same return
  45. // value.
  46.  
  47. Map<Thingp, Thingp> objectMap;
  48.  
  49. Thing *
  50. Triangle::cutover() {
  51.     // we are going to return a pointer to a converted triangle
  52.     Triangle *retval = this;
  53.  
  54.     // instance passed in is really old triangle
  55.     // the old triangle declaration is preserved
  56.     // under the name v2Triangle;  class Triangle
  57.     // is the version 3 one
  58.     v2Triangle *old = (v2Triangle *)this;
  59.     Thingp oldtp = this;
  60.     ShapeRep *oldsr = (ShapeRep*)this;
  61.  
  62.     if (objectMap.element(oldtp)) {
  63.         // if we've converted it already, don't
  64.         // convert it again--just return old
  65.         // converted value
  66.         retval = (Triangle*)(objectMap[oldtp]);
  67.     } else {
  68.         // create a new (version 3) triangle to return
  69.         // store it in several different kinds of pointers
  70.         retval = new Triangle;
  71.         ShapeRep *newsr = retval;
  72.         Thingp newtp = retval;
  73.  
  74.         // copy over just the base class (ShapeRep) part
  75.         *newsr = *oldsr;
  76.  
  77.         // now set up the fields of the new object
  78.         retval->exemplarPointer = triangle;
  79.         retval->p1 = old->p1;
  80.         retval->p2 = old->p2;
  81.         retval->p3 = old->p3;
  82.         retval->color = Black;
  83.  
  84.         // save converted one for later
  85.         objectMap[oldtp] = newtp;
  86.     }
  87.     return retval;
  88. }
  89.  
  90. Triangle::Triangle() { }
  91.