src/Utility/DiscType.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 /***************************************************************************
00003  *
00004  * The IPPL Framework
00005  * 
00006  *
00007  * Visit http://people.web.psi.ch/adelmann/ for more details
00008  *
00009  ***************************************************************************/
00010 
00011 #ifndef DISC_TYPE_H
00012 #define DISC_TYPE_H
00013 
00014 /***************************************************************************
00015  * DiscType<T> is a simple class that returns a string via the static method
00016  * "str()" based on the type T.  It is specialized for our standard types,
00017  * and has a default value of "u" for "user" or "unknown" types.
00018  ***************************************************************************/
00019 
00020 // include files
00021 #include "Utility/Pstring.h"
00022 #include "AppTypes/Vektor.h"
00023 #include "AppTypes/Tenzor.h"
00024 #include "AppTypes/SymTenzor.h"
00025 #include "AppTypes/AntiSymTenzor.h"
00026 #include "AppTypes/dcomplex.h"
00027 
00028 
00030 // a base class for all DiscType's, that provides common routines to
00031 // parse a type string and determine the apptype, the dimension, and the
00032 // scalar type
00033 struct DiscTypeBase {
00034   // enums for scalar types and apptypes
00035   enum { CHAR, SHORT, INT, LONG, FLOAT, DOUBLE, DCOMPLEX, FCOMPLEX,
00036          SCALAR, VEKTOR, TENZOR, SYMTENZOR, ANTISYMTENZOR, UNKNOWN };
00037 
00038   // determine the scalar data type for a given type string
00039   static int scalarType(const string &s) {
00040     if (s.length() == 0 || s.length() == 2 || s.length() > 3)
00041       return UNKNOWN;
00042     char c = tolower(s[s.length() - 1]);
00043     if (c == 'c')
00044       return CHAR;
00045     else if (c == 's')
00046       return SHORT;
00047     else if (c == 'i')
00048       return INT;
00049     else if (c == 'l')
00050       return LONG;
00051     else if (c == 'f')
00052       return FLOAT;
00053     else if (c == 'd')
00054       return DOUBLE;
00055     else if (c == 'y')
00056       return FCOMPLEX;
00057     else if (c == 'z')
00058       return DCOMPLEX;
00059     else
00060       return UNKNOWN;
00061   }
00062 
00063   // determine the dimension of the apptype.  If it is a scalar, return 0.
00064   // if there is an error, return -1.
00065   static int dim(const string &s) {
00066     if (s.length() == 0 || s.length() == 2 || s.length() > 3)
00067       return (-1);
00068     if (s.length() == 1)
00069       return 0;
00070     char c = s[1];
00071     if (c < '1' || c > '9')
00072       return (-1);
00073     return (c - '1' + 1);
00074   }
00075 
00076   // determine the apptype of the string
00077   static int appType(const string &s) {
00078     if (s.length() == 0 || s.length() == 2 || s.length() > 3)
00079       return UNKNOWN;
00080     if (s.length() == 1)
00081       return SCALAR;
00082     char c = tolower(s[0]);
00083     if (c == 'v')
00084       return VEKTOR;
00085     else if (c == 't')
00086       return TENZOR;
00087     else if (c == 's')
00088       return SYMTENZOR;
00089     else if (c == 'a')
00090       return ANTISYMTENZOR;
00091     else
00092       return UNKNOWN;
00093   }
00094 };
00095 
00096 
00098 // the general class, that is the default if the user does not specify
00099 // one of the specialized types below
00100 template<class T>
00101 struct DiscType : public DiscTypeBase {
00102   static string str() { return string("u"); }
00103 };
00104 
00105 
00107 // specializations for char, short, int, float, double
00108 // codes:
00109 //   c = char
00110 //   s = short
00111 //   i = int
00112 //   l = long
00113 //   f = float
00114 //   d = double
00115 //   z = complex<double>
00116 //   y = complex<float>
00117 #define DEFINE_DISCTYPE_SCALAR(TYPE, STRING)            \
00118 template<>                                              \
00119 struct DiscType< TYPE > : public DiscTypeBase {         \
00120   static string str() { return string(STRING); }        \
00121 };
00122 
00123 DEFINE_DISCTYPE_SCALAR(char,   "c")
00124 DEFINE_DISCTYPE_SCALAR(short,  "s")
00125 DEFINE_DISCTYPE_SCALAR(int,    "i")
00126 DEFINE_DISCTYPE_SCALAR(long,   "l")
00127 DEFINE_DISCTYPE_SCALAR(float,  "f")
00128 DEFINE_DISCTYPE_SCALAR(double, "d")
00129 
00130 DEFINE_DISCTYPE_SCALAR(unsigned char,   "c")
00131 DEFINE_DISCTYPE_SCALAR(unsigned short,  "s")
00132 DEFINE_DISCTYPE_SCALAR(unsigned int,    "i")
00133 DEFINE_DISCTYPE_SCALAR(unsigned long,   "l")
00134 
00135 #ifdef IPPL_HAS_TEMPLATED_COMPLEX
00136 DEFINE_DISCTYPE_SCALAR(complex<float>,  "y")
00137 DEFINE_DISCTYPE_SCALAR(complex<double>, "z")
00138 #else
00139 DEFINE_DISCTYPE_SCALAR(complex, "z")
00140 #endif
00141 
00142 
00144 // specializations for Vektor, Tenzor, SymTenzor, AntiSymTenzor
00145 // codes:
00146 //   vNs = Vektor of dimension N and scalar type s
00147 //   tNs = Tenzor of dimension N and scalar type s
00148 //   sNs = SymTenzor of dimension N and scalar type s
00149 //   aNs = AntiSymTenzor of dimension N and scalar type s
00150 #define DEFINE_DISCTYPE_APPTYPE(TYPE, STRING)           \
00151 template<class T, unsigned D>                           \
00152 struct DiscType< TYPE<T, D> > : public DiscTypeBase {   \
00153   static string str() {                                 \
00154     CTAssert(D < 10);                                   \
00155     string retval = STRING;                             \
00156     retval += "0";                                      \
00157     retval += DiscType<T>::str();                       \
00158     retval[1] += D;                                     \
00159     return retval;                                      \
00160   }                                                     \
00161 };
00162 
00163 DEFINE_DISCTYPE_APPTYPE(Vektor,        "v")
00164 DEFINE_DISCTYPE_APPTYPE(Tenzor,        "t")
00165 DEFINE_DISCTYPE_APPTYPE(SymTenzor,     "s")
00166 DEFINE_DISCTYPE_APPTYPE(AntiSymTenzor, "a")
00167 
00168 #endif
00169 
00170 /***************************************************************************
00171  * $RCSfile: DiscType.h,v $   $Author: adelmann $
00172  * $Revision: 1.1.1.1 $   $Date: 2003/01/23 07:40:33 $
00173  * IPPL_VERSION_ID: $Id: DiscType.h,v 1.1.1.1 2003/01/23 07:40:33 adelmann Exp $ 
00174  ***************************************************************************/

Generated on Mon Jan 16 13:23:58 2006 for IPPL by  doxygen 1.4.6