Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

tobjstrm.h

Go to the documentation of this file.
00001 /*
00002  * tobjstrm.h
00003  *
00004  * Turbo Vision - Version 2.0
00005  *
00006  * Copyright (c) 1994 by Borland International
00007  * All Rights Reserved.
00008  *
00009  * Modified by Sergio Sigala <sergio@sigala.it>
00010  */
00011 
00019 typedef unsigned P_id_type;
00020 
00021 /* ------------------------------------------------------------------------*/
00022 /*                                                                         */
00023 /*   class TStreamable                                                     */
00024 /*                                                                         */
00025 /*   This is the base class for all storable objects.  It provides         */
00026 /*   three member functions, streamableName(), read(), and write(), which  */
00027 /*   must be overridden in every derived class.                            */
00028 /*                                                                         */
00029 /* ------------------------------------------------------------------------*/
00030 
00031 #if !defined( __fLink_def )
00032 #define __fLink_def
00033 
00037 struct fLink
00038 {
00042     fLink *f;
00046     class TStreamableClass *t;
00047 };
00048 #endif
00049 
00050 #define __link( s )             \
00051   extern TStreamableClass s;    \
00052   static fLink force ## s =     \
00053     { (fLink *)&force ## s, (TStreamableClass *)&s };
00054 
00055 #if defined( Uses_TStreamable ) && !defined( __TStreamable )
00056 #define __TStreamable
00057 
00080 class TStreamable
00081 {
00082     friend class opstream;
00083     friend class ipstream;
00084 private:
00095     virtual const char *streamableName() const = 0;
00096 protected:
00105     virtual void *read( ipstream& is ) = 0;
00114     virtual void write( opstream& os ) = 0;
00115 };
00116 
00117 #endif  // Uses_TStreamable
00118 
00119 /* ------------------------------------------------------------------------*/
00120 /*                                                                         */
00121 /*   class TStreamableClass                                                */
00122 /*                                                                         */
00123 /*   Used internally by TStreamableTypes and pstream.                      */
00124 /*                                                                         */
00125 /* ------------------------------------------------------------------------*/
00126 
00127 #if defined( Uses_TStreamableClass ) && !defined( __TStreamableClass )
00128 #define __TStreamableClass
00129 
00130 #include <limits.h>
00131 
00135 const P_id_type P_id_notFound = UINT_MAX;
00136 
00145 typedef TStreamable *(*BUILDER)();
00146 
00147 #define __DELTA( d ) ((int)(TStreamable*)(d*)1-1 )
00148 
00155 class TStreamableClass
00156 {
00157     friend TStreamableTypes;
00158     friend opstream;
00159     friend ipstream;
00160 public:
00187     TStreamableClass( const char *aName, BUILDER aBuild, int aDelta );
00188 private:
00189     const char *name;
00190     BUILDER build;
00191     int delta;
00192 };
00193 
00194 #endif  // Uses_TStreamableClass
00195 
00196 /* ------------------------------------------------------------------------*/
00197 /*                                                                         */
00198 /*   class TStreamableTypes                                                */
00199 /*                                                                         */
00200 /*   Maintains a database of all registered types in the application.      */
00201 /*   Used by opstream and ipstream to find the functions to read and       */
00202 /*   write objects.                                                        */
00203 /*                                                                         */
00204 /* ------------------------------------------------------------------------*/
00205 
00206 #if defined( Uses_TStreamableTypes ) && !defined( __TStreamableTypes )
00207 #define __TStreamableTypes
00208 
00221 class TStreamableTypes : private TNSSortedCollection
00222 {
00223 public:
00229     TStreamableTypes();
00234     ~TStreamableTypes();
00238     void registerType( const TStreamableClass *d );
00243     const TStreamableClass *lookup( const char *name );
00247     void *operator new( size_t sz ) { return ::operator new( sz ); }
00251     void *operator new( size_t, void * );
00252 private:
00256     virtual void *keyOf( void * );
00260     int compare( void *, void * );
00261 };
00262 
00263 #endif  // Uses_TStreamableTypes
00264 
00265 /* ------------------------------------------------------------------------*/
00266 /*                                                                         */
00267 /*   class TPWrittenObjects                                                */
00268 /*                                                                         */
00269 /*   Maintains a database of all objects that have been written to the     */
00270 /*   current object stream.                                                */
00271 /*                                                                         */
00272 /*   Used by opstream when it writes a pointer onto a stream to determine  */
00273 /*   whether the object pointed to has already been written to the stream. */
00274 /*                                                                         */
00275 /* ------------------------------------------------------------------------*/
00276 
00277 #if defined( Uses_TPWrittenObjects ) && !defined( __TPWrittenObjects )
00278 #define __TPWrittenObjects
00279 
00287 class TPWrittenObjects : public TNSSortedCollection
00288 {
00289     friend opstream;
00290 public:
00294     void removeAll() { curId = 0; TNSSortedCollection::freeAll(); }
00295 private:
00301     TPWrittenObjects();
00305     ~TPWrittenObjects();
00309     void registerObject( const void *adr );
00313     P_id_type find( const void *adr );
00317     void *keyOf( void * );
00321     int compare( void *, void * );
00325     P_id_type curId;
00326 };
00327 
00328 /* ------------------------------------------------------------------------*/
00329 /*                                                                         */
00330 /*   class TPWObj                                                          */
00331 /*                                                                         */
00332 /*   Used internally by TPWrittenObjects.                                  */
00333 /*                                                                         */
00334 /* ------------------------------------------------------------------------*/
00335 
00342 class TPWObj
00343 {
00344     friend TPWrittenObjects;
00345 private:
00346     TPWObj( const void *adr, P_id_type id );
00347     const void *address;
00348     P_id_type ident;
00349 };
00350 
00351 #endif  // Uses_TPWrittenObjects
00352 
00353 /* ------------------------------------------------------------------------*/
00354 /*                                                                         */
00355 /*   class TPReadObjects                                                   */
00356 /*                                                                         */
00357 /*   Maintains a database of all objects that have been read from the      */
00358 /*   current persistent stream.                                            */
00359 /*                                                                         */
00360 /*   Used by ipstream when it reads a pointer from a stream to determine   */
00361 /*   the address of the object being referred to.                          */
00362 /*                                                                         */
00363 /* ------------------------------------------------------------------------*/
00364 
00365 #if defined( Uses_TPReadObjects ) && !defined( __TPReadObjects )
00366 #define __TPReadObjects
00367 
00376 class TPReadObjects : public TNSCollection
00377 {
00378     friend ipstream;
00379 public:
00383     void removeAll() { curId = 0; TNSCollection::removeAll(); }
00384 private:
00391     TPReadObjects();
00395     ~TPReadObjects();
00396     void registerObject( const void *adr );
00397     const void *find( P_id_type id );
00398     P_id_type curId;
00399 };
00400 
00401 #endif  // Uses_TPReadObjects
00402 
00403 /* ------------------------------------------------------------------------*/
00404 /*                                                                         */
00405 /*   class pstream                                                         */
00406 /*                                                                         */
00407 /*   Base class for handling streamable objects.                           */
00408 /*                                                                         */
00409 /* ------------------------------------------------------------------------*/
00410 
00411 #if defined( Uses_pstream ) && !defined( __pstream )
00412 #define __pstream
00413 
00414 class streambuf;
00415 
00416 #include <iostream.h>
00417 
00418 class TStreamableTypes;
00419 
00424 class pstream
00425 {
00426     friend TStreamableTypes;
00427 public:
00431     enum StreamableError { peNotRegistered, peInvalidType };
00435     enum PointerTypes { ptNull, ptIndexed, ptObject };
00440     pstream( streambuf *buf );
00444     virtual ~pstream();
00448     int rdstate() const;
00452     int eof() const;
00456     int fail() const;
00460     int bad() const;
00464     int good() const;
00469     void clear( int sState = 0 );
00476     operator void *() const;
00480     int operator ! () const;
00484     streambuf * rdbuf() const;
00489     static void initTypes();
00498     void error( StreamableError );
00507     void error( StreamableError, const TStreamable& );
00511     static void registerType( TStreamableClass *ts );
00512 protected:
00516     pstream();
00520     streambuf *bp;
00525     int state;
00529     void init( streambuf *sbp );
00533     void setstate( int b );
00538     static TStreamableTypes * types;
00539 };
00540 
00541 #endif  // Uses_pstream
00542 
00543 /* ------------------------------------------------------------------------*/
00544 /*                                                                         */
00545 /*   class ipstream                                                        */
00546 /*                                                                         */
00547 /*   Base class for reading streamable objects                             */
00548 /*                                                                         */
00549 /* ------------------------------------------------------------------------*/
00550 
00551 #if defined( Uses_ipstream ) && !defined( __ipstream )
00552 #define __ipstream
00553 
00554 #include <iostream.h>
00555 
00556 class TStreamableClass;
00557 
00573 class ipstream : virtual public pstream
00574 {
00575 public:
00581     ipstream( streambuf *buf );
00585     ~ipstream();
00589     streampos tellg();
00594     ipstream& seekg( streampos pos );
00607     ipstream& seekg( streamoff off, ios::seek_dir dir );
00611     uchar readByte();
00616     void readBytes( void *data, size_t sz );
00620     ushort readWord();
00624     char * readString();
00628     char * readString( char *buf, unsigned maxLen );
00632     friend ipstream& operator >> ( ipstream&, char& );
00636     friend ipstream& operator >> ( ipstream&, signed char& );
00640     friend ipstream& operator >> ( ipstream&, unsigned char& );
00644     friend ipstream& operator >> ( ipstream&, signed short& );
00648     friend ipstream& operator >> ( ipstream&, unsigned short& );
00652     friend ipstream& operator >> ( ipstream&, signed int& );
00656     friend ipstream& operator >> ( ipstream&, unsigned int& );
00660     friend ipstream& operator >> ( ipstream&, signed long& );
00664     friend ipstream& operator >> ( ipstream&, unsigned long& );
00668     friend ipstream& operator >> ( ipstream&, float& );
00672     friend ipstream& operator >> ( ipstream&, double& );
00676     friend ipstream& operator >> ( ipstream&, long double& );
00680     friend ipstream& operator >> ( ipstream&, TStreamable& );
00684     friend ipstream& operator >> ( ipstream&, void *& );
00685 protected:
00689     ipstream();
00694     const TStreamableClass * readPrefix();
00700     void * readData( const TStreamableClass *c, TStreamable *mem );
00704     void readSuffix();
00708     const void * find( P_id_type id );
00712     void registerObject( const void *adr );
00713 private:
00714     TPReadObjects objs;
00715 
00716 };
00717 
00718 #endif  // Uses_ipstream
00719 
00720 /* ------------------------------------------------------------------------*/
00721 /*                                                                         */
00722 /*   class opstream                                                        */
00723 /*                                                                         */
00724 /*   Base class for writing streamable objects                             */
00725 /*                                                                         */
00726 /* ------------------------------------------------------------------------*/
00727 
00728 #if defined( Uses_opstream ) && !defined( __opstream )
00729 #define __opstream
00730 
00731 #include <iostream.h>
00732 
00733 class TStreamableClass;
00734 
00750 class opstream : virtual public pstream
00751 {
00752 public:
00758     opstream( streambuf *buf );
00762     ~opstream();
00766     streampos tellp();
00771     opstream& seekp( streampos pos );
00784     opstream& seekp( streamoff off, ios::seek_dir dir );
00788     opstream& flush();
00792     void writeByte( uchar ch );
00796     void writeBytes( const void *data, size_t sz );
00800     void writeWord( ushort us );
00804     void writeString( const char *str );
00808     friend opstream& operator << ( opstream&, char );
00812     friend opstream& operator << ( opstream&, signed char );
00816     friend opstream& operator << ( opstream&, unsigned char );
00820     friend opstream& operator << ( opstream&, signed short );
00824     friend opstream& operator << ( opstream&, unsigned short );
00828     friend opstream& operator << ( opstream&, signed int );
00832     friend opstream& operator << ( opstream&, unsigned int );
00836     friend opstream& operator << ( opstream&, signed long );
00840     friend opstream& operator << ( opstream&, unsigned long );
00844     friend opstream& operator << ( opstream&, float );
00848     friend opstream& operator << ( opstream&, double );
00852     friend opstream& operator << ( opstream&, long double );
00856     friend opstream& operator << ( opstream&, TStreamable& );
00860     friend opstream& operator << ( opstream&, TStreamable * );
00861 protected:
00865     opstream();
00873     void writePrefix( const TStreamable& );
00878     void writeData( TStreamable& );
00886     void writeSuffix( const TStreamable& );
00890     P_id_type find( const void *adr );
00894     void registerObject( const void *adr );
00895 private:
00896     TPWrittenObjects *objs;
00897 };
00898 
00899 #endif  // Uses_opstream
00900 
00901 /* ------------------------------------------------------------------------*/
00902 /*                                                                         */
00903 /*   class iopstream                                                       */
00904 /*                                                                         */
00905 /*   Base class for reading and writing streamable objects                 */
00906 /*                                                                         */
00907 /* ------------------------------------------------------------------------*/
00908 
00909 #if defined( Uses_iopstream ) && !defined( __iopstream )
00910 #define __iopstream
00911 
00912 #include <iostream.h>
00913 
00921 class iopstream : public ipstream, public opstream
00922 {
00923 public:
00928     iopstream( streambuf *buf );
00932     ~iopstream();
00933 protected:
00937     iopstream();
00938 };
00939 
00940 #endif  // Uses_iopstream
00941 
00942 /* ------------------------------------------------------------------------*/
00943 /*                                                                         */
00944 /*   class fpbase                                                          */
00945 /*                                                                         */
00946 /*   Base class for handling streamable objects on file streams            */
00947 /*                                                                         */
00948 /* ------------------------------------------------------------------------*/
00949 
00950 #if defined( Uses_fpbase ) && !defined( __fpbase )
00951 #define __fpbase
00952 
00953 #include <fstream.h>
00954 
00959 class fpbase : virtual public pstream
00960 {
00961 public:
00965     fpbase();
00971     fpbase( const char *name, int omode, int prot = filebuf::openprot );
00976     fpbase( int f );
00983     fpbase( int f, char *b, int len);
00987     ~fpbase();
00993     void open( const char *name, int omode, int prot = filebuf::openprot );
00997     void attach( int f );
01001     void close();
01005     void setbuf( char *buf, int len );
01009     filebuf * rdbuf();
01010 private:
01011     filebuf buf;
01012 };
01013 
01014 #endif  // Uses_fpbase
01015 
01016 /* ------------------------------------------------------------------------*/
01017 /*                                                                         */
01018 /*   class ifpstream                                                       */
01019 /*                                                                         */
01020 /*   Base class for reading streamable objects from file streams           */
01021 /*                                                                         */
01022 /* ------------------------------------------------------------------------*/
01023 
01024 #if defined( Uses_ifpstream ) && !defined( __ifpstream )
01025 #define __ifpstream
01026 
01027 #include <iostream.h>
01028 
01036 class ifpstream : public fpbase, public ipstream
01037 {
01038 public:
01042     ifpstream();
01048     ifpstream(const char *name, int omode = ios::in,
01049         int prot = filebuf::openprot );
01054     ifpstream( int f );
01061     ifpstream( int f, char *b, int len );
01065     ~ifpstream();
01069     filebuf * rdbuf();
01076     void open( const char *name, int omode = ios::in,
01077         int prot = filebuf::openprot );
01078 };
01079 
01080 #endif  // Uses_ifpstream
01081 
01082 /* ------------------------------------------------------------------------*/
01083 /*                                                                         */
01084 /*   class ofpstream                                                       */
01085 /*                                                                         */
01086 /*   Base class for writing streamable objects to file streams             */
01087 /*                                                                         */
01088 /* ------------------------------------------------------------------------*/
01089 
01090 #if defined( Uses_ofpstream ) && !defined( __ofpstream )
01091 #define __ofpstream
01092 
01093 #include <iostream.h>
01094 
01102 class ofpstream : public fpbase, public opstream
01103 {
01104 public:
01108     ofpstream();
01114     ofpstream( const char *name, int omode = ios::out, int prot =
01115         filebuf::openprot );
01120     ofpstream( int f );
01127     ofpstream( int f, char *b, int len );
01131     ~ofpstream();
01135     filebuf * rdbuf();
01142     void open( const char *name, int omode = ios::out,
01143         int prot = filebuf::openprot );
01144 };
01145 
01146 #endif  // Uses_ofpstream
01147 
01148 /* ------------------------------------------------------------------------*/
01149 /*                                                                         */
01150 /*   class fpstream                                                        */
01151 /*                                                                         */
01152 /*   Base class for reading and writing streamable objects to              */
01153 /*   bidirectional file streams                                            */
01154 /*                                                                         */
01155 /* ------------------------------------------------------------------------*/
01156 
01157 #if defined( Uses_fpstream ) && !defined( __fpstream )
01158 #define __fpstream
01159 
01160 #include <iostream.h>
01161 
01170 class fpstream : public fpbase, public iopstream
01171 {
01172 public:
01176     fpstream();
01182     fpstream( const char *name, int omode, int prot = filebuf::openprot );
01187     fpstream( int f );
01194     fpstream( int f, char *b, int len );
01198     ~fpstream();
01202     filebuf * rdbuf();
01208     void open( const char *name, int omode, int prot = filebuf::openprot );
01209 };
01210 
01211 #endif  // Uses_fpstream

Generated at Sat Sep 22 20:19:12 2001 for TVision by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001