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
121class MemoryIO : public ByteIO {
122 public:
123 // Construct a dynamic object with the given initial length.
124 explicit MemoryIO(uInt64 initialSize = 65536, uInt64 expandSize = 32768);
125
126 // Construct from a buffer with the given length.
127 // The buffer is readonly and cannot be expanded.
128 MemoryIO(const void* buffer, uInt64 size);
129
130 // Construct from the given buffer with the given length.
131 // The Byte::Option determines how the buffer will be used.
132 // The seek pointer is set to the beginning of the buffer, unless
133 // told otherwise below.
134 // <dl>
135 // <dt> New, NewNoReplace and Scratch
136 // <dd> The buffer is empty and is read/write.
137 // <dt> Old
138 // <dd> The buffer contains <src>size</src> bytes and is readonly.
139 // <dt> Update, Delete
140 // <dd> The buffer contains <src>size</src> bytes and is read/write.
141 // <dt> Append
142 // <dd> The buffer contains <src>size</src> bytes and is read/write.
143 // The seek pointer is set to the end of the buffer.
144 // </dl>
145 // When the buffer is writable, it will be expanded if needed.
146 // This means that <src>buffer</src> does not point to the data
147 // anymore. However, when <src>expandSize==0</src>, the buffer
148 // cannot be expanded and the pointer is always valid.
149 // <br>When canDelete is True, buffer expansion means that the
150 // old buffer gets deleted.
152 Bool canDelete = False);
153
154 // Delete the Memory object.
155 // The data buffer is not deleted when constructed with the
156 // constructor taking a buffer pointer.
158
159 // Write the number of bytes.
160 // When needed it expands the buffer.
161 // An exception is thrown when the buffer is not writable or
162 // when buffer expansion fails or is not possible.
163 virtual void write(Int64 size, const void* buf);
164
165 // Read <src>size</src> bytes from the memory buffer. Returns the number of
166 // bytes actually read. Will throw an Exception (AipsError) if the
167 // requested number of bytes could not be read unless throwException is set
168 // to False. Will always throw an exception if the buffer is not readable
169 // or the buffer pointer is at an invalid position.
170 virtual Int64 read(Int64 size, void* buf, Bool throwException = True);
171
172 // Clear the buffer; i.e. set the data length and seek pointer to zero.
173 void clear();
174
175 // Get the buffer containing the data.
176 // <br>The length of the data in the buffer can be obtained using the
177 // length() function.
178 const uChar* getBuffer() const;
179
180 // Get the length of the data in the buffer.
181 virtual Int64 length();
182
183 // Get the allocated length of the buffer.
184 uInt64 allocated() const;
185
186 // Get the expand size (0 = not expandable).
187 uInt64 expandSize() const;
188
189 // Is the IO stream readable?
190 virtual Bool isReadable() const;
191
192 // Is the IO stream writable?
193 virtual Bool isWritable() const;
194
195 // Is the IO stream seekable?
196 virtual Bool isSeekable() const;
197
198 // resize the internal buffer (if necessary) so that it is big enough
199 // to hold the specified number of bytes. Returns a non-const pointer
200 // to the buffer that can be used to write up to the specified number
201 // of bytes into the buffer. If less data is written into the buffer
202 // then the setUsed member funtion should be used to indicate how much
203 // of the buffer is valid. Throws an exception if the MemoryIO object
204 // is not writable or if it needs to increase the size of the internal
205 // buffer and the MemoryIO object is not expandable.
206 // <note role=warning> You should not use the supplied pointer to write
207 // more than length data points to the buffer</note>
209
210 // tell the MemoryIO object how much of its internal buffer is valid
211 // data. You only need to use this function if you are directly writing to
212 // the buffer using the pointer returned by the non-const getBuffer
213 // function. This function throws an exception if the number of bytes used
214 // is greater than the number allocated or if the MemoryIO object is not
215 // writeable.
216 void setUsed(uInt64 bytesUsed);
217
218 private:
219 // # Copy constructor, should not be used.
220 MemoryIO(const MemoryIO& that);
221
222 // # Assignment, should not be used.
224
225 // Reset the position pointer to the given value. It returns the
226 // new position.
227 // An exception is thrown when seeking before the start of the
228 // buffer or when seeking past the end of a readonly buffer.
229 // When seeking past the end of a writable buffer, the required
230 // amount of bytes is added and initialized to zero.
232
233 // # Expand the buffer to at least the given size. The specified size
234 // # will be used if it results in a larger buffer size. In this way the
235 // # buffer does not get reallocated too often. It returns a false status
236 // # when the buffer cannot be expanded.
238
247};
248
249inline void MemoryIO::clear() { itsUsed = itsPosition = 0; }
250inline const uChar* MemoryIO::getBuffer() const { return itsBuffer; }
251inline uInt64 MemoryIO::allocated() const { return itsAlloc; }
252inline uInt64 MemoryIO::expandSize() const { return itsExpandSize; }
253
254} // namespace casacore
255
256#endif
SeekOption
Define the possible seek options.
Definition ByteIO.h:77
ByteIO()
The constructor does nothing.
Definition ByteIO.h:167
OpenOption
Define the possible ByteIO open options.
Definition ByteIO.h:60
void clear()
Clear the buffer; i.e.
Definition MemoryIO.h:249
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:251
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:250
uInt64 expandSize() const
Get the expand size (0 = not expandable).
Definition MemoryIO.h:252
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?
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
unsigned char uChar
Definition aipstype.h:45
const Bool False
Definition aipstype.h:42
int offset(int, int) const
compute a linear offset from array indicies
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
size_t size() const
Definition Block.h:566
const Bool True
Definition aipstype.h:41
unsigned long long uInt64
Definition aipsxtype.h:37