misc.hh

Go to the documentation of this file.
00001 #ifndef MISC_HH
00002 #define MISC_HH
00003 
00008 #include <iostream>
00009 #include <sstream>
00010 #include "exceptions.hh"
00011 
00012 namespace Dune {
00013 
00015 template <bool flag> class CompileTimeChecker;
00016 
00019 template <> class CompileTimeChecker<true> { };
00020 
00021 
00027 template <int N>
00028 struct Int2Type {
00029   enum { value = N };
00030 };
00031 
00037 
00038 template <class T>
00039 int sign(const T& val) 
00040 {
00041   return (val < 0 ? -1 : 1);
00042 }
00043 
00045 template<class T>
00046 T SQR (T t)
00047 {
00048   return t*t;
00049 }
00050 
00052 template <int m, int p> 
00053 struct Power_m_p
00054 {
00055   // power stores m^p
00056   enum { power = (m * Power_m_p<m,p-1>::power ) };
00057 };
00058 
00060 template <int m> 
00061 struct Power_m_p< m , 0>
00062 {
00063   // m^0 = 1
00064   enum { power = 1 };
00065 };
00066 
00068 template <int m> 
00069 struct Factorial
00070 {
00072   enum { factorial = m * Factorial<m-1>::factorial };
00073 };
00074 
00076 template <> 
00077 struct Factorial<0>
00078 {
00079   // 0! = 1
00080   enum { factorial = 1 };
00081 };
00082 
00083 //********************************************************************
00084 //
00085 // generate filenames with timestep number in it 
00086 //
00087 //********************************************************************
00088 
00090 inline std::string genFilename(const std::string& path, 
00091                                const std::string& fn, 
00092                                int ntime, 
00093                                int precision = 6)
00094 {
00095   std::ostringstream name;
00096 
00097   if(path.size() > 0)
00098   {
00099     name << path; 
00100     name << "/"; 
00101   }
00102   name << fn;
00103  
00104   char cp[256];
00105   switch(precision)
00106   {
00107     case 2  : { sprintf(cp, "%02d", ntime); break; }
00108     case 3  : { sprintf(cp, "%03d", ntime); break; }
00109     case 4  : { sprintf(cp, "%04d", ntime); break; }
00110     case 5  : { sprintf(cp, "%05d", ntime); break; }
00111     case 6  : { sprintf(cp, "%06d", ntime); break; }
00112     case 7  : { sprintf(cp, "%07d", ntime); break; }
00113     case 8  : { sprintf(cp, "%08d", ntime); break; }
00114     case 9  : { sprintf(cp, "%09d", ntime); break; }
00115     case 10 : { sprintf(cp, "%010d", ntime); break; }
00116     default: 
00117       {
00118         DUNE_THROW(Exception, "Couldn't gernerate filename with precision = "<<precision);
00119       }
00120   }
00121   name << cp;
00122 
00123   // here implicitly a string is generated 
00124   return name.str();
00125 }
00126     
00129 }
00130 
00131 
00132 #endif

Generated on 12 Dec 2007 with Doxygen (ver 1.5.1)