LevelS C++ support library  3.83
datatypes.h
Go to the documentation of this file.
1 /*
2  * This file is part of libcxxsupport.
3  *
4  * libcxxsupport is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * libcxxsupport is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with libcxxsupport; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 /*
20  * libcxxsupport is being developed at the Max-Planck-Institut fuer Astrophysik
21  * and financially supported by the Deutsches Zentrum fuer Luft- und Raumfahrt
22  * (DLR).
23  */
24 
25 /*! \file datatypes.h
26  * This file defines various platform-independent data types.
27  * If any of the requested types is not available, compilation aborts
28  * with an error (unfortunately a rather obscure one).
29  *
30  * Copyright (C) 2004-2015 Max-Planck-Society
31  * \author Martin Reinecke
32  */
33 
34 #ifndef PLANCK_DATATYPES_H
35 #define PLANCK_DATATYPES_H
36 
37 #include <string>
38 #include <cstddef>
39 #include "xcomplex.h"
40 #include "error_handling.h"
41 
42 // Template magic to select the proper data types. These templates
43 // should not be used outside this file.
44 
45 template <typename T, bool equalSize> struct sizeChooserHelper__
46  { typedef void TYPE; };
47 
48 template <typename T> struct sizeChooserHelper__<T,true>
49  { typedef T TYPE; };
50 
51 template <typename T1, typename T2, typename T3> struct sizeChooserHelper2__
52  { typedef T1 TYPE; };
53 
54 template <typename T2, typename T3> struct sizeChooserHelper2__ <void, T2, T3>
55  { typedef T2 TYPE; };
56 
57 template <typename T3> struct sizeChooserHelper2__ <void, void, T3>
58  { typedef T3 TYPE; };
59 
60 template <> struct sizeChooserHelper2__ <void, void, void>
61  { };
62 
63 template <int sz, typename T1, typename T2=char, typename T3=char>
64  struct sizeChooser__
65  {
66  typedef typename sizeChooserHelper2__
67  <typename sizeChooserHelper__<T1,sizeof(T1)==sz>::TYPE,
68  typename sizeChooserHelper__<T2,sizeof(T2)==sz>::TYPE,
69  typename sizeChooserHelper__<T3,sizeof(T3)==sz>::TYPE >::TYPE TYPE;
70  };
71 
72 #if (__cplusplus>=201103L)
73 
74 #include <cstdint>
75 
76 typedef int8_t int8;
77 typedef uint8_t uint8;
78 
79 typedef int16_t int16;
80 typedef uint16_t uint16;
81 
82 typedef int32_t int32;
83 typedef uint32_t uint32;
84 
85 typedef int64_t int64;
86 typedef uint64_t uint64;
87 
88 #else
89 
90 typedef signed char int8;
91 typedef unsigned char uint8;
92 
93 typedef sizeChooser__<2, short, int>::TYPE
94  int16;
95 typedef sizeChooser__<2, unsigned short, unsigned int>::TYPE
96  uint16;
97 
98 typedef sizeChooser__<4, int, long, short>::TYPE
99  int32;
100 typedef sizeChooser__<4, unsigned int, unsigned long, unsigned short>::TYPE
101  uint32;
102 
103 typedef sizeChooser__<8, long, long long>::TYPE
104  int64;
105 typedef sizeChooser__<8, unsigned long, unsigned long long>::TYPE
106  uint64;
107 
108 #endif
109 
110 typedef sizeChooser__<4, float, double>::TYPE
111  float32;
112 typedef sizeChooser__<8, double, long double>::TYPE
113  float64;
114 
115 /*! unsigned integer type which should be used for array sizes */
116 typedef std::size_t tsize;
117 /*! signed integer type which should be used for relative array indices */
118 typedef std::ptrdiff_t tdiff;
119 
120 /*! mapping of Planck data types to integer constants */
121 enum PDT {
122  PLANCK_INT8 = 0,
123  PLANCK_UINT8 = 1,
124  PLANCK_INT16 = 2,
125  PLANCK_UINT16 = 3,
126  PLANCK_INT32 = 4,
127  PLANCK_UINT32 = 5,
128  PLANCK_INT64 = 6,
129  PLANCK_UINT64 = 7,
130  PLANCK_FLOAT32 = 8,
131  PLANCK_FLOAT64 = 9,
132  PLANCK_BOOL = 10,
133  PLANCK_STRING = 11,
134  PLANCK_INVALID = -1 };
135 
136 /*! Returns the \a PDT constant associated with \a T. */
137 template<typename T> inline PDT planckType()
138  { planck_fail(T::UNSUPPORTED_DATA_TYPE); }
139 template<> inline PDT planckType<int8> () { return PLANCK_INT8; }
140 template<> inline PDT planckType<uint8> () { return PLANCK_UINT8; }
141 template<> inline PDT planckType<int16> () { return PLANCK_INT16; }
142 template<> inline PDT planckType<uint16> () { return PLANCK_UINT16; }
143 template<> inline PDT planckType<int32> () { return PLANCK_INT32; }
144 template<> inline PDT planckType<uint32> () { return PLANCK_UINT32; }
145 template<> inline PDT planckType<int64> () { return PLANCK_INT64; }
146 template<> inline PDT planckType<uint64> () { return PLANCK_UINT64; }
147 template<> inline PDT planckType<float32> () { return PLANCK_FLOAT32;}
148 template<> inline PDT planckType<float64> () { return PLANCK_FLOAT64;}
149 template<> inline PDT planckType<bool> () { return PLANCK_BOOL; }
150 template<> inline PDT planckType<std::string>() { return PLANCK_STRING; }
151 
152 /*! Returns the size (in bytes) of the Planck data type \a type. */
153 inline int type2size (PDT type)
154  {
155  switch (type)
156  {
157  case PLANCK_INT8 :
158  case PLANCK_UINT8 :
159  case PLANCK_BOOL :
160  case PLANCK_STRING : return 1;
161  case PLANCK_INT16 :
162  case PLANCK_UINT16 : return 2;
163  case PLANCK_INT32 :
164  case PLANCK_UINT32 :
165  case PLANCK_FLOAT32: return 4;
166  case PLANCK_INT64 :
167  case PLANCK_UINT64 :
168  case PLANCK_FLOAT64: return 8;
169  default:
170  planck_fail ("type2size: unsupported data type");
171  }
172  }
173 
174 /*! Converts the string \a type to a \a PDT. */
175 inline PDT string2type(const std::string &type)
176  {
177  if (type=="FLOAT64") return PLANCK_FLOAT64;
178  if (type=="FLOAT32") return PLANCK_FLOAT32;
179  if (type=="INT8") return PLANCK_INT8;
180  if (type=="UINT8") return PLANCK_UINT8;
181  if (type=="INT16") return PLANCK_INT16;
182  if (type=="UINT16") return PLANCK_UINT16;
183  if (type=="INT32") return PLANCK_INT32;
184  if (type=="UINT32") return PLANCK_UINT32;
185  if (type=="INT64") return PLANCK_INT64;
186  if (type=="UINT64") return PLANCK_UINT64;
187  if (type=="BOOL") return PLANCK_BOOL;
188  if (type=="STRING") return PLANCK_STRING;
189  planck_fail ("string2type: unknown data type '"+type+"'");
190  }
191 
192 /*! Converts the Planck data type \a type to a C string. */
193 inline const char *type2string (PDT type)
194  {
195  switch (type)
196  {
197  case PLANCK_INT8 : return "INT8";
198  case PLANCK_UINT8 : return "UINT8";
199  case PLANCK_INT16 : return "INT16";
200  case PLANCK_UINT16 : return "UINT16";
201  case PLANCK_INT32 : return "INT32";
202  case PLANCK_UINT32 : return "UINT32";
203  case PLANCK_INT64 : return "INT64";
204  case PLANCK_UINT64 : return "UINT64";
205  case PLANCK_FLOAT32: return "FLOAT32";
206  case PLANCK_FLOAT64: return "FLOAT64";
207  case PLANCK_BOOL : return "BOOL";
208  case PLANCK_STRING : return "STRING";
209  default:
210  planck_fail ("type2string: unsupported data type");
211  }
212  }
213 
214 /*! Returns a C string describing the data type \a T. */
215 template<typename T> inline const char *type2typename ()
216  { planck_fail(T::UNSUPPORTED_DATA_TYPE); }
217 template<> inline const char *type2typename<signed char> ()
218  { return "signed char"; }
219 template<> inline const char *type2typename<unsigned char> ()
220  { return "unsigned char"; }
221 template<> inline const char *type2typename<short> ()
222  { return "short"; }
223 template<> inline const char *type2typename<unsigned short> ()
224  { return "unsigned short"; }
225 template<> inline const char *type2typename<int> ()
226  { return "int"; }
227 template<> inline const char *type2typename<unsigned int> ()
228  { return "unsigned int"; }
229 template<> inline const char *type2typename<long> ()
230  { return "long"; }
231 template<> inline const char *type2typename<unsigned long> ()
232  { return "unsigned long"; }
233 template<> inline const char *type2typename<long long> ()
234  { return "long long"; }
235 template<> inline const char *type2typename<unsigned long long> ()
236  { return "unsigned long long"; }
237 template<> inline const char *type2typename<float> ()
238  { return "float"; }
239 template<> inline const char *type2typename<double> ()
240  { return "double"; }
241 template<> inline const char *type2typename<long double> ()
242  { return "long double"; }
243 template<> inline const char *type2typename<bool> ()
244  { return "bool"; }
245 template<> inline const char *type2typename<std::string> ()
246  { return "std::string"; }
247 
248 /*! mapping of "native" data types to integer constants */
249 enum NDT {
250  NAT_CHAR,
251  NAT_SCHAR,
252  NAT_UCHAR,
253  NAT_SHORT,
254  NAT_USHORT,
255  NAT_INT,
256  NAT_UINT,
257  NAT_LONG,
258  NAT_ULONG,
259  NAT_LONGLONG,
260  NAT_ULONGLONG,
261  NAT_FLOAT,
262  NAT_DOUBLE,
263  NAT_LONGDOUBLE,
264  NAT_FCMPLX,
265  NAT_DCMPLX,
266  NAT_BOOL,
267  NAT_STRING };
268 
269 /*! Returns the \a NDT constant associated with \a T. */
270 template<typename T> inline NDT nativeType()
271  { planck_fail(T::UNSUPPORTED_DATA_TYPE); }
272 template<> inline NDT nativeType<char> () { return NAT_CHAR; }
273 template<> inline NDT nativeType<signed char> () { return NAT_SCHAR; }
274 template<> inline NDT nativeType<unsigned char> () { return NAT_UCHAR; }
275 template<> inline NDT nativeType<short> () { return NAT_SHORT; }
276 template<> inline NDT nativeType<unsigned short> () { return NAT_USHORT; }
277 template<> inline NDT nativeType<int> () { return NAT_INT; }
278 template<> inline NDT nativeType<unsigned int> () { return NAT_UINT; }
279 template<> inline NDT nativeType<long> () { return NAT_LONG; }
280 template<> inline NDT nativeType<unsigned long> () { return NAT_ULONG; }
281 template<> inline NDT nativeType<long long> () { return NAT_LONGLONG; }
282 template<> inline NDT nativeType<unsigned long long>() { return NAT_ULONGLONG; }
283 template<> inline NDT nativeType<float> () { return NAT_FLOAT; }
284 template<> inline NDT nativeType<double> () { return NAT_DOUBLE; }
285 template<> inline NDT nativeType<long double> () { return NAT_LONGDOUBLE;}
286 template<> inline NDT nativeType<fcomplex> () { return NAT_FCMPLX; }
287 template<> inline NDT nativeType<dcomplex> () { return NAT_DCMPLX; }
288 template<> inline NDT nativeType<bool> () { return NAT_BOOL; }
289 template<> inline NDT nativeType<std::string> () { return NAT_STRING; }
290 
291 /*! Returns the size (in bytes) of the native data type \a type. */
292 inline int ndt2size (NDT type)
293  {
294  switch (type)
295  {
296  case NAT_CHAR :
297  case NAT_SCHAR :
298  case NAT_UCHAR : return sizeof(char);
299  case NAT_SHORT :
300  case NAT_USHORT : return sizeof(short);
301  case NAT_INT :
302  case NAT_UINT : return sizeof(int);
303  case NAT_LONG :
304  case NAT_ULONG : return sizeof(long);
305  case NAT_LONGLONG :
306  case NAT_ULONGLONG : return sizeof(long long);
307  case NAT_FLOAT : return sizeof(float);
308  case NAT_DOUBLE : return sizeof(double);
309  case NAT_LONGDOUBLE: return sizeof(long double);
310  case NAT_FCMPLX : return sizeof(fcomplex);
311  case NAT_DCMPLX : return sizeof(dcomplex);
312  case NAT_BOOL : return sizeof(bool);
313  default:
314  planck_fail ("ndt2size: unsupported data type");
315  }
316  }
317 
318 #endif
PDT planckType()
Definition: datatypes.h:137
NDT nativeType()
Definition: datatypes.h:270
int type2size(PDT type)
Definition: datatypes.h:153
PDT string2type(const std::string &type)
Definition: datatypes.h:175
std::size_t tsize
Definition: datatypes.h:116
NDT
Definition: datatypes.h:249
std::ptrdiff_t tdiff
Definition: datatypes.h:118
PDT
Definition: datatypes.h:121
const char * type2typename()
Definition: datatypes.h:215
int ndt2size(NDT type)
Definition: datatypes.h:292
#define planck_fail(msg)
const char * type2string(PDT type)
Definition: datatypes.h:193

Generated on Wed Nov 13 2024 12:18:16 for LevelS C++ support library