home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / yacl-012.zip / basedemo / persist / perstest.cxx < prev    next >
C/C++ Source or Header  |  1994-10-14  |  5KB  |  221 lines

  1.  
  2.  
  3.  
  4. // A program to illustrate the use of persistent data structures in YACL.
  5. //
  6. // Author: M. A. Sridhar
  7. // Date:   May 22, 1994
  8.  
  9.  
  10.  
  11. #include <iostream.h>
  12. #include <stdlib.h>
  13.  
  14. #include "base/base.h"
  15. #include "io/io.h"
  16.  
  17.  
  18.  
  19.  
  20. // ----------------------------------------------------
  21. //                Define a small test class
  22. //-----------------------------------------------------
  23.  
  24. class TestClass: public CL_Object {
  25.  
  26. public:
  27.     TestClass (short i);
  28.  
  29.     TestClass () {};
  30.     
  31.     const CL_String& Data () const { return data; };
  32.  
  33.     CL_String AsString () const { return data; };
  34.     // Overrides CL_Object's virtual method
  35.     
  36.     virtual long StorableFormWidth () const
  37.         {return data.StorableFormWidth();};
  38.     // Overrides CL_Object's virtual method
  39.  
  40.     virtual bool ReadFrom (const CL_Stream& s);
  41.     // Overrides CL_Object's virtual method
  42.  
  43.     virtual bool WriteTo  (CL_Stream& s) const
  44.         {return s.Write (ClassId()) && data.WriteTo (s);};
  45.     // Overrides CL_Object's virtual method
  46.  
  47.     const char* ClassName () const {return "TestClass";};
  48.     // Overrides CL_Object's virtual method
  49.  
  50.     CL_ClassId ClassId() const {return 2000;};
  51.     
  52. protected:
  53.     CL_String data;
  54. };
  55.  
  56.  
  57. CL_DEFINE_CLASS(TestClass, 2000);
  58.     
  59. TestClass::TestClass (short i)
  60. {
  61.     data = "String: " + CL_String (i, 4, '0');
  62. }
  63.  
  64. bool TestClass::ReadFrom (const CL_Stream& s)
  65. {
  66.     register CL_ClassId id;
  67.     return ReadClassId (s) && data.ReadFrom (s);
  68. }
  69.  
  70.  
  71. //-----------------------------------------------------
  72. // Main program
  73. //-----------------------------------------------------
  74.  
  75. void ReadObjects  (const char* name);
  76. void WriteObjects (const char* name);
  77.  
  78. void main ()
  79. {
  80.     const char* name = "perstest.dat";
  81.     if (!CL_BinaryFile::Exists (name))
  82.         WriteObjects (name);
  83.     else
  84.         ReadObjects (name);
  85. }
  86.  
  87.  
  88. void WriteObjects (const char* name)
  89. {
  90.     CL_ObjectSequence obj_seq;
  91.     CL_BinaryFile file (name, TRUE);
  92.     long i;
  93.     
  94.     // First, create a sample sequence of objects
  95.     for (i = 0; i < 20; i++) {
  96.         obj_seq.Add (new TestClass (i*13 % 7));
  97.     }
  98.  
  99.     // Save the sequence
  100.     file << obj_seq;
  101.     obj_seq.DestroyContents ();
  102.  
  103.  
  104.     // Now create a set of strings
  105.     CL_StringSet some_set;
  106.     for (i = 0; i < 12; i++)
  107.         some_set.Add ("String in set: " + CL_String (i, 4, '$'));
  108.     file << some_set;
  109.     
  110.     // Next, create a few sample map objects
  111.     CL_IntPtrMap map1;
  112.     for (i = 0; i < 20; i++) {
  113.         long n = i*13 % 7;
  114.         TestClass* p = new TestClass (n);
  115.         if (!map1.Add (n, p))
  116.             delete p; // Guard against memory leaks
  117.     }
  118.  
  119.     CL_StringStringMap map2;
  120.     for (i = 0; i < 30; i++) {
  121.         long n = i*13 % 7;
  122.         map2.Add (CL_String (i), CL_String (n));
  123.     }
  124.  
  125.     // Save the maps
  126.     file << map1 << map2;
  127.     map1.DestroyContents ();
  128.     map2.DestroyContents ();
  129.  
  130.     {
  131.         CL_String* p1, *p2, *p3;
  132.         p1 = new CL_String ("String1");
  133.         p2 = new CL_String ("String2");
  134.         CL_ObjectSequence sq(5);
  135.         sq[0] = p2;
  136.         sq[1] = p1;
  137.         sq[2] = p2;
  138.         sq[3] = p2;
  139.         sq[4] = 0;
  140.         file.Remember ();  // Need to do this explicitly when saving
  141.                            // multi-linked structures
  142.         file << sq;
  143.         delete p1;
  144.         delete p2;
  145.         file.Forget ();
  146.     }
  147. }
  148.  
  149. void ReadObjects (const char* name)
  150. {
  151.     CL_ObjectSequence obj_seq;
  152.     CL_BinaryFile file (name);
  153.     long i;
  154.     
  155.     // Read the sequence from the file
  156.     file >> obj_seq; // Not necessarily equivalent to obj_seq.ReadFrom(file)
  157.     cout << "Contents of restored sequence:\n";
  158.     long n = obj_seq.Size();
  159.     for (i = 0; i < n; i++) {
  160.         TestClass* p = (TestClass*) obj_seq [i];
  161.         cout << p->Data().AsPtr() << endl;
  162.     }
  163.     obj_seq.DestroyContents (); // Without this, there's a memory leak!
  164.  
  165.     // Read back the set
  166.     CL_StringSet aSet;
  167.     file >> aSet;
  168.     n = aSet.Size();
  169.     cout << "Restored set has " << n << " elements:\n";
  170.     CL_StringSetIterator setIter (aSet);
  171.     for (setIter.Reset (); setIter.More(); ) {
  172.         cout << setIter.Next().AsPtr() << endl;
  173.     }
  174.  
  175.  
  176.     // And finally, read back the maps
  177.     CL_IntPtrMap map1;
  178.     CL_StringStringMap map2; // Note that there is no builder needed for
  179.                              // non-pointer-based maps
  180.     file >> map1 >> map2;
  181.  
  182.     cout << "Contents of restored map1:\n";
  183.     CL_IntPtrMapIterator itr1 (map1);
  184.     CL_IntPtrAssoc assoc1;
  185.     for (itr1.Reset(); itr1.More(); ) {
  186.         assoc1 = itr1.Next ();
  187.         cout << assoc1.key << " --> " << 
  188.                 ((TestClass*) assoc1.value)->Data().AsPtr() << endl;
  189.     }
  190.     map1.DestroyContents ();
  191.  
  192.  
  193.  
  194.     cout << "Contents of restored map2:\n";
  195. //     CL_StringStringMapIterator itr2 (map2);
  196. //     CL_StringStringAssoc assoc2;
  197. //     for (itr2.Reset(); itr2.More(); ) {
  198. //         assoc2 = itr2.Next ();
  199. //         printf ("'%s' --> '%s'\n", (const char*) assoc2.key,
  200. //                 (const char *) assoc2.value);
  201. //     }
  202.     cout << map2 << endl;
  203.  
  204.     {
  205.         CL_ObjectSequence sq;
  206.         file >> sq;
  207.         register long n = sq.Size();
  208.         cout << "sq Size: " << n << endl;
  209.         register long i;
  210.         for (i = 0; i < n; i++) {
  211.             CL_String* s = (CL_String*) sq[i];
  212.             cout <<  (s ? s->AsPtr() : "(NULL)") << endl;
  213.         }
  214.         delete sq[0];
  215.         delete sq[1];
  216.     }
  217. }
  218.  
  219.     
  220.  
  221.