QDP++
CreateLeaf.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_CREATELEAF_H
30#define PETE_PETE_CREATELEAF_H
31
32//-----------------------------------------------------------------------------
33// Expression<T> - a class that wraps the contents of an expression
34// (so that we don't need to define operators for all the tree types)
35// Expression<T> provides the following interface:
36// Expression<T>::Expression_t defines the expression type (T)
37// const Expression_t& expression() returns a const ref to the
38// expression object.
39//-----------------------------------------------------------------------------
40
41#ifdef PETE_USER_DEFINED_EXPRESSION
42
43template<class T> class Expression;
44
45#else
46
47template<class T>
49{
50public:
51 // Type of the expression.
52
53 typedef T Expression_t;
54
55 // Construct from an expression.
56
57 Expression(const T& expr) : expr_m(expr)
58 { }
59
60 // Accessor that returns the expression.
61
62 const Expression_t& expression() const
63 {
64 return expr_m;
65 }
66
67private:
68 // Store the expression by value since it is a temporary produced
69 // by operator functions.
70
71 T expr_m;
72};
73
74#endif // !PETE_USER_DEFINED_EXPRESSION
75
76//-----------------------------------------------------------------------------
77// CreateLeaf<T>
78//
79// The class CreateLeaf is used to tell PETE what to stick in expression trees
80// for various objects that can appear in expressions. Users MUST specialize
81// CreateLeaf<T> for any container objects that they intend to use in
82// expressions. The typedef CreateLeaf<T>::Leaf_t is the type of object that
83// appears in the expression tree (it could be T itself, or you might chose
84// to just store iterators in the expression tree in which case you might
85// define it to be T::const_iterator or something). CreateLeaf also needs
86// to provide a function make(const T&) which converts an object of type T
87// into the object of type CreateLeaf<T>::Leaf_t which goes into the tree.
88//-----------------------------------------------------------------------------
89
90// The general case is assumed to be a scalar, since users need to specialize
91// CreateLeaf for their container classes.
92
93template<class T>
95{
97
98 inline static
99 Leaf_t make(const T &a)
100 {
101 return Scalar<T>(a);
102 }
103};
104
105#ifndef PETE_USER_DEFINED_EXPRESSION
106
107// For Expressions, we strip the Expression<> wrapper since it is intended
108// to wrap the whole expression. (Expression<Scalar<>>+Expression<BinaryNode<>>
109// becomes Expression<BinaryNode<OpAdd,Scalar<>,BinaryNode<>>>)
110
111template<class T>
113{
115
116 inline static
117 const Leaf_t &make(const Expression<T> &a)
118 {
119 return a.expression();
120 }
121};
122
123#endif // !PETE_USER_DEFINED_EXPRESSION
124
125//-----------------------------------------------------------------------------
126// MakeReturn<T,C>
127//
128// MakeReturn is used to wrap expression objects (UnaryNode, BinaryNode etc.)
129// inside an Expression<T> object. Usually this indirection is unnecessary,
130// but the indirection allows users to specify their own approach to storing
131// trees. By specializing MakeReturn<UnaryNode<>>, MakeReturn<BinaryNode<>>,
132// etc. you could cause the expression trees to be stored in another format.
133// For example, POOMA stores expressions inside Arrays, so the result of
134// Array+Array is another Array.
135//-----------------------------------------------------------------------------
136
137#ifndef PETE_USER_DEFINED_EXPRESSION
138template<class T>
140{
142 inline static
143 Expression_t make(const T &a) { return Expression_t(a); }
144};
145
146#endif // !PETE_USER_DEFINED_EXPRESSION
147
148
149#endif // PETE_PETE_CREATELEAF_H
150
151// ACL:rcsinfo
152// ----------------------------------------------------------------------
153// $RCSfile: CreateLeaf.h,v $ $Author: edwards $
154// $Revision: 1.1 $ $Date: 2002-09-12 18:22:16 $
155// ----------------------------------------------------------------------
156// ACL:rcsinfo
Expression(const T &expr)
Definition CreateLeaf.h:57
const Expression_t & expression() const
Definition CreateLeaf.h:62
static const Leaf_t & make(const Expression< T > &a)
Definition CreateLeaf.h:117
Expression< T >::Expression_t Leaf_t
Definition CreateLeaf.h:114
static Leaf_t make(const T &a)
Definition CreateLeaf.h:99
Scalar< T > Leaf_t
Definition CreateLeaf.h:96
static Expression_t make(const T &a)
Definition CreateLeaf.h:143
Expression< T > Expression_t
Definition CreateLeaf.h:141