casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DataType.h
Go to the documentation of this file.
1 //# DataType.h: data types (primarily) in the table system
2 //# Copyright (C) 1993,1994,1995,1996,1999,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: 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_DATATYPE_H
29 #define CASA_DATATYPE_H
30 
31 #include <casacore/casa/aips.h>
34 
35 #include <casacore/casa/iosfwd.h>
36 namespace casacore { //# NAMESPACE CASACORE - BEGIN
37 
38 class Table;
39 template<class T> class Array;
40 template<class T> class Quantum;
41 class String;
42 class Record;
43 
44 // <summary> Data types (primarily) in the table system </summary>
45 // <use visibility=export>
46 // <reviewed reviewer="Paul Shannon" date="1995/05/01" tests="tDataType" demos="">
47 // </reviewed>
48 
49 // <synopsis>
50 // DataType enumerates possible data types. While this enum is primarily
51 // used in the <linkto module="Tables:description">table</linkto> system, some
52 // use of it is made elsewhere. Besides the enum
53 // itself, <src>operator<<</src> is defined for DataType; it prints a DataType
54 // in the form <src>DataType=Bool</src>.
55 //
56 // Also, global functions are written which take a "const pointer to type" and
57 // return its DataType (TpOther if unknown). These functions can occasionally
58 // allow one to avoid a switch on type, and can be useful in constructing
59 // templated classes which are only valid for certain types.
60 //
61 // Global functions are also provided which allow one to convert an
62 // array type to the equivalent scalar type and vice versa.
63 //
64 // <note role=warning>
65 // New data types should be added just before TpNumberOfTypes, and after all
66 // the existing enumerations, to avoid changing the number of an existing type
67 // which would cause misinterpretation of data types stored in existing files.
68 // Note also that if any new scalar and array types are added that this
69 // will break the exising isScalar, isArray, asScalar and asArray functions.
70 // </note>
71 //
72 // <note role=tip>
73 // Data types <src>long</src> and <src>unsigned long</src> are not
74 // possible. The types <src>Int</src> and <src>uInt</src> are always
75 // 4 bytes, so <src>long</src> is not needed and may only cause
76 // confusion.
77 // </note>
78 //
79 // </synopsis>
80 
81 // <example>
82 // The simplest uses of the DataType enumeration and functions are fairly
83 // obvious, for example:
84 // <srcblock>
85 // Double d;
86 // DataType type = whatType(&d);
87 // cout << type << endl;
88 // switch(type) {
89 // case TpChar: ...
90 // ...
91 // case TpDouble: ...
92 // }
93 // </srcblock>
94 //
95 // A less obvious use is for "attaching" a templated object or function to a
96 // non-templated object in a safe way. For example:
97 // <srcblock>
98 // class IntFloatContainer {
99 // public:
100 // Int intval;
101 // Float floatval;
102 // void *ptr(DataType type) {
103 // if (type == whatType(&intval))
104 // return &intval;
105 // else if (type == whatType(&floatval))
106 // return &floatval;
107 // else
108 // return 0; // Illegal type
109 // }
110 // };
111 //
112 // template<class T> class ValueAccessor {
113 // public:
114 // ValueAccessor(IntFloatContainer *container) : container_p(container) {
115 // if (container_p->ptr(whatType(static_cast<T *>(0))) == 0)
116 // throw(AipsError("Illegal type..."));
117 // }
118 // T &value() { return *((T*)container_p->ptr(whatType(static_cast<T *>(0)))); }
119 // private:
120 // IntFloatContainer *container_p;
121 // };
122 // </srcblock>
123 //
124 // So, this example provides a typesafe interface to values of only a small
125 // number of types (and it fairly gracefully allows additional types to be
126 // added; in particular the accessor class needs no modification). Techniques
127 // such as this are appropriate for situations where one needs to deal with
128 // many (but finite) numbers of types. For example, with FITS.
129 // </example>
130 
131 // <todo asof="1995/03/01">
132 // <li> Clean up comment as soon as enum's are properly extracted.
133 // </todo>
134 
135 // <linkfrom anchor=DataType modules="Tables">
136 // Enumeration of the <here>data types</here> in the table system
137 // </linkfrom>
138 //
139 // Enumeration of the possible data types for keywords and table columns.
140 // <group name=DataType>
141 enum DataType {TpBool, TpChar, TpUChar,
142  TpShort, TpUShort, TpInt, TpUInt,
143  TpFloat, TpDouble,
144  TpComplex, TpDComplex, TpString,
145  TpTable,
146  TpArrayBool, TpArrayChar, TpArrayUChar,
147  TpArrayShort, TpArrayUShort, TpArrayInt, TpArrayUInt,
148  TpArrayFloat, TpArrayDouble,
149  TpArrayComplex, TpArrayDComplex, TpArrayString,
150  TpRecord, TpOther,
151 //#// TpLDouble,
152 //#// TpArrayLDouble,
153  TpQuantity, TpArrayQuantity,
154  TpInt64, TpArrayInt64,
155  // Since we start at zero, this is the number of types in the
156  // enum.
157  TpNumberOfTypes
158  };
159 
160 
161 // Write a formated representation (e.g., Type=Bool) of the given data type.
162 ostream &operator<<(ostream &os, DataType type);
163 
164 // These (overloaded) functions return DataType that corresponds to to the
165 // type that is being pointed at. A pointer is used to avoid to avoid having
166 // to create the object if it is of Array or Table types. At least for CFront,
167 // it also avoids those types from being instantiated (they are forward
168 // declared). The void* function matches any type (if none other will), and
169 // returns TpOther.
170 // <group>
171 inline DataType whatType(const void *) { return TpOther; }
172 inline DataType whatType(const Bool *) { return TpBool; }
173 inline DataType whatType(const Char *) { return TpChar; }
174 inline DataType whatType(const uChar *) { return TpUChar; }
175 inline DataType whatType(const Short*) {return TpShort ; }
176 inline DataType whatType(const uShort*) {return TpUShort ; }
177 inline DataType whatType(const Int*) {return TpInt ; }
178 inline DataType whatType(const uInt*) {return TpUInt ; }
179 inline DataType whatType(const Int64*) {return TpInt64 ; }
180 inline DataType whatType(const float*) {return TpFloat ; }
181 inline DataType whatType(const double*) {return TpDouble ; }
182 inline DataType whatType(const Complex*) {return TpComplex ; }
183 inline DataType whatType(const DComplex*) {return TpDComplex ; }
184 inline DataType whatType(const String*) {return TpString ; }
185 inline DataType whatType(const Table*) {return TpTable ; }
186 inline DataType whatType(const Array<Bool> *) { return TpArrayBool; }
187 inline DataType whatType(const Array<Char> *) { return TpArrayChar; }
188 inline DataType whatType(const Array<uChar> *) { return TpArrayUChar; }
189 inline DataType whatType(const Array<Short>*) {return TpArrayShort ; }
190 inline DataType whatType(const Array<uShort> *) {return TpArrayUShort ; }
191 inline DataType whatType(const Array<Int> *) {return TpArrayInt ; }
192 inline DataType whatType(const Array<uInt> *) {return TpArrayUInt ; }
193 inline DataType whatType(const Array<Int64> *) {return TpArrayInt64 ; }
194 inline DataType whatType(const Array<float> *) {return TpArrayFloat ; }
195 inline DataType whatType(const Array<double> *) {return TpArrayDouble ; }
196 inline DataType whatType(const Array<Complex> *) {return TpArrayComplex ; }
197 inline DataType whatType(const Array<DComplex> *) {return TpArrayDComplex ; }
198 inline DataType whatType(const Array<String> *) {return TpArrayString ; }
199 inline DataType whatType(const Record *) {return TpRecord ; }
200 inline DataType whatType(const Quantum<Double> *) {return TpQuantity ; }
202  {return TpArrayQuantity ; }
203 // </group>
204 
205 // It is sometimes useful to discover what the corresponding
206 // scalar (or array) type is for a given array (or scalar) type.
207 // Calling these with TpOther, TpTable, and TpRecord results
208 // in an exception being thrown.
209 // <group>
210 DataType asScalar(DataType type);
211 DataType asArray(DataType type);
212 // </group>
213 
214 // It is occasionally useful to discover whether or not a DataType represents
215 // an array or scalar value. Note that TpTable, TpRecord, and TpOther are neither
216 // scalar nor array types.
217 // <group>
218 Bool isScalar(DataType type);
219 Bool isArray(DataType type);
220 Bool isScalarFun(DataType type); //{return isScalar(type);}
221 // </group>
222 
223 // It is sometimes useful to discover if a DataType represents a real
224 // numeric value (i.e., can it be cast to a Double?) This returns True
225 // for both real scalar and array type.
226 Bool isReal(DataType type);
227 
228 // Returns True for Complex or DComplex scalar or array types
229 Bool isComplex(DataType type);
230 
231 // Returns True if the type is either Real or Complex/DComplex
232 Bool isNumeric(DataType type);
233 
234 // </group>
235 
236 
237 } //# NAMESPACE CASACORE - END
238 
239 #endif
long long Int64
Define the extra non-standard types used by Casacore (like proposed uSize, Size)
Definition: aipsxtype.h:38
int Int
Definition: aipstype.h:50
Main interface class to a read/write table.
Definition: Table.h:153
DataType whatType(const Array< Int64 > *)
Definition: DataType.h:193
unsigned char uChar
Definition: aipstype.h:47
DataType whatType(const Array< String > *)
Definition: DataType.h:198
DataType whatType(const Array< Short > *)
Definition: DataType.h:189
char Char
Definition: aipstype.h:46
virtual casacore::String type() const
Implements RegionShape::type.
Definition: RegionShapes.h:548
DataType whatType(const Array< uChar > *)
Definition: DataType.h:188
DataType whatType(const Array< double > *)
Definition: DataType.h:195
ostream & operator<<(ostream &os, const std::pair< T, U > &p)
DataType whatType(const Array< Complex > *)
Definition: DataType.h:196
DataType whatType(const Array< uInt > *)
Definition: DataType.h:192
short Short
Definition: aipstype.h:48
DataType whatType(const DComplex *)
Definition: DataType.h:183
DataType whatType(const Quantum< Double > *)
Definition: DataType.h:200
A hierarchical collection of named fields of various types.
Definition: Record.h:180
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42
DataType whatType(const Array< Quantum< Double > > *)
Definition: DataType.h:201
DataType whatType(const Array< Int > *)
Definition: DataType.h:191
DataType whatType(const Array< Bool > *)
Definition: DataType.h:186
DataType whatType(const Array< float > *)
Definition: DataType.h:194
String: the storage and methods of handling collections of characters.
Definition: String.h:223
DataType whatType(const Complex *)
Definition: DataType.h:182
DataType whatType(const Array< uShort > *)
Definition: DataType.h:190
DataType whatType(const Array< DComplex > *)
Definition: DataType.h:197
DataType whatType(const Array< Char > *)
Definition: DataType.h:187
unsigned int uInt
Definition: aipstype.h:51
DataType whatType(const void *)
These (overloaded) functions return DataType that corresponds to to the type that is being pointed at...
Definition: DataType.h:171
unsigned short uShort
Definition: aipstype.h:49
#define casacore
&lt;X11/Intrinsic.h&gt; #defines true, false, casacore::Bool, and String.
Definition: X11Intrinsic.h:42