home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_C / CL187A.ZIP / EXAMP503.CPP < prev    next >
C/C++ Source or Header  |  1994-03-15  |  8KB  |  377 lines

  1. // examp503.cpp -- link with cl.obj pitem.obj graphics.lib
  2. // Container Lite (CL v 1.87a)
  3. // (C) Copyright 1994  John Webster Small
  4. // All rights reserved
  5.  
  6. //#define CL_NO_TEMPLATES
  7.  
  8. #include "pitem.h"
  9. #define BGI_PATHNAME "\\bc4\\bgi"
  10. #include "gconsole.cpp"
  11.  
  12. #define ID_Shape 1U
  13.  
  14. class Shape : public Streamable  {
  15.  
  16.     int x, y;
  17.  
  18.     void init(
  19.         // Shape level initializers with
  20.         // defaults provided for each
  21.         int x = GETRANDX(),
  22.         int y = GETRANDY()
  23.     )
  24.     { this->x = x; this->y = y; }
  25.  
  26. protected:
  27.  
  28.     void assign(const Shape& s)
  29.         { Streamable::assign
  30.         (*(const Streamable *)&s);
  31.         x = s.x; y = s.y; }
  32.     Shape(defaultConstructor)
  33.         : Streamable(defaultConstruct)
  34.         { init(); }
  35.     virtual int put(ostream& os);
  36.     int    get(istream& is);
  37.     static    StreamablE extract(istream& is);
  38.  
  39. public:
  40.  
  41.  
  42. // Shape polymorphic cluster processing
  43. static  Shape * newShape();
  44. static  void    TallyShowApply(Shape * S,
  45.             va_list args);
  46.  
  47.     static  int register_Class()
  48.         { return clasSv.regClass
  49.         (Shape::extract,ID_Shape); }
  50.  
  51.     Shape(const Shape& s) : Streamable(defaultConstruct)
  52.         { init(); assign(s); }
  53.     Shape(int x = GETRANDX(),
  54.         int y = GETRANDY())
  55.         : Streamable()
  56.         { init(x,y); }
  57.     virtual    int operator=(const Streamable& s)
  58.         { return 0; }
  59.     virtual    unsigned ID() const
  60.         { return ID_Shape; }
  61.     int getx() { return x; }
  62.     int gety() { return y; }
  63.     Shape& setxy(int x = GETRANDX(),
  64.         int y = GETRANDY())
  65.         { this->x = x; this->y = y;
  66.         return *this; }
  67.     virtual void show(int color = GETRANDCOLOR(),
  68.         int xxpose = 0, int yxpose = 0,
  69.         int scale = 1)
  70.         { PUTPIX(x,y,color); }
  71.     virtual char * name()
  72.         { return "shape (pixel)"; }
  73.     virtual    ~Shape()  {}
  74.  
  75. };    /*  class Shape  */
  76.  
  77.  
  78. int Shape::put(ostream& os)
  79. {
  80. //    if (Streamable::put(os))  {
  81.         os << x << endm << y << endm;
  82.         if (os)
  83.             return 1;  // success
  84. //    }
  85.     return 0;
  86. }
  87.  
  88. int Shape::get(istream& is)
  89. {
  90. //    if (Streamable::get(is))  {
  91.         is >> x >> nextm >> y >> nextm;
  92.         if (is)
  93.             return 1;
  94. //    }
  95.     return 0;
  96. }
  97.  
  98.  
  99. StreamablE Shape::extract(istream& is)
  100. {
  101.     Shape* S;
  102.     S = new Shape(defaultConstruct);
  103.     if (S) if (S->get(is))
  104.         return (StreamablE) S;
  105.     else
  106.         delete S;
  107.     return  StreamablE0;
  108. }
  109.  
  110. #define ID_Circle 2U
  111.  
  112. class Circle : public Shape  {
  113.  
  114. protected:
  115.  
  116.     int radius;
  117.  
  118. private:
  119.  
  120.     void init(int radius = GETRANDY()/8+1)
  121.         { this->radius = radius; }
  122.  
  123. protected:
  124.  
  125.     void assign(const Circle& c)
  126.         { Shape::assign(*(const Shape *)&c);
  127.         radius = c.radius; }
  128.     Circle(defaultConstructor)
  129.         : Shape(defaultConstruct)
  130.         { init(); }
  131.     virtual int put(ostream& os);
  132.     int    get(istream& is);
  133.     static    StreamablE extract(istream& is);
  134.  
  135. public:
  136.  
  137.     static  int register_Class()
  138.         { return clasSv.regClass
  139.         (Circle::extract,ID_Circle); }
  140.  
  141.     Circle(const Circle& s) : Shape(defaultConstruct)
  142.         { init(); assign(s); }
  143.     Circle(int radius = GETRANDY()/8+1,
  144.         int x = GETRANDX(),
  145.         int y = GETRANDY())
  146.         : Shape(x,y)
  147.         { init(radius); }
  148.     virtual    unsigned ID() const
  149.         { return ID_Circle; }
  150.     virtual void show(int color, int xxpose,
  151.         int yxpose, int scale)
  152.         {
  153.             SETCOLOR(color);
  154.             CIRCLE(getx()+xxpose,
  155.                 gety()+yxpose,
  156.                 (radius*scale));
  157.         }
  158.     virtual char * name() { return "circle"; }
  159.     virtual    ~Circle()  {}
  160.  
  161. };    /*  class Circle  */
  162.  
  163.  
  164. int Circle::put(ostream& os)
  165. {
  166.     if (Shape::put(os))  {
  167.         os << radius << endm;
  168.         if (os)
  169.             return 1;  // success
  170.     }
  171.     return 0;
  172. }
  173.  
  174. int Circle::get(istream& is)
  175. {
  176.     if (Shape::get(is))  {
  177.         is >> radius >> nextm;
  178.         if (is)
  179.             return 1;
  180.     }
  181.     return 0;
  182. }
  183.  
  184. StreamablE Circle::extract(istream& is)
  185. {
  186.     Circle* S;
  187.     S = new Circle(defaultConstruct);
  188.     if (S) if (S->get(is))
  189.         return (StreamablE) S;
  190.     else
  191.         delete S;
  192.     return  StreamablE0;
  193. }
  194.  
  195.  
  196.  
  197. #define ID_Rectangle 3U
  198.  
  199. class Rectangle : public Shape  {
  200.  
  201.     int width, heighth;
  202.  
  203.     void init(
  204.         int width = GETRANDX()/4+1,
  205.         int heighth = GETRANDY()/4+1
  206.     )
  207.         { this->width = width;
  208.         this->heighth = heighth; }
  209.  
  210. protected:
  211.  
  212.     void assign(const Rectangle& r)
  213.         { Shape::assign(*(const Shape *)&r);
  214.         width = r.width;
  215.         heighth = r.heighth; }
  216.     Rectangle(defaultConstructor)
  217.         : Shape(defaultConstruct)
  218.         { init(); }
  219.     virtual int put(ostream& os);
  220.     int    get(istream& is);
  221.     static    StreamablE extract(istream& is);
  222.  
  223. public:
  224.  
  225.     static  int register_Class()
  226.         { return clasSv.regClass
  227.         (Rectangle::extract,ID_Rectangle); }
  228.  
  229.     Rectangle(const Rectangle& s)
  230.         : Shape(defaultConstruct)
  231.         { init(); assign(s); }
  232.     Rectangle(int width = GETRANDX()/4+1,
  233.         int heighth = GETRANDY()/4+1,
  234.         int x = GETRANDX(),
  235.         int y = GETRANDY()
  236.         ) : Shape(x,y)
  237.         { init(width,heighth); }
  238.  
  239.     virtual    unsigned ID() const
  240.         { return ID_Rectangle; }
  241.     virtual void show(int color, int xxpose,
  242.         int yxpose, int scale);
  243.     virtual char * name() { return "rectangle"; }
  244.     virtual    ~Rectangle()  {}
  245.  
  246. };    /*  class Rectangle  */
  247.  
  248.  
  249. int Rectangle::put(ostream& os)
  250. {
  251.     if (Shape::put(os))  {
  252.         os << width << endm << heighth << endm;
  253.         if (os)
  254.             return 1;  // success
  255.     }
  256.     return 0;
  257. }
  258.  
  259. int Rectangle::get(istream& is)
  260. {
  261.     if (Shape::get(is))  {
  262.         is >> width >> nextm
  263.             >> heighth >> nextm;
  264.         if (is)
  265.             return 1;
  266.     }
  267.     return 0;
  268. }
  269.  
  270. StreamablE Rectangle::extract(istream& is)
  271. {
  272.     Rectangle* S;
  273.     S = new Rectangle(defaultConstruct);
  274.     if (S) if (S->get(is))
  275.         return (StreamablE) S;
  276.     else
  277.         delete S;
  278.     return  StreamablE0;
  279. }
  280.  
  281. #pragma argsused
  282. void Rectangle::show(int color,
  283.     int xxpose, int yxpose,    int scale)
  284. {
  285.     int dw = (width/2*scale);
  286.     int dh = (heighth/2*scale);
  287.     SETCOLOR(color);
  288.     RECTANGLE(getx()+xxpose - dw,
  289.         gety()+yxpose - dh,
  290.         getx()+xxpose + dw,
  291.         gety()+yxpose + dh);
  292. }
  293.  
  294.  
  295. #if defined(CL_NO_TEMPLATES)
  296.     #define CL_ALL_DEF
  297.     CL_PITEM(Shape,Streamable)
  298.     #define ITEM Shape
  299.     #define CL Shapes
  300.     #include "cl.hf"
  301. #else
  302.     CL_PITEM(Shape,Streamable)
  303.     #define Shapes CL<Shape>
  304. #endif
  305.  
  306.  
  307. //  Shape polymorphic cluster processing
  308. //  remains outside compiled cluster library
  309. //  to allow cluster extensibility.
  310.  
  311. Shape * Shape::newShape()
  312. {
  313.     int ID = random(ID_Rectangle-ID_Shape+1)
  314.         + ID_Shape;
  315.  
  316.     switch (ID)  {
  317.       case ID_Shape:     return new Shape();
  318.       case ID_Circle:    return new Circle();
  319.       case ID_Rectangle: return new Rectangle();
  320. // add new cluster members here:
  321.       default:           return (Shape *)0;
  322.     }
  323. }
  324.  
  325. void Shape::TallyShowApply(Shape * S, va_list args)
  326. {
  327.     int   xxpose     =  va_arg(args,int);
  328.     int   yxpose     =  va_arg(args,int);
  329.     int   scale      =  va_arg(args,int);
  330.     int * shapes     =  va_arg(args,int *);
  331.     int * circles    =  va_arg(args,int *);
  332.     int * rectangles =  va_arg(args,int *);
  333. // add new cluster members here:
  334.     switch (S->ID())  {
  335.       case ID_Shape:     ++*shapes;     break;
  336.       case ID_Circle:    ++*circles;    break;
  337.       case ID_Rectangle: ++*rectangles; break;
  338. // and here:
  339.     }
  340.     S->show(GETRANDCOLOR(),xxpose,yxpose,scale);
  341. }
  342.  
  343. #define  ShapesFile "shapes.tmp"
  344.  
  345. main()
  346. {
  347.     openGraphics();
  348.     Shapes sb(CL_ANDS,100);
  349.     Shape *S;
  350.     while (sb.insQ(S = Shape::newShape()));
  351.     delete S;
  352.     Register_Class(Shape);
  353.     Register_Class(Circle);
  354.     Register_Class(Rectangle);
  355.     sb.save(ShapesFile);
  356.     sb.allDel();
  357.     sb.load(ShapesFile);
  358.     int shapes = 0, circles = 0, rectangles = 0;
  359.     sb.forEach(Shape::TallyShowApply,0,0,1,
  360.         &shapes,&circles,&rectangles);
  361.     cout << "Shapes (pixels): " << shapes
  362.         << "  cirlces: " << circles
  363.         << "  rectangles: " << rectangles
  364.         << endl;
  365.     S = Shape::newShape();
  366.     unsigned i = sb.tallyAll(S);
  367.     cout << "A total of " << i << " "
  368.         << S->name() << "(s)"
  369.         << " were found"
  370.         << endl;
  371.     delete S;
  372.     cout << "Press <enter> to exit"  << endl;
  373.     (void) cin.get();
  374.     closeGraphics();
  375.     return 0;
  376. }
  377.