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> class Quantum;
39class String;
40class Record;
41
42// <summary> Data types (primarily) in the table system </summary>
43// <use visibility=export>
44// <reviewed reviewer="Paul Shannon" date="1995/05/01" tests="tDataType" demos="">
45// </reviewed>
46
47// <synopsis>
48// DataType enumerates possible data types. While this enum is primarily
49// used in the <linkto module="Tables:description">table</linkto> system, some
50// use of it is made elsewhere. Besides the enum
51// itself, <src>operator<<</src> is defined for DataType; it prints a DataType
52// in the form <src>DataType=Bool</src>.
53//
54// Also, global functions are written which take a "const pointer to type" and
55// return its DataType (TpOther if unknown). These functions can occasionally
56// allow one to avoid a switch on type, and can be useful in constructing
57// templated classes which are only valid for certain types.
58//
59// Global functions are also provided which allow one to convert an
60// array type to the equivalent scalar type and vice versa.
61//
62// <note role=warning>
63// New data types should be added just before TpNumberOfTypes, and after all
64// the existing enumerations, to avoid changing the number of an existing type
65// which would cause misinterpretation of data types stored in existing files.
66// Note also that if any new scalar and array types are added that this
67// will break the exising isScalar, isArray, asScalar and asArray functions.
68// </note>
69//
70// <note role=tip>
71// Data types <src>long</src> and <src>unsigned long</src> are not
72// possible. The types <src>Int</src> and <src>uInt</src> are always
73// 4 bytes, so <src>long</src> is not needed and may only cause
74// confusion.
75// </note>
76//
77// </synopsis>
78
79// <example>
80// The simplest uses of the DataType enumeration and functions are fairly
81// obvious, for example:
82// <srcblock>
83// Double d;
84// DataType type = whatType(&d);
85// cout << type << endl;
86// switch(type) {
87// case TpChar: ...
88// ...
89// case TpDouble: ...
90// }
91// </srcblock>
92//
93// A less obvious use is for "attaching" a templated object or function to a
94// non-templated object in a safe way. For example:
95// <srcblock>
96// class IntFloatContainer {
97// public:
98// Int intval;
99// Float floatval;
100// void *ptr(DataType type) {
101// if (type == whatType(&intval))
102// return &intval;
103// else if (type == whatType(&floatval))
104// return &floatval;
105// else
106// return 0; // Illegal type
107// }
108// };
109//
110// template<class T> class ValueAccessor {
111// public:
112// ValueAccessor(IntFloatContainer *container) : container_p(container) {
113// if (container_p->ptr(whatType(static_cast<T *>(0))) == 0)
114// throw(AipsError("Illegal type..."));
115// }
116// T &value() { return *((T*)container_p->ptr(whatType(static_cast<T *>(0)))); }
117// private:
118// IntFloatContainer *container_p;
119// };
120// </srcblock>
121//
122// So, this example provides a typesafe interface to values of only a small
123// number of types (and it fairly gracefully allows additional types to be
124// added; in particular the accessor class needs no modification). Techniques
125// such as this are appropriate for situations where one needs to deal with
126// many (but finite) numbers of types. For example, with FITS.
127// </example>
128
129// <todo asof="1995/03/01">
130// <li> Clean up comment as soon as enum's are properly extracted.
131// </todo>
132
133// <linkfrom anchor=DataType modules="Tables">
134// Enumeration of the <here>data types</here> in the table system
135// </linkfrom>
136//
137// Enumeration of the possible data types for keywords and table columns.
138// <group name=DataType>
157
158
159// Write a formated representation (e.g., Type=Bool) of the given data type.
160ostream &operator<<(ostream &os, DataType type);
161
162// These (specialized) functions return the DataType that corresponds
163// to the template type. TpOther is returned for types that are
164// not specialized, as is void.
165// <group>
166template<typename T>
167inline DataType whatType() { return TpOther; }
168
169#define DEFINE_WHATTYPE(SPECIALIZED_TYPE, RETURN_TYPE) \
170 template<> inline DataType whatType<SPECIALIZED_TYPE>() { return RETURN_TYPE; }
171
172DEFINE_WHATTYPE(void, TpOther)
173DEFINE_WHATTYPE(Bool, TpBool)
174DEFINE_WHATTYPE(Char, TpChar)
175DEFINE_WHATTYPE(uChar, TpUChar)
176DEFINE_WHATTYPE(Short, TpShort)
177DEFINE_WHATTYPE(uShort, TpUShort)
178DEFINE_WHATTYPE(Int, TpInt)
179DEFINE_WHATTYPE(uInt, TpUInt)
180DEFINE_WHATTYPE(Int64, TpInt64)
181DEFINE_WHATTYPE(float, TpFloat)
182DEFINE_WHATTYPE(double, TpDouble)
183DEFINE_WHATTYPE(Complex, TpComplex)
184DEFINE_WHATTYPE(DComplex, TpDComplex)
185DEFINE_WHATTYPE(String, TpString)
186DEFINE_WHATTYPE(Table, TpTable)
187DEFINE_WHATTYPE(Array<Bool>, TpArrayBool)
188DEFINE_WHATTYPE(Array<Char>, TpArrayChar)
189DEFINE_WHATTYPE(Array<uChar>, TpArrayUChar)
190DEFINE_WHATTYPE(Array<Short>, TpArrayShort)
191DEFINE_WHATTYPE(Array<uShort>, TpArrayUShort)
192DEFINE_WHATTYPE(Array<Int>, TpArrayInt)
193DEFINE_WHATTYPE(Array<uInt>, TpArrayUInt)
194DEFINE_WHATTYPE(Array<Int64>, TpArrayInt64)
195DEFINE_WHATTYPE(Array<float>, TpArrayFloat)
196DEFINE_WHATTYPE(Array<double>, TpArrayDouble)
197DEFINE_WHATTYPE(Array<Complex>, TpArrayComplex)
198DEFINE_WHATTYPE(Array<DComplex>, TpArrayDComplex)
199DEFINE_WHATTYPE(Array<String>, TpArrayString)
200DEFINE_WHATTYPE(Record, TpRecord)
201DEFINE_WHATTYPE(Quantum<Double>, TpQuantity)
202DEFINE_WHATTYPE(Array<Quantum<Double>>, TpArrayQuantity)
203
204#undef DEFINE_WHATTYPE
205
206// </group>
207
208// It is sometimes useful to discover what the corresponding
209// scalar (or array) type is for a given array (or scalar) type.
210// Calling these with TpOther, TpTable, and TpRecord results
211// in an exception being thrown.
212// <group>
215// </group>
216
223constexpr size_t SizeOfType(DataType dtype) {
224 switch (dtype) {
225 case DataType::TpBool:
226 case DataType::TpChar:
228 return 1;
231 return 2;
232 case DataType::TpInt:
233 case DataType::TpUInt:
235 return 4;
239 return 8;
241 return 16;
262 return 0;
263 }
264 return 0;
265}
266
267// It is occasionally useful to discover whether or not a DataType represents
268// an array or scalar value. Note that TpTable, TpRecord, and TpOther are neither
269// scalar nor array types.
270// <group>
273Bool isScalarFun(DataType type); //{return isScalar(type);}
274// </group>
275
276// It is sometimes useful to discover if a DataType represents a real
277// numeric value (i.e., can it be cast to a Double?) This returns True
278// for both real scalar and array type.
280
281// Returns True for Complex or DComplex scalar or array types
283
284// Returns True if the type is either Real or Complex/DComplex
286
287// </group>
288
289
290} //# NAMESPACE CASACORE - END
291
292#endif
#define DEFINE_WHATTYPE(SPECIALIZED_TYPE, RETURN_TYPE)
Definition DataType.h:169
String: the storage and methods of handling collections of characters.
Definition String.h:223
this file contains all the compiler specific defines
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:223
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:167
@ TpNumberOfTypes
Since we start at zero, this is the number of types in the enum.
Definition DataType.h:155