Chris@16
|
1 /*=============================================================================
|
Chris@16
|
2 Adaptable closures
|
Chris@16
|
3
|
Chris@16
|
4 Phoenix V0.9
|
Chris@16
|
5 Copyright (c) 2001-2002 Joel de Guzman
|
Chris@16
|
6
|
Chris@16
|
7 Distributed under the Boost Software License, Version 1.0. (See
|
Chris@16
|
8 accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
9 http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
10
|
Chris@16
|
11 URL: http://spirit.sourceforge.net/
|
Chris@16
|
12
|
Chris@16
|
13 ==============================================================================*/
|
Chris@16
|
14 #ifndef PHOENIX_CLOSURES_HPP
|
Chris@16
|
15 #define PHOENIX_CLOSURES_HPP
|
Chris@16
|
16
|
Chris@16
|
17 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
18 #include "boost/lambda/core.hpp"
|
Chris@16
|
19 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
20 namespace boost {
|
Chris@16
|
21 namespace lambda {
|
Chris@16
|
22
|
Chris@16
|
23 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
24 //
|
Chris@16
|
25 // Adaptable closures
|
Chris@16
|
26 //
|
Chris@16
|
27 // The framework will not be complete without some form of closures
|
Chris@16
|
28 // support. Closures encapsulate a stack frame where local
|
Chris@16
|
29 // variables are created upon entering a function and destructed
|
Chris@16
|
30 // upon exiting. Closures provide an environment for local
|
Chris@16
|
31 // variables to reside. Closures can hold heterogeneous types.
|
Chris@16
|
32 //
|
Chris@16
|
33 // Phoenix closures are true hardware stack based closures. At the
|
Chris@16
|
34 // very least, closures enable true reentrancy in lambda functions.
|
Chris@16
|
35 // A closure provides access to a function stack frame where local
|
Chris@16
|
36 // variables reside. Modeled after Pascal nested stack frames,
|
Chris@16
|
37 // closures can be nested just like nested functions where code in
|
Chris@16
|
38 // inner closures may access local variables from in-scope outer
|
Chris@16
|
39 // closures (accessing inner scopes from outer scopes is an error
|
Chris@16
|
40 // and will cause a run-time assertion failure).
|
Chris@16
|
41 //
|
Chris@16
|
42 // There are three (3) interacting classes:
|
Chris@16
|
43 //
|
Chris@16
|
44 // 1) closure:
|
Chris@16
|
45 //
|
Chris@16
|
46 // At the point of declaration, a closure does not yet create a
|
Chris@16
|
47 // stack frame nor instantiate any variables. A closure declaration
|
Chris@16
|
48 // declares the types and names[note] of the local variables. The
|
Chris@16
|
49 // closure class is meant to be subclassed. It is the
|
Chris@16
|
50 // responsibility of a closure subclass to supply the names for
|
Chris@16
|
51 // each of the local variable in the closure. Example:
|
Chris@16
|
52 //
|
Chris@16
|
53 // struct my_closure : closure<int, string, double> {
|
Chris@16
|
54 //
|
Chris@16
|
55 // member1 num; // names the 1st (int) local variable
|
Chris@16
|
56 // member2 message; // names the 2nd (string) local variable
|
Chris@16
|
57 // member3 real; // names the 3rd (double) local variable
|
Chris@16
|
58 // };
|
Chris@16
|
59 //
|
Chris@16
|
60 // my_closure clos;
|
Chris@16
|
61 //
|
Chris@16
|
62 // Now that we have a closure 'clos', its local variables can be
|
Chris@16
|
63 // accessed lazily using the dot notation. Each qualified local
|
Chris@16
|
64 // variable can be used just like any primitive actor (see
|
Chris@16
|
65 // primitives.hpp). Examples:
|
Chris@16
|
66 //
|
Chris@16
|
67 // clos.num = 30
|
Chris@16
|
68 // clos.message = arg1
|
Chris@16
|
69 // clos.real = clos.num * 1e6
|
Chris@16
|
70 //
|
Chris@16
|
71 // The examples above are lazily evaluated. As usual, these
|
Chris@16
|
72 // expressions return composite actors that will be evaluated
|
Chris@16
|
73 // through a second function call invocation (see operators.hpp).
|
Chris@16
|
74 // Each of the members (clos.xxx) is an actor. As such, applying
|
Chris@16
|
75 // the operator() will reveal its identity:
|
Chris@16
|
76 //
|
Chris@16
|
77 // clos.num() // will return the current value of clos.num
|
Chris@16
|
78 //
|
Chris@16
|
79 // *** [note] Acknowledgement: Juan Carlos Arevalo-Baeza (JCAB)
|
Chris@16
|
80 // introduced and initilally implemented the closure member names
|
Chris@16
|
81 // that uses the dot notation.
|
Chris@16
|
82 //
|
Chris@16
|
83 // 2) closure_member
|
Chris@16
|
84 //
|
Chris@16
|
85 // The named local variables of closure 'clos' above are actually
|
Chris@16
|
86 // closure members. The closure_member class is an actor and
|
Chris@16
|
87 // conforms to its conceptual interface. member1..memberN are
|
Chris@16
|
88 // predefined typedefs that correspond to each of the listed types
|
Chris@16
|
89 // in the closure template parameters.
|
Chris@16
|
90 //
|
Chris@16
|
91 // 3) closure_frame
|
Chris@16
|
92 //
|
Chris@16
|
93 // When a closure member is finally evaluated, it should refer to
|
Chris@16
|
94 // an actual instance of the variable in the hardware stack.
|
Chris@16
|
95 // Without doing so, the process is not complete and the evaluated
|
Chris@16
|
96 // member will result to an assertion failure. Remember that the
|
Chris@16
|
97 // closure is just a declaration. The local variables that a
|
Chris@16
|
98 // closure refers to must still be instantiated.
|
Chris@16
|
99 //
|
Chris@16
|
100 // The closure_frame class does the actual instantiation of the
|
Chris@16
|
101 // local variables and links these variables with the closure and
|
Chris@16
|
102 // all its members. There can be multiple instances of
|
Chris@16
|
103 // closure_frames typically situated in the stack inside a
|
Chris@16
|
104 // function. Each closure_frame instance initiates a stack frame
|
Chris@16
|
105 // with a new set of closure local variables. Example:
|
Chris@16
|
106 //
|
Chris@16
|
107 // void foo()
|
Chris@16
|
108 // {
|
Chris@16
|
109 // closure_frame<my_closure> frame(clos);
|
Chris@16
|
110 // /* do something */
|
Chris@16
|
111 // }
|
Chris@16
|
112 //
|
Chris@16
|
113 // where 'clos' is an instance of our closure 'my_closure' above.
|
Chris@16
|
114 // Take note that the usage above precludes locally declared
|
Chris@16
|
115 // classes. If my_closure is a locally declared type, we can still
|
Chris@16
|
116 // use its self_type as a paramater to closure_frame:
|
Chris@16
|
117 //
|
Chris@16
|
118 // closure_frame<my_closure::self_type> frame(clos);
|
Chris@16
|
119 //
|
Chris@16
|
120 // Upon instantiation, the closure_frame links the local variables
|
Chris@16
|
121 // to the closure. The previous link to another closure_frame
|
Chris@16
|
122 // instance created before is saved. Upon destruction, the
|
Chris@16
|
123 // closure_frame unlinks itself from the closure and relinks the
|
Chris@16
|
124 // preceding closure_frame prior to this instance.
|
Chris@16
|
125 //
|
Chris@16
|
126 // The local variables in the closure 'clos' above is default
|
Chris@16
|
127 // constructed in the stack inside function 'foo'. Once 'foo' is
|
Chris@16
|
128 // exited, all of these local variables are destructed. In some
|
Chris@16
|
129 // cases, default construction is not desirable and we need to
|
Chris@16
|
130 // initialize the local closure variables with some values. This
|
Chris@16
|
131 // can be done by passing in the initializers in a compatible
|
Chris@16
|
132 // tuple. A compatible tuple is one with the same number of
|
Chris@16
|
133 // elements as the destination and where each element from the
|
Chris@16
|
134 // destination can be constructed from each corresponding element
|
Chris@16
|
135 // in the source. Example:
|
Chris@16
|
136 //
|
Chris@16
|
137 // tuple<int, char const*, int> init(123, "Hello", 1000);
|
Chris@16
|
138 // closure_frame<my_closure> frame(clos, init);
|
Chris@16
|
139 //
|
Chris@16
|
140 // Here now, our closure_frame's variables are initialized with
|
Chris@16
|
141 // int: 123, char const*: "Hello" and int: 1000.
|
Chris@16
|
142 //
|
Chris@16
|
143 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
144
|
Chris@16
|
145
|
Chris@16
|
146
|
Chris@16
|
147 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
148 //
|
Chris@16
|
149 // closure_frame class
|
Chris@16
|
150 //
|
Chris@16
|
151 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
152 template <typename ClosureT>
|
Chris@16
|
153 class closure_frame : public ClosureT::tuple_t {
|
Chris@16
|
154
|
Chris@16
|
155 public:
|
Chris@16
|
156
|
Chris@16
|
157 closure_frame(ClosureT& clos)
|
Chris@16
|
158 : ClosureT::tuple_t(), save(clos.frame), frame(clos.frame)
|
Chris@16
|
159 { clos.frame = this; }
|
Chris@16
|
160
|
Chris@16
|
161 template <typename TupleT>
|
Chris@16
|
162 closure_frame(ClosureT& clos, TupleT const& init)
|
Chris@16
|
163 : ClosureT::tuple_t(init), save(clos.frame), frame(clos.frame)
|
Chris@16
|
164 { clos.frame = this; }
|
Chris@16
|
165
|
Chris@16
|
166 ~closure_frame()
|
Chris@16
|
167 { frame = save; }
|
Chris@16
|
168
|
Chris@16
|
169 private:
|
Chris@16
|
170
|
Chris@16
|
171 closure_frame(closure_frame const&); // no copy
|
Chris@16
|
172 closure_frame& operator=(closure_frame const&); // no assign
|
Chris@16
|
173
|
Chris@16
|
174 closure_frame* save;
|
Chris@16
|
175 closure_frame*& frame;
|
Chris@16
|
176 };
|
Chris@16
|
177
|
Chris@16
|
178 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
179 //
|
Chris@16
|
180 // closure_member class
|
Chris@16
|
181 //
|
Chris@16
|
182 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
183 template <int N, typename ClosureT>
|
Chris@16
|
184 class closure_member {
|
Chris@16
|
185
|
Chris@16
|
186 public:
|
Chris@16
|
187
|
Chris@16
|
188 typedef typename ClosureT::tuple_t tuple_t;
|
Chris@16
|
189
|
Chris@16
|
190 closure_member()
|
Chris@16
|
191 : frame(ClosureT::closure_frame_ref()) {}
|
Chris@16
|
192
|
Chris@16
|
193 template <typename TupleT>
|
Chris@16
|
194 struct sig {
|
Chris@16
|
195
|
Chris@16
|
196 typedef typename detail::tuple_element_as_reference<
|
Chris@16
|
197 N, typename ClosureT::tuple_t
|
Chris@16
|
198 >::type type;
|
Chris@16
|
199 };
|
Chris@16
|
200
|
Chris@16
|
201 template <class Ret, class A, class B, class C>
|
Chris@16
|
202 // typename detail::tuple_element_as_reference
|
Chris@16
|
203 // <N, typename ClosureT::tuple_t>::type
|
Chris@16
|
204 Ret
|
Chris@16
|
205 call(A&, B&, C&) const
|
Chris@16
|
206 {
|
Chris@16
|
207 assert(frame);
|
Chris@16
|
208 return boost::tuples::get<N>(*frame);
|
Chris@16
|
209 }
|
Chris@16
|
210
|
Chris@16
|
211
|
Chris@16
|
212 private:
|
Chris@16
|
213
|
Chris@16
|
214 typename ClosureT::closure_frame_t*& frame;
|
Chris@16
|
215 };
|
Chris@16
|
216
|
Chris@16
|
217 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
218 //
|
Chris@16
|
219 // closure class
|
Chris@16
|
220 //
|
Chris@16
|
221 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
222 template <
|
Chris@16
|
223 typename T0 = null_type,
|
Chris@16
|
224 typename T1 = null_type,
|
Chris@16
|
225 typename T2 = null_type,
|
Chris@16
|
226 typename T3 = null_type,
|
Chris@16
|
227 typename T4 = null_type
|
Chris@16
|
228 >
|
Chris@16
|
229 class closure {
|
Chris@16
|
230
|
Chris@16
|
231 public:
|
Chris@16
|
232
|
Chris@16
|
233 typedef tuple<T0, T1, T2, T3, T4> tuple_t;
|
Chris@16
|
234 typedef closure<T0, T1, T2, T3, T4> self_t;
|
Chris@16
|
235 typedef closure_frame<self_t> closure_frame_t;
|
Chris@16
|
236
|
Chris@16
|
237 closure()
|
Chris@16
|
238 : frame(0) { closure_frame_ref(&frame); }
|
Chris@16
|
239 closure_frame_t& context() { assert(frame); return frame; }
|
Chris@16
|
240 closure_frame_t const& context() const { assert(frame); return frame; }
|
Chris@16
|
241
|
Chris@16
|
242 typedef lambda_functor<closure_member<0, self_t> > member1;
|
Chris@16
|
243 typedef lambda_functor<closure_member<1, self_t> > member2;
|
Chris@16
|
244 typedef lambda_functor<closure_member<2, self_t> > member3;
|
Chris@16
|
245 typedef lambda_functor<closure_member<3, self_t> > member4;
|
Chris@16
|
246 typedef lambda_functor<closure_member<4, self_t> > member5;
|
Chris@16
|
247
|
Chris@16
|
248 private:
|
Chris@16
|
249
|
Chris@16
|
250 closure(closure const&); // no copy
|
Chris@16
|
251 closure& operator=(closure const&); // no assign
|
Chris@16
|
252
|
Chris@16
|
253 template <int N, typename ClosureT>
|
Chris@16
|
254 friend class closure_member;
|
Chris@16
|
255
|
Chris@16
|
256 template <typename ClosureT>
|
Chris@16
|
257 friend class closure_frame;
|
Chris@16
|
258
|
Chris@16
|
259 static closure_frame_t*&
|
Chris@16
|
260 closure_frame_ref(closure_frame_t** frame_ = 0)
|
Chris@16
|
261 {
|
Chris@16
|
262 static closure_frame_t** frame = 0;
|
Chris@16
|
263 if (frame_ != 0)
|
Chris@16
|
264 frame = frame_;
|
Chris@16
|
265 return *frame;
|
Chris@16
|
266 }
|
Chris@16
|
267
|
Chris@16
|
268 closure_frame_t* frame;
|
Chris@16
|
269 };
|
Chris@16
|
270
|
Chris@16
|
271 }}
|
Chris@16
|
272 // namespace
|
Chris@16
|
273
|
Chris@16
|
274 #endif
|