TCStream.h

Go to the documentation of this file.
00001 //*******************************************************************************
00002 //
00003 // *******   ***   ***               *
00004 //    *     *     *                  *
00005 //    *    *      *                *****
00006 //    *    *       ***  *   *   **   *    **    ***
00007 //    *    *          *  * *   *     *   ****  * * *
00008 //    *     *         *   *      *   * * *     * * *
00009 //    *      ***   ***    *     **   **   **   *   *
00010 //                        *
00011 //*******************************************************************************
00012 // see http://sourceforge.net/projects/tcsystem/ for details.
00013 // Copyright (C) 2003 - 2008 Thomas Goessler. All Rights Reserved. 
00014 //*******************************************************************************
00015 //
00016 // TCSystem is the legal property of its developers.
00017 // Please refer to the COPYRIGHT file distributed with this source distribution.
00018 // 
00019 // This library is free software; you can redistribute it and/or             
00020 // modify it under the terms of the GNU Lesser General Public                
00021 // License as published by the Free Software Foundation; either              
00022 // version 2.1 of the License, or (at your option) any later version.        
00023 //                                                                           
00024 // This library is distributed in the hope that it will be useful,           
00025 // but WITHOUT ANY WARRANTY; without even the implied warranty of            
00026 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         
00027 // Lesser General Public License for more details.                           
00028 //                                                                           
00029 // You should have received a copy of the GNU Lesser General Public          
00030 // License along with this library; if not, write to the Free Software       
00031 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
00032 //*******************************************************************************
00033 //  $Id: TCStream.h 870 2008-07-21 14:15:33Z the_____tiger $
00034 //*******************************************************************************
00035 #ifndef _TCSTREAM_H_
00036 #define _TCSTREAM_H_
00037 
00038 #include "TCNonCopyable.h"
00039 #include "TCSharedPtr.h"
00040 
00041 #include <string>
00042 
00043 namespace TC
00044 {
00061    class Stream: protected NonCopyAble
00062    {
00063    public:
00067       enum StreamErrorFlags
00068       {
00070          error_none,
00072          error_streamopen,
00074          error_streamclose,
00076          error_streamdirection,
00078          error_last
00079       };
00080 
00084       enum StreamDirection
00085       {
00087          stream_dead,
00089          stream_read,
00091          stream_write,
00093          stream_readwrite
00094       };
00095 
00096    public:
00097       virtual ~Stream() {}
00098 
00103       virtual sint32 GetStatus() const = 0;
00105       virtual void ResetStatus() = 0;
00107       virtual bool Error() const = 0;
00109       virtual bool IsOk() const = 0;
00110 
00115       virtual void EnableDisplayErrorMessages(bool displ) = 0;
00116 
00117       enum StreamPosition
00118       {
00119          POSITION_SET,
00120          POSITION_CURRENT,
00121          POSITION_END,
00122       };
00123       virtual bool SetPosition(uint32, StreamPosition pos) = 0;
00124       virtual uint32 GetPosition() const = 0;
00125 
00133       virtual uint32 ReadBytes (uint32 nBytes, void *bytes) = 0;
00141       virtual uint32 WriteBytes(uint32 nBytes, const void *bytes) = 0;
00142 
00147       virtual uint32 Read(sint16 &val) = 0;
00152       virtual uint32 Read(uint16 &val) = 0;
00157       virtual uint32 Read(sint32 &val) = 0;
00162       virtual uint32 Read(uint32 &val) = 0;
00167       virtual uint32 Read(sint64 &val) = 0;
00172       virtual uint32 Read(uint64 &val) = 0;
00177       virtual uint32 Read(float &val) = 0;
00182       virtual uint32 Read(double &val) = 0;
00187       virtual uint32 Read(char *val) = 0;
00192       virtual uint32 Read(std::string& val) = 0;
00197       virtual uint32 Read(char &val) = 0;
00202       virtual uint32 Read(uchar &val) = 0;
00203 
00204 
00209       virtual uint32 Write(sint16 val) = 0;
00214       virtual uint32 Write(uint16 val) = 0;
00219       virtual uint32 Write(sint32 val) = 0;
00224       virtual uint32 Write(uint32 val) = 0;
00229       virtual uint32 Write(sint64 val) = 0;
00234       virtual uint32 Write(uint64 val) = 0;
00239       virtual uint32 Write(float val) = 0;
00244       virtual uint32 Write(double val) = 0;
00249       virtual uint32 Write(const char *val) = 0;
00254       virtual uint32 Write(const std::string& val) = 0;
00259       virtual uint32 Write(char val) = 0;
00264       virtual uint32 Write(uchar val) = 0;
00265 
00267       virtual uint32 WriteEndOfLine() = 0;
00272       virtual uint32 WriteSpace() = 0;
00273 
00275       virtual void Flush() = 0;
00277       virtual void CloseStream() = 0;
00278    };
00279 
00281    typedef SharedPtr<Stream> StreamPtr;
00282 
00283    // -----------------------------------------------------------------
00284    // read operators
00285    // -----------------------------------------------------------------
00286    inline StreamPtr operator>>(StreamPtr stream, std::string& val) { stream->Read(val); return stream; }
00287 
00288    inline StreamPtr operator>>(StreamPtr stream, uchar& val) { stream->Read(val); return stream; }
00289    inline StreamPtr operator>>(StreamPtr stream, char& val)  { stream->Read(val); return stream; }
00290    inline StreamPtr operator>>(StreamPtr stream, char* val)  { stream->Read(val); return stream; }
00291 
00292    inline StreamPtr operator>>(StreamPtr stream, sint16& val) { stream->Read(val); return stream; }
00293    inline StreamPtr operator>>(StreamPtr stream, sint32& val) { stream->Read(val); return stream; }
00294    inline StreamPtr operator>>(StreamPtr stream, sint64& val) { stream->Read(val); return stream; }
00295    inline StreamPtr operator>>(StreamPtr stream, uint16& val) { stream->Read(val); return stream; }
00296    inline StreamPtr operator>>(StreamPtr stream, uint32& val) { stream->Read(val); return stream; }
00297    inline StreamPtr operator>>(StreamPtr stream, uint64& val) { stream->Read(val); return stream; }
00298 
00299    inline StreamPtr operator>>(StreamPtr stream, float& val)  { stream->Read(val); return stream; }
00300    inline StreamPtr operator>>(StreamPtr stream, double& val) { stream->Read(val); return stream; }
00301 
00302    // -----------------------------------------------------------------
00303    // write operators
00304    // -----------------------------------------------------------------
00305    inline StreamPtr operator<<(StreamPtr stream, StreamPtr (*_Pfn)(StreamPtr)) {return ((*_Pfn)(stream));}
00306 
00307    inline StreamPtr operator<<(StreamPtr stream, const std::string& val) { stream->Write(val.c_str()); return stream; }
00308 
00309    inline StreamPtr operator<<(StreamPtr stream, uchar val)       { stream->Write(val); return stream; }
00310    inline StreamPtr operator<<(StreamPtr stream, char val)        { stream->Write(val); return stream; }
00311    inline StreamPtr operator<<(StreamPtr stream, const char* val) { stream->Write(val); return stream; }
00312 
00313    inline StreamPtr operator<<(StreamPtr stream, sint16 val)      { stream->Write(val); return stream; }
00314    inline StreamPtr operator<<(StreamPtr stream, sint32 val)      { stream->Write(val); return stream; }
00315    inline StreamPtr operator<<(StreamPtr stream, sint64 val)      { stream->Write(val); return stream; }
00316    inline StreamPtr operator<<(StreamPtr stream, uint16 val)      { stream->Write(val); return stream; }
00317    inline StreamPtr operator<<(StreamPtr stream, uint32 val)      { stream->Write(val); return stream; }
00318    inline StreamPtr operator<<(StreamPtr stream, uint64 val)      { stream->Write(val); return stream; }
00319 
00320    inline StreamPtr operator<<(StreamPtr stream, float val)       { stream->Write(val); return stream; }
00321    inline StreamPtr operator<<(StreamPtr stream, double val)      { stream->Write(val); return stream; }
00322 
00328    inline StreamPtr flush(StreamPtr stream) { stream->Flush(); return stream; }
00334    inline StreamPtr endl(StreamPtr stream) { stream->WriteEndOfLine(); return stream; }
00340    inline StreamPtr space(StreamPtr stream) { stream->WriteSpace(); return stream; }
00341 
00346 } // namespace TC
00347 
00348 #endif // _TCSTREAM_H_

Copyright (c) Thomas Goessler 2003 - 2008