File: adding.qbk

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (111 lines) | stat: -rw-r--r-- 3,433 bytes parent folder | download | duplicates (15)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
[/==============================================================================
    Copyright (C) 2001-2010 Joel de Guzman
    Copyright (C) 2001-2005 Dan Marsden
    Copyright (C) 2001-2010 Thomas Heller

    Distributed under the Boost Software License, Version 1.0. (See accompanying
    file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
===============================================================================/]

[section Adding an expression]

This is not a toy example. This is actually part of the library. Remember the
[link phoenix.modules.statement.while__statement `while`] lazy statement? Putting together
everything we've learned so far, we eill present it here in its entirety
(verbatim):

    BOOST_PHOENIX_DEFINE_EXPRESSION(
        (boost)(phoenix)(while_)
      , (meta_grammar)           // Cond
        (meta_grammar)           // Do
    )

    namespace boost { namespace phoenix
    {
        struct while_eval
        {
            typedef void result_type;

            template <typename Cond, typename Do, typename Context>
            result_type
            operator()(Cond const& cond, Do const& do_, Context & ctx) const
            {
                while(eval(cond, ctx))
                {
                    eval(do_, ctx);
                }
            }
        };
        
        template <typename Dummy>
        struct default_actions::when<rule::while_, Dummy>
            : call<while_eval, Dummy>
        {};

        template <typename Cond>
        struct while_gen
        {
            while_gen(Cond const& cond) : cond(cond) {}

            template <typename Do>
            typename expression::while_<Cond, Do>::type const
            operator[](Do const& do_) const
            {
                return expression::while_<Cond, Do>::make(cond, do_);
            }

            Cond const& cond;
        };

        template <typename Cond>
        while_gen<Cond> const
        while_(Cond const& cond)
        {
            return while_gen<Cond>(cond);
        }
    }}

`while_eval` is an example of how to evaluate an expression. It gets called in
the `rule::while` action. `while_gen` and `while_` are the expression template
front ends. Let's break this apart to undestand what's happening. Let's start at
the bottom. It's easier that way.

When you write:

    while_(cond)

we generate an instance of `while_gen<Cond>`, where `Cond` is the type of `cond`.
`cond` can be an arbitrarily complex actor expression. The `while_gen` template
class has an `operator[]` accepting another expression. If we write:

    while_(cond)
    [
        do_
    ]

it will generate a proper composite with the type:

    expression::while_<Cond, Do>::type

where `Cond` is the type of `cond` and `Do` is the type of `do_`. Notice how we are using Phoenix's
[link phoenix.inside.expression Expression] mechanism here
        
    template <typename Do>
    typename expression::while_<Cond, Do>::type const
    operator[](Do const& do_) const
    {
        return expression::while_<Cond, Do>::make(cond, do_);
    }

Finally, the `while_eval` does its thing:

    while(eval(cond, ctx))
    {
        eval(do_, ctx);
    }

`cond` and `do_`, at this point, are instances of [link phoenix.inside.actor Actor]. `cond` and `do_` are the [link phoenix.inside.actor Actors] 
passed as parameters by `call`, ctx is the [link phoenix.inside.actor Context]

[endsect]