casa::AipsIO Class Reference
[IO]

#include <AipsIO.h>

Collaboration diagram for casa::AipsIO:

Collaboration graph
[legend]
List of all members.

Detailed Description

AipsIO is the object persistency mechanism of AIPS++.

Intended use:

Part of API

Review Status

Reviewed By:
ghunt
Date Reviewed:
95Feb21

Etymology

AipsIO is simply the conventional shorthand for "aips++ input/output".

Synopsis

AipsIO is a class designed to do I/O for objects. It reads/writes the data using a class derived from TypeIO . For instance, class CanonicalIO can be used to read/write the data in canonical format.

The TypeIO class in its turn uses a class derived from ByteIO to determine where the data has to be written.

An object is written by writing all its data members. It will be preceeded by a header containing type and version. The I/O can be done via de overloaded << and >> operators to write or read a single item (e.g., an int or an object). These operators are already defined for all built-in data types and for Complex, DComplex, String, and Bool. Since each enumeration is a specific type, it is hard to handle them. Casting to Bool (which is also an enumerated type) is a possibility, but that assumes that each enumerated type has the same size (which is probably true for all compilers). Another possibility is to store it in an int when writing. Reading can be done the opposite way, although the ARM says that an int cannot be assigned to an enumerated type.

There are also functions put, get and getnew to write or read an array of values. These functions are defined for the same data types as << and >> (so one can write, for example, an array of Strings). AipsIO.put (nr, arr) writes nr values from the given array. AipsIO.get (nr, arr) reads nr values into the given user-supplied array. AipsIO.getnew (&nr, &arr) reads the number of values written into a new array allocated on the heap. It returns the nr of values read and a pointer to the array. The data must be read back in the same order as it was written.

The functions putstart(type,version) and putend() must be called before resp. after writing all values of the object. It stores the given type and version of the object. Similarly getstart(type) and getend() must be called. getstart checks the type and returns the version. By using the version the read function of the object can convert older versions of the object (which may still reside on disk) to the latest version. The function getNextType is available to get the type of the next object stored. This can be used to check the type or to open (i.e. issue a getstart) in the correct way.

When implementing a class, one should also define the operators << and >> for the class to allow users to write or read an object in this simple way (e.g., as io >> obj; to read an object). One has to define the friend functions:

         friend AipsIO& operator<< (AipsIO&, const YourClass&);
         friend AipsIO& operator>> (AipsIO&, YourClass&);
since they cannot be stored in the class itself. The type of an object is usually passed as the class name.
         AipsIO& operator<< (AipsIO& ios, const YourClass& object) {
             ios.putstart ("YourClass", version);
             ios << .\..\.;
             ios.putend ();
         }

The functions getpos() and setpos(offset) can be used to get and set the offset in the file to a given point. They can be used to point to a position in the file where an object must be written or read. Obviously these functions are to be used by a storage manager and are not for public use. Someday they should be made private with a friend defined.

Example

        MyClass myObject(.\..);                   // some object
        AipsIO ios("file.name", ByteIO::New);    // create new file
        ios << myObject;                         // write object
        MyClass myObject2;
        ios >> myObject2;                        // read it back
This example creates an object, writes it to AipsIO and reads it back into another object. The shift functions for MyClass could be defined as follows:
    AipsIO& operator<< (AipsIO& ios, const MyClass& myObject)
    {
        ios.putstart ("MyClass", 1);    // MyClass version 1
        ios << .\..;                     // write all data members
        ios.putend();
    }
    AipsIO& operator>> (AipsIO& ios, const MyClass& myObject)
    {
        // If needed, delete current data members first.
        // Now read in the object.
        uInt version = ios.getstart ("MyClass");
        ios >> .\..;                     // read all data members
        ios.getend();
    }
In this example the version is not used. In more complex objects it will probably be used when data members get added or changed in future versions of a software system.

Definition at line 165 of file AipsIO.h.

Public Member Functions

 AipsIO ()
 No file attached yet.
 AipsIO (const String &fileName, ByteIO::OpenOption=ByteIO::Old, uInt filebufSize=65536)
 Construct and open/create a file with the given name.
Construct and open create
a file with the given name
This can for instance by used
to use AipsIO on a file descriptor
for which a link FilebufIO
FilebufIO endlink object has
been created The actual IO
is done via a CanonicalIO
object on top of it * 
AipsIO (ByteIO *)
 AipsIO (TypeIO *)
 Construct and open by connecting to the given file descriptor.
 ~AipsIO ()
 Close if not done yet.
void open (const String &fileName, ByteIO::OpenOption=ByteIO::Old, uInt filebufSize=65536)
 Open/create file.
void open (ByteIO *)
 Open by connecting to the given byte stream.
void open (TypeIO *)
 Open by connecting to the given file descriptor.
void close ()
 Close file opened.
ByteIO::OpenOption fileOption () const
 Return the file option.
uInt putend ()
 End putting an object.
const StringgetNextType ()
 Get the type of the next object stored.
uInt getend ()
 End reading an object.
uInt putstart (const String &objectType, uInt objectVersion)
 Start putting an object.
uInt putstart (const Char *objectType, uInt objectVersion)
AipsIOoperator<< (const Bool &value)
 Put a single value.
AipsIOoperator<< (const Char &value)
AipsIOoperator<< (const uChar &value)
AipsIOoperator<< (const short &value)
AipsIOoperator<< (const unsigned short &value)
AipsIOoperator<< (const int &value)
AipsIOoperator<< (const unsigned int &value)
AipsIOoperator<< (const Int64 &value)
AipsIOoperator<< (const uInt64 &value)
AipsIOoperator<< (const float &value)
AipsIOoperator<< (const double &value)
AipsIOoperator<< (const Complex &value)
AipsIOoperator<< (const DComplex &value)
AipsIOoperator<< (const String &value)
AipsIOoperator<< (const Char *value)
AipsIOput (uInt nrval, const Bool *values, Bool putNR=True)
 Put an array of values with the given number of values.
AipsIOput (uInt nrval, const Char *values, Bool putNR=True)
AipsIOput (uInt nrval, const uChar *values, Bool putNR=True)
AipsIOput (uInt nrval, const short *values, Bool putNR=True)
AipsIOput (uInt nrval, const unsigned short *values, Bool putNR=True)
AipsIOput (uInt nrval, const int *values, Bool putNR=True)
AipsIOput (uInt nrval, const unsigned int *values, Bool putNR=True)
AipsIOput (uInt nrval, const Int64 *values, Bool putNR=True)
AipsIOput (uInt nrval, const uInt64 *values, Bool putNR=True)
AipsIOput (uInt nrval, const float *values, Bool putNR=True)
AipsIOput (uInt nrval, const double *values, Bool putNR=True)
AipsIOput (uInt nrval, const Complex *values, Bool putNR=True)
AipsIOput (uInt nrval, const DComplex *values, Bool putNR=True)
AipsIOput (uInt nrval, const String *values, Bool putNR=True)
Int64 getpos ()
 Get and set file-offset.
Int64 setpos (Int64 offset)
uInt getstart (const String &objectType)
 Start reading an object.
uInt getstart (const Char *objectType)
AipsIOoperator>> (Bool &value)
 Get a single value.
AipsIOoperator>> (Char &value)
AipsIOoperator>> (uChar &value)
AipsIOoperator>> (short &value)
AipsIOoperator>> (unsigned short &value)
AipsIOoperator>> (int &value)
AipsIOoperator>> (unsigned int &value)
AipsIOoperator>> (Int64 &value)
AipsIOoperator>> (uInt64 &value)
AipsIOoperator>> (float &value)
AipsIOoperator>> (double &value)
AipsIOoperator>> (Complex &value)
AipsIOoperator>> (DComplex &value)
AipsIOoperator>> (String &value)
AipsIOget (uInt nrval, Bool *values)
 Read in nrval values into the user-supplied values buffer.
AipsIOget (uInt nrval, Char *values)
AipsIOget (uInt nrval, uChar *values)
AipsIOget (uInt nrval, short *values)
AipsIOget (uInt nrval, unsigned short *values)
AipsIOget (uInt nrval, int *values)
AipsIOget (uInt nrval, unsigned int *values)
AipsIOget (uInt nrval, Int64 *values)
AipsIOget (uInt nrval, uInt64 *values)
AipsIOget (uInt nrval, float *values)
AipsIOget (uInt nrval, double *values)
AipsIOget (uInt nrval, Complex *values)
AipsIOget (uInt nrval, DComplex *values)
AipsIOget (uInt nrval, String *values)
AipsIOgetnew (uInt &nrval, Bool *&values)
 Read in values as written by the function put.
AipsIOgetnew (uInt &nrval, Char *&values)
AipsIOgetnew (uInt &nrval, uChar *&values)
AipsIOgetnew (uInt &nrval, short *&values)
AipsIOgetnew (uInt &nrval, unsigned short *&values)
AipsIOgetnew (uInt &nrval, int *&values)
AipsIOgetnew (uInt &nrval, unsigned int *&values)
AipsIOgetnew (uInt &nrval, Int64 *&values)
AipsIOgetnew (uInt &nrval, uInt64 *&values)
AipsIOgetnew (uInt &nrval, float *&values)
AipsIOgetnew (uInt &nrval, double *&values)
AipsIOgetnew (uInt &nrval, Complex *&values)
AipsIOgetnew (uInt &nrval, DComplex *&values)
AipsIOgetnew (uInt &nrval, String *&values)

Public Attributes

uInt filebufSize

Private Member Functions

void openInit (ByteIO::OpenOption)
 Initialize everything for the open.
void testput ()
 testput tests if a put can be done; ie.
void testget ()
 Test if get is possible (throw exception if not).
void testgetLength ()
 Test if get did not exceed object.
void testputerr ()
 Throw exception for testput.
void testgeterr ()
 Throw exception for testget.
void testgeterrLength ()
 Throw exception for testgetLength.

Private Attributes

Int opened_p
 1 = file was opened by AipsIO 0 = file not opened -1 = file opened by user (=fd passed)
ByteIO::OpenOption fopt_p
 File open option.
int swput_p
 <0 = not opened for put 0 = no putstart done >0 = put is possible
int swget_p
 <0 = not opened for get 0 = no getstart done >0 = get is possible
uInt level_p
 Nested object level.
uInt maxlev_p
 Current size of objlen and objptr.
Block< uIntobjlen_p
 Object length at each level.
Block< uIntobjtln_p
 Object length to be read at each level.
Block< Int64objptr_p
 Offset of length at each level.
Bool hasCachedType_p
 True = the object type has already been read.
String objectType_p
 The cached object type.
RegularFileIOfile_p
 The file object.
TypeIOio_p
 The actual IO object.
Bool seekable_p
 Is the file is seekable?

Static Private Attributes

static const uInt magicval_p
 magic value to check sync.


Constructor & Destructor Documentation

casa::AipsIO::AipsIO (  ) 

No file attached yet.

casa::AipsIO::AipsIO ( const String fileName,
ByteIO::OpenOption  = ByteIO::Old,
uInt  filebufSize = 65536 
) [explicit]

Construct and open/create a file with the given name.

The actual IO is done via a TypeIO object using a filebuf with a buffer of the given size.

Construct and open create a file with the given name This can for instance by used to use AipsIO on a file descriptor for which a link FilebufIO FilebufIO endlink object has been created The actual IO is done via a CanonicalIO object on top of it* casa::AipsIO::AipsIO ( ByteIO  )  [explicit]

casa::AipsIO::AipsIO ( TypeIO  )  [explicit]

Construct and open by connecting to the given file descriptor.

The actual IO is done via a filebuf object with a buffer of the given size.

casa::AipsIO::~AipsIO (  ) 

Close if not done yet.


Member Function Documentation

void casa::AipsIO::open ( const String fileName,
ByteIO::OpenOption  = ByteIO::Old,
uInt  filebufSize = 65536 
)

Open/create file.

An exception is thrown if the object contains an already open file.

void casa::AipsIO::open ( ByteIO  ) 

Open by connecting to the given byte stream.

This can for instance by used to use AipsIO on a file descriptor for which a FilebufIO object has been created. The actual IO is done via a CanonicalIO object on top of it. An exception is thrown if the object contains an already open file.

void casa::AipsIO::open ( TypeIO  ) 

Open by connecting to the given file descriptor.

An exception is thrown if the object contains an already open file.

void casa::AipsIO::close (  ) 

Close file opened.

ByteIO::OpenOption casa::AipsIO::fileOption (  )  const [inline]

Return the file option.

Definition at line 429 of file AipsIO.h.

References fopt_p.

uInt casa::AipsIO::putstart ( const String objectType,
uInt  objectVersion 
)

Start putting an object.

This writes the object type and version. When reading back getstart calls have to be done in the same way. Getstart checks the type and returns the version. The user can use that to correctly read back objects with different versions.
Data in the outermost object cannot be put before a putstart is done. Data in nested objects can be put without an intermediate putstart. However, for complex objects it is recommended to do a putstart to have a better checking.
After all values (inclusing nested objects) of the object have been put, a call to putend has to be done.

uInt casa::AipsIO::putstart ( const Char objectType,
uInt  objectVersion 
)

AipsIO& casa::AipsIO::operator<< ( const Bool value  ) 

Put a single value.

AipsIO& casa::AipsIO::operator<< ( const Char value  ) 

AipsIO& casa::AipsIO::operator<< ( const uChar value  ) 

AipsIO& casa::AipsIO::operator<< ( const short &  value  ) 

AipsIO& casa::AipsIO::operator<< ( const unsigned short &  value  ) 

AipsIO& casa::AipsIO::operator<< ( const int &  value  ) 

AipsIO& casa::AipsIO::operator<< ( const unsigned int &  value  ) 

AipsIO& casa::AipsIO::operator<< ( const Int64 value  ) 

AipsIO& casa::AipsIO::operator<< ( const uInt64 value  ) 

AipsIO& casa::AipsIO::operator<< ( const float &  value  ) 

AipsIO& casa::AipsIO::operator<< ( const double &  value  ) 

AipsIO& casa::AipsIO::operator<< ( const Complex &  value  ) 

AipsIO& casa::AipsIO::operator<< ( const DComplex &  value  ) 

AipsIO& casa::AipsIO::operator<< ( const String value  ) 

AipsIO& casa::AipsIO::operator<< ( const Char value  ) 

AipsIO& casa::AipsIO::put ( uInt  nrval,
const Bool values,
Bool  putNR = True 
)

Put an array of values with the given number of values.

If the flag putNr is set, the number of values is put first.

AipsIO& casa::AipsIO::put ( uInt  nrval,
const Char values,
Bool  putNR = True 
)

AipsIO& casa::AipsIO::put ( uInt  nrval,
const uChar values,
Bool  putNR = True 
)

AipsIO& casa::AipsIO::put ( uInt  nrval,
const short *  values,
Bool  putNR = True 
)

AipsIO& casa::AipsIO::put ( uInt  nrval,
const unsigned short *  values,
Bool  putNR = True 
)

AipsIO& casa::AipsIO::put ( uInt  nrval,
const int *  values,
Bool  putNR = True 
)

AipsIO& casa::AipsIO::put ( uInt  nrval,
const unsigned int *  values,
Bool  putNR = True 
)

AipsIO& casa::AipsIO::put ( uInt  nrval,
const Int64 values,
Bool  putNR = True 
)

AipsIO& casa::AipsIO::put ( uInt  nrval,
const uInt64 values,
Bool  putNR = True 
)

AipsIO& casa::AipsIO::put ( uInt  nrval,
const float *  values,
Bool  putNR = True 
)

AipsIO& casa::AipsIO::put ( uInt  nrval,
const double *  values,
Bool  putNR = True 
)

AipsIO& casa::AipsIO::put ( uInt  nrval,
const Complex *  values,
Bool  putNR = True 
)

AipsIO& casa::AipsIO::put ( uInt  nrval,
const DComplex *  values,
Bool  putNR = True 
)

AipsIO& casa::AipsIO::put ( uInt  nrval,
const String values,
Bool  putNR = True 
)

uInt casa::AipsIO::putend (  ) 

End putting an object.

It returns the object length (including possible nested objects).

Int64 casa::AipsIO::getpos (  ) 

Get and set file-offset.

Int64 casa::AipsIO::setpos ( Int64  offset  ) 

const String& casa::AipsIO::getNextType (  ) 

Get the type of the next object stored.

This is not possible if a put is in progress.

uInt casa::AipsIO::getstart ( const String objectType  ) 

Start reading an object.

It will check if the given type matches the one stored by putstart. It returns the object version which can be used to read in older version of the object correctly.
After all values (inclusing nested objects) of the object have been read, a call to getend has to be done.

uInt casa::AipsIO::getstart ( const Char objectType  ) 

AipsIO& casa::AipsIO::operator>> ( Bool value  ) 

Get a single value.

AipsIO& casa::AipsIO::operator>> ( Char value  ) 

AipsIO& casa::AipsIO::operator>> ( uChar value  ) 

AipsIO& casa::AipsIO::operator>> ( short &  value  ) 

AipsIO& casa::AipsIO::operator>> ( unsigned short &  value  ) 

AipsIO& casa::AipsIO::operator>> ( int &  value  ) 

AipsIO& casa::AipsIO::operator>> ( unsigned int &  value  ) 

AipsIO& casa::AipsIO::operator>> ( Int64 value  ) 

AipsIO& casa::AipsIO::operator>> ( uInt64 value  ) 

AipsIO& casa::AipsIO::operator>> ( float &  value  ) 

AipsIO& casa::AipsIO::operator>> ( double &  value  ) 

AipsIO& casa::AipsIO::operator>> ( Complex &  value  ) 

AipsIO& casa::AipsIO::operator>> ( DComplex &  value  ) 

AipsIO& casa::AipsIO::operator>> ( String value  ) 

AipsIO& casa::AipsIO::get ( uInt  nrval,
Bool values 
)

Read in nrval values into the user-supplied values buffer.

The buffer must be long enough.

AipsIO& casa::AipsIO::get ( uInt  nrval,
Char values 
)

AipsIO& casa::AipsIO::get ( uInt  nrval,
uChar values 
)

AipsIO& casa::AipsIO::get ( uInt  nrval,
short *  values 
)

AipsIO& casa::AipsIO::get ( uInt  nrval,
unsigned short *  values 
)

AipsIO& casa::AipsIO::get ( uInt  nrval,
int *  values 
)

AipsIO& casa::AipsIO::get ( uInt  nrval,
unsigned int *  values 
)

AipsIO& casa::AipsIO::get ( uInt  nrval,
Int64 values 
)

AipsIO& casa::AipsIO::get ( uInt  nrval,
uInt64 values 
)