include/corelib/rwstream.hpp

Go to the documentation of this file.
00001 #ifndef CORELIB___RWSTREAM__HPP
00002 #define CORELIB___RWSTREAM__HPP
00003 
00004 /*  $Id: rwstream.hpp 129455 2008-06-02 16:51:02Z lavr $
00005  * ===========================================================================
00006  *
00007  *                            PUBLIC DOMAIN NOTICE
00008  *               National Center for Biotechnology Information
00009  *
00010  *  This software/database is a "United States Government Work" under the
00011  *  terms of the United States Copyright Act.  It was written as part of
00012  *  the author's official duties as a United States Government employee and
00013  *  thus cannot be copyrighted.  This software/database is freely available
00014  *  to the public for use. The National Library of Medicine and the U.S.
00015  *  Government have not placed any restriction on its use or reproduction.
00016  *
00017  *  Although all reasonable efforts have been taken to ensure the accuracy
00018  *  and reliability of the software and data, the NLM and the U.S.
00019  *  Government do not and cannot warrant the performance or results that
00020  *  may be obtained by using this software or data. The NLM and the U.S.
00021  *  Government disclaim all warranties, express or implied, including
00022  *  warranties of performance, merchantability or fitness for any particular
00023  *  purpose.
00024  *
00025  *  Please cite the author in any work or product based on this material.
00026  *
00027  * ===========================================================================
00028  *
00029  * Authors:  Anton Lavrentiev
00030  *
00031  * File Description:
00032  *   Reader-writer based streams
00033  *
00034  */
00035 
00036 /// @file rwstream.hpp
00037 /// Reader-writer based streams
00038 /// @sa IReader, IWriter, IReaderWriter, CRWStreambuf
00039 
00040 
00041 #include <corelib/ncbimisc.hpp>
00042 #include <corelib/impl/rwstreambuf.hpp>
00043 
00044 
00045 BEGIN_NCBI_SCOPE
00046 
00047 
00048 /** @addtogroup Stream
00049  *
00050  * @{
00051  */
00052 
00053 
00054 /// Reader-based stream; @sa IReader
00055 ///
00056 /// @param buf_size
00057 ///     specifies the number bytes for internal I/O buffer, entirely used
00058 ///     for reading by the underlying stream buffer CRWStreambuf;
00059 ///     0 causes to create the buffer of some default size.
00060 ///
00061 /// @param buf
00062 ///     may specify the buffer location (if 0, an internal storage gets
00063 ///     allocated and later freed upon stream destruction).
00064 ///
00065 /// @param flags
00066 ///     controls whether IReader is destroyed upon stream destruction,
00067 ///     and whether excpetions cause logging (or caught silently).
00068 ///
00069 /// Special case of "buf_size" == 1 and "buf" == 0 creates unbuffered stream.
00070 ///
00071 /// @sa IWStream, IRWStream
00072 
00073 class  CRStream : public CNcbiIstream
00074 {
00075 public:
00076     CRStream(IReader*             r,
00077              streamsize           buf_size = 0,
00078              CT_CHAR_TYPE*        buf      = 0,
00079              CRWStreambuf::TFlags flags    = 0) :
00080         CNcbiIstream(0), m_Sb(r, 0, buf_size, buf, flags)
00081     {
00082         init(&m_Sb);
00083     }
00084 
00085 #ifdef AUTOMATIC_STREAMBUF_DESTRUCTION
00086     virtual ~CRStream()
00087     {
00088         rdbuf(0);
00089     }
00090 #endif
00091 
00092 private:
00093     CRWStreambuf m_Sb;
00094 };
00095 
00096 
00097 /// Writer-based stream; @sa IWriter
00098 ///
00099 /// @param buf_size
00100 ///     specifies the number bytes for internal I/O buffer, entirely used
00101 ///     for writing by the underlying stream buffer CRWStreambuf;
00102 ///     0 causes to create the buffer of some default size.
00103 ///
00104 /// @param buf
00105 ///     may specify the buffer location (if 0, an internal storage gets
00106 ///     allocated and later freed upon stream destruction).
00107 ///
00108 /// @param flags
00109 ///     controls whether IWriter is destroyed upon stream destruction,
00110 ///     and whether excpetions cause logging (or caught silently).
00111 ///
00112 /// Special case of "buf_size" == 1 and "buf" == 0 creates unbuffered stream.
00113 ///
00114 /// @sa IRStream, IRWStream
00115 
00116 class  CWStream : public CNcbiOstream
00117 {
00118 public:
00119     CWStream(IWriter*             w,
00120              streamsize           buf_size = 0,
00121              CT_CHAR_TYPE*        buf      = 0,
00122              CRWStreambuf::TFlags flags    = 0) :
00123         CNcbiOstream(0), m_Sb(0, w, buf_size, buf, flags)
00124     {
00125         init(&m_Sb);
00126     }
00127 
00128 #ifdef AUTOMATIC_STREAMBUF_DESTRUCTION
00129     virtual ~CWStream()
00130     {
00131         rdbuf(0);
00132     }
00133 #endif
00134 
00135 private:
00136     CRWStreambuf m_Sb;
00137 };
00138 
00139 
00140 /// Reader-writer based stream; @sa IReaderWriter
00141 ///
00142 /// @param buf_size
00143 ///     specifies the number bytes for internal I/O buffer,
00144 ///     with half used for reading and the other half for writing
00145 ///     by underlying stream buffer CRWStreambuf;
00146 ///     0 causes to create the buffer of some default size.
00147 ///
00148 /// @param buf
00149 ///     may specify the buffer location (if 0, an internal storage gets
00150 ///     allocated and later freed upon stream destruction).
00151 ///
00152 /// @param flags
00153 ///     controls whether IReader is destroyed upon stream destruction,
00154 ///     and whether excpetions cause logging (or caught silently).
00155 ///
00156 /// Special case of "buf_size" == 1 and "buf" == 0 creates unbuffered stream.
00157 ///
00158 /// @sa IRStream, IWStream
00159 
00160 class  CRWStream : public CNcbiIostream
00161 {
00162 public:
00163     CRWStream(IReaderWriter*       rw,
00164               streamsize           buf_size = 0,
00165               CT_CHAR_TYPE*        buf      = 0,
00166               CRWStreambuf::TFlags flags    = 0) :
00167         CNcbiIostream(0), m_Sb(rw, buf_size, buf, flags)
00168     {
00169         init(&m_Sb);
00170     }
00171 
00172 #ifdef AUTOMATIC_STREAMBUF_DESTRUCTION
00173     virtual ~CRWStream()
00174     {
00175         rdbuf(0);
00176     }
00177 #endif
00178 
00179 private:
00180     CRWStreambuf m_Sb;
00181 };
00182 
00183 
00184 /// istream based IReader
00185 class  CStreamReader : public IReader
00186 {
00187 public:
00188     CStreamReader(CNcbiIstream& is, EOwnership own = eNoOwnership)
00189         : m_Stream(&is, own)
00190         {
00191         }
00192     ~CStreamReader();
00193 
00194     virtual ERW_Result Read(void* buf, size_t count, size_t* bytes_read = 0);
00195     virtual ERW_Result PendingCount(size_t* count);
00196 
00197 private:
00198     AutoPtr<CNcbiIstream> m_Stream;
00199 
00200 private: // prevent copy
00201     CStreamReader(const CStreamReader&);
00202     void operator=(const CStreamReader&);
00203 };
00204 
00205 
00206 /* @} */
00207 
00208 
00209 END_NCBI_SCOPE
00210 
00211 #endif /* CORELIB___RWSTREAM__HPP */
00212 
00213 

Generated on Sun Dec 6 22:01:32 2009 for NCBI C++ ToolKit by  doxygen 1.4.6
Modified on Mon Dec 07 16:20:35 2009 by modify_doxy.py rev. 173732