casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
ByteIO.h
Go to the documentation of this file.
00001 //# ByteIO.h: Abstract base class for IO on a byte stream
00002 //# Copyright (C) 1996,1999,2001
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //# $Id: ByteIO.h 20551 2009-03-25 00:11:33Z Malte.Marquarding $
00027 
00028 #ifndef CASA_BYTEIO_H
00029 #define CASA_BYTEIO_H
00030 
00031 //# Includes
00032 #include <casa/aips.h>
00033 
00034 
00035 namespace casa { //# NAMESPACE CASA - BEGIN
00036 
00037 // <summary>Abstract base class for IO on a byte stream.</summary>
00038 
00039 // <use visibility=export>
00040 
00041 // <reviewed reviewer="Friso Olnon" date="1996/11/06" tests="tByteIO" demos="">
00042 // </reviewed>
00043 
00044 // <synopsis> 
00045 // ByteIO is the abstract base class for all classes doing IO on
00046 // byte streams. Examples of derived classes are
00047 // <linkto class=RegularFileIO>RegularFileIO</linkto> and
00048 // <linkto class=MemoryIO>MemoryIO</linkto>.
00049 // <p>
00050 // ByteIO contains two enumerations, which define the possible
00051 // open and seek options on byte streams. These enumerations
00052 // are used throughout the IO framework.
00053 // </synopsis>
00054 
00055 // <motivation> 
00056 // Make polymorphic operations on byte streams possible.
00057 // </motivation>
00058 
00059 
00060 class ByteIO
00061 {
00062 public:
00063     // Define the possible ByteIO open options.
00064     enum OpenOption {
00065         Old=1,
00066         // read/write; file must exist.
00067         Update,
00068         // read/write; create file if not exist.
00069         Append,
00070         // read/write; create file if not exist.
00071         New,
00072         // read/write; file may not exist yet.
00073         NewNoReplace,
00074         // read/write; delete file at close.
00075         Scratch,
00076         // read/write; file must exist; delete at close.
00077         Delete
00078     };
00079 
00080     // Define the possible seek options.
00081     enum SeekOption {
00082         // Seek from beginning of file.
00083         Begin=1,
00084         // Seek from current position.
00085         Current,
00086         // Seek from the end of the file.
00087         End
00088     };
00089 
00090 
00091     // The constructor does nothing.
00092     ByteIO();
00093 
00094     virtual ~ByteIO();
00095 
00096     // Write <src>size</src> bytes to the byte stream.
00097     virtual void write (uInt size, const void* buf) = 0;
00098 
00099     // Read <src>size</src> bytes from the byte stream. Returns the number of
00100     // bytes actually read, or a negative number if an error occured. Will also
00101     // throw an Exception (AipsError) if the requested number of bytes could
00102     // not be read unless throwException is set to False.
00103     virtual Int read (uInt size, void* buf, Bool throwException=True) = 0;    
00104 
00105     // Reopen the underlying IO stream for read/write access.
00106     // Nothing will be done if the stream is writable already.
00107     // Otherwise it will be reopened and an exception will be thrown
00108     // if it is not possible to reopen it for read/write access.
00109     // The default implementation in this base class throws a "not possible"
00110     // exception if a reopen has to be done.
00111     virtual void reopenRW();
00112 
00113     // This function sets the position on the given offset.
00114     // The seek option defines from which file position the seek is done.
00115     // -1 is returned if not seekable.
00116     // <group>
00117     Int64 seek (Int offset, ByteIO::SeekOption = ByteIO::Begin);
00118     Int64 seek (Int64 offset, ByteIO::SeekOption = ByteIO::Begin);
00119     // </group>
00120 
00121     // Get the length of the byte stream.
00122     virtual Int64 length() = 0;
00123     
00124     // Is the byte stream readable?
00125     virtual Bool isReadable() const = 0;
00126 
00127     // Is the byte stream writable?
00128     virtual Bool isWritable() const = 0;
00129 
00130     // Is the byte stream seekable?
00131     virtual Bool isSeekable() const = 0;
00132 
00133 
00134 protected:
00135     // Make copy constructor and assignment protected, so a user cannot
00136     // use them (but a derived class can).
00137     // <group>
00138     ByteIO (const ByteIO& byteIO);
00139     ByteIO& operator= (const ByteIO& byteIO);
00140     // </group>
00141 
00142     virtual Int64 doSeek (Int64 offset, ByteIO::SeekOption) = 0;
00143 };
00144 
00145 
00146 
00147 inline ByteIO::ByteIO()
00148 {}
00149 
00150 inline ByteIO::ByteIO (const ByteIO&)
00151 {}
00152 
00153 inline ByteIO& ByteIO::operator= (const ByteIO&)
00154 {
00155    return *this;
00156 }
00157 
00158 inline Int64 ByteIO::seek (Int64 offset, ByteIO::SeekOption option)
00159 {
00160     return doSeek (offset, option);
00161 }
00162 inline Int64 ByteIO::seek (Int offset, ByteIO::SeekOption option)
00163 {
00164     return doSeek (Int64(offset), option);
00165 }
00166 
00167 
00168 } //# NAMESPACE CASA - END
00169 
00170 #endif