Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

StringArgumentConversion.hpp

Go to the documentation of this file.
00001 /* ImLib3D
00002  * Copyright (c) 2001, ULP-IPB Strasbourg.
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or (at
00007  * your option) any later version.
00008  * 
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  * 
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00020 #ifndef _StringArgumentConversion_hpp
00021 #define _StringArgumentConversion_hpp
00022 
00023 #include<ImLib3D/FFT.hpp>
00024 #include<ImLib3D/Image3DSet.hpp>
00025 #include<ImLib3D/BitImage3D.hpp>
00026 #include<ImLib3D/TypeSafeGenericPointer.hpp>
00027 
00028 template<class T>
00029 void ImProcOutputArgToFile(const T&arg,const string &str){arg.WriteToFile(str);}
00030 template<> inline void ImProcOutputArgToFile<double>(const double&arg,const string &str){cout << "result:" << arg << endl;}
00031 template<> inline void ImProcOutputArgToFile<float>(const float&arg,const string &str){cout << "result:" << arg << endl;}
00032 template<> inline void ImProcOutputArgToFile<int>(const int&arg,const string &str){cout << "result:" << arg << endl;}
00033 template<> inline void ImProcOutputArgToFile<uint>(const uint&arg,const string &str){cout << "result:" << arg << endl;}
00034 template<> inline void ImProcOutputArgToFile<short int>(const short int&arg,const string &str){cout << "result:" << arg << endl;}
00035 template<> inline void ImProcOutputArgToFile<byte>(const byte &arg,const string &str){cout << "result:" << arg << endl;}
00036 template<> inline void ImProcOutputArgToFile<bool>(const bool &arg,const string &str){cout << "result:" << arg << endl;}
00037 template<> inline void ImProcOutputArgToFile<complexd>(const complexd&arg,const string &str){cout << "result:" << arg << endl;}
00038 template<> inline void ImProcOutputArgToFile<complexf>(const complexf&arg,const string &str){cout << "result:" << arg << endl;}
00039 template<> inline void ImProcOutputArgToFile<Vect3Di>(const Vect3Di&arg,const string &str){cout << "result:" << arg << endl;}
00040 template<> inline void ImProcOutputArgToFile<Vect3Df>(const Vect3Df&arg,const string &str){cout << "result:" << arg << endl;}
00041 template<> inline void ImProcOutputArgToFile<RectZone3Di>(const RectZone3Di&arg,const string &str){cout << "result:" << arg << endl;}
00042 template<> inline void ImProcOutputArgToFile<BitMask3D::BitValue>(const BitMask3D::BitValue &arg,const string &str) {cout << "result:" << (uint)arg << endl;}
00043 template<> inline void ImProcOutputArgToFile<BitImage3D<byte,2,byte>::BitValue>(const BitImage3D<byte,2,byte>::BitValue &arg,const string &str) {cout << "result:" << (uint)arg << endl;}
00044 
00045 template<class T>
00046 void ImProcOutputArg(T &arg,const string &str,vector<TypeSafeGenericPointer> &alternateArgs,int argNum)
00047 {
00048     if(alternateArgs.size()>(uint)argNum && alternateArgs[argNum].IsSet())
00049     {
00050         *alternateArgs[argNum].template Get<T *>()=arg;
00051     }
00052     else
00053     {
00054         // note: str may ="" for optional output pointer arg
00055         if(str!="") {ImProcOutputArgToFile<T>(arg,str);}
00056     }
00057 }
00058 
00059 template<class T>
00060 void CleanupArg(T &arg,vector<TypeSafeGenericPointer> &alternateArgs,int argNum)
00061 {
00062     if(alternateArgs.size()>(uint)argNum && alternateArgs[argNum].IsSet())
00063     {
00064 //      *alternateArgs[argNum].template Get<T *>()=arg;
00065     }
00066     else
00067     {
00068         if(&arg){delete &arg;}
00069     }
00070 }
00071 
00072 
00073 
00074 template<class T>
00075 T FromString(const string &str){return T(str);}
00076 template<>        inline  complexd    FromString <complexd   >(const string &s) {return (complexd)Double(s);}
00077 template<>        inline  complexf    FromString <complexf   >(const string &s) {return (complexf)Double(s);}
00078 template<>        inline  int         FromString <int      >(const string &s) {return (int)LongInt(s);}
00079 template<>        inline  uint        FromString <uint     >(const string &s) {return (uint)LongUInt(s);}
00080 template<>        inline  short int FromString <short int>(const string &s) {return (short int)LongInt(s);}
00081 template<>        inline  unsigned short FromString <unsigned short>(const string &s) {return (unsigned short)LongUInt(s);}
00082 template<>        inline  byte   FromString <byte    >(const string &s) {return (byte)LongInt(s);}
00083 template<>        inline  double FromString <double  >(const string &s) {return Double(s);}
00084 template<>        inline  float  FromString <float   >(const string &s) {return Float(s);}
00085 template<>        inline  Image3DSet<Image3Df>    FromString <Image3DSet<Image3Df> >(const string &s) 
00086 {
00087 //  printf ("In FromString for Image3DSet\n");
00088     vector<string> filenames;
00089     Split(s, " ", filenames);
00090     Image3DSet<Image3Df> set(filenames);
00091     return set;
00092 }
00093 template<>        inline  vector<float>    FromString <vector<float> >(const string &s) 
00094 {
00095     vector<float> res;
00096     vector<string> values;
00097     Split(s, " ", values);
00098     for(uint i=0;i<values.size();i++){res.push_back(Float(values[i]));}
00099     return res;
00100 }
00101 template<>        inline  vector<double>    FromString <vector<double> >(const string &s) 
00102 {
00103     vector<double> res;
00104     vector<string> values;
00105     Split(s, " ", values);
00106     for(uint i=0;i<values.size();i++){res.push_back(Float(values[i]));}
00107     return res;
00108 }
00109 template<>        inline  Vect3Df  FromString <Vect3Df   >(const string &s) 
00110 {
00111     vector<string> els;
00112     Split(s,els);
00113     if(els.size()!=3){ThrowError("FromString Vect3Df bad number of args, found %d",els.size());}
00114     return Vect3Df(Float(els[0]),Float(els[1]),Float(els[2]));
00115 }
00116 template<>        inline  Vect3Di  FromString <Vect3Di   >(const string &s) 
00117 {
00118     vector<string> els;
00119     Split(s,els);
00120     if(els.size()!=3){ThrowError("FromString Vect3Di bad number of args, found %d",els.size());}
00121     return Vect3Di(LongInt(els[0]),LongInt(els[1]),LongInt(els[2]));
00122 }
00123 template<>        inline  RectZone3Di  FromString <RectZone3Di   >(const string &s) 
00124 {
00125     vector<string> els;
00126     Split(s,els);
00127     if(els.size()!=6){ThrowError("FromString RectZone3Di bad number of args, found %d",els.size());}
00128     return RectZone3Di(LongInt(els[0]),LongInt(els[1]),LongInt(els[2]),
00129                        LongInt(els[3]),LongInt(els[4]),LongInt(els[5]));
00130 }
00131 template<>        inline  Size3D  FromString <Size3D   >(const string &s) 
00132 {
00133     vector<string> els;
00134     Split(s,els);
00135     if(els.size()!=3){ThrowError("FromString Size3D bad number of args, found %d",els.size());}
00136     return Size3D(LongInt(els[0]),LongInt(els[1]),LongInt(els[2]));
00137 }
00138 template<>        inline  bool  FromString <bool >(const string &s)
00139 {
00140     if(s=="true"  || s=="TRUE"  || s=="True"  || s=="yes"  || s=="on"  || s=="1" ){return true;}
00141     else
00142     if(s=="false" || s=="FALSE" || s=="False" || s=="no"   || s=="off" || s=="0"){return false;}
00143     else
00144     {ThrowError("Cannot convert \"%s\" to boolean",s.c_str());}
00145     return false;
00146 }
00147 
00148 template<>        inline  BitMask3D::BitValue   FromString <BitMask3D::BitValue    >(const string &s) 
00149 {return ( BitMask3D::BitValue)LongInt(s);}
00150 template<>        inline  BitImage3D<byte,2,byte>::BitValue   FromString <BitImage3D<byte,2,byte>::BitValue    >(const string &s) 
00151 {return ( BitImage3D<byte,2,byte>::BitValue)LongInt(s);}
00152 
00153 template<class T>
00154 T *StringOrAlternateInputArg(const string &str,vector<TypeSafeGenericPointer> &alternateArgs,int argNum)
00155 {
00156     if(alternateArgs.size()>(uint)argNum && alternateArgs[argNum].IsSet())
00157     {
00158         return alternateArgs[argNum].template Get<T *>();
00159     }
00160     else
00161     {
00162         return new T(FromString<T>(str));
00163     }
00164 }
00165 
00166 template<class T>
00167 T *CreateOutputArg(vector<TypeSafeGenericPointer> &alternateArgs,int argNum)
00168 {
00169     if(alternateArgs.size()>(uint)argNum && alternateArgs[argNum].IsSet())
00170     {
00171         return alternateArgs[argNum].template Get<T *>();
00172     }
00173     else
00174     {
00175         return new T();
00176     }
00177 }
00178 
00179 
00180 template<class T>
00181 T *OptionalPointerArg(const string &str,vector<TypeSafeGenericPointer> &alternateArgs,int argNum)
00182 {
00183     if(alternateArgs.size()>(uint)argNum && alternateArgs[argNum].IsSet())
00184     {
00185         return alternateArgs[argNum].template Get<T *>();
00186     }
00187     else
00188     if(str!="")
00189     {
00190         return new T(FromString<T>(str));
00191     }
00192     else{return NULL;}
00193 }
00194 template<class T>
00195 T *OptionalPointerArgCreateOutput(const string &str,vector<TypeSafeGenericPointer> &alternateArgs,int argNum)
00196 {
00197     if(alternateArgs.size()>(uint)argNum && alternateArgs[argNum].IsSet())
00198     {
00199         return alternateArgs[argNum].template Get<T *>();
00200     }
00201     else
00202     if(str!="")
00203     {
00204         return new T();
00205     }
00206     else{return NULL;}
00207 }
00208 template<class T>
00209 T *OptionalDefaultValueArg(const string &str,vector<TypeSafeGenericPointer> &alternateArgs,int argNum,const T &defaultValue)
00210 {
00211     if(alternateArgs.size()>(uint)argNum && alternateArgs[argNum].IsSet())
00212     {
00213         return alternateArgs[argNum].template Get<T *>();
00214     }
00215     else
00216     if(str!="")
00217     {
00218         return new T(FromString<T>(str));
00219     }
00220     else{return new T(defaultValue);}
00221 }
00222 #endif // _StringArgumentConversion_hpp

Generated on Fri Jun 17 13:36:07 2005 for ImLib3D by  doxygen 1.4.2