ODIN
tjtypes.h
1 /***************************************************************************
2  tjtypes.h - description
3  -------------------
4  begin : Thu Jul 24 2001
5  copyright : (C) 2000-2021 by Thies H. Jochimsen
6  email : thies@jochimsen.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #ifndef TJTYPES_H
19 #define TJTYPES_H
20 
21 #include <tjutils/tjutils.h>
22 #include <tjutils/tjcomplex.h>
23 
24 
31 // Get appropriate data types for signed/unsigned 8, 16 and 32 bit data on this platform
32 
33 #if SIZEOF_CHAR == 1
34 typedef char s8bit;
35 typedef unsigned char u8bit;
36 #else
37 #error "sizeof(char)!=1"
38 #endif
39 
40 #if SIZEOF_SHORT == 2
41 typedef short s16bit;
42 typedef unsigned short u16bit;
43 #else
44 #error "sizeof(short)!=2"
45 #endif
46 
47 #if SIZEOF_INT == 4
48 typedef int s32bit;
49 typedef unsigned int u32bit;
50 #else
51 #if SIZEOF_LONG == 4
52 typedef long s32bit;
53 typedef unsigned long u32bit;
54 #else
55 #error "sizeof(int)!=4 and sizeof(long)!=4"
56 #endif
57 #endif
58 
59 
66 class TypeTraits {
67 
68  public:
69 
70  // Convert a type's value to a string
71  static STD_string type2string(s8bit v) {return itos(v);}
72  static STD_string type2string(u8bit v) {return itos(v);}
73  static STD_string type2string(s16bit v) {return itos(v);}
74  static STD_string type2string(u16bit v) {return itos(v);}
75  static STD_string type2string(s32bit v) {return itos(v);}
76  static STD_string type2string(u32bit v) {return itos(v);}
77  static STD_string type2string(float v) {return ftos(v);}
78  static STD_string type2string(double v) {return ftos(float(v));}
79  static STD_string type2string(const STD_complex& v) {return ctos(v);}
80  static STD_string type2string(const STD_string& v) {return v;}
81 
82  // Parses type's value from string
83  static void string2type(const STD_string& s, s8bit& v) {v=(s8bit)atoi(s.c_str());}
84  static void string2type(const STD_string& s, u8bit& v) {v=(u8bit)atoi(s.c_str());}
85  static void string2type(const STD_string& s, s16bit& v) {v=(s16bit)atoi(s.c_str());}
86  static void string2type(const STD_string& s, u16bit& v) {v=(u16bit)atoi(s.c_str());}
87  static void string2type(const STD_string& s, s32bit& v) {v=(s32bit)atoi(s.c_str());}
88  static void string2type(const STD_string& s, u32bit& v) {v=(u32bit)atoi(s.c_str());}
89  static void string2type(const STD_string& s, float& v) {v=(float)atof(s.c_str());}
90  static void string2type(const STD_string& s, double& v) {v=atof(s.c_str());}
91  static void string2type(const STD_string& s, STD_complex& v) {v=stoc(s);}
92  static void string2type(const STD_string& s, STD_string& v) {v=s;}
93 
94  // Type label (identifier) as string
95  static const char* type2label(s8bit) {return "s8bit";}
96  static const char* type2label(u8bit) {return "u8bit";}
97  static const char* type2label(s16bit) {return "s16bit";}
98  static const char* type2label(u16bit) {return "u16bit";}
99  static const char* type2label(s32bit) {return "s32bit";}
100  static const char* type2label(u32bit) {return "u32bit";}
101  static const char* type2label(float) {return "float";}
102  static const char* type2label(double) {return "double";}
103  static const char* type2label(const STD_complex&) {return "complex";}
104  static const char* type2label(const STD_string&) {return "string";}
105 
109  static unsigned int typesize(const STD_string& typelabel);
110 
111 
112 };
113 
114 
117 #endif
static unsigned int typesize(const STD_string &typelabel)
STD_string ctos(const STD_complex &z)
STD_complex stoc(const STD_string &s)
STD_string ftos(double f, unsigned int digits=_DEFAULT_DIGITS_, expFormat eformat=autoExp)
STD_string itos(int i, unsigned int maxabs=0)