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

Last change on this file since 19805 was 19804, checked in by tbretz, 5 years ago
Unified the use of the mapping for the entry type in a file or database, moved the code to FileEntry.h, added the misisng 'unique' resource in fits2sql
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 std::cout << "Add 1" << std::endl;
116 switch (t)
117 {
118 case kInt8: ptr = new int8_t[n]; break;
119 case kUInt8: ptr = new uint8_t[n]; break;
120 case kBool: ptr = new uint8_t[n]; break;
121 case kFloat: ptr = new float[n]; break;
122 case kDouble: ptr = new double[n]; break;
123 case kInt16: ptr = new int16_t[n]; break;
124 case kUInt16: ptr = new uint16_t[n]; break;
125 case kInt32: ptr = new int32_t[n]; break;
126 case kUInt32: ptr = new uint32_t[n]; break;
127 case kInt64: ptr = new int64_t[n]; break;
128 case kUInt64: ptr = new uint64_t[n]; break;
129 case kVarchar:
130 case kConst:
131 case kNone:
132 break;
133 }
134 // Indicate that this was allocated and how often
135 counter[ptr]++;
136 }
137
138 Container(const std::string &c, const std::string &value)
139 : branch(value), column(c), type(kConst), num(1), ptr(0)
140 {
141 }
142
143 Container(const Container &c) : branch(c.branch), column(c.column),
144 type(c.type), num(c.num), ptr(c.ptr)
145 {
146 // If this was not allocated in the constructor do not increate the counter
147 if (counter[ptr])
148 counter[ptr]++;
149 }
150
151 // FitsFile (do not mix with RootFile!)
152 Container(const std::string &b, const std::string &c,
153 const BasicType_t &t, const size_t &n, void *_ptr)
154 : branch(b), column(c), type(t), num(n), ptr(_ptr)
155 {
156 }
157
158 ~Container()
159 {
160 // Count how often it gets deleted.
161 counter[ptr]--;
162 // Now it is time to delete it. Note that pointers which were
163 // never allocated (counter==0) now are != 0
164 if (counter[ptr]==0)
165 ::operator delete[](ptr); // It seems root is deleting it already
166 }
167
168 std::string fmt(const size_t &index) const
169 {
170 std::ostringstream str;
171
172 switch (type)
173 {
174 case kVarchar: str << std::string(reinterpret_cast<char*>(ptr), num).c_str(); break;
175 case kFloat: str << std::setprecision(8) << reinterpret_cast<float*>(ptr)[index]; break;
176 case kDouble: str << std::setprecision(16) << reinterpret_cast<double*>(ptr)[index]; break;
177 case kBool:
178 case kInt8: str << int32_t(reinterpret_cast<int8_t*>(ptr)[index]); break;
179 case kUInt8: str << uint32_t(reinterpret_cast<uint8_t*>(ptr)[index]); break;
180 case kInt16: str << reinterpret_cast<int16_t*>(ptr)[index]; break;
181 case kUInt16: str << reinterpret_cast<uint16_t*>(ptr)[index]; break;
182 case kInt32: str << reinterpret_cast<int32_t*>(ptr)[index]; break;
183 case kUInt32: str << reinterpret_cast<uint32_t*>(ptr)[index]; break;
184 case kInt64: str << reinterpret_cast<int64_t*>(ptr)[index]; break;
185 case kUInt64: str << reinterpret_cast<uint64_t*>(ptr)[index]; break;
186 case kConst: str << branch; break;
187 case kNone:
188 break;
189 }
190
191 //if (str.str()=="nan" || str.str()=="-nan" || str.str()=="inf" || str.str()=="-inf")
192 // return "NULL";
193
194 return str.str();
195 }
196 };
197
198 std::map<void*, size_t> Container::counter;
199}
200
201#endif
Note: See TracBrowser for help on using the repository browser.