CCfits  2.5
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 
25 namespace 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, ValueType keytype, const T &value, HDU* p, // A pointer to the HDU containing the keyword. This is passed to the base class constructor.
37  const String &comment = "");
38  virtual ~KeyData();
39 
40  virtual KeyData <T>* clone () const;
41  virtual void write ();
42  const T& keyval () const;
43  void keyval (const T& value);
44 
45  // Additional Public Declarations
46 
47  protected:
48  virtual void copy (const Keyword& right);
49  virtual bool compare (const Keyword &right) const;
50  virtual std::ostream & put (std::ostream &s) const;
51 
52  // Additional Protected Declarations
53 
54  private:
55  // Data Members for Class Attributes
56  T m_keyval;
57 
58  // Additional Private Declarations
59 
60  private: //## implementation
61  // Additional Implementation Declarations
62 
63  };
64 #if SPEC_TEMPLATE_IMP_DEFECT || SPEC_TEMPLATE_DECL_DEFECT
65  template<>
66  inline void KeyData<String>::write()
67  {
69  int status = 0;
70  if (fits_update_key(fitsPointer(), Tstring,
71  const_cast<char *>(name().c_str()),
72  const_cast<char*>(m_keyval.c_str()),
73  const_cast<char *>(comment().c_str()),
74  &status)) throw FitsError(status);
75 
76  }
77 #else
78 template<> void KeyData<String>::write();
79 #endif
80 
81 #if SPEC_TEMPLATE_IMP_DEFECT || SPEC_TEMPLATE_DECL_DEFECT
82  template<>
83  inline void KeyData<bool>::write()
84  {
86  int status = 0;
87  int value(0);
88  if (m_keyval) value=1;
89  if (fits_update_key(fitsPointer(), Tlogical,
90  const_cast<char *>(name().c_str()),
91  &value,
92  const_cast<char *>(comment().c_str()),
93  &status)) throw FitsError(status);
94 
95  }
96 #else
97 template<> void KeyData<bool>::write();
98 #endif
99 
100 #ifdef SPEC_TEMPLATE_DECL_DEFECT
101  template <>
102  inline const String& KeyData<String>::keyval() const
103  {
104  return m_keyval;
105 
106  }
107 #else
108 template<> const String& KeyData<String>::keyval() const;
109 #endif
110 
111 #ifndef SPEC_TEMPLATE_DECL_DEFECT
112 template<> void KeyData<String>::keyval(const String& );
113 #endif
114 
115 #if SPEC_TEMPLATE_IMP_DEFECT || SPEC_TEMPLATE_DECL_DEFECT
116  template <>
117  inline std::ostream & KeyData<String>::put (std::ostream &s) const
118  {
119  using std::setw;
120  s << "Keyword Name: " << setw(10) << name() << " Value: " << setw(14)
121  << keyval() << " Type: " << setw(20) << " string " << " Comment: " << comment();
122  return s;
123  }
124 
125 #else
126 template<> std::ostream& KeyData<String>::put(std::ostream& s) const;
127 #endif
128 
129 
130 #if SPEC_TEMPLATE_IMP_DEFECT || SPEC_TEMPLATE_DECL_DEFECT
131  template <>
132  inline std::ostream & KeyData<bool>::put (std::ostream &s) const
133  {
134  using std::setw;
135  s << "Keyword Name: " << setw(10) << name()
136  << " Value: " << std::boolalpha << setw(8) << keyval()
137  << " Type: " << setw(20) << " logical " << " Comment: " << comment();
138  return s;
139  }
140 
141 #else
142 template<> std::ostream& KeyData<bool>::put(std::ostream& s) const;
143 #endif
144 
145 #if SPEC_TEMPLATE_IMP_DEFECT || SPEC_TEMPLATE_DECL_DEFECT
146  template<>
147  inline void KeyData<std::complex<float> >::write()
148  {
149  Keyword::write();
150  int status = 0;
151  FITSUtil::auto_array_ptr<float> keyVal( new float[2]);
152  keyVal[0] = m_keyval.real();
153  keyVal[1] = m_keyval.imag();
154  if (fits_update_key(fitsPointer(), Tcomplex,
155  const_cast<char *>(name().c_str()),
156  keyVal.get(),
157  const_cast<char *>(comment().c_str()),
158  &status)) throw FitsError(status);
159 
160  }
161 #else
162 template<> void KeyData<std::complex<float> >::write();
163 #endif
164 
165 #if SPEC_TEMPLATE_IMP_DEFECT || SPEC_TEMPLATE_DECL_DEFECT
166  template<>
167  inline void KeyData<std::complex<double> >::write()
168  {
169  Keyword::write();
170  int status = 0;
171  FITSUtil::auto_array_ptr<double> keyVal(new double[2]);
172  keyVal[0] = m_keyval.real();
173  keyVal[1] = m_keyval.imag();
174  if (fits_update_key(fitsPointer(), Tdblcomplex,
175  const_cast<char *>(name().c_str()),
176  keyVal.get(),
177  const_cast<char *>(comment().c_str()),
178  &status)) throw FitsError(status);
179 
180  }
181 #else
182 template<> void KeyData<std::complex<double> >::write();
183 #endif
184 
185 #if SPEC_TEMPLATE_IMP_DEFECT || SPEC_TEMPLATE_DECL_DEFECT
186  template <>
187  inline std::ostream & KeyData<std::complex<float> >::put (std::ostream &s) const
188  {
189  using std::setw;
190  s << "Keyword Name: " << name() << " Value: " << m_keyval.real() << " + i "
191  << m_keyval.imag() << " Type: " << setw(20) << " complex<float> "
192  << " Comment: " << comment() << std::endl;
193  return s;
194  }
195 
196  template <>
197  inline std::ostream & KeyData<std::complex<double> >::put (std::ostream &s) const
198  {
199  using std::setw;
200  s << "Keyword Name: " << name() << " Value: " << m_keyval.real() << " + i "
201  << m_keyval.imag() << " Type: " << setw(20) << " complex<double> "
202  << " Comment: " << comment() << std::endl;
203 
204  return s;
205  }
206 #else
207 template<> std::ostream& KeyData<std::complex<float> >::put(std::ostream& s) const;
208 template<> std::ostream& KeyData<std::complex<double> >::put(std::ostream& s) const;
209 #endif
210 
211 #ifdef SPEC_TEMPLATE_DECL_DEFECT
212  template <>
213  inline const std::complex<float>& KeyData<std::complex<float> >::keyval() const
214  {
215  return m_keyval;
216 
217  }
218 
219  template <>
220  inline void KeyData<std::complex<float> >::keyval(const std::complex<float>& newVal)
221  {
222  m_keyval = newVal;
223 
224  }
225 
226  template <>
227  inline const std::complex<double>& KeyData<std::complex<double> >::keyval() const
228  {
229  return m_keyval;
230 
231  }
232 
233  template <>
234  inline void KeyData<std::complex<double> >::keyval(const std::complex<double>& newVal)
235  {
236  m_keyval = newVal;
237 
238  }
239 
240 #else
241 template<> const std::complex<float>& KeyData<std::complex<float> >::keyval() const;
242 template<> void KeyData<std::complex<float> >::keyval(const std::complex<float>& );
243 
244 
245 
246 template<> const std::complex<double>& KeyData<std::complex<double> >::keyval() const;
247 template<> void KeyData<std::complex<double> >::keyval(const std::complex<double>& );
248 #endif
249 
250  // Parameterized Class CCfits::KeyData
251 
252  template <typename T>
253  inline std::ostream & KeyData<T>::put (std::ostream &s) const
254  {
255  s << "Keyword Name: " << name() << "\t Value: " << keyval() <<
256  "\t Type: " << keytype() << "\t Comment: " << comment();
257 
258  return s;
259  }
260 
261  template <typename T>
262  inline const T& KeyData<T>::keyval () const
263  {
264  return m_keyval;
265  }
266 
267  template <typename T>
268  inline void KeyData<T>::keyval (const T& value)
269  {
270  m_keyval = value;
271  }
272 
273  // Parameterized Class CCfits::KeyData
274 
275  template <typename T>
276  KeyData<T>::KeyData(const KeyData<T> &right)
277  :Keyword(right),
278  m_keyval(right.m_keyval)
279  {
280  }
281 
282  template <typename T>
283  KeyData<T>::KeyData (const String &keyname, ValueType keytype, const T &value, HDU* p, const String &comment)
284  : Keyword(keyname, keytype, p, comment),
285  m_keyval(value)
286  {
287  }
288 
289 
290  template <typename T>
291  KeyData<T>::~KeyData()
292  {
293  }
294 
295 
296  template <typename T>
297  void KeyData<T>::copy (const Keyword& right)
298  {
299  Keyword::copy(right);
300  const KeyData<T>& that = static_cast<const KeyData<T>&>(right);
301  m_keyval = that.m_keyval;
302  }
303 
304  template <typename T>
305  bool KeyData<T>::compare (const Keyword &right) const
306  {
307  if ( !Keyword::compare(right) ) return false;
308  const KeyData<T>& that = static_cast<const KeyData<T>&>(right);
309  if (this->m_keyval != that.m_keyval) return false;
310  return true;
311  }
312 
313  template <typename T>
314  KeyData <T>* KeyData<T>::clone () const
315  {
316  return new KeyData<T>(*this);
317  }
318 
319  template <typename T>
320  void KeyData<T>::write ()
321  {
322  Keyword::write();
323  int status = 0;
324  FITSUtil::MatchType<T> keyType;
325  if ( fits_update_key(fitsPointer(),keyType(),
326  const_cast<char *>(name().c_str()),
327  &m_keyval, // fits_write_key takes a void* here
328  const_cast<char *>(comment().c_str()),
329  &status) ) throw FitsError(status);
330  }
331 
332  // Additional Declarations
333 
334 } // namespace CCfits
335 
336 
337 #endif
T & value(T &val) const
get the keyword value
Definition: KeywordT.h:29
fitsfile * fitsPointer() const
return the fitsfile pointer for the FITS object containing the HDU
Definition: HDU.cxx:310
const string & comment() const
return the comment string previously read by getComment()
Definition: HDU.h:852
virtual void write()
left in for historical reasons, this seldom needs to be called by users
Definition: Keyword.cxx:95
const String & name() const
return the name of a keyword
Definition: Keyword.h:314
Namespace enclosing all CCfits classes and globals definitions.
Definition: AsciiTable.cxx:26
Keyword(const Keyword &right)
copy constructor
Definition: Keyword.cxx:36
ValueType
CCfits value types and their CFITSIO equivalents (in caps)
Definition: CCfits.h:81
HDU(const HDU &right)
copy constructor
Definition: HDU.cxx:88
ValueType keytype() const
return the type of a keyword
Definition: Keyword.h:294
const String & comment() const
return the comment field of the keyword
Definition: Keyword.h:309
fitsfile * fitsPointer() const
return a pointer to the FITS file containing the parent HDU.
Definition: Keyword.cxx:107