QDP++
TypeComputations.h
Go to the documentation of this file.
1// -*- C++ -*-
2// ACL:license
3// ----------------------------------------------------------------------
4// This software and ancillary information (herein called "SOFTWARE")
5// called PETE (Portable Expression Template Engine) is
6// made available under the terms described here. The SOFTWARE has been
7// approved for release with associated LA-CC Number LA-CC-99-5.
8//
9// Unless otherwise indicated, this SOFTWARE has been authored by an
10// employee or employees of the University of California, operator of the
11// Los Alamos National Laboratory under Contract No. W-7405-ENG-36 with
12// the U.S. Department of Energy. The U.S. Government has rights to use,
13// reproduce, and distribute this SOFTWARE. The public may copy, distribute,
14// prepare derivative works and publicly display this SOFTWARE without
15// charge, provided that this Notice and any statement of authorship are
16// reproduced on all copies. Neither the Government nor the University
17// makes any warranty, express or implied, or assumes any liability or
18// responsibility for the use of this SOFTWARE.
19//
20// If SOFTWARE is modified to produce derivative works, such modified
21// SOFTWARE should be clearly marked, so as not to confuse it with the
22// version available from LANL.
23//
24// For more information about PETE, send e-mail to pete@acl.lanl.gov,
25// or visit the PETE web page at http://www.acl.lanl.gov/pete/.
26// ----------------------------------------------------------------------
27// ACL:license
28
29#ifndef PETE_PETE_TYPECOMPUTATIONS_H
30#define PETE_PETE_TYPECOMPUTATIONS_H
31
32//-----------------------------------------------------------------------------
33//
34// CLASS NAME
35// UnaryReturn<T, Op>
36//
37// DESCRIPTION
38// This template describes the default mechanism for calculating the
39// return type to a unary expression given the argument type T and
40// the operation type Op.
41//
42// There are a several sensible things one can do:
43// o make the return type the same as the argument to the
44// function/operation. For example, operator-(T) should return a T.
45// o return a type based entirely on the operation.
46// For example, operator! always returns a bool.
47// o sythesize a type based on the type of the argument and the operation.
48// The first case is most common. We therefore make it the behavior
49// for the base template. The other cases are handled by partial
50// specialization.
51//
52// For example, the abs function typically returns a double when the
53// argument is a complex<double>. The appropriate specialization here
54// would be:
55// template<> struct PETEUnaryReturn<complex<double>, FnAbs> {
56// typedef double Type_t;
57// };
58//
59//-----------------------------------------------------------------------------
60
61template<class T, class Op>
63 typedef T Type_t;
64};
65
66//-----------------------------------------------------------------------------
67//
68// CLASS NAME
69// Promote<T1, T2>
70//
71// DESCRIPTION
72// General template and specializations to implement C++ type promotion
73// for basic types.
74//
75//-----------------------------------------------------------------------------
76
77// Base template: don't do anything by default.
78
79template<class T1, class T2>
80struct Promote { typedef T1 Type_t; };
81
82// bool
83
84template<>
85struct Promote<bool, bool> {
86 typedef bool Type_t;
87};
88
89template<>
90struct Promote<bool, char> {
91 typedef char Type_t;
92};
93
94template<>
95struct Promote<bool, short> {
96 typedef short Type_t;
97};
98
99template<>
100struct Promote<bool, int> {
101 typedef int Type_t;
102};
103
104template<>
105struct Promote<bool, long> {
106 typedef long Type_t;
107};
108
109template<>
110struct Promote<bool, float> {
111 typedef float Type_t;
112};
113
114template<>
115struct Promote<bool, double> {
116 typedef double Type_t;
117};
118
119// char
120
121template<>
122struct Promote<char, bool> {
123 typedef char Type_t;
124};
125
126template<>
127struct Promote<char, char> {
128 typedef char Type_t;
129};
130
131template<>
132struct Promote<char, short> {
133 typedef short Type_t;
134};
135
136template<>
137struct Promote<char, int> {
138 typedef int Type_t;
139};
140
141template<>
142struct Promote<char, long> {
143 typedef long Type_t;
144};
145
146template<>
147struct Promote<char, float> {
148 typedef float Type_t;
149};
150
151template<>
152struct Promote<char, double> {
153 typedef double Type_t;
154};
155
156// short
157
158template<>
159struct Promote<short, bool> {
160 typedef short Type_t;
161};
162
163template<>
164struct Promote<short, char> {
165 typedef short Type_t;
166};
167
168template<>
169struct Promote<short, short> {
170 typedef short Type_t;
171};
172
173template<>
174struct Promote<short, int> {
175 typedef int Type_t;
176};
177
178template<>
179struct Promote<short, long> {
180 typedef long Type_t;
181};
182
183template<>
184struct Promote<short, float> {
185 typedef float Type_t;
186};
187
188template<>
189struct Promote<short, double> {
190 typedef double Type_t;
191};
192
193// int
194
195template<>
196struct Promote<int, bool> {
197 typedef int Type_t;
198};
199
200template<>
201struct Promote<int, char> {
202 typedef int Type_t;
203};
204
205template<>
206struct Promote<int, short> {
207 typedef int Type_t;
208};
209
210template<>
211struct Promote<int, int> {
212 typedef int Type_t;
213};
214
215template<>
216struct Promote<int, long> {
217 typedef long Type_t;
218};
219
220template<>
221struct Promote<int, float> {
222 typedef float Type_t;
223};
224
225template<>
226struct Promote<int, double> {
227 typedef double Type_t;
228};
229
230// long
231
232template<>
233struct Promote<long, bool> {
234 typedef long Type_t;
235};
236
237template<>
238struct Promote<long, char> {
239 typedef long Type_t;
240};
241
242template<>
243struct Promote<long, short> {
244 typedef long Type_t;
245};
246
247template<>
248struct Promote<long, int> {
249 typedef long Type_t;
250};
251
252template<>
253struct Promote<long, long> {
254 typedef long Type_t;
255};
256
257template<>
258struct Promote<long, float> {
259 typedef float Type_t;
260};
261
262template<>
263struct Promote<long, double> {
264 typedef double Type_t;
265};
266
267// float
268
269template<>
270struct Promote<float, bool> {
271 typedef float Type_t;
272};
273
274template<>
275struct Promote<float, char> {
276 typedef float Type_t;
277};
278
279template<>
280struct Promote<float, short> {
281 typedef float Type_t;
282};
283
284template<>
285struct Promote<float, int> {
286 typedef float Type_t;
287};
288
289template<>
290struct Promote<float, long> {
291 typedef float Type_t;
292};
293
294template<>
295struct Promote<float, float> {
296 typedef float Type_t;
297};
298
299template<>
300struct Promote<float, double> {
301 typedef double Type_t;
302};
303
304// double
305
306template<>
307struct Promote<double, bool> {
308 typedef double Type_t;
309};
310
311template<>
312struct Promote<double, char> {
313 typedef double Type_t;
314};
315
316template<>
317struct Promote<double, short> {
318 typedef double Type_t;
319};
320
321template<>
322struct Promote<double, int> {
323 typedef double Type_t;
324};
325
326template<>
327struct Promote<double, long> {
328 typedef double Type_t;
329};
330
331template<>
332struct Promote<double, float> {
333 typedef double Type_t;
334};
335
336template<>
337struct Promote<double, double> {
338 typedef double Type_t;
339};
340
341//-----------------------------------------------------------------------------
342//
343// CLASS NAME
344// BinaryReturn<T1, T2, Op>
345//
346// DESCRIPTION
347// This template describes the default mechanism for calculating the
348// return type to a binary expression given the argument types T1 and
349// T2 and the operation type Op.
350//
351// There are several sensible things one can do:
352// o make the return type by promoting/converting the "simpler" type
353// into the more "complex." For example, we typically want to do
354// this with addition.
355// o return the type of the left-hand operand. For example, this is
356// what happens with operator<<.
357// o return the type of the right-hand operand.
358// o return a type based entirely on the operation.
359// For example, operator!= always returns a bool.
360// o synthesize the return type based on the operation and types.
361// The first option is most common, so we make that the behavior of the
362// base template. The other cases are handled by partial specialization.
363//
364// For example, the multiplication between a matrix and a vector might do a
365// matrix/vector product, thereby returning a vector. The appropriate
366// specialization here would be:
367// struct BinaryReturn<Mat<double,3>, Vec<float,3>, OpMultiply> {
368// typedef Vec<double,3> Type_t;
369// };
370// Notice how the element type is promoted.
371//
372//-----------------------------------------------------------------------------
373
374template<class T1, class T2, class Op>
377};
378
379
380//-----------------------------------------------------------------------------
381//
382// CLASS NAME
383// TrinaryReturn<T1, T2, T3, Op>
384//
385// DESCRIPTION
386// This template describes the default mechanism for calculating the
387// return type to a trinary expression given the argument types T1, T2, and
388// T3 and the operation type Op. The only trinary expression supported
389// in C++ is the ?: operation. In this case, T1 should end up being bool
390// and the result of the calculation is of type Binary_Promotion(T2,T3)
391// with the value being that associated with T2 if T1's associated value
392// turns out to be true and T3 if T1's associated value turns out to be
393// false.
394//
395//-----------------------------------------------------------------------------
396
397template<class T1, class T2, class T3, class Op>
401
402
403#endif // PETE_PETE_TYPE_COMPUTATIONS_H
404
405// ACL:rcsinfo
406// ----------------------------------------------------------------------
407// $RCSfile: TypeComputations.h,v $ $Author: edwards $
408// $Revision: 1.1 $ $Date: 2002-09-12 18:22:16 $
409// ----------------------------------------------------------------------
410// ACL:rcsinfo
Promote< A, B >::Type_t Type_t
BinaryReturn< B, C, Op >::Type_t Type_t