1 [/==============================================================================
2 Copyright (C) 2001-2010 Joel de Guzman
3 Copyright (C) 2001-2005 Dan Marsden
4 Copyright (C) 2001-2010 Thomas Heller
6 Distributed under the Boost Software License, Version 1.0. (See accompanying
7 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 ===============================================================================/]
10 [section Adding an expression]
12 This is not a toy example. This is actually part of the library. Remember the
13 [link phoenix.modules.statement.while__statement `while`] lazy statement? Putting together
14 everything we've learned so far, we eill present it here in its entirety
17 BOOST_PHOENIX_DEFINE_EXPRESSION(
18 (boost)(phoenix)(while_)
19 , (meta_grammar) // Cond
23 namespace boost { namespace phoenix
27 typedef void result_type;
29 template <typename Cond, typename Do, typename Context>
31 operator()(Cond const& cond, Do const& do_, Context & ctx) const
33 while(eval(cond, ctx))
40 template <typename Dummy>
41 struct default_actions::when<rule::while_, Dummy>
42 : call<while_eval, Dummy>
45 template <typename Cond>
48 while_gen(Cond const& cond) : cond(cond) {}
50 template <typename Do>
51 typename expression::while_<Cond, Do>::type const
52 operator[](Do const& do_) const
54 return expression::while_<Cond, Do>::make(cond, do_);
60 template <typename Cond>
62 while_(Cond const& cond)
64 return while_gen<Cond>(cond);
68 `while_eval` is an example of how to evaluate an expression. It gets called in
69 the `rule::while` action. `while_gen` and `while_` are the expression template
70 front ends. Let's break this apart to undestand what's happening. Let's start at
71 the bottom. It's easier that way.
77 we generate an instance of `while_gen<Cond>`, where `Cond` is the type of `cond`.
78 `cond` can be an arbitrarily complex actor expression. The `while_gen` template
79 class has an `operator[]` accepting another expression. If we write:
86 it will generate a proper composite with the type:
88 expression::while_<Cond, Do>::type
90 where `Cond` is the type of `cond` and `Do` is the type of `do_`. Notice how we are using Phoenix's
91 [link phoenix.inside.expression Expression] mechanism here
93 template <typename Do>
94 typename expression::while_<Cond, Do>::type const
95 operator[](Do const& do_) const
97 return expression::while_<Cond, Do>::make(cond, do_);
100 Finally, the `while_eval` does its thing:
102 while(eval(cond, ctx))
107 `cond` and `do_`, at this point, are instances of [link phoenix.inside.actor Actor]. `cond` and `do_` are the [link phoenix.inside.actor Actors]
108 passed as parameters by `call`, ctx is the [link phoenix.inside.actor Context]