casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Public Types | Public Member Functions
casa::DataType_global_functions_DataType Struct Reference

Data types (primarily) in the table system. More...

#include <DataType.h>

List of all members.

Public Types

enum  DataType {
  TpBool,
  TpChar,
  TpUChar,
  TpShort,
  TpUShort,
  TpInt,
  TpUInt,
  TpFloat,
  TpDouble,
  TpComplex,
  TpDComplex,
  TpString,
  TpTable,
  TpArrayBool,
  TpArrayChar,
  TpArrayUChar,
  TpArrayShort,
  TpArrayUShort,
  TpArrayInt,
  TpArrayUInt,
  TpArrayFloat,
  TpArrayDouble,
  TpArrayComplex,
  TpArrayDComplex,
  TpArrayString,
  TpRecord,
  TpOther,
  TpQuantity,
  TpArrayQuantity,
  TpInt64,
  TpArrayInt64,
  TpNumberOfTypes
}

Public Member Functions

ostream & operator<< (ostream &os, DataType type)
 Write a formated representation (e.g., Type=Bool) of the given data type.
DataType whatType (const void *)
 These (overloaded) functions return DataType that corresponds to to the type that is being pointed at.
DataType whatType (const Bool *)
DataType whatType (const Char *)
DataType whatType (const uChar *)
DataType whatType (const Short *)
DataType whatType (const uShort *)
DataType whatType (const Int *)
DataType whatType (const uInt *)
DataType whatType (const Int64 *)
DataType whatType (const float *)
DataType whatType (const double *)
DataType whatType (const Complex *)
DataType whatType (const DComplex *)
DataType whatType (const String *)
DataType whatType (const Table *)
DataType whatType (const Array< Bool > *)
DataType whatType (const Array< Char > *)
DataType whatType (const Array< uChar > *)
DataType whatType (const Array< Short > *)
DataType whatType (const Array< uShort > *)
DataType whatType (const Array< Int > *)
DataType whatType (const Array< uInt > *)
DataType whatType (const Array< Int64 > *)
DataType whatType (const Array< float > *)
DataType whatType (const Array< double > *)
DataType whatType (const Array< Complex > *)
DataType whatType (const Array< DComplex > *)
DataType whatType (const Array< String > *)
DataType whatType (const Record *)
DataType whatType (const Quantum< Double > *)
DataType whatType (const Array< Quantum< Double > > *)
DataType asScalar (DataType type)
 It is sometimes useful to discover what the corresponding scalar (or array) type is for a given array (or scalar) type.
DataType asArray (DataType type)
Bool isScalar (DataType type)
 It is occasionally useful to discover whether or not a DataType represents an array or scalar value.
Bool isArray (DataType type)
Bool isScalarFun (DataType type)
Bool isReal (DataType type)
 It is sometimes useful to discover if a DataType represents a real numeric value (i.e., can it be cast to a Double?) This returns True for both real scalar and array type.
Bool isComplex (DataType type)
 Returns True for Complex or DComplex scalar or array types.
Bool isNumeric (DataType type)
 Returns True if the type is either Real or Complex/DComplex.

Detailed Description

Data types (primarily) in the table system.

Intended use:

Public interface

Review Status

Reviewed By:
Paul Shannon
Date Reviewed:
1995/05/01
Test programs:
tDataType

Synopsis

DataType enumerates possible data types. While this enum is primarily used in the table system, some use of it is made elsewhere. Besides the enum itself, operator<< is defined for DataType; it prints a DataType in the form DataType=Bool.

Also, global functions are written which take a "const pointer to type" and return its DataType (TpOther if unknown). These functions can occasionally allow one to avoid a switch on type, and can be useful in constructing templated classes which are only valid for certain types.

Global functions are also provided which allow one to convert an array type to the equivalent scalar type and vice versa.


Warning: New data types should be added just before TpNumberOfTypes, and after all the existing enumerations, to avoid changing the number of an existing type which would cause misinterpretation of data types stored in existing files; Note also that if any new scalar and array types are added that this will break the exising isScalar, isArray, asScalar and asArray functions;


Tip: Data types long and unsigned long are not possible; The types Int and uInt are always 4 bytes, so long is not needed and may only cause confusion;

Example

The simplest uses of the DataType enumeration and functions are fairly obvious, for example:

       Double d;
       DataType type = whatType(&d);
       cout << type << endl;
       switch(type) {
       case TpChar:   ..\.
       ..\.
       case TpDouble: ..\.
       }

A less obvious use is for "attaching" a templated object or function to a non-templated object in a safe way. For example:

    class IntFloatContainer {
    public:
        Int intval;
        Float floatval;
        void *ptr(DataType type) {
            if (type == whatType(&intval))
                return &intval;
            else if (type == whatType(&floatval))
                return &floatval;
            else
                return 0; // Illegal type
        }
    };
   
    template<class T> class ValueAccessor {
    public:
        ValueAccessor(IntFloatContainer *container) : container_p(container) {
            if (container_p->ptr(whatType(static_cast<T *>(0))) == 0)
                throw(AipsError("Illegal type..."));
        }
        T &value() { return *((T*)container_p->ptr(whatType(static_cast<T *>(0)))); }
    private:
        IntFloatContainer *container_p;
    };

So, this example provides a typesafe interface to values of only a small number of types (and it fairly gracefully allows additional types to be added; in particular the accessor class needs no modification). Techniques such as this are appropriate for situations where one needs to deal with many (but finite) numbers of types. For example, with FITS.

To Do

Enumeration of the possible data types for keywords and table columns.

Definition at line 141 of file DataType.h.


Member Enumeration Documentation

Enumerator:
TpBool 
TpChar 
TpUChar 
TpShort 
TpUShort 
TpInt 
TpUInt 
TpFloat 
TpDouble 
TpComplex 
TpDComplex 
TpString 
TpTable 
TpArrayBool 
TpArrayChar 
TpArrayUChar 
TpArrayShort 
TpArrayUShort 
TpArrayInt 
TpArrayUInt 
TpArrayFloat 
TpArrayDouble 
TpArrayComplex 
TpArrayDComplex 
TpArrayString 
TpRecord 
TpOther 
TpQuantity 
TpArrayQuantity 
TpInt64 
TpArrayInt64 
TpNumberOfTypes 

Since we start at zero, this is the number of types in the enum.

Definition at line 142 of file DataType.h.


Member Function Documentation

It is sometimes useful to discover what the corresponding scalar (or array) type is for a given array (or scalar) type.

Calling these with TpOther, TpTable, and TpRecord results in an exception being thrown.

Returns True for Complex or DComplex scalar or array types.

Returns True if the type is either Real or Complex/DComplex.

It is sometimes useful to discover if a DataType represents a real numeric value (i.e., can it be cast to a Double?) This returns True for both real scalar and array type.

It is occasionally useful to discover whether or not a DataType represents an array or scalar value.

Note that TpTable, TpRecord, and TpOther are neither scalar nor array types.

ostream& casa::DataType_global_functions_DataType::operator<< ( ostream &  os,
DataType  type 
)

Write a formated representation (e.g., Type=Bool) of the given data type.

These (overloaded) functions return DataType that corresponds to to the type that is being pointed at.

A pointer is used to avoid to avoid having to create the object if it is of Array or Table types. At least for CFront, it also avoids those types from being instantiated (they are forward declared). The void* function matches any type (if none other will), and returns TpOther.

Definition at line 171 of file DataType.h.

Definition at line 172 of file DataType.h.

Definition at line 173 of file DataType.h.

Definition at line 174 of file DataType.h.

Definition at line 175 of file DataType.h.

Definition at line 176 of file DataType.h.

Definition at line 177 of file DataType.h.

Definition at line 178 of file DataType.h.

Definition at line 179 of file DataType.h.

Definition at line 180 of file DataType.h.

Definition at line 181 of file DataType.h.

Definition at line 182 of file DataType.h.

Definition at line 183 of file DataType.h.

Definition at line 184 of file DataType.h.

Definition at line 185 of file DataType.h.

Definition at line 186 of file DataType.h.

Definition at line 187 of file DataType.h.

Definition at line 188 of file DataType.h.

Definition at line 189 of file DataType.h.

Definition at line 190 of file DataType.h.

Definition at line 191 of file DataType.h.

Definition at line 192 of file DataType.h.

Definition at line 193 of file DataType.h.

Definition at line 194 of file DataType.h.

Definition at line 195 of file DataType.h.

Definition at line 196 of file DataType.h.

Definition at line 197 of file DataType.h.

Definition at line 198 of file DataType.h.

Definition at line 199 of file DataType.h.

Definition at line 200 of file DataType.h.

Definition at line 201 of file DataType.h.


The documentation for this struct was generated from the following file: