home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c017 / 34.ddi / CSTRM.ZIP / BDRDEM3.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-27  |  3.6 KB  |  181 lines

  1.     // bdrdem3.cpp
  2.     // Demo Binder with streamable nodes
  3.     // Redirect cout to a file for this demo!
  4.  
  5.     #include <fstream.h>
  6.     #include <iomanip.h>
  7.     #include <binder.hpp>
  8.  
  9.  
  10.     #define ID_StreamableInt 3
  11.     #define ID_SINT_CMP  1
  12.     #define ORIG_NODES   4
  13.  
  14.     class StreamableInt : Streamable  {
  15.         int i;
  16.     protected:
  17.         void construct(int i)
  18.             { this->i = i; }
  19.     public:
  20.         STREAMABLE(StreamableInt,
  21.             ID_StreamableInt,
  22.             Streamable);
  23.         StreamableInt(int i)
  24.             : Streamable(UNIQUE_STREAMABLE,
  25.             CLASS_ID)
  26.             { construct(i); }
  27.         int Int() { return i; }
  28.         ~StreamableInt() {}
  29.     };
  30.     typedef StreamableInt * StreamableInT;
  31.     #define StreamableInT0 ((StreamableInT)0)
  32.  
  33.  
  34.     ostream& StreamableInt::store(ostream& os)
  35.         { return os << i << endm; }
  36.  
  37.     StreamablE StreamableInt::load(istream& is,
  38.         StreamablE InstancE)
  39.     {
  40.         int i;
  41.  
  42.         if (!(is >> i >> nextm))  {
  43.             lserror("loading StreamableInt",
  44.                 CLASS_ID);
  45.             return StreamablE0;
  46.         }
  47.         if (!InstancE)
  48.             if ((InstancE =    (StreamablE)
  49.                 new StreamableInt
  50.                 (UNIQUE_STREAMABLE))
  51.                 == StreamablE0)  {
  52.                 lserror("unable to construct"
  53.                     " StreamableInt",
  54.                     CLASS_ID);
  55.                 return StreamablE0;
  56.             }
  57.         ((StreamableInT)InstancE)->construct(i);
  58.         return InstancE;
  59.  
  60.     }
  61.  
  62.     void StreamableInt::restream()
  63.         { Streamable::restream(); }
  64.  
  65.     int sintcmp(const StreamableInT I1,
  66.         const StreamableInT I2)
  67.         { return (I1->Int() - I2->Int()); }
  68.  
  69.     void display(StreamableInT I)
  70.     {
  71.         cout << "Address of node: "
  72.             << setw(6) << I
  73.             << "   Contents of node: "
  74.             << setw(6) << I->Int() << endl;
  75.     }
  76.  
  77.     main()
  78.     {
  79.         Binder::registerClass();
  80.         StreamableInt::registerClass();
  81.  
  82.         // When debugging redirect cout to a file
  83.         // so that you can examine the trace!
  84.         cerr = cout;
  85.         Streamable::debug = 1;
  86.         // Let's watch on cerr for stream errors!
  87.         Streamable::refDebug = 1;
  88.         // Let's watch the multiple linking -
  89.         // unlinking on cerr!
  90.  
  91.         Binder B(Binder::STREAMABLE_NODES,
  92.                 ORIG_NODES);
  93.  
  94.         int i = 0;
  95.  
  96.         while (B.vacancyNonElastic())
  97.             B.push(new StreamableInt(i++));
  98.  
  99.         B.setMaxNodes();
  100.         
  101.         cout << "\n\nBinder with streamable "
  102.             << "nodes and \nmultiple links "
  103.             << "to those nodes, unsorted:\n\n";
  104.  
  105.         i = 0;
  106.  
  107.         while (i < ORIG_NODES)
  108.  
  109.             B.atIns(ORIG_NODES,
  110.                 ((StreamableInT)B[i++])
  111.                     ->link());
  112.  
  113.             // Let the node know there is
  114.             // more than one link so that
  115.             // it only destructs after all
  116.             // references are removed!
  117.  
  118.             // Like pushing a stack
  119.             // at ORIG_NODES.
  120.  
  121.  
  122.         cout << endl << endl;
  123.  
  124.         B.forEach((BDRforEachBlocK)display);
  125.  
  126.         B.setComparE((BDRcomparE)sintcmp);
  127.  
  128.         RegisterFunction(ID_SINT_CMP,
  129.             (GenericFnC)sintcmp);
  130.  
  131.         ofstream oS("bdrdem3.txt");
  132.         if (oS)  {
  133.  
  134.           oS << (StreamablE) B;
  135.  
  136.           B.restream();
  137.  
  138.           // Don't stream B again
  139.           // without restreaming!!!
  140.  
  141.           oS.close();
  142.           ifstream iS("bdrdem3.txt");
  143.           if (iS)  {
  144.  
  145.             StreamablE C;
  146.             
  147.             cout << "\n\nStreamed and "
  148.             << "reloaded Binder with "
  149.             << "multiple links maintained "
  150.             << "\nand sorted with streamed "
  151.             << "compare fnc: \n\n";
  152.             
  153.             iS >> C;
  154.  
  155.             RestreamRegistry();
  156.  
  157.             // Don't load again from
  158.             // any stream with
  159.             // restreaming!!!
  160.  
  161.             if (C)
  162.             {
  163.               ((BindeR)C)->sort();
  164.               cout << endl << endl;
  165.               ((BindeR)C)->forEach(
  166.             (BDRforEachBlocK)display);
  167.               delete C;
  168.             }
  169.             else
  170.               cout << "\n\nUnable to reload"
  171.             << " Binder \n\n";
  172.           }
  173.           else
  174.             cout << "\n\nUnable to reopen"
  175.               << " stream for input of"
  176.               << " of Binder \n\n";
  177.         }
  178.  
  179.  
  180.         return 0;
  181.     }