CCfits
2.5
|
00001 // Astrophysics Science Division, 00002 // NASA/ Goddard Space Flight Center 00003 // HEASARC 00004 // http://heasarc.gsfc.nasa.gov 00005 // e-mail: ccfits@legacy.gsfc.nasa.gov 00006 // 00007 // Original author: Ben Dorman 00008 00009 #ifndef FITSUTILT_H 00010 #define FITSUTILT_H 00011 00012 #ifdef _MSC_VER 00013 #include "MSconfig.h" // for truncation warning 00014 #endif 00015 00016 00017 #include "FITSUtil.h" 00018 00019 #include<typeinfo> 00020 #include<iostream> 00021 00022 #ifdef SSTREAM_DEFECT 00023 #include <strstream> 00024 #else 00025 #include<sstream> 00026 #endif 00027 00028 namespace CCfits 00029 { 00030 00031 namespace FITSUtil 00032 { 00033 00034 // vector to vector conversion. 00035 00036 template <typename S, typename T> 00037 void 00038 fill(std::vector<S>& outArray, const std::vector<T>& inArray, size_t first, size_t last) 00039 { 00040 // vector to vector assign. stdlib takes care of deletion. 00041 int range = last - first + 1; 00042 if (outArray.size() != static_cast<size_t>(range)) outArray.resize(range); 00043 for (size_t j = first - 1; j < last; ++j) 00044 { 00045 outArray[j - first + 1] = static_cast<S>(inArray[j]); 00046 } 00047 } 00048 00049 // vector to valarray conversion. 00050 00051 template <typename S, typename T> 00052 void fill(std::valarray<S>& outArray, const std::vector<T>& inArray, size_t first, size_t last) 00053 { 00054 // vector to valarray assign 00055 int range = last - first + 1; 00056 if (outArray.size() != static_cast<size_t>(range)) outArray.resize(range); 00057 for (size_t j = first - 1; j < last; ++j) 00058 { 00059 outArray[j - first + 1] = static_cast<S>(inArray[j]); 00060 } 00061 } 00062 // valarray to valarray conversion. 00063 00064 00065 template <typename S, typename T> 00066 void fill(std::valarray<S>& outArray, const std::valarray<T>& inArray) 00067 { 00068 size_t n = inArray.size(); 00069 if (outArray.size() != n) outArray.resize(n); 00070 for (size_t j = 0;j < n; ++j) outArray[j] 00071 = static_cast<S>(inArray[j]); 00072 } 00073 00074 #ifdef TEMPLATE_AMBIG7_DEFECT 00075 template <typename S, typename T> 00076 void fillMSva(std::vector<S>& outArray, const std::valarray<T>& inArray) 00077 { 00078 size_t n = inArray.size(); 00079 if (outArray.size() != n) outArray.resize(n); 00080 for (size_t j = 0;j < n; ++j) outArray[j] 00081 = static_cast<S>(inArray[j]); 00082 } 00083 00084 #else 00085 template <typename S, typename T> 00086 void fill(std::vector<S>& outArray, const std::valarray<T>& inArray) 00087 { 00088 size_t n = inArray.size(); 00089 if (outArray.size() != n) outArray.resize(n); 00090 for (size_t j = 0;j < n; ++j) outArray[j] 00091 = static_cast<S>(inArray[j]); 00092 } 00093 #endif 00094 00095 // throw exceptions for string conversions to anything other than string. 00096 00097 00098 template <typename T> 00099 void 00100 fill(std::vector<string>& outArray, const std::vector<T>& inArray, size_t first, size_t last) 00101 { 00102 first = 0; 00103 last = 0; 00104 throw InvalidConversion(errorMessage(outArray,inArray),false); 00105 00106 } 00107 00108 template <typename T> 00109 void fill(std::vector<T>& outArray, const std::vector<string>& inArray, size_t first, size_t last) 00110 { 00111 first = 0; 00112 last = 0; 00113 throw InvalidConversion(errorMessage(outArray,inArray),false); 00114 } 00115 00116 00117 00118 00119 template<typename S, typename T> 00120 string errorMessage( const S& out, const T& in) 00121 { 00122 #ifdef SSTREAM_DEFECT 00123 std::ostrstream errMsg; 00124 #else 00125 std::ostringstream errMsg; 00126 #endif 00127 errMsg << " Error: no conversion from " << typeid(in).name() << " to " 00128 << typeid(out).name() << std::endl; 00129 return errMsg.str(); 00130 00131 } 00132 00133 } 00134 00135 } // namespace CCfits 00136 00137 00138 #endif