casacore
Loading...
Searching...
No Matches
AipsIO.h
Go to the documentation of this file.
1//# AipsIO.h: AipsIO is the object persistency mechanism of Casacore
2//# Copyright (C) 1993,1994,1995,1996,1998,2000,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_AIPSIO_H
27#define CASA_AIPSIO_H
28
29
30//# Includes
31#include <casacore/casa/aips.h>
32#include <casacore/casa/Containers/Block.h>
33#include <casacore/casa/BasicSL/String.h>
34#include <casacore/casa/BasicSL/Complex.h>
35#include <casacore/casa/IO/ByteIO.h>
36#include <casacore/casa/vector.h>
37
38namespace casacore { //# NAMESPACE CASACORE - BEGIN
39
40//# Forward Declarations
41class TypeIO;
42class ByteIO;
43class RegularFileIO;
44class MultiFileBase;
45
46
47// <summary>
48// AipsIO is the object persistency mechanism of Casacore
49// </summary>
50
51// <use visibility=export>
52
53// <reviewed reviewer="ghunt" date="95Feb21" tests="" demos="">
54
55// <etymology>
56// AipsIO is simply the conventional shorthand for "AIPS++ input/output".
57// Note that Casacore is the successor of the old AIPS++ project.
58// </etymology>
59
60// <synopsis>
61// AipsIO is a class designed to do I/O for objects.
62// It reads/writes the data using a class derived from
63// <linkto class=TypeIO>TypeIO</linkto>. For instance, class
64// <linkto class=CanonicalIO>CanonicalIO</linkto> can be used
65// to read/write the data in canonical format.
66// <p>
67// The TypeIO class in its turn uses a class derived from
68// <linkto class=ByteIO>ByteIO</linkto> to determine where the data
69// has to be written.
70// <p>
71// An object is written by writing all its data members. It will be
72// preceeded by a header containing type and version.
73// The I/O can be done via de overloaded << and >> operators to write or
74// read a single item (e.g., an int or an object). These operators are
75// already defined for all built-in data types and for Complex, DComplex,
76// String, and Bool.
77// Since each enumeration is a specific type, it is hard to handle them.
78// Casting to Bool (which is also an enumerated type) is a possibility,
79// but that assumes that each enumerated type has the same size (which
80// is probably true for all compilers).
81// Another possibility is to store it in an int when writing. Reading
82// can be done the opposite way, although the ARM says that an int
83// cannot be assigned to an enumerated type.
84// <p>
85// There are also functions put, get and getnew to write or read an
86// array of values. These functions are defined for the same data types
87// as << and >> (so one can write, for example, an array of Strings).
88// AipsIO.put (nr, arr) writes nr values from the given array.
89// AipsIO.get (nr, arr) reads nr values into the given user-supplied array.
90// AipsIO.getnew (&nr, &arr) reads the number of values written into
91// a new array allocated on the heap. It returns the nr of values read
92// and a pointer to the array.
93// The data must be read back in the same order as it was written.
94// <p>
95// The functions <src>putstart(type,version)</src>
96// and <src>putend()</src> must be called
97// before resp. after writing all values of the object.
98// It stores the given type and version of the object.
99// Similarly <src>getstart(type)</src> and <src>getend()</src> must be called.
100// getstart checks the type and returns the version. By using the version
101// the read function of the object can convert older versions of the
102// object (which may still reside on disk) to the latest version.
103// The function getNextType is available to get the type of the next
104// object stored. This can be used to check the type or to open (i.e.
105// issue a getstart) in the correct way.
106// <p>
107// When implementing a class, one should also define the operators << and >>
108// for the class to allow users to write or read an object in this
109// simple way (e.g., as io >> obj; to read an object).
110// One has to define the friend functions:
111// <srcblock>
112// friend AipsIO& operator<< (AipsIO&, const YourClass&);
113// friend AipsIO& operator>> (AipsIO&, YourClass&);
114// </srcblock>
115// since they cannot be stored in the class itself.
116// The type of an object is usually passed as the class name.
117// <srcblock>
118// AipsIO& operator<< (AipsIO& ios, const YourClass& object) {
119// ios.putstart ("YourClass", version);
120// ios << ....;
121// ios.putend ();
122// }
123// </srcblock>
124//
125// The functions getpos() and setpos(offset) can be used to get and set
126// the offset in the file to a given point. They can be used to point
127// to a position in the file where an object must be written or read.
128// Obviously these functions are to be used by a storage manager and
129// are not for public use. Someday they should be made private with
130// a friend defined.
131// </synopsis>
132
133// <example>
134// <srcblock>
135// MyClass myObject(...); // some object
136// AipsIO ios("file.name", ByteIO::New); // create new file
137// ios << myObject; // write object
138// MyClass myObject2;
139// ios >> myObject2; // read it back
140// </srcblock>
141// This example creates an object, writes it to AipsIO and reads it
142// back into another object.
143// The shift functions for MyClass could be defined as follows:
144// <srcblock>
145// AipsIO& operator<< (AipsIO& ios, const MyClass& myObject)
146// {
147// ios.putstart ("MyClass", 1); // MyClass version 1
148// ios << ...; // write all data members
149// ios.putend();
150// }
151// AipsIO& operator>> (AipsIO& ios, const MyClass& myObject)
152// {
153// // If needed, delete current data members first.
154// // Now read in the object.
155// uInt version = ios.getstart ("MyClass");
156// ios >> ...; // read all data members
157// ios.getend();
158// }
159// </srcblock>
160// In this example the version is not used. In more complex objects
161// it will probably be used when data members get added or changed
162// in future versions of a software system.
163// </example>
164
165
167{
168public:
169 // No file attached yet
171
172 // Construct and open/create a file with the given name.
173 // The actual IO is done via a CanonicalIO object on a regular file
174 // using buffered IO with a buffer of the given size.
175 // <br>If the MultiFileBase pointer is not null, a virtual file in the
176 // MultiFileBase will be used instead of a regular file.
177 explicit AipsIO (const String& fileName,
179 uInt filebufSize=65536,
180 const std::shared_ptr<MultiFileBase>& = std::shared_ptr<MultiFileBase>());
181
182 // Construct from a stream object derived from ByteIO.
183 // This can for instance by used to use AipsIO on a file descriptor
184 // for which a <linkto class=FilebufIO>FilebufIO</linkto>
185 // object has been created.
186 // The actual IO is done via a CanonicalIO object on top of it.
187 explicit AipsIO (const std::shared_ptr<ByteIO>&);
188
189 // Construct from a stream object derived from TypeIO, thus from
190 // a stream on top of ByteIOn doing the possible conversions.
191 explicit AipsIO (const std::shared_ptr<TypeIO>&);
192
193 // Close if not done yet
195
196 // Copy constructor and assignment are not allowed.
197 AipsIO (const AipsIO&) = delete;
198 AipsIO& operator= (const AipsIO&) = delete;
199
200 // Open/create file (either a regular file or a MultiFileBase virtual file).
201 // An exception is thrown if the object contains an already open file.
202 void open (const String& fileName,
204 uInt filebufSize=65536,
205 const std::shared_ptr<MultiFileBase>& = std::shared_ptr<MultiFileBase>());
206
207 // Open by connecting to the given byte stream.
208 // This can for instance by used to use AipsIO on a file descriptor
209 // for which a <linkto class=FilebufIO>FilebufIO</linkto>
210 // object has been created.
211 // The actual IO is done via a CanonicalIO object on top of it.
212 // An exception is thrown if the object contains an already open file.
213 void open (const std::shared_ptr<ByteIO>&);
214
215 // Open by connecting to the given typed byte stream.
216 // An exception is thrown if the object contains an already open file.
217 void open (const std::shared_ptr<TypeIO>&);
218
219 // Close file opened
220 void close();
221
222 // Return the file option.
224
225 // Start putting an object.
226 // This writes the object type and version. When reading back getstart
227 // calls have to be done in the same way. Getstart
228 // checks the type and returns the version. The user can use that to
229 // correctly read back objects with different versions.
230 // <br>
231 // Data in the outermost object cannot be put before a putstart is done.
232 // Data in nested objects can be put without an intermediate putstart.
233 // However, for complex objects it is recommended to do a putstart
234 // to have a better checking.
235 // <br>
236 // After all values (inclusing nested objects) of the object have
237 // been put, a call to putend has to be done.
238 // <group>
239 uInt putstart (const String& objectType, uInt objectVersion);
240 uInt putstart (const Char* objectType, uInt objectVersion);
241 // </group>
242
243 // Put a single value.
244 // <group>
248 AipsIO& operator<< (const short& value);
249 AipsIO& operator<< (const unsigned short& value);
251 AipsIO& operator<< (const unsigned int& value);
254 AipsIO& operator<< (const float& value);
255 AipsIO& operator<< (const double& value);
256 AipsIO& operator<< (const Complex& value);
257 AipsIO& operator<< (const DComplex& value);
260 // </group>
261
262 // Put an array of values with the given number of values.
263 // If the flag putNr is set, the number of values is put first.
264 // <group>
265 AipsIO& put (uInt nrval, const Bool* values, Bool putNR = True);
266 AipsIO& put (uInt nrval, const Char* values, Bool putNR = True);
267 AipsIO& put (uInt nrval, const uChar* values, Bool putNR = True);
268 AipsIO& put (uInt nrval, const short* values, Bool putNR = True);
269 AipsIO& put (uInt nrval, const unsigned short* values, Bool putNR = True);
270 AipsIO& put (uInt nrval, const int* values, Bool putNR = True);
271 AipsIO& put (uInt nrval, const unsigned int* values, Bool putNR = True);
272 AipsIO& put (uInt nrval, const Int64* values, Bool putNR = True);
273 AipsIO& put (uInt nrval, const uInt64* values, Bool putNR = True);
274 AipsIO& put (uInt nrval, const float* values, Bool putNR = True);
275 AipsIO& put (uInt nrval, const double* values, Bool putNR = True);
276 AipsIO& put (uInt nrval, const Complex* values, Bool putNR = True);
277 AipsIO& put (uInt nrval, const DComplex* values, Bool putNR = True);
278 AipsIO& put (uInt nrval, const String* values, Bool putNR = True);
279 // </group>
280
281 // Put a vector as an array of values
282 // For standard types it has the same result as put with putNR=True.
283 template<typename T>
284 AipsIO& put (const vector<T>& vec)
285 { *this << uInt(vec.size());
286 for (typename vector<T>::const_iterator iter=vec.begin();
287 iter!=vec.end(); ++iter) {
288 *this << *iter;
289 }
290 return *this;
291 }
292 //# Possibly specialize for standard types to make it faster.
293 //# Specialize for a bool vector.
294 AipsIO& put (const vector<Bool>& vec);
295
296
297 // End putting an object. It returns the object length (including
298 // possible nested objects).
300
301 // Get and set file-offset.
302 // <group>
305 // </group>
306
307 // Get the type of the next object stored.
308 // This is not possible if a put is in progress.
310
311 // Start reading an object. It will check if the given type matches
312 // the one stored by putstart. It returns the object version which
313 // can be used to read in older version of the object correctly.
314 // <br>
315 // After all values (inclusing nested objects) of the object have
316 // been read, a call to getend has to be done.
317 // <group>
318 uInt getstart (const String& objectType);
319 uInt getstart (const Char* objectType);
320 // </group>
321
322 // Get a single value.
323 // <group>
328 AipsIO& operator>> (unsigned short& value);
330 AipsIO& operator>> (unsigned int& value);
338 // </group>
339
340 // Read in nrval values into the user-supplied values buffer.
341 // The buffer must be long enough.
342 // <group>
343 AipsIO& get (uInt nrval, Bool* values);
344 AipsIO& get (uInt nrval, Char* values);
345 AipsIO& get (uInt nrval, uChar* values);
346 AipsIO& get (uInt nrval, short* values);
347 AipsIO& get (uInt nrval, unsigned short* values);
348 AipsIO& get (uInt nrval, int* values);
349 AipsIO& get (uInt nrval, unsigned int* values);
350 AipsIO& get (uInt nrval, Int64* values);
351 AipsIO& get (uInt nrval, uInt64* values);
352 AipsIO& get (uInt nrval, float* values);
353 AipsIO& get (uInt nrval, double* values);
354 AipsIO& get (uInt nrval, Complex* values);
355 AipsIO& get (uInt nrval, DComplex* values);
356 AipsIO& get (uInt nrval, String* values);
357 // </group>
358
359 // Get a vector as an array of values (similar to getnew).
360 // It resizes the vector as needed.
361 template<typename T>
362 AipsIO& get (vector<T>& vec)
363 { uInt sz;
364 *this >> sz;
365 vec.resize(sz);
366 for (typename vector<T>::iterator iter=vec.begin();
367 iter!=vec.end(); ++iter) {
368 *this >> *iter;
369 }
370 return *this;
371 }
372 //# Specialize for a bool vector.
373 AipsIO& get (vector<Bool>& vec);
374
375
376 // Read in values as written by the function put.
377 // It will read the number of values (into nrval), allocate a
378 // values buffer of that length and read the values into that buffer.
379 // A pointer to the buffer is returned into values.
380 // <warn=caution> Although the buffer is allocated by this function,
381 // the user has to delete it (using <src>delete [] values;</src>).
382 // <group>
383 AipsIO& getnew (uInt& nrval, Bool*& values);
384 AipsIO& getnew (uInt& nrval, Char*& values);
385 AipsIO& getnew (uInt& nrval, uChar*& values);
386 AipsIO& getnew (uInt& nrval, short*& values);
387 AipsIO& getnew (uInt& nrval, unsigned short*& values);
388 AipsIO& getnew (uInt& nrval, int*& values);
389 AipsIO& getnew (uInt& nrval, unsigned int*& values);
390 AipsIO& getnew (uInt& nrval, Int64*& values);
391 AipsIO& getnew (uInt& nrval, uInt64*& values);
392 AipsIO& getnew (uInt& nrval, float*& values);
393 AipsIO& getnew (uInt& nrval, double*& values);
394 AipsIO& getnew (uInt& nrval, Complex*& values);
395 AipsIO& getnew (uInt& nrval, DComplex*& values);
396 AipsIO& getnew (uInt& nrval, String*& values);
397 // </group>
398
399 // End reading an object. It returns the object length (including
400 // possible nested objects).
401 // It checks if the entire object has been read (to keep the data
402 // stream in sync). If not, an exception is thrown.
404
405private:
406 // Initialize everything for the open.
407 // It checks if there is no outstanding open file.
409
410 // Test if put is possible (throw exception if not).
411 void testput();
412
413 // Test if get is possible (throw exception if not).
414 void testget();
415
416 // Test if get did not exceed object.
417 void testgetLength();
418
419 // Throw exception for testput
421
422 // Throw exception for testget
424
425 // Throw exception for testgetLength
427
428
429 // 1 = file was opened by AipsIO
430 // 0 = file not opened
431 // -1 = file opened by user (=fd passed)
433 // File open option
435 // <0 = not opened for put
436 // 0 = no putstart done
437 // >0 = put is possible
439 // <0 = not opened for get
440 // 0 = no getstart done
441 // >0 = get is possible
443 // Nested object level
445 // Current size of objlen and objptr
447 // Object length at each level
449 // Object length to be read at each level
451 // Offset of length at each level
453 // True = the object type has already been read
455 // The cached object type.
457 // The file object.
458 std::shared_ptr<ByteIO> file_p;
459 // The actual IO object.
460 std::shared_ptr<TypeIO> io_p;
461 // Is the file is seekable?
463 // magic value to check sync.
464 static const uInt magicval_p;
465};
466
467
468
469// Return the file option.
471{ return fopt_p; }
472
473
474// testput tests if a put can be done; ie. if putstart has been done.
475// It throws an exception if not.
476// testget is similar to test if a get can be done.
477inline void AipsIO::testput()
478{
479 if (swput_p <= 0) {
480 testputerr();
481 }
482}
483inline void AipsIO::testget()
484{
485 if (swget_p <= 0) {
486 testgeterr();
487 }
488}
490{
493 }
494}
495
496
497
498} //# NAMESPACE CASACORE - END
499
500#endif
AipsIO & put(uInt nrval, const unsigned int *values, Bool putNR=True)
AipsIO & get(uInt nrval, uInt64 *values)
uInt getstart(const Char *objectType)
AipsIO & get(uInt nrval, Int64 *values)
AipsIO & put(uInt nrval, const Char *values, Bool putNR=True)
uInt getstart(const String &objectType)
Start reading an object.
ByteIO::OpenOption fileOption() const
Return the file option.
Definition AipsIO.h:470
void testput()
Test if put is possible (throw exception if not).
Definition AipsIO.h:477
std::shared_ptr< ByteIO > file_p
The file object.
Definition AipsIO.h:458
AipsIO & getnew(uInt &nrval, uInt64 *&values)
AipsIO()
No file attached yet.
AipsIO(const std::shared_ptr< ByteIO > &)
Construct from a stream object derived from ByteIO.
AipsIO & put(uInt nrval, const DComplex *values, Bool putNR=True)
AipsIO & getnew(uInt &nrval, Complex *&values)
AipsIO & get(uInt nrval, unsigned int *values)
uInt level_p
Nested object level.
Definition AipsIO.h:444
AipsIO & get(uInt nrval, unsigned short *values)
String objectType_p
The cached object type.
Definition AipsIO.h:456
ByteIO::OpenOption fopt_p
File open option.
Definition AipsIO.h:434
AipsIO(const String &fileName, ByteIO::OpenOption=ByteIO::Old, uInt filebufSize=65536, const std::shared_ptr< MultiFileBase > &=std::shared_ptr< MultiFileBase >())
Construct and open/create a file with the given name.
AipsIO & put(uInt nrval, const unsigned short *values, Bool putNR=True)
void testgetLength()
Test if get did not exceed object.
Definition AipsIO.h:489
Int64 getpos()
Get and set file-offset.
Bool hasCachedType_p
True = the object type has already been read.
Definition AipsIO.h:454
int swget_p
<0 = not opened for get 0 = no getstart done >0 = get is possible
Definition AipsIO.h:442
AipsIO & get(uInt nrval, float *values)
uInt putend()
End putting an object.
AipsIO & put(uInt nrval, const String *values, Bool putNR=True)
Bool seekable_p
Is the file is seekable?
Definition AipsIO.h:462
AipsIO & getnew(uInt &nrval, Char *&values)
AipsIO & getnew(uInt &nrval, uChar *&values)
AipsIO & get(uInt nrval, Complex *values)
AipsIO & get(uInt nrval, double *values)
void testputerr()
Throw exception for testput.
AipsIO & getnew(uInt &nrval, short *&values)
AipsIO & put(const vector< Bool > &vec)
AipsIO & operator=(const AipsIO &)=delete
AipsIO & put(uInt nrval, const Int64 *values, Bool putNR=True)
Int opened_p
1 = file was opened by AipsIO 0 = file not opened -1 = file opened by user (=fd passed)
Definition AipsIO.h:432
void open(const std::shared_ptr< ByteIO > &)
Open by connecting to the given byte stream.
AipsIO & getnew(uInt &nrval, DComplex *&values)
uInt maxlev_p
Current size of objlen and objptr.
Definition AipsIO.h:446
AipsIO & operator<<(const Bool &value)
Put a single value.
AipsIO & put(uInt nrval, const uInt64 *values, Bool putNR=True)
AipsIO & get(uInt nrval, String *values)
AipsIO & put(uInt nrval, const Complex *values, Bool putNR=True)
std::shared_ptr< TypeIO > io_p
The actual IO object.
Definition AipsIO.h:460
AipsIO & getnew(uInt &nrval, unsigned short *&values)
const String & getNextType()
Get the type of the next object stored.
uInt putstart(const Char *objectType, uInt objectVersion)
AipsIO & getnew(uInt &nrval, String *&values)
void testgeterrLength()
Throw exception for testgetLength.
AipsIO & get(vector< T > &vec)
Get a vector as an array of values (similar to getnew).
Definition AipsIO.h:362
void open(const String &fileName, ByteIO::OpenOption=ByteIO::Old, uInt filebufSize=65536, const std::shared_ptr< MultiFileBase > &=std::shared_ptr< MultiFileBase >())
Open/create file (either a regular file or a MultiFileBase virtual file).
Int64 setpos(Int64 offset)
AipsIO & operator>>(Bool &value)
Get a single value.
AipsIO & get(uInt nrval, int *values)
AipsIO & get(uInt nrval, DComplex *values)
uInt putstart(const String &objectType, uInt objectVersion)
Start putting an object.
AipsIO & getnew(uInt &nrval, unsigned int *&values)
AipsIO & getnew(uInt &nrval, int *&values)
AipsIO & get(uInt nrval, Char *values)
AipsIO & getnew(uInt &nrval, Bool *&values)
Read in values as written by the function put.
AipsIO & get(uInt nrval, Bool *values)
Read in nrval values into the user-supplied values buffer.
AipsIO(const std::shared_ptr< TypeIO > &)
Construct from a stream object derived from TypeIO, thus from a stream on top of ByteIOn doing the po...
~AipsIO()
Close if not done yet.
AipsIO & getnew(uInt &nrval, double *&values)
AipsIO & get(vector< Bool > &vec)
Block< uInt > objlen_p
Object length at each level.
Definition AipsIO.h:448
Block< uInt > objtln_p
Object length to be read at each level.
Definition AipsIO.h:450
AipsIO & put(uInt nrval, const int *values, Bool putNR=True)
AipsIO & put(uInt nrval, const double *values, Bool putNR=True)
AipsIO & put(uInt nrval, const float *values, Bool putNR=True)
AipsIO & put(uInt nrval, const short *values, Bool putNR=True)
void testgeterr()
Throw exception for testget.
AipsIO & get(uInt nrval, uChar *values)
void testget()
Test if get is possible (throw exception if not).
Definition AipsIO.h:483
int swput_p
<0 = not opened for put 0 = no putstart done >0 = put is possible
Definition AipsIO.h:438
AipsIO & put(uInt nrval, const Bool *values, Bool putNR=True)
Put an array of values with the given number of values.
AipsIO & put(const vector< T > &vec)
Put a vector as an array of values For standard types it has the same result as put with putNR=True.
Definition AipsIO.h:284
AipsIO & getnew(uInt &nrval, float *&values)
AipsIO(const AipsIO &)=delete
Copy constructor and assignment are not allowed.
uInt getend()
End reading an object.
AipsIO & getnew(uInt &nrval, Int64 *&values)
AipsIO & get(uInt nrval, short *values)
AipsIO & put(uInt nrval, const uChar *values, Bool putNR=True)
void openInit(ByteIO::OpenOption)
Initialize everything for the open.
static const uInt magicval_p
magic value to check sync.
Definition AipsIO.h:464
void open(const std::shared_ptr< TypeIO > &)
Open by connecting to the given typed byte stream.
Block< Int64 > objptr_p
Offset of length at each level.
Definition AipsIO.h:452
void close()
Close file opened.
simple 1-D array
Definition Block.h:198
OpenOption
Define the possible ByteIO open options.
Definition ByteIO.h:63
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
unsigned char uChar
Definition aipstype.h:45
unsigned int uInt
Definition aipstype.h:49
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
LatticeExprNode value(const LatticeExprNode &expr)
This function returns the value of the expression without a mask.
const Bool True
Definition aipstype.h:41
char Char
Definition aipstype.h:44
unsigned long long uInt64
Definition aipsxtype.h:37