Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

stream.H

Go to the documentation of this file.
00001 #ifndef STD_STREAM_HAS_BEEN_INCLUDED
00002 #define STD_STREAM_HAS_BEEN_INCLUDED
00003 
00004 #include "std/support.H"
00005 #include "mem_push.H"
00006 
00007 /* --------------------------    Constants     ----------------------------- */
00008 
00009 /* --------------------------      Types       ----------------------------- */
00010 
00011 /*
00012  * TITLE   :    Data Stream Class
00013  * DESCR   :   This class provides safe data transfer for objects and
00014  *       non-object data with a syntax similar to that of the
00015  *       standard C++ iostreams.  The simple non-object data types
00016  *       have pack and unpack methods defined for them with the
00017  *       appropriate conversions built in to avoid byte-order
00018  *       problems and ensure architecture independence.  Objects
00019  *       that define their own pack and unpack methods should use
00020  *       these primitive routines.
00021  */
00022 class STDdstream {
00023  public:
00024    STDdstream();
00025 
00026    // New policy: 5/2004:
00027    //   The STDdstream either contains an iostream, 
00028    //   or it contains just an istream or just an ostream.
00029    STDdstream(iostream* s);     // has both
00030    STDdstream( istream* i);     // has just istream
00031    STDdstream( ostream* o);     // has just ostream
00032 
00033    virtual ~STDdstream ()  {}
00034 
00035    void    write(const char * const data, int count);
00036    void    read (     UGAptr data, int count, int pop = 1);
00037    void    flush();
00038 
00039    iostream* iostr() const { return _iostream; }
00040    istream*  istr()  const { return _iostream ? (istream*)_iostream : _istream; }
00041    ostream*  ostr()  const { return _iostream ? (ostream*)_iostream : _ostream; }
00042 
00043    int     ascii() const { return istr() || ostr(); }
00044 
00045    char    peekahead();
00046    void    read_open_delim();
00047    void    read_close_delim();
00048 
00049    void    write_open_delim()         { write_delim('{');if(ascii()) _indent++;}
00050    void    write_close_delim()        { write_delim('}');if(ascii()) _indent--;}
00051    void    write_newline()            { if (ascii()) {ws("\n");
00052                                         for (int i=0;i<_indent;i++) ws("\t"); }}
00053    void    write_delim(char);
00054    char    read_delim();
00055 
00056    int     check() { char brace = peekahead(); return brace == '}' ? 0 : 1; }
00057    int     eof() const { return istr() ? istr()->eof(): !_in_queue.count(); }
00058 
00059    void    ws(char *);
00060    STDbool operator ! ()        const { return _fail; }
00061    STDbool fail       ()        const { return _fail; }
00062    STDbool block      ()        const { return _block; }
00063    STDbool block(STDbool flag)        { STDbool old=_block; _block = flag; return old; }
00064    // Get a string with spaces in it - the same as STDdstream >> str_ptr when
00065    // using a binary stream, but different when doing ascii
00066    str_ptr get_string_with_spaces();
00067 
00068  protected:
00069 
00070    virtual size_t send (const UGAptr data, int count) { _in_queue.put(data,count); return count; }
00071    virtual size_t recv (UGAptr data, int count)       { return _out_queue.get (data, count); }
00072 
00073    STDmem_queue _in_queue;
00074    STDmem_queue _out_queue;
00075    iostream*    _iostream;      // iostream, may be null
00076    istream*     _istream;       // just an in stream (used when iostream is null)
00077    ostream*     _ostream;       // just an out stream (used when iostream is null)
00078    int          _indent;
00079 
00080  private:
00081    STDbool      _fail;
00082    STDbool      _block;
00083 
00084  //So _fail can get set by ascii IO...
00085    friend STDdstream & operator >> (STDdstream &, char &);
00086    friend STDdstream & operator >> (STDdstream &, char *&);
00087    friend STDdstream & operator >> (STDdstream &, str_ptr&);
00088    friend STDdstream & operator >> (STDdstream &, short &);
00089    friend STDdstream & operator >> (STDdstream &, int &);
00090    friend STDdstream & operator >> (STDdstream &, long &);
00091    friend STDdstream & operator >> (STDdstream &, unsigned char &);
00092    friend STDdstream & operator >> (STDdstream &, unsigned short &);
00093    friend STDdstream & operator >> (STDdstream &, unsigned int &);
00094    friend STDdstream & operator >> (STDdstream &, unsigned long &);
00095    friend STDdstream & operator >> (STDdstream &, float &);
00096    friend STDdstream & operator >> (STDdstream &, double &); 
00097 
00098    friend STDdstream & operator << (STDdstream &, char);
00099    friend STDdstream & operator << (STDdstream &, const char * const);
00100    friend STDdstream & operator << (STDdstream &, str_ptr);
00101    friend STDdstream & operator << (STDdstream &, short);
00102    friend STDdstream & operator << (STDdstream &, int);
00103    friend STDdstream & operator << (STDdstream &, long);
00104    friend STDdstream & operator << (STDdstream &, unsigned char);
00105    friend STDdstream & operator << (STDdstream &, unsigned short);
00106    friend STDdstream & operator << (STDdstream &, unsigned int);
00107    friend STDdstream & operator << (STDdstream &, unsigned long);
00108    friend STDdstream & operator << (STDdstream &, float);
00109    friend STDdstream & operator << (STDdstream &, double); 
00110 };
00111 
00112 /* --------------------------      Methods     ----------------------------- */
00113 
00114 inline STDdstream & flush (STDdstream &ds)  { ds.flush(); return (ds); }
00115 
00116 STDdstream & operator >> (STDdstream &, char &);
00117 STDdstream & operator >> (STDdstream &, char *&);
00118 STDdstream & operator >> (STDdstream &, str_ptr&);
00119 STDdstream & operator >> (STDdstream &, short &);
00120 STDdstream & operator >> (STDdstream &, int &);
00121 STDdstream & operator >> (STDdstream &, long &);
00122 STDdstream & operator >> (STDdstream &, unsigned char &);
00123 STDdstream & operator >> (STDdstream &, unsigned short &);
00124 STDdstream & operator >> (STDdstream &, unsigned int &);
00125 STDdstream & operator >> (STDdstream &, unsigned long &);
00126 STDdstream & operator >> (STDdstream &, float &);
00127 STDdstream & operator >> (STDdstream &, double &); 
00128 
00129 STDdstream & operator << (STDdstream &, char);
00130 STDdstream & operator << (STDdstream &, const char * const);
00131 STDdstream & operator << (STDdstream &, str_ptr);
00132 STDdstream & operator << (STDdstream &, short);
00133 STDdstream & operator << (STDdstream &, int);
00134 STDdstream & operator << (STDdstream &, long);
00135 STDdstream & operator << (STDdstream &, unsigned char);
00136 STDdstream & operator << (STDdstream &, unsigned short);
00137 STDdstream & operator << (STDdstream &, unsigned int);
00138 STDdstream & operator << (STDdstream &, unsigned long);
00139 STDdstream & operator << (STDdstream &, float);
00140 STDdstream & operator << (STDdstream &, double); 
00141 
00142 /* --------------------------      Macros      ----------------------------- */
00143 
00144 
00145 #endif  /* STD_STREAM_HAS_BEEN_INCLUDED */

Generated on Mon Sep 18 11:39:33 2006 for jot by  doxygen 1.4.4