OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
DiscType.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 /***************************************************************************
3  *
4  * The IPPL Framework
5  *
6  *
7  * Visit http://people.web.psi.ch/adelmann/ for more details
8  *
9  ***************************************************************************/
10 
11 #ifndef DISC_TYPE_H
12 #define DISC_TYPE_H
13 
14 /***************************************************************************
15  * DiscType<T> is a simple class that returns a string via the static method
16  * "str()" based on the type T. It is specialized for our standard types,
17  * and has a default value of "u" for "user" or "unknown" types.
18  ***************************************************************************/
19 
20 // include files
21 #include "AppTypes/Vektor.h"
22 #include "AppTypes/Tenzor.h"
23 #include "AppTypes/SymTenzor.h"
24 #include "AppTypes/AntiSymTenzor.h"
25 #include "AppTypes/dcomplex.h"
26 
27 
29 // a base class for all DiscType's, that provides common routines to
30 // parse a type string and determine the apptype, the dimension, and the
31 // scalar type
32 struct DiscTypeBase {
33  // enums for scalar types and apptypes
36 
37  // determine the scalar data type for a given type string
38  static int scalarType(const std::string &s) {
39  if (s.length() == 0 || s.length() == 2 || s.length() > 3)
40  return UNKNOWN;
41  char c = static_cast<char>(tolower(s[s.length() - 1]));
42  if (c == 'c')
43  return CHAR;
44  else if (c == 's')
45  return SHORT;
46  else if (c == 'i')
47  return INT;
48  else if (c == 'l')
49  return LONG;
50  else if (c == 'f')
51  return FLOAT;
52  else if (c == 'd')
53  return DOUBLE;
54  else if (c == 'y')
55  return FCOMPLEX;
56  else if (c == 'z')
57  return DCOMPLEX;
58  else
59  return UNKNOWN;
60  }
61 
62  // determine the dimension of the apptype. If it is a scalar, return 0.
63  // if there is an error, return -1.
64  static int dim(const std::string &s) {
65  if (s.length() == 0 || s.length() == 2 || s.length() > 3)
66  return (-1);
67  if (s.length() == 1)
68  return 0;
69  char c = s[1];
70  if (c < '1' || c > '9')
71  return (-1);
72  return (c - '1' + 1);
73  }
74 
75  // determine the apptype of the string
76  static int appType(const std::string &s) {
77  if (s.length() == 0 || s.length() == 2 || s.length() > 3)
78  return UNKNOWN;
79  if (s.length() == 1)
80  return SCALAR;
81  char c = static_cast<char>(tolower(s[0]));
82  if (c == 'v')
83  return VEKTOR;
84  else if (c == 't')
85  return TENZOR;
86  else if (c == 's')
87  return SYMTENZOR;
88  else if (c == 'a')
89  return ANTISYMTENZOR;
90  else
91  return UNKNOWN;
92  }
93 };
94 
95 
97 // the general class, that is the default if the user does not specify
98 // one of the specialized types below
99 template<class T>
100 struct DiscType : public DiscTypeBase {
101  static std::string str() { return std::string("u"); }
102 };
103 
104 
106 // specializations for char, short, int, float, double
107 // codes:
108 // c = char
109 // s = short
110 // i = int
111 // l = long
112 // f = float
113 // d = double
114 // z = complex<double>
115 // y = complex<float>
116 #define DEFINE_DISCTYPE_SCALAR(TYPE, STRING) \
117 template<> \
118 struct DiscType< TYPE > : public DiscTypeBase { \
119  static std::string str() { return std::string(STRING); } \
120 };
121 
128 
129 DEFINE_DISCTYPE_SCALAR(unsigned char, "c")
130 DEFINE_DISCTYPE_SCALAR(unsigned short, "s")
131 DEFINE_DISCTYPE_SCALAR(unsigned int, "i")
132 DEFINE_DISCTYPE_SCALAR(unsigned long, "l")
133 
135 DEFINE_DISCTYPE_SCALAR(std::complex<double>, "z")
136 
138 // specializations for Vektor, Tenzor, SymTenzor, AntiSymTenzor
139 // codes:
140 // vNs = Vektor of dimension N and scalar type s
141 // tNs = Tenzor of dimension N and scalar type s
142 // sNs = SymTenzor of dimension N and scalar type s
143 // aNs = AntiSymTenzor of dimension N and scalar type s
144 #define DEFINE_DISCTYPE_APPTYPE(TYPE, STRING) \
145 template<class T, unsigned D> \
146 struct DiscType< TYPE<T, D> > : public DiscTypeBase { \
147  static std::string str() { \
148  CTAssert(D < 10); \
149  std::string retval = STRING; \
150  retval += "0"; \
151  retval += DiscType<T>::str(); \
152  retval[1] += D; \
153  return retval; \
154  } \
155 };
156 
161 
162 #endif
163 
164 /***************************************************************************
165  * $RCSfile: DiscType.h,v $ $Author: adelmann $
166  * $Revision: 1.1.1.1 $ $Date: 2003/01/23 07:40:33 $
167  * IPPL_VERSION_ID: $Id: DiscType.h,v 1.1.1.1 2003/01/23 07:40:33 adelmann Exp $
168  ***************************************************************************/
static int appType(const std::string &s)
Definition: DiscType.h:76
MMatrix< m_complex > complex(MMatrix< double > real)
Definition: MMatrix.cpp:407
Definition: TSVMeta.h:24
static int scalarType(const std::string &s)
Definition: DiscType.h:38
constexpr double c
The velocity of light in m/s.
Definition: Physics.h:52
static int dim(const std::string &s)
Definition: DiscType.h:64
#define DEFINE_DISCTYPE_SCALAR(TYPE, STRING)
Definition: DiscType.h:116
#define DEFINE_DISCTYPE_APPTYPE(TYPE, STRING)
Definition: DiscType.h:144
static std::string str()
Definition: DiscType.h:101