home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / CSTREAM.ZIP / CSTREAM.HPP < prev   
C/C++ Source or Header  |  1991-05-28  |  2KB  |  112 lines

  1. /*
  2.  
  3.     cstream.hpp
  4.     5-28-91
  5.     class stream
  6.  
  7.     Copyright 1991
  8.     John W. Small
  9.     All rights reserved
  10.     Use freely but acknowledge authorship and copyright.
  11.     CIS: 73757,2233
  12.  
  13.  
  14.     PSW / Power SoftWare
  15.     P.O. Box 10072
  16.     McLean, Virginia 22102 8072
  17.     USA (703) 759-3838
  18.  
  19. */
  20.  
  21. #ifndef CSTREAM_HPP
  22. #define CSTREAM_HPP
  23.  
  24.  
  25. #include <iostream.h>
  26. #include <iomanip.h>
  27. #include <stdlib.h>
  28.  
  29. class StreamableClass;
  30. typedef StreamableClass *StreamC;
  31. #define StreamC0 ((StreamC)0)
  32.  
  33.  
  34. class StreamableClass  {
  35.     unsigned id;
  36. public:
  37. static  char FieldTermChar;
  38.     enum { CLASS_ID = 1 };
  39.     unsigned ID() { return id; }
  40.     operator StreamC() { return (StreamC)this; }
  41.     virtual void store(ostream& os);
  42.     static StreamC load(istream& is, StreamC C);
  43.     StreamableClass(unsigned id = CLASS_ID)
  44.         { this->id = id; }
  45.     virtual ~StreamableClass() {}
  46. };
  47.  
  48. extern ostream& endf(ostream& os);
  49.  
  50.  
  51. #pragma argsused
  52. inline void StreamableClass::store(ostream& os) {}
  53.  
  54. #pragma argsused
  55. inline StreamC StreamableClass::load(istream& is, StreamC C)
  56.     { return (C? C : new StreamableClass()); }
  57.  
  58. #define StreamableClassID(id)  \
  59.     enum { CLASS_ID = id };  \
  60.     StreamableClass::ID;          \
  61.     operator StreamC() { return (StreamC)this; }  \
  62.     virtual void store(ostream& os);             \
  63.     static StreamC load(istream& is, StreamC C)
  64.  
  65.  
  66.  
  67.  
  68. typedef struct {
  69.     unsigned id;
  70.     StreamC (*load)(istream& is, StreamC C);
  71. } StreamRecord;
  72.  
  73.  
  74. class StreamRegistry  {
  75. static StreamRecord recs[];
  76. static unsigned count, limit;
  77. public:
  78.     StreamRegistry()  { count = 0; }
  79.     void reg(unsigned id, StreamC (*loader)
  80.         (istream& is, StreamC C));
  81.     istream& get(istream& is, StreamC& C);
  82.     ostream& put(ostream& os, StreamC C);
  83.     virtual void error(char *msg, unsigned id)
  84.         { cerr << msg << id << endl; }
  85. };
  86.  
  87. extern StreamRegistry SR;
  88.  
  89. #define StreamableClasses(ClassCount)  \
  90.     StreamRecord StreamRegistry::recs[ClassCount]; \
  91.     unsigned StreamRegistry::count = 0; \
  92.     unsigned StreamRegistry::limit = ClassCount; \
  93.     StreamRegistry SR;
  94.  
  95. #define RegisterClass(ClassName)  \
  96.     SR.reg(ClassName::CLASS_ID, \
  97.     ClassName::load)
  98.  
  99.  
  100.  
  101. inline istream& operator>>(istream& is, StreamC& C)
  102. {
  103.     return SR.get(is,C);
  104. }
  105.  
  106. inline ostream& operator<<(ostream& os, StreamC C)
  107. {
  108.     return SR.put(os, C);
  109. }
  110.  
  111. #endif
  112.