casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
DataType.h
Go to the documentation of this file.
00001 //# DataType.h: data types (primarily) in the table system
00002 //# Copyright (C) 1993,1994,1995,1996,1999,2000,2001
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //# $Id: DataType.h 20901 2010-06-09 07:23:37Z gervandiepen $
00027 
00028 #ifndef CASA_DATATYPE_H
00029 #define CASA_DATATYPE_H
00030 
00031 #include <casa/aips.h>
00032 #include <casa/BasicSL/Complex.h>
00033 #include <casa/BasicSL/String.h>
00034 
00035 #include <casa/iosfwd.h>
00036 namespace casa { //# NAMESPACE CASA - BEGIN
00037 
00038 class Table;
00039 template<class T> class Array;
00040 template<class T> class Quantum;
00041 class String;
00042 class Record;
00043 
00044 // <summary> Data types (primarily) in the table system </summary>
00045 // <use visibility=export>
00046 // <reviewed reviewer="Paul Shannon" date="1995/05/01" tests="tDataType" demos="">
00047 // </reviewed>
00048 
00049 // <synopsis>
00050 // DataType enumerates possible data types. While this enum is primarily
00051 // used in the <linkto module="Tables:description">table</linkto> system, some
00052 // use of it is made elsewhere. Besides the enum
00053 // itself, <src>operator<<</src> is defined for DataType; it prints a DataType
00054 // in the form <src>DataType=Bool</src>.
00055 //
00056 // Also, global functions are written which take a "const pointer to type" and
00057 // return its DataType (TpOther if unknown). These functions can occasionally
00058 // allow one to avoid a switch on type, and can be useful in constructing
00059 // templated classes which are only valid for certain types.
00060 //
00061 // Global functions are also provided which allow one to convert an
00062 // array type to the equivalent scalar type and vice versa.
00063 //
00064 // <note role=warning>
00065 // New data types should be added just before TpNumberOfTypes, and after all
00066 // the existing enumerations, to avoid changing the number of an existing type
00067 // which would cause misinterpretation of data types stored in existing  files.
00068 // Note also that if any new scalar and array types are added that this
00069 // will break the exising isScalar, isArray, asScalar and asArray functions.
00070 // </note>
00071 //
00072 // <note role=tip>
00073 // Data types <src>long</src> and <src>unsigned long</src> are not
00074 // possible.  The types <src>Int</src> and <src>uInt</src> are always
00075 // 4 bytes, so <src>long</src> is not needed and may only cause
00076 // confusion.
00077 // </note>
00078 // 
00079 // </synopsis>
00080 
00081 // <example>
00082 // The simplest uses of the DataType enumeration and functions are fairly 
00083 // obvious, for example:
00084 // <srcblock>
00085 //    Double d;
00086 //    DataType type = whatType(&d);
00087 //    cout << type << endl;
00088 //    switch(type) {
00089 //    case TpChar:   ...
00090 //    ...
00091 //    case TpDouble: ...
00092 //    }
00093 // </srcblock>
00094 //
00095 // A less obvious use is for "attaching" a templated object or function to a
00096 // non-templated object in a safe way. For example:
00097 // <srcblock>
00098 // class IntFloatContainer {
00099 // public:
00100 //     Int intval;
00101 //     Float floatval;
00102 //     void *ptr(DataType type) {
00103 //         if (type == whatType(&intval))
00104 //             return &intval;
00105 //         else if (type == whatType(&floatval))
00106 //             return &floatval;
00107 //         else
00108 //             return 0; // Illegal type
00109 //     }
00110 // };
00111 //
00112 // template<class T> class ValueAccessor {
00113 // public:
00114 //     ValueAccessor(IntFloatContainer *container) : container_p(container) {
00115 //         if (container_p->ptr(whatType(static_cast<T *>(0))) == 0)
00116 //             throw(AipsError("Illegal type..."));
00117 //     }
00118 //     T &value() { return *((T*)container_p->ptr(whatType(static_cast<T *>(0)))); }
00119 // private:
00120 //     IntFloatContainer *container_p;
00121 // };
00122 // </srcblock>
00123 //
00124 // So, this example provides a typesafe interface to values of only a small
00125 // number of types (and it fairly gracefully allows additional types to be
00126 // added; in particular the accessor class needs no modification). Techniques
00127 // such as this are appropriate for situations where one needs to deal with
00128 // many (but finite) numbers of types. For example, with FITS.
00129 // </example>
00130 
00131 // <todo asof="1995/03/01">
00132 //   <li> Clean up comment as soon as enum's are properly extracted.
00133 // </todo>
00134 
00135 // <linkfrom anchor=DataType modules="Tables">
00136 //   Enumeration of the <here>data types</here> in the table system
00137 // </linkfrom>
00138 //
00139 // Enumeration of the possible data types for keywords and table columns.
00140 // <group name=DataType>
00141 enum DataType {TpBool,    TpChar,     TpUChar,
00142                TpShort,   TpUShort,   TpInt,     TpUInt,
00143                TpFloat,   TpDouble,   
00144                TpComplex, TpDComplex, TpString,
00145                TpTable,
00146                TpArrayBool,    TpArrayChar,     TpArrayUChar,
00147                TpArrayShort,   TpArrayUShort,   TpArrayInt,   TpArrayUInt,
00148                TpArrayFloat,   TpArrayDouble,
00149                TpArrayComplex, TpArrayDComplex, TpArrayString,
00150                TpRecord, TpOther,
00151 //#// TpLDouble,
00152 //#// TpArrayLDouble,
00153                TpQuantity, TpArrayQuantity,
00154                TpInt64, TpArrayInt64,
00155                // Since we start at zero, this is the number of types in the
00156                // enum.
00157                TpNumberOfTypes
00158               };
00159 
00160 
00161 // Write a formated representation (e.g., Type=Bool) of the given data type.
00162 ostream &operator<<(ostream &os, DataType type);
00163 
00164 // These (overloaded) functions return DataType that corresponds to to the
00165 // type that is being pointed at. A pointer is used to avoid to avoid having
00166 // to create the object if it is of Array or Table types. At least for CFront,
00167 // it also avoids those types from being instantiated (they are forward
00168 // declared). The void* function matches any type (if none other will), and
00169 // returns TpOther.
00170 // <group>
00171 inline DataType whatType(const void *)   { return TpOther; }
00172 inline DataType whatType(const Bool *)   { return TpBool; }
00173 inline DataType whatType(const Char *)   { return TpChar; }
00174 inline DataType whatType(const uChar *)  { return TpUChar; }
00175 inline DataType whatType(const Short*) {return TpShort ; }
00176 inline DataType whatType(const uShort*) {return TpUShort ; }
00177 inline DataType whatType(const Int*) {return TpInt ; }
00178 inline DataType whatType(const uInt*) {return TpUInt ; }
00179 inline DataType whatType(const Int64*) {return TpInt64 ; }
00180 inline DataType whatType(const float*) {return TpFloat ; }
00181 inline DataType whatType(const double*) {return TpDouble ; }
00182 inline DataType whatType(const Complex*) {return TpComplex ; }
00183 inline DataType whatType(const DComplex*) {return TpDComplex ; }
00184 inline DataType whatType(const String*) {return TpString ; }
00185 inline DataType whatType(const Table*) {return TpTable ; }
00186 inline DataType whatType(const Array<Bool> *)   { return TpArrayBool; }
00187 inline DataType whatType(const Array<Char> *)   { return TpArrayChar; }
00188 inline DataType whatType(const Array<uChar> *)  { return TpArrayUChar; }
00189 inline DataType whatType(const Array<Short>*) {return TpArrayShort ; }
00190 inline DataType whatType(const Array<uShort> *) {return TpArrayUShort ; }
00191 inline DataType whatType(const Array<Int> *) {return TpArrayInt ; }
00192 inline DataType whatType(const Array<uInt> *) {return TpArrayUInt ; }
00193 inline DataType whatType(const Array<Int64> *) {return TpArrayInt64 ; }
00194 inline DataType whatType(const Array<float> *) {return TpArrayFloat ; }
00195 inline DataType whatType(const Array<double> *) {return TpArrayDouble ; }
00196 inline DataType whatType(const Array<Complex> *) {return TpArrayComplex ; }
00197 inline DataType whatType(const Array<DComplex> *) {return TpArrayDComplex ; }
00198 inline DataType whatType(const Array<String> *) {return TpArrayString ; }
00199 inline DataType whatType(const Record *) {return TpRecord ; }
00200 inline DataType whatType(const Quantum<Double> *) {return TpQuantity ; }
00201 inline DataType whatType(const Array<Quantum<Double> > *)
00202                                                   {return TpArrayQuantity ; }
00203 // </group>
00204 
00205 // It is sometimes useful to discover what the corresponding
00206 // scalar (or array) type is for a given array (or scalar) type.
00207 // Calling these with TpOther, TpTable, and TpRecord results
00208 // in an exception being thrown.
00209 // <group>
00210 DataType asScalar(DataType type);
00211 DataType asArray(DataType type);
00212 // </group>
00213 
00214 // It is occasionally useful to discover whether or not a DataType represents
00215 // an array or scalar value. Note that TpTable, TpRecord, and TpOther are neither
00216 // scalar nor array types.
00217 // <group>
00218 Bool isScalar(DataType type);
00219 Bool isArray(DataType type);
00220 Bool isScalarFun(DataType type); //{return isScalar(type);}
00221 // </group>
00222 
00223 // It is sometimes useful to discover if a DataType represents a real 
00224 // numeric value (i.e., can it be cast to a Double?) This returns True
00225 // for both real scalar and array type.
00226 Bool isReal(DataType type);
00227 
00228 // Returns True for Complex or DComplex scalar or array types
00229 Bool isComplex(DataType type);
00230 
00231 // Returns True if the type is either Real or Complex/DComplex
00232 Bool isNumeric(DataType type);
00233 
00234 // </group>
00235 
00236 
00237 } //# NAMESPACE CASA - END
00238 
00239 #endif