-
Notifications
You must be signed in to change notification settings - Fork 1
/
KeywordT.h
270 lines (249 loc) · 7.25 KB
/
KeywordT.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// Astrophysics Science Division,
// NASA/ Goddard Space Flight Center
// HEASARC
// http://heasarc.gsfc.nasa.gov
// e-mail: [email protected]
//
// Original author: Ben Dorman
#ifndef KEYWORDT_H
#define KEYWORDT_H
#include "KeyData.h"
#include "HDU.h"
#include <typeinfo>
#include <sstream>
#ifdef _MSC_VER
#include "MSconfig.h"
#endif
// contains definitions of templated member functions for Keyword. This separate
// file organization is necessary to break cyclic dependency of Keyword on its
// subclass, KeyData.
namespace CCfits
{
template <typename T>
T& Keyword::value (T& val) const
{
try
{
const KeyData<T>& thisKey = dynamic_cast<const KeyData<T>&>(*this);
val = thisKey.keyval();
}
catch (std::bad_cast&)
{
throw Keyword::WrongKeywordValueType(name());
}
return val;
}
template <typename T>
void Keyword::setValue (const T& newValue)
{
try
{
KeyData<T>& thisKey = dynamic_cast<KeyData<T>&>(*this);
thisKey.keyval(newValue);
thisKey.write();
}
catch (std::bad_cast&)
{
throw Keyword::WrongKeywordValueType(name());
}
}
#if SPEC_TEMPLATE_IMP_DEFECT || SPEC_TEMPLATE_DECL_DEFECT
template<>
inline double& Keyword::value(double& val) const
{
switch (m_keytype)
{
case Tint:
{
const KeyData<int>& thisKey = static_cast<const KeyData<int>&>(*this);
val = thisKey.keyval();
}
break;
case Tfloat:
{
const KeyData<float>& thisKey = static_cast<const KeyData<float>&>(*this);
val = thisKey.keyval();
}
break;
case Tdouble:
{
// Note: if val is of type float some precision will be lost here,
// but allow anyway. Presumably the user doesn't mind or they
// wouldn't be using single precision.
const KeyData<double>& thisKey = static_cast<const KeyData<double>&>(*this);
val = thisKey.keyval();
}
break;
case Tstring:
{
// Allow only if string can be converted to an integer.
const KeyData<String>& thisKey = static_cast<const KeyData<String>&>(*this);
std::istringstream testStream(thisKey.keyval());
int stringInt = 0;
if (!(testStream >> stringInt) || !testStream.eof())
{
throw Keyword::WrongKeywordValueType(name());
}
val = stringInt;
}
break;
default:
throw Keyword::WrongKeywordValueType(name());
break;
}
return val;
}
// NOTE: This function actually instantiates Keyword::value<double>
// and therefore must be defined AFTER the specialized
// definition/declaration.
template<>
inline float& Keyword::value(float& val) const
{
double dval=.0;
val = static_cast<float>(value(dval));
return val;
}
template <>
inline int& Keyword::value(int& val) const
{
if (m_keytype == Tstring)
{
// Allow only if string can be converted to an integer.
const KeyData<String>& thisKey = static_cast<const KeyData<String>&>(*this);
std::istringstream testStream(thisKey.keyval());
int stringInt = 0;
if (!(testStream >> stringInt) || !testStream.eof())
{
throw Keyword::WrongKeywordValueType(name());
}
val = stringInt;
}
else if (m_keytype == Tint)
{
const KeyData<int>& thisKey = static_cast<const KeyData<int>&>(*this);
val = thisKey.keyval();
}
else
{
throw Keyword::WrongKeywordValueType(name());
}
return val;
}
template <>
inline String& Keyword::value(String& val) const
{
switch (m_keytype)
{
case Tint:
{
const KeyData<int>& thisKey = static_cast<const KeyData<int>&>(*this);
std::ostringstream oss;
oss << thisKey.keyval();
val = oss.str();
}
break;
case Tfloat:
{
const KeyData<float>& thisKey = static_cast<const KeyData<float>&>(*this);
std::ostringstream oss;
oss << thisKey.keyval();
val = oss.str();
}
break;
case Tdouble:
{
const KeyData<double>& thisKey = static_cast<const KeyData<double>&>(*this);
std::ostringstream oss;
oss << thisKey.keyval();
val = oss.str();
}
break;
case Tstring:
{
const KeyData<String>& thisKey = static_cast<const KeyData<String>&>(*this);
val = thisKey.keyval();
}
break;
default:
throw Keyword::WrongKeywordValueType(name());
}
return val;
}
template <>
inline void Keyword::setValue(const float& newValue)
{
if (m_keytype == Tfloat)
{
KeyData<float>& thisKey = static_cast<KeyData<float>&>(*this);
thisKey.keyval(newValue);
thisKey.write();
}
else if (m_keytype == Tdouble)
{
KeyData<double>& thisKey = static_cast<KeyData<double>&>(*this);
thisKey.keyval(static_cast<double>(newValue));
thisKey.write();
}
else
{
throw Keyword::WrongKeywordValueType(name());
}
}
template <>
inline void Keyword::setValue(const double& newValue)
{
if (m_keytype == Tdouble)
{
KeyData<double>& thisKey = static_cast<KeyData<double>&>(*this);
thisKey.keyval(newValue);
thisKey.write();
}
else if (m_keytype == Tfloat)
{
// This will lose precision but allow it anyway.
KeyData<float>& thisKey = static_cast<KeyData<float>&>(*this);
thisKey.keyval(static_cast<float>(newValue));
thisKey.write();
}
else
{
throw Keyword::WrongKeywordValueType(name());
}
}
template <>
inline void Keyword::setValue(const int& newValue)
{
if (m_keytype == Tint)
{
KeyData<int>& thisKey = static_cast<KeyData<int>&>(*this);
thisKey.keyval(newValue);
thisKey.write();
}
else if (m_keytype == Tfloat)
{
KeyData<float>& thisKey = static_cast<KeyData<float>&>(*this);
thisKey.keyval(static_cast<float>(newValue));
thisKey.write();
}
else if (m_keytype == Tdouble)
{
KeyData<double>& thisKey = static_cast<KeyData<double>&>(*this);
thisKey.keyval(static_cast<double>(newValue));
thisKey.write();
}
else if (m_keytype == Tstring)
{
KeyData<String>& thisKey = static_cast<KeyData<String>&>(*this);
std::ostringstream oss;
oss << newValue;
thisKey.keyval(oss.str());
thisKey.write();
}
else
{
throw Keyword::WrongKeywordValueType(name());
}
}
#endif
} // namespace CCfits
#endif