FITSUtilT.h

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

Generated on Thu Nov 6 10:44:38 2008 for CCfits by  doxygen 1.5.4