casacore
Loading...
Searching...
No Matches
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: 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_DATATYPE_H
27#define CASA_DATATYPE_H
28
29#include <casacore/casa/aips.h>
30#include <casacore/casa/Arrays/ArrayFwd.h>
31#include <casacore/casa/BasicSL/Complex.h>
32#include <casacore/casa/BasicSL/String.h>
33
34#include <casacore/casa/iosfwd.h>
35namespace casacore { // # NAMESPACE CASACORE - BEGIN
36
37class Table;
38template <class T>
39class Quantum;
40class String;
41class Record;
42
43// <summary> Data types (primarily) in the table system </summary>
44// <use visibility=export>
45// <reviewed reviewer="Paul Shannon" date="1995/05/01" tests="tDataType" demos="">
46// </reviewed>
47
48// <synopsis>
49// DataType enumerates possible data types. While this enum is primarily
50// used in the <linkto module="Tables:description">table</linkto> system, some
51// use of it is made elsewhere. Besides the enum
52// itself, <src>operator<<</src> is defined for DataType; it prints a DataType
53// in the form <src>DataType=Bool</src>.
54//
55// Also, global functions are written which take a "const pointer to type" and
56// return its DataType (TpOther if unknown). These functions can occasionally
57// allow one to avoid a switch on type, and can be useful in constructing
58// templated classes which are only valid for certain types.
59//
60// Global functions are also provided which allow one to convert an
61// array type to the equivalent scalar type and vice versa.
62//
63// <note role=warning>
64// New data types should be added just before TpNumberOfTypes, and after all
65// the existing enumerations, to avoid changing the number of an existing type
66// which would cause misinterpretation of data types stored in existing files.
67// Note also that if any new scalar and array types are added that this
68// will break the exising isScalar, isArray, asScalar and asArray functions.
69// </note>
70//
71// <note role=tip>
72// Data types <src>long</src> and <src>unsigned long</src> are not
73// possible. The types <src>Int</src> and <src>uInt</src> are always
74// 4 bytes, so <src>long</src> is not needed and may only cause
75// confusion.
76// </note>
77//
78// </synopsis>
79
80// <example>
81// The simplest uses of the DataType enumeration and functions are fairly
82// obvious, for example:
83// <srcblock>
84// Double d;
85// DataType type = whatType(&d);
86// cout << type << endl;
87// switch(type) {
88// case TpChar: ...
89// ...
90// case TpDouble: ...
91// }
92// </srcblock>
93//
94// A less obvious use is for "attaching" a templated object or function to a
95// non-templated object in a safe way. For example:
96// <srcblock>
97// class IntFloatContainer {
98// public:
99// Int intval;
100// Float floatval;
101// void *ptr(DataType type) {
102// if (type == whatType(&intval))
103// return &intval;
104// else if (type == whatType(&floatval))
105// return &floatval;
106// else
107// return 0; // Illegal type
108// }
109// };
110//
111// template<class T> class ValueAccessor {
112// public:
113// ValueAccessor(IntFloatContainer *container) : container_p(container) {
114// if (container_p->ptr(whatType(static_cast<T *>(0))) == 0)
115// throw(AipsError("Illegal type..."));
116// }
117// T &value() { return *((T*)container_p->ptr(whatType(static_cast<T *>(0)))); }
118// private:
119// IntFloatContainer *container_p;
120// };
121// </srcblock>
122//
123// So, this example provides a typesafe interface to values of only a small
124// number of types (and it fairly gracefully allows additional types to be
125// added; in particular the accessor class needs no modification). Techniques
126// such as this are appropriate for situations where one needs to deal with
127// many (but finite) numbers of types. For example, with FITS.
128// </example>
129
130// <todo asof="1995/03/01">
131// <li> Clean up comment as soon as enum's are properly extracted.
132// </todo>
133
134// <linkfrom anchor=DataType modules="Tables">
135// Enumeration of the <here>data types</here> in the table system
136// </linkfrom>
137//
138// Enumeration of the possible data types for keywords and table columns.
139// <group name=DataType>
178
179// Write a formated representation (e.g., Type=Bool) of the given data type.
180ostream &operator<<(ostream &os, DataType type);
181
182// These (specialized) functions return the DataType that corresponds
183// to the template type. TpOther is returned for types that are
184// not specialized, as is void.
185// <group>
186template <typename T>
188 return TpOther;
189}
190
191#define DEFINE_WHATTYPE(SPECIALIZED_TYPE, RETURN_TYPE) \
192 template <> \
193 inline DataType whatType<SPECIALIZED_TYPE>() { \
194 return RETURN_TYPE; \
195 }
196
197DEFINE_WHATTYPE(void, TpOther)
198DEFINE_WHATTYPE(Bool, TpBool)
199DEFINE_WHATTYPE(Char, TpChar)
200DEFINE_WHATTYPE(uChar, TpUChar)
201DEFINE_WHATTYPE(Short, TpShort)
202DEFINE_WHATTYPE(uShort, TpUShort)
203DEFINE_WHATTYPE(Int, TpInt)
204DEFINE_WHATTYPE(uInt, TpUInt)
205DEFINE_WHATTYPE(Int64, TpInt64)
206DEFINE_WHATTYPE(float, TpFloat)
207DEFINE_WHATTYPE(double, TpDouble)
208DEFINE_WHATTYPE(Complex, TpComplex)
209DEFINE_WHATTYPE(DComplex, TpDComplex)
210DEFINE_WHATTYPE(String, TpString)
211DEFINE_WHATTYPE(Table, TpTable)
212DEFINE_WHATTYPE(Array<Bool>, TpArrayBool)
213DEFINE_WHATTYPE(Array<Char>, TpArrayChar)
214DEFINE_WHATTYPE(Array<uChar>, TpArrayUChar)
215DEFINE_WHATTYPE(Array<Short>, TpArrayShort)
216DEFINE_WHATTYPE(Array<uShort>, TpArrayUShort)
217DEFINE_WHATTYPE(Array<Int>, TpArrayInt)
218DEFINE_WHATTYPE(Array<uInt>, TpArrayUInt)
219DEFINE_WHATTYPE(Array<Int64>, TpArrayInt64)
220DEFINE_WHATTYPE(Array<float>, TpArrayFloat)
221DEFINE_WHATTYPE(Array<double>, TpArrayDouble)
222DEFINE_WHATTYPE(Array<Complex>, TpArrayComplex)
223DEFINE_WHATTYPE(Array<DComplex>, TpArrayDComplex)
224DEFINE_WHATTYPE(Array<String>, TpArrayString)
225DEFINE_WHATTYPE(Record, TpRecord)
226DEFINE_WHATTYPE(Quantum<Double>, TpQuantity)
227DEFINE_WHATTYPE(Array<Quantum<Double>>, TpArrayQuantity)
228
229#undef DEFINE_WHATTYPE
230
231// </group>
232
233// It is sometimes useful to discover what the corresponding
234// scalar (or array) type is for a given array (or scalar) type.
235// Calling these with TpOther, TpTable, and TpRecord results
236// in an exception being thrown.
237// <group>
240// </group>
241
248constexpr size_t SizeOfType(DataType dtype) {
249 switch (dtype) {
250 case DataType::TpBool:
251 case DataType::TpChar:
253 return 1;
256 return 2;
257 case DataType::TpInt:
258 case DataType::TpUInt:
260 return 4;
264 return 8;
266 return 16;
287 return 0;
288 }
289 return 0;
290}
291
292// It is occasionally useful to discover whether or not a DataType represents
293// an array or scalar value. Note that TpTable, TpRecord, and TpOther are neither
294// scalar nor array types.
295// <group>
298Bool isScalarFun(DataType type); //{return isScalar(type);}
299// </group>
300
301// It is sometimes useful to discover if a DataType represents a real
302// numeric value (i.e., can it be cast to a Double?) This returns True
303// for both real scalar and array type.
305
306// Returns True for Complex or DComplex scalar or array types
308
309// Returns True if the type is either Real or Complex/DComplex
311
312// </group>
313
314} // namespace casacore
315
316#endif
#define DEFINE_WHATTYPE(SPECIALIZED_TYPE, RETURN_TYPE)
Definition DataType.h:191
String: the storage and methods of handling collections of characters.
Definition String.h:355
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
unsigned char uChar
Definition aipstype.h:45
short Short
Definition aipstype.h:46
unsigned int uInt
Definition aipstype.h:49
unsigned short uShort
Definition aipstype.h:47
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
char Char
Definition aipstype.h:44
Bool isScalar(DataType type)
It is occasionally useful to discover whether or not a DataType represents an array or scalar value.
ostream & operator<<(ostream &os, DataType type)
Write a formated representation (e.g., Type=Bool) of the given data type.
Bool isComplex(DataType type)
Returns True for Complex or DComplex scalar or array types.
Bool isReal(DataType type)
It is sometimes useful to discover if a DataType represents a real numeric value (i....
DataType asScalar(DataType type)
It is sometimes useful to discover what the corresponding scalar (or array) type is for a given array...
constexpr size_t SizeOfType(DataType dtype)
Returns the number of bytes that this type takes when serialized to disk.
Definition DataType.h:248
Bool isNumeric(DataType type)
Returns True if the type is either Real or Complex/DComplex.
DataType whatType()
These (specialized) functions return the DataType that corresponds to the template type.
Definition DataType.h:187
@ TpNumberOfTypes
Since we start at zero, this is the number of types in the enum.
Definition DataType.h:176