home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_144 / 2.ddi / TVINC.ZIP / TOBJSTRM.H < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  21.3 KB  |  683 lines

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