casacore
Loading...
Searching...
No Matches
MemoryIO.h
Go to the documentation of this file.
1//# MemoryIO.h: Class for IO in memory
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_MEMORYIO_H
27#define CASA_MEMORYIO_H
28
29//# Includes
30#include <casacore/casa/aips.h>
31#include <casacore/casa/IO/ByteIO.h>
32
33namespace casacore { //# NAMESPACE CASACORE - BEGIN
34
35// <summary>Class for IO to a memory buffer.</summary>
36
37// <use visibility=export>
38
39// <reviewed reviewer="Friso Olnon" date="1996/11/06" tests="tByteIO" demos="">
40// </reviewed>
41
42// <prerequisite>
43// <li> <linkto class=ByteIO>ByteIO</linkto>
44// </prerequisite>
45
46// <synopsis>
47// This class is doing IO in a buffer in memory.
48// It is part of the entire IO framework. It can for
49// instance be used to store data in canonical format in a
50// memory string and obtain it later.
51// <p>
52// The memory buffer can be dynamic, so it will be expanded when needed.
53// This is done by allocating a larger buffer, copy the contents and
54// throw the old buffer away.
55// <br>
56// The memory buffer can also be static to be sure that the pointer to
57// the buffer will not change.
58// The expand size determines if the memory buffer is static or dynamic.
59// An expand size zero indicates a static buffer.
60// <p>
61// The memory buffer is seekable and readable. It depends on the
62// constructor whether it is writable.
63// <p>
64// There are several ways in which the buffer can be created/passed:
65// <ul>
66// <li> Dynamic by passing an initial size and an expand size.
67// However, an expand size zero can be used to assure that no more
68// data is written than fits in the initial buffer (so once the
69// buffer is created it gets static).
70// In this way the buffer is readable and writable.
71// <li> Static by passing a const buffer and its length.
72// In this way the buffer is not writable.
73// <li> Dynamic or static by passing a non-const buffer, its length,
74// and an expand size (zero = static, >0 = dynamic)
75// . The OpenOption indicates whether the buffer will be writable.
76// Only for ByteIO::Old it will not be writable.
77// The OpenOption also determines the initial seek position.
78// Usually it is 0, but for ByteIO::Append it is the end of the buffer.
79// </ul>
80// The user can obtain a pointer to the buffer to extract the
81// stored data from it. The length of the data can also be obtained.
82// <p>
83// Usually this class will be used in combination with, say, CanonicalIO
84// and AipsIO.
85
86// <example>
87// <srcblock>
88// // Create dynamic (expandable) memory buffer of length 100.
89// // Use that as the sink of RawIO in AipsIO.
90// MemoryIO membuf (100);
91// RawIO rawio (&membuf);
92// AipsIO stream (&rawio);
93// // Write values.
94// stream << (Int)10;
95// stream << True;
96// // Seek to beginning of buffer and read data in.
97// stream.setpos (0);
98// Int vali;
99// Bool valb;
100// stream >> vali >> valb;
101//
102// // One can obtain the buffer and its length and use it later.
103// // (e.g. to write it in a non-AipsIO file).
104// uChar* bufptr = membuf.getBuffer();
105// uInt64 length = membuf.length();
106//
107// // It can also used to construct another MemoryIO object from it.
108// // The following memory buffer is static and readonly.
109// MemoryIO membuf2 (bufptr, length);
110// membuf2.read (sizeof(vali), vali);
111// membuf2.read (sizeof(valb), valb);
112// </srcblock>
113// </example>
114
115// <motivation>
116// Make it possible to do IO in a memory buffer.
117// The first implementation used strstreambuf from the iostream package.
118// However, that did not allow seeking and it was hard to get the length.
119// </motivation>
120
121
122class MemoryIO: public ByteIO
123{
124public:
125 // Construct a dynamic object with the given initial length.
126 explicit MemoryIO (uInt64 initialSize=65536, uInt64 expandSize=32768);
127
128 // Construct from a buffer with the given length.
129 // The buffer is readonly and cannot be expanded.
130 MemoryIO (const void* buffer, uInt64 size);
131
132 // Construct from the given buffer with the given length.
133 // The Byte::Option determines how the buffer will be used.
134 // The seek pointer is set to the beginning of the buffer, unless
135 // told otherwise below.
136 // <dl>
137 // <dt> New, NewNoReplace and Scratch
138 // <dd> The buffer is empty and is read/write.
139 // <dt> Old
140 // <dd> The buffer contains <src>size</src> bytes and is readonly.
141 // <dt> Update, Delete
142 // <dd> The buffer contains <src>size</src> bytes and is read/write.
143 // <dt> Append
144 // <dd> The buffer contains <src>size</src> bytes and is read/write.
145 // The seek pointer is set to the end of the buffer.
146 // </dl>
147 // When the buffer is writable, it will be expanded if needed.
148 // This means that <src>buffer</src> does not point to the data
149 // anymore. However, when <src>expandSize==0</src>, the buffer
150 // cannot be expanded and the pointer is always valid.
151 // <br>When canDelete is True, buffer expansion means that the
152 // old buffer gets deleted.
153 MemoryIO (void* buffer, uInt64 size, ByteIO::OpenOption,
154 uInt64 expandSize=0, Bool canDelete=False);
155
156 // Delete the Memory object.
157 // The data buffer is not deleted when constructed with the
158 // constructor taking a buffer pointer.
160
161 // Write the number of bytes.
162 // When needed it expands the buffer.
163 // An exception is thrown when the buffer is not writable or
164 // when buffer expansion fails or is not possible.
165 virtual void write (Int64 size, const void* buf);
166
167 // Read <src>size</src> bytes from the memory buffer. Returns the number of
168 // bytes actually read. Will throw an Exception (AipsError) if the
169 // requested number of bytes could not be read unless throwException is set
170 // to False. Will always throw an exception if the buffer is not readable
171 // or the buffer pointer is at an invalid position.
172 virtual Int64 read (Int64 size, void* buf, Bool throwException=True);
173
174 // Clear the buffer; i.e. set the data length and seek pointer to zero.
175 void clear();
176
177 // Get the buffer containing the data.
178 // <br>The length of the data in the buffer can be obtained using the
179 // length() function.
180 const uChar* getBuffer() const;
181
182 // Get the length of the data in the buffer.
183 virtual Int64 length();
184
185 // Get the allocated length of the buffer.
186 uInt64 allocated() const;
187
188 // Get the expand size (0 = not expandable).
189 uInt64 expandSize() const;
190
191 // Is the IO stream readable?
192 virtual Bool isReadable() const;
193
194 // Is the IO stream writable?
195 virtual Bool isWritable() const;
196
197 // Is the IO stream seekable?
198 virtual Bool isSeekable() const;
199
200 // resize the internal buffer (if necessary) so that it is big enough
201 // to hold the specified number of bytes. Returns a non-const pointer
202 // to the buffer that can be used to write up to the specified number
203 // of bytes into the buffer. If less data is written into the buffer
204 // then the setUsed member funtion should be used to indicate how much
205 // of the buffer is valid. Throws an exception if the MemoryIO object
206 // is not writable or if it needs to increase the size of the internal
207 // buffer and the MemoryIO object is not expandable.
208 // <note role=warning> You should not use the supplied pointer to write
209 // more than length data points to the buffer</note>
211
212 // tell the MemoryIO object how much of its internal buffer is valid
213 // data. You only need to use this function if you are directly writing to
214 // the buffer using the pointer returned by the non-const getBuffer
215 // function. This function throws an exception if the number of bytes used
216 // is greater than the number allocated or if the MemoryIO object is not
217 // writeable.
218 void setUsed(uInt64 bytesUsed);
219
220private:
221 //# Copy constructor, should not be used.
222 MemoryIO (const MemoryIO& that);
223
224 //# Assignment, should not be used.
226
227 // Reset the position pointer to the given value. It returns the
228 // new position.
229 // An exception is thrown when seeking before the start of the
230 // buffer or when seeking past the end of a readonly buffer.
231 // When seeking past the end of a writable buffer, the required
232 // amount of bytes is added and initialized to zero.
234
235 //# Expand the buffer to at least the given size. The specified size
236 //# will be used if it results in a larger buffer size. In this way the
237 //# buffer does not get reallocated too often. It returns a false status
238 //# when the buffer cannot be expanded.
239 Bool expand (uInt64 minSize);
240
241
250};
251
252
253inline void MemoryIO::clear()
254{
255 itsUsed = itsPosition = 0;
256}
257inline const uChar* MemoryIO::getBuffer() const
258{
259 return itsBuffer;
260}
262{
263 return itsAlloc;
264}
266{
267 return itsExpandSize;
268}
269
270
271
272} //# NAMESPACE CASACORE - END
273
274#endif
SeekOption
Define the possible seek options.
Definition ByteIO.h:80
OpenOption
Define the possible ByteIO open options.
Definition ByteIO.h:63
void clear()
Clear the buffer; i.e.
Definition MemoryIO.h:253
MemoryIO(uInt64 initialSize=65536, uInt64 expandSize=32768)
Construct a dynamic object with the given initial length.
virtual Int64 length()
Get the length of the data in the buffer.
virtual Int64 doSeek(Int64 offset, ByteIO::SeekOption)
Reset the position pointer to the given value.
virtual void write(Int64 size, const void *buf)
Write the number of bytes.
MemoryIO & operator=(const MemoryIO &that)
void setUsed(uInt64 bytesUsed)
tell the MemoryIO object how much of its internal buffer is valid data.
uChar * setBuffer(uInt64 length)
resize the internal buffer (if necessary) so that it is big enough to hold the specified number of by...
uInt64 allocated() const
Get the allocated length of the buffer.
Definition MemoryIO.h:261
MemoryIO(const MemoryIO &that)
virtual Bool isSeekable() const
Is the IO stream seekable?
Bool expand(uInt64 minSize)
const uChar * getBuffer() const
Get the buffer containing the data.
Definition MemoryIO.h:257
uInt64 expandSize() const
Get the expand size (0 = not expandable).
Definition MemoryIO.h:265
MemoryIO(void *buffer, uInt64 size, ByteIO::OpenOption, uInt64 expandSize=0, Bool canDelete=False)
Construct from the given buffer with the given length.
virtual Bool isReadable() const
Is the IO stream readable?
virtual Int64 read(Int64 size, void *buf, Bool throwException=True)
Read size bytes from the memory buffer.
~MemoryIO()
Delete the Memory object.
MemoryIO(const void *buffer, uInt64 size)
Construct from a buffer with the given length.
virtual Bool isWritable() const
Is the IO stream writable?
this file contains all the compiler specific defines
Definition mainpage.dox:28
unsigned char uChar
Definition aipstype.h:45
const Bool False
Definition aipstype.h:42
long long Int64
Define the extra non-standard types used by Casacore (like proposed uSize, Size)
Definition aipsxtype.h:36
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
const Bool True
Definition aipstype.h:41
unsigned long long uInt64
Definition aipsxtype.h:37