casacore
Loading...
Searching...
No Matches
ByteIO.h
Go to the documentation of this file.
1//# ByteIO.h: Abstract base class for IO on a byte stream
2//# Copyright (C) 1996,1999,2001
3//# Associated Universities, Inc. Washington DC, USA.
4//#
5//# This library is free software; you can redistribute it and/or modify it
6//# under the terms of the GNU Library General Public License as published by
7//# the Free Software Foundation; either version 2 of the License, or (at your
8//# option) any later version.
9//#
10//# This library is distributed in the hope that it will be useful, but WITHOUT
11//# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12//# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13//# License for more details.
14//#
15//# You should have received a copy of the GNU Library General Public License
16//# along with this library; if not, write to the Free Software Foundation,
17//# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18//#
19//# Correspondence concerning AIPS++ should be addressed as follows:
20//# Internet email: casa-feedback@nrao.edu.
21//# Postal address: AIPS++ Project Office
22//# National Radio Astronomy Observatory
23//# 520 Edgemont Road
24//# Charlottesville, VA 22903-2475 USA
25
26#ifndef CASA_BYTEIO_H
27#define CASA_BYTEIO_H
28
29//# Includes
30#include <casacore/casa/aips.h>
31#include <casacore/casa/BasicSL/String.h>
32
33
34namespace casacore { //# NAMESPACE CASACORE - BEGIN
35
36// <summary>Abstract base class for IO on a byte stream.</summary>
37
38// <use visibility=export>
39
40// <reviewed reviewer="Friso Olnon" date="1996/11/06" tests="tByteIO" demos="">
41// </reviewed>
42
43// <synopsis>
44// ByteIO is the abstract base class for all classes doing IO on
45// byte streams. Examples of derived classes are
46// <linkto class=RegularFileIO>RegularFileIO</linkto> and
47// <linkto class=MemoryIO>MemoryIO</linkto>.
48// <p>
49// ByteIO contains two enumerations, which define the possible
50// open and seek options on byte streams. These enumerations
51// are used throughout the IO framework.
52// </synopsis>
53
54// <motivation>
55// Make polymorphic operations on byte streams possible.
56// </motivation>
57
58
59class ByteIO
60{
61public:
62 // Define the possible ByteIO open options.
64 Old=1,
65 // read/write; file must exist.
67 // read/write; create file if not exist.
69 // read/write; create file if not exist.
71 // read/write; file may not exist yet.
73 // read/write; delete file at close.
75 // read/write; file must exist; delete at close.
76 Delete
77 };
78
79 // Define the possible seek options.
81 // Seek from beginning of file.
83 // Seek from current position.
85 // Seek from the end of the file.
86 End
87 };
88
89
90 // The constructor does nothing.
91 ByteIO();
92
93 virtual ~ByteIO();
94
95 // Write <src>size</src> bytes to the byte stream.
96 virtual void write (Int64 size, const void* buf) = 0;
97
98 // Write <src>size</src> bytes to the byte stream at <src>offset</src>.
99 // The file offset is not changed
100 virtual void pwrite (Int64 size, Int64 offset, const void* buf);
101
102 // Read <src>size</src> bytes from the byte stream. Returns the number of
103 // bytes actually read, or a negative number if an error occurred. Will also
104 // throw an Exception (AipsError) if the requested number of bytes could
105 // not be read unless throwException is set to False.
106 virtual Int64 read (Int64 size, void* buf, Bool throwException=True) = 0;
107
108 // Like read but reads from offset of start of the file
109 // The file offset is not changed
110 virtual Int64 pread (Int64 size, Int64 offset, void* buf, Bool throwException=True);
111
112 // Reopen the underlying IO stream for read/write access.
113 // Nothing will be done if the stream is writable already.
114 // Otherwise it will be reopened and an exception will be thrown
115 // if it is not possible to reopen it for read/write access.
116 // The default implementation in this base class throws a "not possible"
117 // exception if a reopen has to be done.
118 virtual void reopenRW();
119
120 // This function sets the position on the given offset.
121 // The seek option defines from which file position the seek is done.
122 // -1 is returned if not seekable.
123 // <group>
126 // </group>
127
128 // Flush the data to the file.
129 // The default implementation does nothing.
130 virtual void flush();
131
132 // Fsync the file (i.e. force the data to be physically written).
133 // The default implementation does nothing.
134 virtual void fsync();
135
136 // Resync the file (i.e. empty the current buffer).
137 // The default implementation does nothing.
138 virtual void resync();
139
140 // Truncate the file to the given size.
141 // The default implementation does nothing.
142 virtual void truncate (Int64 size);
143
144 // Get the file name of the file attached.
145 // The default implementation returns an empty string.
146 virtual String fileName() const;
147
148 // Get the length of the byte stream.
149 virtual Int64 length() = 0;
150
151 // Is the byte stream readable?
152 virtual Bool isReadable() const = 0;
153
154 // Is the byte stream writable?
155 virtual Bool isWritable() const = 0;
156
157 // Is the byte stream seekable?
158 virtual Bool isSeekable() const = 0;
159
160
161protected:
162 // Make copy constructor and assignment protected, so a user cannot
163 // use them (but a derived class can).
164 // <group>
165 ByteIO (const ByteIO& byteIO);
166 ByteIO& operator= (const ByteIO& byteIO);
167 // </group>
168
169 virtual Int64 doSeek (Int64 offset, ByteIO::SeekOption) = 0;
170};
171
172
173
175{}
176
177inline ByteIO::ByteIO (const ByteIO&)
178{}
179
181{
182 return *this;
183}
184
186{
187 return doSeek (offset, option);
188}
190{
191 return doSeek (Int64(offset), option);
192}
193
194
195} //# NAMESPACE CASACORE - END
196
197#endif
virtual Int64 doSeek(Int64 offset, ByteIO::SeekOption)=0
virtual void reopenRW()
Reopen the underlying IO stream for read/write access.
virtual Int64 length()=0
Get the length of the byte stream.
virtual Bool isWritable() const =0
Is the byte stream writable?
SeekOption
Define the possible seek options.
Definition ByteIO.h:80
@ Begin
Seek from beginning of file.
Definition ByteIO.h:82
@ Current
Seek from current position.
Definition ByteIO.h:84
@ End
Seek from the end of the file.
Definition ByteIO.h:86
virtual Bool isReadable() const =0
Is the byte stream readable?
ByteIO & operator=(const ByteIO &byteIO)
Definition ByteIO.h:180
virtual String fileName() const
Get the file name of the file attached.
virtual void flush()
Flush the data to the file.
virtual Int64 read(Int64 size, void *buf, Bool throwException=True)=0
Read size bytes from the byte stream.
virtual void resync()
Resync the file (i.e.
virtual Int64 pread(Int64 size, Int64 offset, void *buf, Bool throwException=True)
Like read but reads from offset of start of the file The file offset is not changed.
Int64 seek(Int offset, ByteIO::SeekOption=ByteIO::Begin)
This function sets the position on the given offset.
Definition ByteIO.h:189
virtual void truncate(Int64 size)
Truncate the file to the given size.
virtual void write(Int64 size, const void *buf)=0
Write size bytes to the byte stream.
virtual void fsync()
Fsync the file (i.e.
virtual Bool isSeekable() const =0
Is the byte stream seekable?
ByteIO()
The constructor does nothing.
Definition ByteIO.h:174
virtual void pwrite(Int64 size, Int64 offset, const void *buf)
Write size bytes to the byte stream at offset.
virtual ~ByteIO()
OpenOption
Define the possible ByteIO open options.
Definition ByteIO.h:63
@ Scratch
read/write; delete file at close.
Definition ByteIO.h:74
@ Delete
read/write; file must exist; delete at close.
Definition ByteIO.h:76
@ Append
read/write; create file if not exist.
Definition ByteIO.h:68
@ NewNoReplace
read/write; file may not exist yet.
Definition ByteIO.h:72
@ New
read/write; create file if not exist.
Definition ByteIO.h:70
@ Update
read/write; file must exist.
Definition ByteIO.h:66
String: the storage and methods of handling collections of characters.
Definition String.h:223
this file contains all the compiler specific defines
Definition mainpage.dox:28
long long Int64
Define the extra non-standard types used by Casacore (like proposed uSize, Size)
Definition aipsxtype.h:36
int Int
Definition aipstype.h:48
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
const Bool True
Definition aipstype.h:41