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

Last change on this file since 19878 was 19807, checked in by tbretz, 5 years ago
Removed a stray output line.
File size: 6.6 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 kVarchar,
15 kBool,
16 kInt8,
17 kUInt8,
18 kFloat,
19 kDouble,
20 kInt16,
21 kUInt16,
22 kInt32,
23 kUInt32,
24 kInt64,
25 kUInt64,
26 };
27
28 struct Type
29 {
30 BasicType_t type;
31 std::string root;
32 char fits;
33 std::string sql;
34 };
35
36 struct Types : std::vector<Type>
37 {
38 Types(std::initializer_list<Type> il, const allocator_type& alloc = allocator_type())
39 : std::vector<Type>(il, alloc)
40 {
41 }
42
43 // Note that the result is not unique!
44 const std::vector<Type>::const_iterator root(const std::string &str) const
45 {
46 for (auto it=cbegin(); it!=cend(); it++)
47 if (str==it->root)
48 return it;
49 return cend();
50 }
51
52 // Note that the result is not unique!
53 const std::vector<Type>::const_iterator type(const BasicType_t &t) const
54 {
55 for (auto it=cbegin(); it!=cend(); it++)
56 if (t==it->type)
57 return it;
58 return cend();
59 }
60
61 const std::vector<Type>::const_iterator fits(const char &f) const
62 {
63 for (auto it=cbegin(); it!=cend(); it++)
64 if (f==it->fits)
65 return it;
66 return cend();
67 }
68
69 const std::vector<Type>::const_iterator sql(const std::string &str) const
70 {
71 for (auto it=cbegin(); it!=cend(); it++)
72 if (str==it->sql)
73 return it;
74 return cend();
75 }
76 };
77
78 static const Types LUT =
79 {
80 { kVarchar, "Char_t", 'A', "VARCHAR" },
81 { kVarchar, "Char_t", 'a', "VARCHAR" },
82 { kBool, "Bool_t", 'L', "BOOLEAN" },
83 { kBool, "Bool_t", 'l', "BOOLEAN" },
84 { kInt8, "Char_t", 'B', "TINYINT" },
85 { kUInt8, "UInt8_t", 'b', "TINYINT UNSIGNED" },
86 { kInt16, "Short_t", 'I', "SMALLINT" },
87 { kUInt16, "UShort_t", 'i', "SMALLINT UNSIGNED" },
88 { kInt32, "Int_t", 'J', "INT" },
89 { kUInt32, "UInt_t", 'j', "INT UNSIGNED" },
90 { kInt64, "Long64_t", 'K', "BIGINT" },
91 { kUInt64, "ULong64_t", 'k', "BIGINT UNSIGNED" },
92 { kFloat, "Float_t", 'E', "FLOAT" },
93 { kFloat, "Float_t", 'e', "FLOAT" },
94 { kDouble, "Double_t", 'D', "DOUBLE" },
95 { kDouble, "Double_t", 'd', "DOUBLE" },
96 };
97
98 struct Container
99 {
100 static std::map<void*, size_t> counter;
101
102 std::string branch; // branch name
103 std::string column; // column name
104
105 BasicType_t type;
106
107 size_t num;
108 void *ptr;
109
110 // Root File (do not mix with FitsFile!)
111 Container(const std::string &b, const std::string &c,
112 const BasicType_t &t, const size_t &n)
113 : branch(b), column(c), type(t), num(n), ptr(0)
114 {
115 switch (t)
116 {
117 case kInt8: ptr = new int8_t[n]; break;
118 case kUInt8: ptr = new uint8_t[n]; break;
119 case kBool: ptr = new uint8_t[n]; break;
120 case kFloat: ptr = new float[n]; break;
121 case kDouble: ptr = new double[n]; break;
122 case kInt16: ptr = new int16_t[n]; break;
123 case kUInt16: ptr = new uint16_t[n]; break;
124 case kInt32: ptr = new int32_t[n]; break;
125 case kUInt32: ptr = new uint32_t[n]; break;
126 case kInt64: ptr = new int64_t[n]; break;
127 case kUInt64: ptr = new uint64_t[n]; break;
128 case kVarchar:
129 case kConst:
130 case kNone:
131 break;
132 }
133 // Indicate that this was allocated and how often
134 counter[ptr]++;
135 }
136
137 Container(const std::string &c, const std::string &value)
138 : branch(value), column(c), type(kConst), num(1), ptr(0)
139 {
140 }
141
142 Container(const Container &c) : branch(c.branch), column(c.column),
143 type(c.type), num(c.num), ptr(c.ptr)
144 {
145 // If this was not allocated in the constructor do not increate the counter
146 if (counter[ptr])
147 counter[ptr]++;
148 }
149
150 // FitsFile (do not mix with RootFile!)
151 Container(const std::string &b, const std::string &c,
152 const BasicType_t &t, const size_t &n, void *_ptr)
153 : branch(b), column(c), type(t), num(n), ptr(_ptr)
154 {
155 }
156
157 ~Container()
158 {
159 // Count how often it gets deleted.
160 counter[ptr]--;
161 // Now it is time to delete it. Note that pointers which were
162 // never allocated (counter==0) now are != 0
163 if (counter[ptr]==0)
164 ::operator delete[](ptr); // It seems root is deleting it already
165 }
166
167 std::string fmt(const size_t &index) const
168 {
169 std::ostringstream str;
170
171 switch (type)
172 {
173 case kVarchar: str << std::string(reinterpret_cast<char*>(ptr), num).c_str(); break;
174 case kFloat: str << std::setprecision(8) << reinterpret_cast<float*>(ptr)[index]; break;
175 case kDouble: str << std::setprecision(16) << reinterpret_cast<double*>(ptr)[index]; break;
176 case kBool:
177 case kInt8: str << int32_t(reinterpret_cast<int8_t*>(ptr)[index]); break;
178 case kUInt8: str << uint32_t(reinterpret_cast<uint8_t*>(ptr)[index]); break;
179 case kInt16: str << reinterpret_cast<int16_t*>(ptr)[index]; break;
180 case kUInt16: str << reinterpret_cast<uint16_t*>(ptr)[index]; break;
181 case kInt32: str << reinterpret_cast<int32_t*>(ptr)[index]; break;
182 case kUInt32: str << reinterpret_cast<uint32_t*>(ptr)[index]; break;
183 case kInt64: str << reinterpret_cast<int64_t*>(ptr)[index]; break;
184 case kUInt64: str << reinterpret_cast<uint64_t*>(ptr)[index]; break;
185 case kConst: str << branch; break;
186 case kNone:
187 break;
188 }
189
190 //if (str.str()=="nan" || str.str()=="-nan" || str.str()=="inf" || str.str()=="-inf")
191 // return "NULL";
192
193 return str.str();
194 }
195 };
196
197 std::map<void*, size_t> Container::counter;
198}
199
200#endif
Note: See TracBrowser for help on using the repository browser.