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 #include "deprecated.hh"
00012 
00013 namespace Dune {
00014 
00016 template <bool flag> class CompileTimeChecker;
00017 
00020 template <> class CompileTimeChecker<true> { } DUNE_DEPRECATED;
00021 
00022 
00028 template <int N>
00029 struct Int2Type {
00030   enum { value = N };
00031 };
00032 
00038 
00039 template <class T>
00040 int sign(const T& val) 
00041 {
00042   return (val < 0 ? -1 : 1);
00043 }
00044 
00046 template<class T>
00047 T SQR (T t)
00048 {
00049   return t*t;
00050 }
00051 
00053 template <int m, int p> 
00054 struct Power_m_p
00055 {
00056   // power stores m^p
00057   enum { power = (m * Power_m_p<m,p-1>::power ) };
00058 };
00059 
00061 template <int m> 
00062 struct Power_m_p< m , 0>
00063 {
00064   // m^0 = 1
00065   enum { power = 1 };
00066 };
00067 
00069 template <int m> 
00070 struct Factorial
00071 {
00073   enum { factorial = m * Factorial<m-1>::factorial };
00074 };
00075 
00077 template <> 
00078 struct Factorial<0>
00079 {
00080   // 0! = 1
00081   enum { factorial = 1 };
00082 };
00083 
00084 //********************************************************************
00085 //
00086 // generate filenames with timestep number in it 
00087 //
00088 //********************************************************************
00089 
00091 inline std::string genFilename(const std::string& path, 
00092                                const std::string& fn, 
00093                                int ntime, 
00094                                int precision = 6)
00095 {
00096   std::ostringstream name;
00097 
00098   if(path.size() > 0)
00099   {
00100     name << path; 
00101     name << "/"; 
00102   }
00103   name << fn;
00104  
00105   char cp[256];
00106   switch(precision)
00107   {
00108     case 2  : { sprintf(cp, "%02d", ntime); break; }
00109     case 3  : { sprintf(cp, "%03d", ntime); break; }
00110     case 4  : { sprintf(cp, "%04d", ntime); break; }
00111     case 5  : { sprintf(cp, "%05d", ntime); break; }
00112     case 6  : { sprintf(cp, "%06d", ntime); break; }
00113     case 7  : { sprintf(cp, "%07d", ntime); break; }
00114     case 8  : { sprintf(cp, "%08d", ntime); break; }
00115     case 9  : { sprintf(cp, "%09d", ntime); break; }
00116     case 10 : { sprintf(cp, "%010d", ntime); break; }
00117     default: 
00118       {
00119         DUNE_THROW(Exception, "Couldn't gernerate filename with precision = "<<precision);
00120       }
00121   }
00122   name << cp;
00123 
00124   // here implicitly a string is generated 
00125   return name.str();
00126 }
00127     
00130 }
00131 
00132 
00133 #endif

Generated on Tue Jul 28 22:27:50 2009 for dune-common by  doxygen 1.5.6