CCfits  2.4
FITSUtilT.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 FITSUTILT_H
10 #define FITSUTILT_H
11 
12 #ifdef _MSC_VER
13 #include "MSconfig.h" // for truncation warning
14 #endif
15 
16 #ifdef HAVE_CONFIG_H
17 #include "config.h"
18 #endif
19 
20 #include "FITSUtil.h"
21 
22 #include<typeinfo>
23 #include<iostream>
24 
25 #ifdef SSTREAM_DEFECT
26 #include <strstream>
27 #else
28 #include<sstream>
29 #endif
30 
31 namespace CCfits
32 {
33 
34  namespace FITSUtil
35  {
36 
37  // vector to vector conversion.
38 
39  template <typename S, typename T>
40  void
41  fill(std::vector<S>& outArray, const std::vector<T>& inArray, size_t first, size_t last)
42  {
43  // vector to vector assign. stdlib takes care of deletion.
44  int range = last - first + 1;
45  if (outArray.size() != static_cast<size_t>(range)) outArray.resize(range);
46  for (size_t j = first - 1; j < last; ++j)
47  {
48  outArray[j - first + 1] = static_cast<S>(inArray[j]);
49  }
50  }
51 
52  // vector to valarray conversion.
53 
54  template <typename S, typename T>
55  void fill(std::valarray<S>& outArray, const std::vector<T>& inArray, size_t first, size_t last)
56  {
57  // vector to valarray assign
58  int range = last - first + 1;
59  if (outArray.size() != static_cast<size_t>(range)) outArray.resize(range);
60  for (size_t j = first - 1; j < last; ++j)
61  {
62  outArray[j - first + 1] = static_cast<S>(inArray[j]);
63  }
64  }
65  // valarray to valarray conversion.
66 
67 
68  template <typename S, typename T>
69  void fill(std::valarray<S>& outArray, const std::valarray<T>& inArray)
70  {
71  size_t n = inArray.size();
72  if (outArray.size() != n) outArray.resize(n);
73  for (size_t j = 0;j < n; ++j) outArray[j]
74  = static_cast<S>(inArray[j]);
75  }
76 
77 #ifdef TEMPLATE_AMBIG7_DEFECT
78  template <typename S, typename T>
79  void fillMSva(std::vector<S>& outArray, const std::valarray<T>& inArray)
80  {
81  size_t n = inArray.size();
82  if (outArray.size() != n) outArray.resize(n);
83  for (size_t j = 0;j < n; ++j) outArray[j]
84  = static_cast<S>(inArray[j]);
85  }
86 
87 #else
88  template <typename S, typename T>
89  void fill(std::vector<S>& outArray, const std::valarray<T>& inArray)
90  {
91  size_t n = inArray.size();
92  if (outArray.size() != n) outArray.resize(n);
93  for (size_t j = 0;j < n; ++j) outArray[j]
94  = static_cast<S>(inArray[j]);
95  }
96 #endif
97 
98  // throw exceptions for string conversions to anything other than string.
99 
100 
101  template <typename T>
102  void
103  fill(std::vector<string>& outArray, const std::vector<T>& inArray, size_t first, size_t last)
104  {
105  first = 0;
106  last = 0;
107  throw InvalidConversion(errorMessage(outArray,inArray),false);
108 
109  }
110 
111  template <typename T>
112  void fill(std::vector<T>& outArray, const std::vector<string>& inArray, size_t first, size_t last)
113  {
114  first = 0;
115  last = 0;
116  throw InvalidConversion(errorMessage(outArray,inArray),false);
117  }
118 
119 
120 
121 
122  template<typename S, typename T>
123  string errorMessage( const S& out, const T& in)
124  {
125 #ifdef SSTREAM_DEFECT
126  std::ostrstream errMsg;
127 #else
128  std::ostringstream errMsg;
129 #endif
130  errMsg << " Error: no conversion from " << typeid(in).name() << " to "
131  << typeid(out).name() << std::endl;
132  return errMsg.str();
133 
134  }
135 
136  }
137 
138 } // namespace CCfits
139 
140 
141 #endif