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