source: trunk/FACT++/src/FileEntry.h@ 19982

Last change on this file since 19982 was 19965, checked in by tbretz, 4 years ago
Added a decription for more (particularly SQL) types, such as kTime, kDate, kDateTime, kDecimal, kNumeric and also added a column to describe the conversion to a root-file branch.
File size: 8.3 KB
Line 
1#ifndef FACT_FileEntry
2#define FACT_FileEntry
3
4#include <vector>
5#include <string>
6#include <iomanip>
7
8namespace FileEntry
9{
10 enum BasicType_t
11 {
12 kNone = 0,
13 kConst,
14 kChar,
15 kVarchar,
16 kBool,
17 kInt8,
18 kUInt8,
19 kFloat,
20 kDouble,
21 kInt16,
22 kUInt16,
23 kInt32,
24 kUInt32,
25 kInt64,
26 kUInt64,
27 kDecimal,
28 kNumeric,
29 kTime,
30 kDate,
31 kDateTime,
32 };
33
34 struct Type
35 {
36 BasicType_t type;
37 std::string root; // root basic type
38 char branch; // root branch
39 char fits; // fits type
40 std::string sql; // sql type
41 };
42
43 struct Types : std::vector<Type>
44 {
45 Types(std::initializer_list<Type> il, const allocator_type& alloc = allocator_type())
46 : std::vector<Type>(il, alloc)
47 {
48 }
49
50 // Note that the result is not unique!
51 const std::vector<Type>::const_iterator root(const std::string &str) const
52 {
53 for (auto it=cbegin(); it!=cend(); it++)
54 if (str==it->root)
55 return it;
56 return cend();
57 }
58
59 // Note that the result is not unique!
60 const std::vector<Type>::const_iterator type(const BasicType_t &t) const
61 {
62 for (auto it=cbegin(); it!=cend(); it++)
63 if (t==it->type)
64 return it;
65 return cend();
66 }
67
68 const std::vector<Type>::const_iterator fits(const char &f) const
69 {
70 for (auto it=cbegin(); it!=cend(); it++)
71 if (f==it->fits)
72 return it;
73 return cend();
74 }
75
76 const std::vector<Type>::const_iterator sql(const std::string &str) const
77 {
78 for (auto it=cbegin(); it!=cend(); it++)
79 if (str==it->sql)
80 return it;
81 return cend();
82 }
83 };
84
85 static const Types LUT =
86 {
87 // type root branch fits sql
88 { kChar, "Char_t", ' ', 'A', "CHAR" },
89 { kChar, "Char_t", ' ', 'a', "CHAR" },
90 { kVarchar, "Char_t", ' ', 'A', "VARCHAR" },
91 { kVarchar, "Char_t", ' ', 'a', "VARCHAR" },
92 { kBool, "Bool_t", 'O', 'L', "BOOLEAN" },
93 { kBool, "Bool_t", 'O', 'l', "BOOLEAN" },
94 { kInt8, "Char_t", 'B', 'B', "TINYINT" },
95 { kUInt8, "UInt8_t", 'b', 'b', "TINYINT UNSIGNED" },
96 { kInt16, "Short_t", 'S', 'I', "SMALLINT" },
97 { kUInt16, "UShort_t", 's', 'i', "SMALLINT UNSIGNED" },
98 { kInt32, "Int_t", 'I', 'J', "INT" },
99 { kUInt32, "UInt_t", 'i', 'j', "INT UNSIGNED" },
100 { kInt32, "Int_t", 'I', 'J', "MEDIUMINT" },
101 { kUInt32, "UInt_t", 'i', 'j', "MEDIUMINT UNSIGNED" },
102 { kInt64, "Long64_t", 'L', 'K', "BIGINT" },
103 { kUInt64, "ULong64_t", 'l', 'k', "BIGINT UNSIGNED" },
104 { kFloat, "Float_t", 'F', 'E', "FLOAT" },
105 { kFloat, "Float_t", 'F', 'e', "FLOAT" },
106 { kDouble, "Double_t", 'D', 'D', "DOUBLE" },
107 { kDouble, "Double_t", 'D', 'd', "DOUBLE" },
108 { kTime, "UInt32_t", 'i', 'j', "TIME" },
109 { kDate, "UInt64_t", 'l', 'k', "DATE" },
110 { kDateTime, "UInt64_t", 'l', 'k', "DATETIME" },
111 { kDecimal, "Double_t", 'D', 'D', "DECIMAL" },
112 { kDecimal, "Double_t", 'D', 'd', "DECIMAL" },
113 { kNumeric, "Double_t", 'D', 'D', "NUMERIC" },
114 { kNumeric, "Double_t", 'D', 'd', "NUMERIC" },
115 };
116
117 struct Container
118 {
119 static std::map<void*, size_t> counter;
120
121 std::string branch; // branch name
122 std::string column; // column name
123
124 BasicType_t type;
125
126 size_t num;
127 void *ptr;
128
129 Container() : type(kNone), num(0), ptr(0)
130 {
131 }
132
133 // Root File (do not mix with FitsFile!)
134 Container(const std::string &b, const std::string &c,
135 const BasicType_t &t, const size_t &n=1)
136 : branch(b), column(c), type(t), num(n), ptr(0)
137 {
138 switch (t)
139 {
140 case kInt8: ptr = new int8_t[n]; break;
141 case kUInt8: ptr = new uint8_t[n]; break;
142 case kBool: ptr = new uint8_t[n]; break;
143 case kFloat: ptr = new float[n]; break;
144 case kDecimal:
145 case kNumeric:
146 case kDouble: ptr = new double[n]; break;
147 case kInt16: ptr = new int16_t[n]; break;
148 case kUInt16: ptr = new uint16_t[n]; break;
149 case kInt32: ptr = new int32_t[n]; break;
150 case kTime:
151 case kUInt32: ptr = new uint32_t[n]; break;
152 case kInt64: ptr = new int64_t[n]; break;
153 case kDate:
154 case kDateTime:
155 case kUInt64: ptr = new uint64_t[n]; break;
156 case kChar:
157 case kVarchar:
158 case kConst:
159 case kNone:
160 break;
161 }
162 // Indicate that this was allocated and how often
163 counter[ptr]++;
164 }
165
166 // This is for rootifysql!
167 Container(const std::string &b, const BasicType_t &t)
168 : branch(b), type(t), num(1)
169 {
170 ptr = new double[1];
171 // Indicate that this was allocated and how often
172 counter[ptr]++;
173 }
174
175 Container(const std::string &c, const std::string &value)
176 : branch(value), column(c), type(kConst), num(1), ptr(0)
177 {
178 }
179
180 Container(const Container &c) : branch(c.branch), column(c.column),
181 type(c.type), num(c.num), ptr(c.ptr)
182 {
183 // If this was not allocated in the constructor do not increate the counter
184 if (counter[ptr])
185 counter[ptr]++;
186 }
187
188 // FitsFile (do not mix with RootFile!)
189 Container(const std::string &b, const std::string &c,
190 const BasicType_t &t, const size_t &n, void *_ptr)
191 : branch(b), column(c), type(t), num(n), ptr(_ptr)
192 {
193 }
194
195 ~Container()
196 {
197 // Count how often it gets deleted.
198 counter[ptr]--;
199 // Now it is time to delete it. Note that pointers which were
200 // never allocated (counter==0) now are != 0
201 if (counter[ptr]==0)
202 ::operator delete[](ptr); // It seems root is deleting it already
203 }
204
205 std::string fmt(const size_t &index) const
206 {
207 std::ostringstream str;
208
209 switch (type)
210 {
211 case kVarchar: str << std::string(reinterpret_cast<char*>(ptr), num).c_str(); break;
212 case kFloat: str << std::setprecision(8) << reinterpret_cast<float*>(ptr)[index]; break;
213 case kDecimal:
214 case kNumeric:
215 case kDouble: str << std::setprecision(16) << reinterpret_cast<double*>(ptr)[index]; break;
216 case kBool:
217 case kInt8: str << int32_t(reinterpret_cast<int8_t*>(ptr)[index]); break;
218 case kUInt8: str << uint32_t(reinterpret_cast<uint8_t*>(ptr)[index]); break;
219 case kInt16: str << reinterpret_cast<int16_t*>(ptr)[index]; break;
220 case kUInt16: str << reinterpret_cast<uint16_t*>(ptr)[index]; break;
221 case kInt32: str << reinterpret_cast<int32_t*>(ptr)[index]; break;
222 case kTime:
223 case kUInt32: str << reinterpret_cast<uint32_t*>(ptr)[index]; break;
224 case kInt64: str << reinterpret_cast<int64_t*>(ptr)[index]; break;
225 case kDate:
226 case kDateTime:
227 case kUInt64: str << reinterpret_cast<uint64_t*>(ptr)[index]; break;
228 case kConst: str << branch; break;
229 case kChar:
230 case kNone:
231 break;
232 }
233
234 //if (str.str()=="nan" || str.str()=="-nan" || str.str()=="inf" || str.str()=="-inf")
235 // return "NULL";
236
237 return str.str();
238 }
239 };
240
241 std::map<void*, size_t> Container::counter;
242}
243
244#endif
Note: See TracBrowser for help on using the repository browser.