CCfits 2.7
KeyData.h
1// Astrophysics Science Division,
2// NASA/ Goddard Space Flight Center
3// HEASARC
4// http://heasarc.gsfc.nasa.gov
5// e-mail: ccfits@legacy.gsfc.nasa.gov
6//
7// Original author: Ben Dorman
8
9#ifndef KEYDATA_H
10#define KEYDATA_H 1
11#ifdef _MSC_VER
12#include "MSconfig.h"
13#endif
14
15#include "CCfits.h"
16
17// Keyword
18#include "Keyword.h"
19#include <complex>
20#include <iomanip>
21#include "FitsError.h"
22#include "FITSUtil.h"
23
24
25namespace CCfits {
26//class Keyword;
27
28
29
30 template <typename T>
31 class KeyData : public Keyword //## Inherits: <unnamed>%381F43399D58
32 {
33
34 public:
35 KeyData (const KeyData< T > &right);
36 KeyData (const String &keyname,
38 const T &value,
39 HDU* p, // A pointer to the HDU containing the keyword. This is passed to the base class constructor.
40 const String &comment = "",
41 bool isLongStr = false);
42 virtual ~KeyData();
43
44 virtual KeyData <T>* clone () const;
45 virtual void write ();
46 const T& keyval () const;
47 void keyval (const T& value);
48
49 // Additional Public Declarations
50
51 protected:
52 virtual void copy (const Keyword& right);
53 virtual bool compare (const Keyword &right) const;
54 virtual std::ostream & put (std::ostream &s) const;
55
56 // Additional Protected Declarations
57
58 private:
59 // Data Members for Class Attributes
60 T m_keyval;
61
62 // Additional Private Declarations
63
64 private: //## implementation
65 // Additional Implementation Declarations
66
67 };
68
69 class KeyNull : public Keyword
70 {
71 public:
72 enum class ValState {Empty, NumData, StrData};
73 KeyNull (const KeyNull &right);
74 KeyNull (const String &keyname,
75 HDU* p,
76 const String &comment = "");
77 virtual ~KeyNull();
78 virtual KeyNull* clone () const;
79 virtual void write ();
80 double numValue() const;
81 string stringValue() const;
82 ValState state() const;
83 void setStringValue(const string& val);
84 void setNumValue(double val, ValueType writeAs);
85 protected:
86 virtual void copy (const Keyword& right);
87 virtual bool compare (const Keyword &right) const;
88 virtual std::ostream & put (std::ostream &s) const;
89 private:
90 double m_numValue;
91 string m_stringValue;
92 ValState m_state;
93 ValueType m_writeAs;
94 };
95
96#if SPEC_TEMPLATE_IMP_DEFECT || SPEC_TEMPLATE_DECL_DEFECT
97 template<>
98 inline void KeyData<String>::write()
99 {
101 int status = 0;
102 if (fits_update_key(fitsPointer(), Tstring,
103 const_cast<char *>(name().c_str()),
104 const_cast<char*>(m_keyval.c_str()),
105 const_cast<char *>(comment().c_str()),
106 &status)) throw FitsError(status);
107
108 }
109#else
110template<> void KeyData<String>::write();
111#endif
112
113#if SPEC_TEMPLATE_IMP_DEFECT || SPEC_TEMPLATE_DECL_DEFECT
114 template<>
115 inline void KeyData<bool>::write()
116 {
118 int status = 0;
119 int value(0);
120 if (m_keyval) value=1;
121 if (fits_update_key(fitsPointer(), Tlogical,
122 const_cast<char *>(name().c_str()),
123 &value,
124 const_cast<char *>(comment().c_str()),
125 &status)) throw FitsError(status);
126
127 }
128#else
129template<> void KeyData<bool>::write();
130#endif
131
132#ifdef SPEC_TEMPLATE_DECL_DEFECT
133 template <>
134 inline const String& KeyData<String>::keyval() const
135 {
136 return m_keyval;
137
138 }
139#else
140template<> const String& KeyData<String>::keyval() const;
141#endif
142
143#ifndef SPEC_TEMPLATE_DECL_DEFECT
144template<> void KeyData<String>::keyval(const String& );
145#endif
146
147#if SPEC_TEMPLATE_IMP_DEFECT || SPEC_TEMPLATE_DECL_DEFECT
148 template <>
149 inline std::ostream & KeyData<String>::put (std::ostream &s) const
150 {
151 using std::setw;
152 s << "Keyword Name: " << setw(10) << name() << " Value: " << setw(14)
153 << keyval() << " Type: " << setw(20) << " string " << " Comment: " << comment();
154 return s;
155 }
156
157#else
158template<> std::ostream& KeyData<String>::put(std::ostream& s) const;
159#endif
160
161
162#if SPEC_TEMPLATE_IMP_DEFECT || SPEC_TEMPLATE_DECL_DEFECT
163 template <>
164 inline std::ostream & KeyData<bool>::put (std::ostream &s) const
165 {
166 using std::setw;
167 s << "Keyword Name: " << setw(10) << name()
168 << " Value: " << std::boolalpha << setw(8) << keyval()
169 << " Type: " << setw(20) << " logical " << " Comment: " << comment();
170 return s;
171 }
172
173#else
174template<> std::ostream& KeyData<bool>::put(std::ostream& s) const;
175#endif
176
177#if SPEC_TEMPLATE_IMP_DEFECT || SPEC_TEMPLATE_DECL_DEFECT
178 template<>
179 inline void KeyData<std::complex<float> >::write()
180 {
182 int status = 0;
183 FITSUtil::auto_array_ptr<float> keyVal( new float[2]);
184 keyVal[0] = m_keyval.real();
185 keyVal[1] = m_keyval.imag();
186 if (fits_update_key(fitsPointer(), Tcomplex,
187 const_cast<char *>(name().c_str()),
188 keyVal.get(),
189 const_cast<char *>(comment().c_str()),
190 &status)) throw FitsError(status);
191
192 }
193#else
194template<> void KeyData<std::complex<float> >::write();
195#endif
196
197#if SPEC_TEMPLATE_IMP_DEFECT || SPEC_TEMPLATE_DECL_DEFECT
198 template<>
199 inline void KeyData<std::complex<double> >::write()
200 {
202 int status = 0;
203 FITSUtil::auto_array_ptr<double> keyVal(new double[2]);
204 keyVal[0] = m_keyval.real();
205 keyVal[1] = m_keyval.imag();
206 if (fits_update_key(fitsPointer(), Tdblcomplex,
207 const_cast<char *>(name().c_str()),
208 keyVal.get(),
209 const_cast<char *>(comment().c_str()),
210 &status)) throw FitsError(status);
211
212 }
213#else
214template<> void KeyData<std::complex<double> >::write();
215#endif
216
217#if SPEC_TEMPLATE_IMP_DEFECT || SPEC_TEMPLATE_DECL_DEFECT
218 template <>
219 inline std::ostream & KeyData<std::complex<float> >::put (std::ostream &s) const
220 {
221 using std::setw;
222 s << "Keyword Name: " << name() << " Value: " << m_keyval.real() << " + i "
223 << m_keyval.imag() << " Type: " << setw(20) << " complex<float> "
224 << " Comment: " << comment() << std::endl;
225 return s;
226 }
227
228 template <>
229 inline std::ostream & KeyData<std::complex<double> >::put (std::ostream &s) const
230 {
231 using std::setw;
232 s << "Keyword Name: " << name() << " Value: " << m_keyval.real() << " + i "
233 << m_keyval.imag() << " Type: " << setw(20) << " complex<double> "
234 << " Comment: " << comment() << std::endl;
235
236 return s;
237 }
238#else
239template<> std::ostream& KeyData<std::complex<float> >::put(std::ostream& s) const;
240template<> std::ostream& KeyData<std::complex<double> >::put(std::ostream& s) const;
241#endif
242
243#ifdef SPEC_TEMPLATE_DECL_DEFECT
244 template <>
245 inline const std::complex<float>& KeyData<std::complex<float> >::keyval() const
246 {
247 return m_keyval;
248
249 }
250
251 template <>
252 inline void KeyData<std::complex<float> >::keyval(const std::complex<float>& newVal)
253 {
254 m_keyval = newVal;
255
256 }
257
258 template <>
259 inline const std::complex<double>& KeyData<std::complex<double> >::keyval() const
260 {
261 return m_keyval;
262
263 }
264
265 template <>
266 inline void KeyData<std::complex<double> >::keyval(const std::complex<double>& newVal)
267 {
268 m_keyval = newVal;
269
270 }
271
272#else
273template<> const std::complex<float>& KeyData<std::complex<float> >::keyval() const;
274template<> void KeyData<std::complex<float> >::keyval(const std::complex<float>& );
275
276
277
278template<> const std::complex<double>& KeyData<std::complex<double> >::keyval() const;
279template<> void KeyData<std::complex<double> >::keyval(const std::complex<double>& );
280#endif
281
282 // Parameterized Class CCfits::KeyData
283
284 template <typename T>
285 inline std::ostream & KeyData<T>::put (std::ostream &s) const
286 {
287 s << "Keyword Name: " << name() << "\t Value: " << keyval() <<
288 "\t Type: " << keytype() << "\t Comment: " << comment();
289
290 return s;
291 }
292
293 template <typename T>
294 inline const T& KeyData<T>::keyval () const
295 {
296 return m_keyval;
297 }
298
299 template <typename T>
300 inline void KeyData<T>::keyval (const T& value)
301 {
302 m_keyval = value;
303 }
304
305 // Parameterized Class CCfits::KeyData
306
307 template <typename T>
308 KeyData<T>::KeyData(const KeyData<T> &right)
309 :Keyword(right),
310 m_keyval(right.m_keyval)
311 {
312 }
313
314 template <typename T>
315 KeyData<T>::KeyData (const String &keyname,
316 ValueType keytype,
317 const T &value,
318 HDU* p,
319 const String &comment,
320 bool isLongStr)
321 : Keyword(keyname, keytype, p, comment, isLongStr),
322 m_keyval(value)
323 {
324 }
325
326
327 template <typename T>
328 KeyData<T>::~KeyData()
329 {
330 }
331
332
333 template <typename T>
334 void KeyData<T>::copy (const Keyword& right)
335 {
336 Keyword::copy(right);
337 const KeyData<T>& that = static_cast<const KeyData<T>&>(right);
338 m_keyval = that.m_keyval;
339 }
340
341 template <typename T>
342 bool KeyData<T>::compare (const Keyword &right) const
343 {
344 if ( !Keyword::compare(right) ) return false;
345 const KeyData<T>& that = static_cast<const KeyData<T>&>(right);
346 if (this->m_keyval != that.m_keyval) return false;
347 return true;
348 }
349
350 template <typename T>
351 KeyData <T>* KeyData<T>::clone () const
352 {
353 return new KeyData<T>(*this);
354 }
355
356 template <typename T>
357 void KeyData<T>::write ()
358 {
359 Keyword::write();
360 int status = 0;
361 FITSUtil::MatchType<T> keyType;
362 if ( fits_update_key(fitsPointer(),keyType(),
363 const_cast<char *>(name().c_str()),
364 &m_keyval, // fits_write_key takes a void* here
365 const_cast<char *>(comment().c_str()),
366 &status) ) throw FitsError(status);
367 }
368
369 inline double KeyNull::numValue() const
370 {
371 return m_numValue;
372 }
373
374 inline string KeyNull::stringValue() const
375 {
376 return m_stringValue;
377 }
378
379 inline KeyNull::ValState KeyNull::state() const
380 {
381 return m_state;
382 }
383
384 inline void KeyNull::setNumValue(double val, ValueType writeAs)
385 {
386 m_numValue = val;
387 m_state = ValState::NumData;
388 m_writeAs = writeAs;
389 }
390
391 inline void KeyNull::setStringValue(const string& val)
392 {
393 m_stringValue = val;
394 m_state = ValState::StrData;
395 }
396
397 // Additional Declarations
398
399} // namespace CCfits
400
401
402#endif
T & value(T &val) const
get the keyword value
Definition KeywordT.h:29
ValueType keytype() const
return the type of a keyword
Definition Keyword.h:302
virtual void write()
left in for historical reasons, this seldom needs to be called by users
Definition Keyword.cxx:97
const String & comment() const
return the comment field of the keyword
Definition Keyword.h:317
Namespace enclosing all CCfits classes and globals definitions.
Definition AsciiTable.cxx:26
ValueType
CCfits value types and their CFITSIO equivalents (in caps)
Definition CCfits.h:81