home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tv20cpp.zip / tvision / tobjstrm.h < prev    next >
C/C++ Source or Header  |  1998-01-19  |  18KB  |  608 lines

  1. /*
  2.  * tobjstrm.h
  3.  *
  4.  * Turbo Vision - Version 2.0
  5.  *
  6.  * Copyright (c) 1994 by Borland International
  7.  * All Rights Reserved.
  8.  *
  9.  * Modified by Sergio Sigala <ssigala@globalnet.it>
  10.  */
  11.  
  12. typedef unsigned P_id_type;
  13.  
  14. /* ------------------------------------------------------------------------*/
  15. /*                                                                         */
  16. /*   class TStreamable                                                     */
  17. /*                                                                         */
  18. /*   This is the base class for all storable objects.  It provides         */
  19. /*   three member functions, streamableName(), read(), and write(), which  */
  20. /*   must be overridden in every derived class.                            */
  21. /*                                                                         */
  22. /* ------------------------------------------------------------------------*/
  23.  
  24. #if !defined( __fLink_def )
  25. #define __fLink_def
  26. struct fLink
  27. {
  28.     fLink *f;
  29.     class TStreamableClass *t;
  30. };
  31. #endif
  32.  
  33. #define __link( s )             \
  34.   extern TStreamableClass s;    \
  35.   static fLink force ## s =     \
  36.     { (fLink *)&force ## s, (TStreamableClass *)&s };
  37.  
  38. #if defined( Uses_TStreamable ) && !defined( __TStreamable )
  39. #define __TStreamable
  40.  
  41. class TStreamable
  42. {
  43.  
  44.     friend class opstream;
  45.     friend class ipstream;
  46.  
  47. private:
  48.  
  49.     virtual const char *streamableName() const = 0;
  50.  
  51. protected:
  52.  
  53.     virtual void *read( ipstream& ) = 0;
  54.     virtual void write( opstream& ) = 0;
  55.  
  56. };
  57.  
  58. #endif  // Uses_TStreamable
  59.  
  60. /* ------------------------------------------------------------------------*/
  61. /*                                                                         */
  62. /*   class TStreamableClass                                                */
  63. /*                                                                         */
  64. /*   Used internally by TStreamableTypes and pstream.                      */
  65. /*                                                                         */
  66. /* ------------------------------------------------------------------------*/
  67.  
  68. #if defined( Uses_TStreamableClass ) && !defined( __TStreamableClass )
  69. #define __TStreamableClass
  70.  
  71. #include <limits.h>
  72.  
  73. const P_id_type P_id_notFound = UINT_MAX;
  74.  
  75. typedef TStreamable *(*BUILDER)();
  76.  
  77. #define __DELTA( d ) ((int)(TStreamable*)(d*)1-1 )
  78.  
  79. class TStreamableClass
  80. {
  81.  
  82.     friend TStreamableTypes;
  83.     friend opstream;
  84.     friend ipstream;
  85.  
  86. public:
  87.  
  88.     TStreamableClass( const char *n, BUILDER b, int d );
  89.  
  90. private:
  91.  
  92.     const char *name;
  93.     BUILDER build;
  94.     int delta;
  95.  
  96. };
  97.  
  98. #endif  // Uses_TStreamableClass
  99.  
  100. /* ------------------------------------------------------------------------*/
  101. /*                                                                         */
  102. /*   class TStreamableTypes                                                */
  103. /*                                                                         */
  104. /*   Maintains a database of all registered types in the application.      */
  105. /*   Used by opstream and ipstream to find the functions to read and       */
  106. /*   write objects.                                                        */
  107. /*                                                                         */
  108. /* ------------------------------------------------------------------------*/
  109.  
  110. #if defined( Uses_TStreamableTypes ) && !defined( __TStreamableTypes )
  111. #define __TStreamableTypes
  112.  
  113. class TStreamableTypes : private TNSSortedCollection
  114. {
  115.  
  116. public:
  117.  
  118.     TStreamableTypes();
  119.     ~TStreamableTypes();
  120.  
  121.     void registerType( const TStreamableClass * );
  122.     const TStreamableClass *lookup( const char * );
  123.  
  124.     void *operator new( size_t sz ) { return ::operator new( sz ); }
  125.     void *operator new( size_t, void * );
  126.  
  127. private:
  128.  
  129.     virtual void *keyOf( void * );
  130.     int compare( void *, void * );
  131.  
  132. };
  133.  
  134. #endif  // Uses_TStreamableTypes
  135.  
  136. /* ------------------------------------------------------------------------*/
  137. /*                                                                         */
  138. /*   class TPWrittenObjects                                                */
  139. /*                                                                         */
  140. /*   Maintains a database of all objects that have been written to the     */
  141. /*   current object stream.                                                */
  142. /*                                                                         */
  143. /*   Used by opstream when it writes a pointer onto a stream to determine  */
  144. /*   whether the object pointed to has already been written to the stream. */
  145. /*                                                                         */
  146. /* ------------------------------------------------------------------------*/
  147.  
  148. #if defined( Uses_TPWrittenObjects ) && !defined( __TPWrittenObjects )
  149. #define __TPWrittenObjects
  150.  
  151. class TPWrittenObjects : public TNSSortedCollection
  152. {
  153.  
  154.     friend opstream;
  155.  
  156. public:
  157.  
  158.     void removeAll() { curId = 0; TNSSortedCollection::freeAll(); }
  159.  
  160. private:
  161.  
  162.     TPWrittenObjects();
  163.     ~TPWrittenObjects();
  164.  
  165.     void registerObject( const void *adr );
  166.     P_id_type find( const void *adr );
  167.  
  168.     void *keyOf( void * );
  169.     int compare( void *, void * );
  170.  
  171.     P_id_type curId;
  172.  
  173. };
  174.  
  175. /* ------------------------------------------------------------------------*/
  176. /*                                                                         */
  177. /*   class TPWObj                                                          */
  178. /*                                                                         */
  179. /*   Used internally by TPWrittenObjects.                                  */
  180. /*                                                                         */
  181. /* ------------------------------------------------------------------------*/
  182.  
  183. class TPWObj
  184. {
  185.  
  186.     friend TPWrittenObjects;
  187.  
  188. private:
  189.  
  190.     TPWObj( const void *adr, P_id_type id );
  191.  
  192.     const void *address;
  193.     P_id_type ident;
  194.  
  195. };
  196.  
  197. #endif  // Uses_TPWrittenObjects
  198.  
  199. /* ------------------------------------------------------------------------*/
  200. /*                                                                         */
  201. /*   class TPReadObjects                                                   */
  202. /*                                                                         */
  203. /*   Maintains a database of all objects that have been read from the      */
  204. /*   current persistent stream.                                            */
  205. /*                                                                         */
  206. /*   Used by ipstream when it reads a pointer from a stream to determine   */
  207. /*   the address of the object being referred to.                          */
  208. /*                                                                         */
  209. /* ------------------------------------------------------------------------*/
  210.  
  211. #if defined( Uses_TPReadObjects ) && !defined( __TPReadObjects )
  212. #define __TPReadObjects
  213.  
  214. class TPReadObjects : public TNSCollection
  215. {
  216.  
  217.     friend ipstream;
  218.  
  219. public:
  220.  
  221.     void removeAll() { curId = 0; TNSCollection::removeAll(); }
  222.  
  223. private:
  224.  
  225.     TPReadObjects();
  226.     ~TPReadObjects();
  227.  
  228.     void registerObject( const void *adr );
  229.     const void *find( P_id_type id );
  230.  
  231.     P_id_type curId;
  232.  
  233. };
  234.  
  235. #endif  // Uses_TPReadObjects
  236.  
  237. /* ------------------------------------------------------------------------*/
  238. /*                                                                         */
  239. /*   class pstream                                                         */
  240. /*                                                                         */
  241. /*   Base class for handling streamable objects.                           */
  242. /*                                                                         */
  243. /* ------------------------------------------------------------------------*/
  244.  
  245. #if defined( Uses_pstream ) && !defined( __pstream )
  246. #define __pstream
  247.  
  248. #include <iostream.h>
  249.  
  250. class TStreamableTypes;
  251.  
  252. class pstream
  253. {
  254.  
  255.     friend TStreamableTypes;
  256.  
  257. public:
  258.  
  259.     enum StreamableError { peNotRegistered, peInvalidType };
  260.     enum PointerTypes { ptNull, ptIndexed, ptObject };
  261.  
  262.     pstream( streambuf * );
  263.     virtual ~pstream();
  264.  
  265.     int rdstate() const;
  266.     int eof() const;
  267.     int fail() const;
  268.     int bad() const;
  269.     int good() const;
  270.     void clear( int = 0 );
  271.     operator void *() const;
  272.     int operator ! () const;
  273.  
  274.     streambuf * rdbuf() const;
  275.  
  276.     static void initTypes();
  277.  
  278.     void error( StreamableError );
  279.     void error( StreamableError, const TStreamable& );
  280.     static void registerType( TStreamableClass *ts );
  281.  
  282. protected:
  283.  
  284.     pstream();
  285.  
  286.     streambuf *bp;
  287.     int state;
  288.  
  289.     void init( streambuf * );
  290.     void setstate( int );
  291.  
  292.     static TStreamableTypes * types;
  293.  
  294. };
  295.  
  296. #endif  // Uses_pstream
  297.  
  298. /* ------------------------------------------------------------------------*/
  299. /*                                                                         */
  300. /*   class ipstream                                                        */
  301. /*                                                                         */
  302. /*   Base class for reading streamable objects                             */
  303. /*                                                                         */
  304. /* ------------------------------------------------------------------------*/
  305.  
  306. #if defined( Uses_ipstream ) && !defined( __ipstream )
  307. #define __ipstream
  308.  
  309. #include <iostream.h>
  310.  
  311. class TStreamableClass;
  312.  
  313. class ipstream : virtual public pstream
  314. {
  315.  
  316. public:
  317.  
  318.     ipstream( streambuf * );
  319.     ~ipstream();
  320.  
  321.     streampos tellg();
  322.     ipstream& seekg( streampos );
  323.     ipstream& seekg( streamoff, ios::seek_dir );
  324.  
  325.     uchar readByte();
  326.     void readBytes( void *, size_t );
  327.     ushort readWord();
  328.     char * readString();
  329.     char * readString( char *, unsigned );
  330.  
  331.     friend ipstream& operator >> ( ipstream&, char& );
  332.     friend ipstream& operator >> ( ipstream&, signed char& );
  333.     friend ipstream& operator >> ( ipstream&, unsigned char& );
  334.     friend ipstream& operator >> ( ipstream&, signed short& );
  335.     friend ipstream& operator >> ( ipstream&, unsigned short& );
  336.     friend ipstream& operator >> ( ipstream&, signed int& );
  337.     friend ipstream& operator >> ( ipstream&, unsigned int& );
  338.     friend ipstream& operator >> ( ipstream&, signed long& );
  339.     friend ipstream& operator >> ( ipstream&, unsigned long& );
  340.     friend ipstream& operator >> ( ipstream&, float& );
  341.     friend ipstream& operator >> ( ipstream&, double& );
  342.     friend ipstream& operator >> ( ipstream&, long double& );
  343.  
  344.     friend ipstream& operator >> ( ipstream&, TStreamable& );
  345.     friend ipstream& operator >> ( ipstream&, void *& );
  346.  
  347. protected:
  348.  
  349.     ipstream();
  350.  
  351.     const TStreamableClass * readPrefix();
  352.     void * readData( const TStreamableClass *,
  353.                                         TStreamable * );
  354.     void readSuffix();
  355.  
  356.     const void * find( P_id_type );
  357.     void registerObject( const void *adr );
  358.  
  359. private:
  360.  
  361.     TPReadObjects objs;
  362.  
  363. };
  364.  
  365. #endif  // Uses_ipstream
  366.  
  367. /* ------------------------------------------------------------------------*/
  368. /*                                                                         */
  369. /*   class opstream                                                        */
  370. /*                                                                         */
  371. /*   Base class for writing streamable objects                             */
  372. /*                                                                         */
  373. /* ------------------------------------------------------------------------*/
  374.  
  375. #if defined( Uses_opstream ) && !defined( __opstream )
  376. #define __opstream
  377.  
  378. #include <iostream.h>
  379.  
  380. class TStreamableClass;
  381.  
  382. class opstream : virtual public pstream
  383. {
  384.  
  385. public:
  386.  
  387.     opstream( streambuf * );
  388.     ~opstream();
  389.  
  390.     streampos tellp();
  391.     opstream& seekp( streampos );
  392.     opstream& seekp( streamoff, ios::seek_dir );
  393.     opstream& flush();
  394.  
  395.     void writeByte( uchar );
  396.     void writeBytes( const void *, size_t );
  397.     void writeWord( ushort );
  398.     void writeString( const char * );
  399.  
  400.     friend opstream& operator << ( opstream&, char );
  401.     friend opstream& operator << ( opstream&, signed char );
  402.     friend opstream& operator << ( opstream&, unsigned char );
  403.     friend opstream& operator << ( opstream&, signed short );
  404.     friend opstream& operator << ( opstream&, unsigned short );
  405.     friend opstream& operator << ( opstream&, signed int );
  406.     friend opstream& operator << ( opstream&, unsigned int );
  407.     friend opstream& operator << ( opstream&, signed long );
  408.     friend opstream& operator << ( opstream&, unsigned long );
  409.     friend opstream& operator << ( opstream&, float );
  410.     friend opstream& operator << ( opstream&, double );
  411.     friend opstream& operator << ( opstream&, long double );
  412.  
  413.     friend opstream& operator << ( opstream&, TStreamable& );
  414.     friend opstream& operator << ( opstream&, TStreamable * );
  415.  
  416. protected:
  417.  
  418.     opstream();
  419.  
  420.     void writePrefix( const TStreamable& );
  421.     void writeData( TStreamable& );
  422.     void writeSuffix( const TStreamable& );
  423.  
  424.     P_id_type find( const void *adr );
  425.     void registerObject( const void *adr );
  426.  
  427. private:
  428.  
  429.     TPWrittenObjects *objs;
  430.  
  431. };
  432.  
  433. #endif  // Uses_opstream
  434.  
  435. /* ------------------------------------------------------------------------*/
  436. /*                                                                         */
  437. /*   class iopstream                                                       */
  438. /*                                                                         */
  439. /*   Base class for reading and writing streamable objects                 */
  440. /*                                                                         */
  441. /* ------------------------------------------------------------------------*/
  442.  
  443. #if defined( Uses_iopstream ) && !defined( __iopstream )
  444. #define __iopstream
  445.  
  446. #include <iostream.h>
  447.  
  448. class iopstream : public ipstream, public opstream
  449. {
  450.  
  451. public:
  452.  
  453.     iopstream( streambuf * );
  454.     ~iopstream();
  455.  
  456. protected:
  457.  
  458.     iopstream();
  459.  
  460. };
  461.  
  462. #endif  // Uses_iopstream
  463.  
  464. /* ------------------------------------------------------------------------*/
  465. /*                                                                         */
  466. /*   class fpbase                                                          */
  467. /*                                                                         */
  468. /*   Base class for handling streamable objects on file streams            */
  469. /*                                                                         */
  470. /* ------------------------------------------------------------------------*/
  471.  
  472. #if defined( Uses_fpbase ) && !defined( __fpbase )
  473. #define __fpbase
  474.  
  475. #include <fstream.h>
  476.  
  477. class fpbase : virtual public pstream
  478. {
  479.  
  480. public:
  481.  
  482.     fpbase();
  483.     fpbase( const char *, int, int = filebuf::openprot );
  484.     fpbase( int );
  485.     fpbase( int, char *, int );
  486.     ~fpbase();
  487.  
  488.     void open( const char *, int, int = filebuf::openprot );
  489.     void attach( int );
  490.     void close();
  491.     void setbuf( char *, int );
  492.     filebuf * rdbuf();
  493.  
  494. private:
  495.  
  496.     filebuf buf;
  497.  
  498. };
  499.  
  500. #endif  // Uses_fpbase
  501.  
  502. /* ------------------------------------------------------------------------*/
  503. /*                                                                         */
  504. /*   class ifpstream                                                       */
  505. /*                                                                         */
  506. /*   Base class for reading streamable objects from file streams           */
  507. /*                                                                         */
  508. /* ------------------------------------------------------------------------*/
  509.  
  510. #if defined( Uses_ifpstream ) && !defined( __ifpstream )
  511. #define __ifpstream
  512.  
  513. #include <iostream.h>
  514.  
  515. class ifpstream : public fpbase, public ipstream
  516. {
  517.  
  518. public:
  519.  
  520.     ifpstream();
  521.     ifpstream( const char *,
  522.                       int = ios::in,
  523.                       int = filebuf::openprot
  524.                     );
  525.     ifpstream( int );
  526.     ifpstream( int, char *, int );
  527.     ~ifpstream();
  528.  
  529.     filebuf * rdbuf();
  530.     void open( const char *,
  531.                       int = ios::in,
  532.                       int = filebuf::openprot
  533.                     );
  534.  
  535. };
  536.  
  537. #endif  // Uses_ifpstream
  538.  
  539. /* ------------------------------------------------------------------------*/
  540. /*                                                                         */
  541. /*   class ofpstream                                                       */
  542. /*                                                                         */
  543. /*   Base class for writing streamable objects to file streams             */
  544. /*                                                                         */
  545. /* ------------------------------------------------------------------------*/
  546.  
  547. #if defined( Uses_ofpstream ) && !defined( __ofpstream )
  548. #define __ofpstream
  549.  
  550. #include <iostream.h>
  551.  
  552. class ofpstream : public fpbase, public opstream
  553. {
  554.  
  555. public:
  556.  
  557.     ofpstream();
  558.     ofpstream( const char *,
  559.                       int = ios::out,
  560.                       int = filebuf::openprot
  561.                     );
  562.     ofpstream( int );
  563.     ofpstream( int, char *, int );
  564.     ~ofpstream();
  565.  
  566.     filebuf * rdbuf();
  567.     void open( const char *,
  568.                       int = ios::out,
  569.                       int = filebuf::openprot
  570.                     );
  571.  
  572. };
  573.  
  574. #endif  // Uses_ofpstream
  575.  
  576. /* ------------------------------------------------------------------------*/
  577. /*                                                                         */
  578. /*   class fpstream                                                        */
  579. /*                                                                         */
  580. /*   Base class for reading and writing streamable objects to              */
  581. /*   bidirectional file streams                                            */
  582. /*                                                                         */
  583. /* ------------------------------------------------------------------------*/
  584.  
  585. #if defined( Uses_fpstream ) && !defined( __fpstream )
  586. #define __fpstream
  587.  
  588. #include <iostream.h>
  589.  
  590. class fpstream : public fpbase, public iopstream
  591. {
  592.  
  593. public:
  594.  
  595.     fpstream();
  596.     fpstream( const char *, int, int = filebuf::openprot );
  597.     fpstream( int );
  598.     fpstream( int, char *, int );
  599.     ~fpstream();
  600.  
  601.     filebuf * rdbuf();
  602.     void open( const char *, int, int = filebuf::openprot );
  603.  
  604. };
  605.  
  606.  
  607. #endif  // Uses_fpstream
  608.