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