00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00019 #ifndef _CppTools_hpp
00020 #define _CppTools_hpp
00021
00022 #include<stdio.h>
00023 #include<stdlib.h>
00024 #include<string>
00025 #include<vector>
00026 #include<typeinfo>
00027 #include <boost/preprocessor/arithmetic/inc.hpp>
00028 #include <boost/preprocessor/punctuation/comma_if.hpp>
00029 #include <boost/preprocessor/repetition.hpp>
00030 #include<iostream>
00031 #include<stdarg.h>
00032 #include<math.h>
00033
00034
00035 #if __GNUC__ >= 3
00036 #define deprecated __attribute__((deprecated))
00037 #else
00038 #define deprecated
00039 #endif // __GNUC__
00040 using namespace std;
00041
00042
00043
00044
00045
00046 typedef unsigned char byte;
00047 typedef unsigned int uint;
00048
00049
00050
00052
00053
00055
00057 template<class T>
00058 struct VarArgTypeConversion
00059 {
00060 static const T &Convert(const T&a){return a;}
00061 };
00062
00063 template<>
00064 struct VarArgTypeConversion<string>
00065 {
00066 static const char * Convert(const string& a){return a.c_str();}
00067 };
00068
00069
00070 #define VARARG_TYPE_CONVERSION_ARGLIST(Z,INDEX,DATA)\
00071 const T ## INDEX & a ## INDEX
00072
00073
00074 #define VARARG_TYPE_CONVERSION_TYPE_CONVERSION_MACRO(Z,INDEX,DATA)\
00075 VarArgTypeConversion<T ## INDEX>::Convert(a ## INDEX)
00076
00077
00078
00080
00081
00082 enum ErrorManagementType {ERROR_USE_EXCEPTIONS,ERROR_SEGFAULT,ERROR_CALL_HANDLER};
00084 extern void SetErrorManagementType(ErrorManagementType _errorManagementType,void(*_ErrorHandler)(string)=NULL);
00085
00086
00088 void ThrowErrorVA(const char *fmt, ...);
00089
00090 #define ThrowError_MACRO(INDEX)\
00091 template< BOOST_PP_ENUM_PARAMS ( INDEX , class T )> \
00092 void ThrowError( const char *fmt,BOOST_PP_ENUM(INDEX,VARARG_TYPE_CONVERSION_ARGLIST, unused) ) \
00093 { ThrowErrorVA(fmt,BOOST_PP_ENUM(INDEX,VARARG_TYPE_CONVERSION_TYPE_CONVERSION_MACRO, unused)); }
00094
00095 ThrowError_MACRO(1) ThrowError_MACRO(7) ThrowError_MACRO(13)
00096 ThrowError_MACRO(2) ThrowError_MACRO(8) ThrowError_MACRO(14)
00097 ThrowError_MACRO(3) ThrowError_MACRO(9) ThrowError_MACRO(15)
00098 ThrowError_MACRO(4) ThrowError_MACRO(10) ThrowError_MACRO(16)
00099 ThrowError_MACRO(5) ThrowError_MACRO(11) ThrowError_MACRO(17)
00100 ThrowError_MACRO(6) ThrowError_MACRO(12) ThrowError_MACRO(18)
00101
00102
00103 inline void ThrowError( const char *fmt){ThrowErrorVA(fmt);}
00104
00106
00107
00109
00110 extern int globalVerbose;
00111
00112 #define mprintf_MACRO(INDEX)\
00113 template< BOOST_PP_ENUM_PARAMS ( INDEX , class T )> \
00114 void mprintf( const char *fmt,BOOST_PP_ENUM(INDEX,VARARG_TYPE_CONVERSION_ARGLIST,(T,a )) ) \
00115 { if(globalVerbose)printf(fmt,BOOST_PP_ENUM(INDEX,VARARG_TYPE_CONVERSION_TYPE_CONVERSION_MACRO,a)); }
00116
00117 mprintf_MACRO(1) mprintf_MACRO(7) mprintf_MACRO(13)
00118 mprintf_MACRO(2) mprintf_MACRO(8) mprintf_MACRO(14)
00119 mprintf_MACRO(3) mprintf_MACRO(9) mprintf_MACRO(15)
00120 mprintf_MACRO(4) mprintf_MACRO(10) mprintf_MACRO(16)
00121 mprintf_MACRO(5) mprintf_MACRO(11) mprintf_MACRO(17)
00122 mprintf_MACRO(6) mprintf_MACRO(12) mprintf_MACRO(18)
00123
00124 inline void mprintf( const char *fmt){if(globalVerbose)printf(fmt);}
00125
00127
00128
00130
00131
00132 extern string SPrintfVA(const char *fmt, ...);
00133 #define SPrintf_MACRO(INDEX)\
00134 template< BOOST_PP_ENUM_PARAMS ( INDEX , class T )> \
00135 string SPrintf( const char *fmt,BOOST_PP_ENUM(INDEX,VARARG_TYPE_CONVERSION_ARGLIST,(T,a )) ) \
00136 { return SPrintfVA(fmt,BOOST_PP_ENUM(INDEX,VARARG_TYPE_CONVERSION_TYPE_CONVERSION_MACRO,a)); }
00137
00138 SPrintf_MACRO(1) SPrintf_MACRO(7) SPrintf_MACRO(13)
00139 SPrintf_MACRO(2) SPrintf_MACRO(8) SPrintf_MACRO(14)
00140 SPrintf_MACRO(3) SPrintf_MACRO(9) SPrintf_MACRO(15)
00141 SPrintf_MACRO(4) SPrintf_MACRO(10) SPrintf_MACRO(16)
00142 SPrintf_MACRO(5) SPrintf_MACRO(11) SPrintf_MACRO(17)
00143 SPrintf_MACRO(6) SPrintf_MACRO(12) SPrintf_MACRO(18)
00144
00145 inline string SPrintf( const char *fmt) { return fmt;}
00146
00147 extern void Replace(string &res,const string & s1,const string & s2);
00149 inline void Replace(string &res,const string & s1,const char *s2){Replace(res,s1,(string)(s2 ? s2 : ""));}
00150 extern int Split( const string &s, char c, vector<string> &res);
00152 inline int Split( const string &s, vector<string> &res ) {return Split(s,' ',res);}
00153 extern int Split( const string &s, const string &sep, vector<string> &res);
00154 extern int Read(const string& fname,string& res,bool fail=false);
00155 extern void *Read(const string& fname,int *size=NULL,bool fail=false);
00156 extern void Write(const string& fname,void *data,int size,bool fail=false);
00157 extern string Chop(const string&s, char c);
00158
00160 inline long int LongInt(const string &s){return strtol( s.c_str(), NULL, 10);}
00162 inline unsigned long int LongUInt(const string &s){return strtoul( s.c_str(), NULL, 10);}
00164 inline double Double(const string &s){return strtod( s.c_str(), NULL);}
00166 inline float Float(const string &s){return (float)Double(s);}
00168
00169
00171 extern vector<string> Strings(string s1,string s2,string s3,string s4,string s5,string s6,string s7,string s8,string s9,string s10);
00172 extern vector<string> Strings(string s1,string s2,string s3,string s4,string s5,string s6,string s7,string s8,string s9);
00173 extern vector<string> Strings(string s1,string s2,string s3,string s4,string s5,string s6,string s7,string s8);
00174 extern vector<string> Strings(string s1,string s2,string s3,string s4,string s5,string s6,string s7);
00175 extern vector<string> Strings(string s1,string s2,string s3,string s4,string s5,string s6);
00176 extern vector<string> Strings(string s1,string s2,string s3,string s4,string s5);
00177 extern vector<string> Strings(string s1,string s2,string s3,string s4);
00178 extern vector<string> Strings(string s1,string s2,string s3);
00179 extern vector<string> Strings(string s1,string s2);
00180 extern vector<string> Strings(string s1);
00181 extern vector<string> Strings();
00183
00184
00186
00187 extern bool IsSameFile(string filename1,string filename2);
00188 extern bool FileIsMoreRecent(const string &filename1,const string &filename2);
00189 extern string WriteTmpFile(const string &data="");
00190 extern void WriteStringToFile(const string& fname,const string& data);
00191 extern string FilenameExtension(const string& fname);
00192 extern string FilenamePath(const string& fname);
00193 extern string FilenameBasename(const string& fname);
00194 extern ostream& operator<<(ostream& out, const vector<string>& v);
00195 extern int FileSize(const string& fname);
00196 extern void AppendToFile(const string &fname,const string &data);
00197 inline bool FileExists(const string &fName){return FileSize(fName)>=0;}
00198 extern string FindFileInPath(const string &path,const string &fname);
00199 extern string FindFileInPath(const vector<string> &path,const string &fname);
00200 inline string FindFileInPath(const char *path,const string &fname)
00201 {return path ? FindFileInPath(string(path),fname) : string("");}
00203
00204
00206
00207
00209 extern void RemoveFile(string fname);
00210 extern void RemoveFiles(const vector<string>& fnames);
00212 extern string Command(const string &cmd);
00214
00215
00217
00218 extern void GnuPlot(const vector<double> &x,const vector<double> &y,const string &xlabel="",const string &ylabel="");
00219 extern void GnuPlot(const vector<double> &y,const string &ylabel="");
00220 extern void GnuPlot(const vector<float> &x,const vector<float> &y,const string &xlabel="",const string &ylabel="");
00221 extern void GnuPlot(const vector<float> &y,const string &ylabel="");
00223
00224
00225
00226
00228
00229 #include <sys/time.h>
00230 #include <time.h>
00231
00233 inline double DTime()
00234 {
00235 struct timeval tv;
00236 gettimeofday(&tv,NULL);
00237 return(tv.tv_usec/1000000.0+tv.tv_sec);
00238 }
00240 inline double DTime(double T0)
00241 {
00242 struct timeval tv;
00243 gettimeofday(&tv,NULL);
00244 return((tv.tv_usec/1000000.0+tv.tv_sec)-T0);
00245 }
00246
00247
00249
00251 class Periodic
00252 {
00253 public:
00254 Periodic(double nperiod=.1)
00255 {
00256 period=nperiod;
00257 t0=DTime();
00258 last=0;
00259 }
00260 double total(){return(DTime(t0));}
00261 void change_period(double nperiod){period=nperiod;}
00262 double real_period(){return rperiod;}
00263 int operator()(double *prperiod=NULL)
00264 {
00265 double t=DTime(t0);
00266 rperiod=t-last;
00267 if(prperiod){*prperiod=rperiod;}
00268 if((t-last)>period)
00269 {
00270 last=t;
00271 return(1);
00272 }
00273 return(0);
00274 }
00275 private:
00276 double t0;
00277 double last;
00278 double period;
00279 double rperiod;
00280 };
00282
00283
00285
00287 template<class T1,class T2>
00288 inline bool IsSameType(){return typeid(T1)==typeid(T2);}
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303 extern string TypeNameParser(string typeName);
00304 template<class T>
00305 inline string TypeName(){return TypeNameParser(typeid(T).name());}
00306 template<> inline string TypeName<float >(){return "float" ;}
00307 template<> inline string TypeName<double >(){return "double";}
00308 template<> inline string TypeName<byte >(){return "byte" ;}
00309 template<> inline string TypeName<int >(){return "int" ;}
00310 template<> inline string TypeName<short int>(){return "short int" ;}
00311 template<> inline string TypeName<string >(){return "string";}
00312
00313
00315
00316
00317
00319
00321 inline bool IsSmallEndian()
00322 {
00323 int a=0xff00;
00324 byte *p=(byte *)&a;
00325 p++;
00326 if(*p==0xff){return true;}
00327 return false;
00328 }
00329 template<class T>
00330 inline void ReverseEndian(const T &src,T &dst)
00331 {
00332 const byte *psrc=((const byte *)&src)+sizeof(T)-1;
00333 byte *pdst=((byte *)&dst);
00334 for(uint i=0;i<sizeof(T);i++){*pdst++=*psrc--;}
00335 }
00337
00338
00339
00341
00342 template <class Im3DValue> inline Im3DValue AbsoluteValue(const Im3DValue &v)
00343 {ThrowError("AbsoluteValue unimplemented for type:%s",TypeName<Im3DValue>());return v;}
00344 template <> inline double AbsoluteValue<double >(const double &v){return fabs(v);}
00345 template <> inline float AbsoluteValue<float >(const float &v){return fabs(v);}
00346 template <> inline int AbsoluteValue<int >(const int &v){return abs(v);}
00347 template <> inline long int AbsoluteValue<long int>(const long int &v){return labs(v);}
00348 template <> inline short int AbsoluteValue<short int>(const short int &v){return labs(v);}
00349 template <> inline byte AbsoluteValue<byte >(const byte &v){return v ;}
00350 inline double RandomDouble(double vmin,double vmax){return vmin+((vmax-vmin)*rand())/RAND_MAX;}
00351 inline double RandomDouble(){return rand()/(double)RAND_MAX;}
00353
00354 #endif // _CppTools_hpp