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

stream.h

Go to the documentation of this file.
00001 /* -*- C++ -*- */
00002 /**************************************************************************
00003  Copyright (c) 2000-2001, Tony Garnock-Jones
00004  All rights reserved.
00005 
00006  Redistribution and use in source and binary forms, with or without
00007  modification, are permitted provided that the following conditions are
00008  met:
00009 
00010      * Redistributions of source code must retain the above copyright
00011        notice, this list of conditions and the following disclaimer.
00012 
00013      * Redistributions in binary form must reproduce the above
00014        copyright notice, this list of conditions and the following
00015        disclaimer in the documentation and/or other materials provided
00016        with the distribution.
00017 
00018      * Neither the names of the copyright holders nor the names of this
00019        software's contributors may be used to endorse or promote
00020        products derived from this software without specific prior
00021        written permission.
00022 
00023  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00024  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00025  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00026  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
00027  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00028  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00029  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00030  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00031  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00032  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00033  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034 **************************************************************************/
00035 
00036 /* -*- c++ -*- */
00037 #ifndef MPS_Stream_H
00038 #define MPS_Stream_H
00039 
00040 namespace MPS {
00041 
00042 /**
00043  * Used to stream data from a Connection.
00044  */
00045 class InputStream {
00046 protected:
00047   /**
00048    * Reads a single byte out of the message body.  Subclasses must
00049    * override this.
00050    *
00051    * @return the byte read */
00052   virtual int read() = 0;
00053 
00054 public:
00055   virtual ~InputStream() {}
00056 
00057   // These methods are not const because they call read(), which modifies <i>state</i>.
00058   int readint();                                ///< Reads an encoded 32-bit signed int.
00059   string readstring();                          ///< Reads an encoded ASCIZ byte string.
00060   bool readbool();                              ///< Reads an encoded boolean.
00061   long long readlong();                         ///< Reads an encoded 64-bit signed long.
00062   char readchar();                              ///< Reads an encoded char.
00063   string readReference();                       ///< Reads a reference to another MPS object.
00064   float readfloat();                            ///< Reads an encoded 32-bit float.
00065 };
00066 
00067 /**
00068  * Used to stream data to a Connection.
00069  */
00070 class OutputStream {
00071 protected:
00072   string buffer;                        ///< The message's body
00073 
00074   /**
00075    * Appends a single byte to the message body.
00076    *
00077    * @param b the byte to append */
00078   void write(int b) {
00079     buffer.append(1, (char) b);
00080   }
00081 
00082   /**
00083    * Returns the name of the transport we should look for addresses
00084    * within when writing out Interface references in
00085    * writeReference. Override this in subclasses to set the preferred
00086    * transport-address-type for references we pickle for
00087    * transmission.
00088    *
00089    * @return the name of a transport for use in writeReference()
00090    * @see writeReference() */
00091   virtual string referenceTransport() const { return "UNDEFINED-REFERENCETRANSPORT"; }
00092 
00093 public:
00094   OutputStream()
00095     : buffer()
00096   {}
00097 
00098   virtual ~OutputStream() {}
00099 
00100   int getLength() const { return buffer.size(); }               ///< Returns the buffer length
00101   char const *getBody() const { return buffer.c_str(); }        ///< Returns the buffer content
00102 
00103   void writeint(int what);                      ///< Serialises a 32-bit signed int.
00104   void writestring(string const &what);         ///< Serialises an ASCIZ byte string.
00105   void writebool(bool what);                    ///< Serialises a boolean.
00106   void writelong(long long what);               ///< Serialises a 64-bit signed long.
00107   void writechar(char what);                    ///< Serialises a char.
00108   void writeReference(ref<Interface> const &what);
00109                                                 ///< Serialises a reference to another MPS object.
00110   void writefloat(float what);                  ///< Serialises a 32-bit float.
00111 
00112   virtual void flush() {
00113     buffer.erase();
00114   }
00115 };
00116 
00117 }
00118 
00119 #endif

Generated at Wed Aug 15 01:05:16 2001 for mps-cpp by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001