~ubuntu-branches/ubuntu/warty/aqsis/warty

« back to all changes in this revision

Viewing changes to boost/boost/spirit/phoenix/statements.hpp

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2004-08-24 07:25:04 UTC
  • Revision ID: james.westby@ubuntu.com-20040824072504-zf993vnevvisdsvb
Tags: upstream-0.9.1
ImportĀ upstreamĀ versionĀ 0.9.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*=============================================================================
 
2
    Phoenix V1.0
 
3
    Copyright (c) 2001-2002 Joel de Guzman
 
4
 
 
5
    Permission to copy, use, modify, sell and distribute this software
 
6
    is granted provided this copyright notice appears in all copies.
 
7
    This software is provided "as is" without express or implied
 
8
    warranty, and with no claim as to its suitability for any purpose.
 
9
==============================================================================*/
 
10
#ifndef PHOENIX_STATEMENTS_HPP
 
11
#define PHOENIX_STATEMENTS_HPP
 
12
 
 
13
///////////////////////////////////////////////////////////////////////////////
 
14
#include "boost/spirit/phoenix/composite.hpp"
 
15
 
 
16
///////////////////////////////////////////////////////////////////////////////
 
17
namespace phoenix {
 
18
 
 
19
///////////////////////////////////////////////////////////////////////////////
 
20
//
 
21
//  sequential_composite
 
22
//
 
23
//      Two or more actors separated by the comma generates a
 
24
//      sequential_composite which is a composite actor. Example:
 
25
//
 
26
//          actor,
 
27
//          actor,
 
28
//          actor
 
29
//
 
30
//      The actors are evaluated sequentially. The result type of this
 
31
//      is void. Note that the last actor should not have a trailing
 
32
//      comma.
 
33
//
 
34
///////////////////////////////////////////////////////////////////////////////
 
35
template <typename A0, typename A1>
 
36
struct sequential_composite {
 
37
 
 
38
    typedef sequential_composite<A0, A1> self_t;
 
39
 
 
40
    template <typename TupleT>
 
41
    struct result { typedef void type; };
 
42
 
 
43
    sequential_composite(A0 const& _0, A1 const& _1)
 
44
    :   a0(_0), a1(_1) {}
 
45
 
 
46
    template <typename TupleT>
 
47
    void
 
48
    eval(TupleT const& args) const
 
49
    {
 
50
        a0.eval(args);
 
51
        a1.eval(args);
 
52
    }
 
53
 
 
54
    A0 a0; A1 a1; //  actors
 
55
};
 
56
 
 
57
//////////////////////////////////
 
58
template <typename BaseT0, typename BaseT1>
 
59
inline actor<sequential_composite<actor<BaseT0>, actor<BaseT1> > >
 
60
operator,(actor<BaseT0> const& _0, actor<BaseT1> const& _1)
 
61
{
 
62
    return sequential_composite<actor<BaseT0>, actor<BaseT1> >(_0, _1);
 
63
}
 
64
 
 
65
///////////////////////////////////////////////////////////////////////////////
 
66
//
 
67
//  if_then_else_composite
 
68
//
 
69
//      This composite has two (2) forms:
 
70
//
 
71
//          if_(condition)
 
72
//          [
 
73
//              statement
 
74
//          ]
 
75
//
 
76
//      and
 
77
//
 
78
//          if_(condition)
 
79
//          [
 
80
//              true_statement
 
81
//          ]
 
82
//          .else_
 
83
//          [
 
84
//              false_statement
 
85
//          ]
 
86
//
 
87
//      where condition is an actor that evaluates to bool. If condition
 
88
//      is true, the true_statement (again an actor) is executed
 
89
//      otherwise, the false_statement (another actor) is executed. The
 
90
//      result type of this is void. Note the trailing underscore after
 
91
//      if_ and the the leading dot and the trailing underscore before
 
92
//      and after .else_.
 
93
//
 
94
///////////////////////////////////////////////////////////////////////////////
 
95
template <typename CondT, typename ThenT, typename ElseT>
 
96
struct if_then_else_composite {
 
97
 
 
98
    typedef if_then_else_composite<CondT, ThenT, ElseT> self_t;
 
99
 
 
100
    template <typename TupleT>
 
101
    struct result {
 
102
 
 
103
        typedef void type;
 
104
    };
 
105
 
 
106
    if_then_else_composite(
 
107
        CondT const& cond_,
 
108
        ThenT const& then_,
 
109
        ElseT const& else__)
 
110
    :   cond(cond_), then(then_), else_(else__) {}
 
111
 
 
112
    template <typename TupleT>
 
113
    void eval(TupleT const& args) const
 
114
    {
 
115
        if (cond.eval(args))
 
116
            then.eval(args);
 
117
        else
 
118
            else_.eval(args);
 
119
    }
 
120
 
 
121
    CondT cond; ThenT then; ElseT else_; //  actors
 
122
};
 
123
 
 
124
//////////////////////////////////
 
125
template <typename CondT, typename ThenT>
 
126
struct else_gen {
 
127
 
 
128
    else_gen(CondT const& cond_, ThenT const& then_)
 
129
    :   cond(cond_), then(then_) {}
 
130
 
 
131
    template <typename ElseT>
 
132
    actor<if_then_else_composite<CondT, ThenT,
 
133
        typename as_actor<ElseT>::type> >
 
134
    operator[](ElseT const& else_)
 
135
    {
 
136
        typedef if_then_else_composite<CondT, ThenT,
 
137
            typename as_actor<ElseT>::type>
 
138
        result;
 
139
 
 
140
        return result(cond, then, as_actor<ElseT>::convert(else_));
 
141
    }
 
142
 
 
143
    CondT cond; ThenT then;
 
144
};
 
145
 
 
146
//////////////////////////////////
 
147
template <typename CondT, typename ThenT>
 
148
struct if_then_composite {
 
149
 
 
150
    typedef if_then_composite<CondT, ThenT> self_t;
 
151
 
 
152
    template <typename TupleT>
 
153
    struct result { typedef void type; };
 
154
 
 
155
    if_then_composite(CondT const& cond_, ThenT const& then_)
 
156
    :   cond(cond_), then(then_), else_(cond, then) {}
 
157
 
 
158
    template <typename TupleT>
 
159
    void eval(TupleT const& args) const
 
160
    {
 
161
        if (cond.eval(args))
 
162
            then.eval(args);
 
163
    }
 
164
 
 
165
    CondT cond; ThenT then; //  actors
 
166
    else_gen<CondT, ThenT> else_;
 
167
};
 
168
 
 
169
//////////////////////////////////
 
170
template <typename CondT>
 
171
struct if_gen {
 
172
 
 
173
    if_gen(CondT const& cond_)
 
174
    :   cond(cond_) {}
 
175
 
 
176
    template <typename ThenT>
 
177
    actor<if_then_composite<
 
178
        typename as_actor<CondT>::type,
 
179
        typename as_actor<ThenT>::type> >
 
180
    operator[](ThenT const& then) const
 
181
    {
 
182
        typedef if_then_composite<
 
183
            typename as_actor<CondT>::type,
 
184
            typename as_actor<ThenT>::type>
 
185
        result;
 
186
 
 
187
        return result(
 
188
            as_actor<CondT>::convert(cond),
 
189
            as_actor<ThenT>::convert(then));
 
190
    }
 
191
 
 
192
    CondT cond;
 
193
};
 
194
 
 
195
//////////////////////////////////
 
196
template <typename CondT>
 
197
inline if_gen<CondT>
 
198
if_(CondT const& cond)
 
199
{
 
200
    return if_gen<CondT>(cond);
 
201
}
 
202
 
 
203
///////////////////////////////////////////////////////////////////////////////
 
204
//
 
205
//  while_composite
 
206
//
 
207
//      This composite has the form:
 
208
//
 
209
//          while_(condition)
 
210
//          [
 
211
//              statement
 
212
//          ]
 
213
//
 
214
//      While the condition (an actor) evaluates to true, statement
 
215
//      (another actor) is executed. The result type of this is void.
 
216
//      Note the trailing underscore after while_.
 
217
//
 
218
///////////////////////////////////////////////////////////////////////////////
 
219
template <typename CondT, typename DoT>
 
220
struct while_composite {
 
221
 
 
222
    typedef while_composite<CondT, DoT> self_t;
 
223
 
 
224
    template <typename TupleT>
 
225
    struct result { typedef void type; };
 
226
 
 
227
    while_composite(CondT const& cond_, DoT const& do__)
 
228
    :   cond(cond_), do_(do__) {}
 
229
 
 
230
    template <typename TupleT>
 
231
    void eval(TupleT const& args) const
 
232
    {
 
233
        while (cond.eval(args))
 
234
            do_.eval(args);
 
235
    }
 
236
 
 
237
    CondT cond;
 
238
    DoT do_;
 
239
};
 
240
 
 
241
//////////////////////////////////
 
242
template <typename CondT>
 
243
struct while_gen {
 
244
 
 
245
    while_gen(CondT const& cond_)
 
246
    :   cond(cond_) {}
 
247
 
 
248
    template <typename DoT>
 
249
    actor<while_composite<
 
250
        typename as_actor<CondT>::type,
 
251
        typename as_actor<DoT>::type> >
 
252
    operator[](DoT const& do_) const
 
253
    {
 
254
        typedef while_composite<
 
255
            typename as_actor<CondT>::type,
 
256
            typename as_actor<DoT>::type>
 
257
        result;
 
258
 
 
259
        return result(
 
260
            as_actor<CondT>::convert(cond),
 
261
            as_actor<DoT>::convert(do_));
 
262
    }
 
263
 
 
264
    CondT cond;
 
265
};
 
266
 
 
267
//////////////////////////////////
 
268
template <typename CondT>
 
269
inline while_gen<CondT>
 
270
while_(CondT const& cond)
 
271
{
 
272
    return while_gen<CondT>(cond);
 
273
}
 
274
 
 
275
///////////////////////////////////////////////////////////////////////////////
 
276
//
 
277
//  do_composite
 
278
//
 
279
//      This composite has the form:
 
280
//
 
281
//          do_
 
282
//          [
 
283
//              statement
 
284
//          ]
 
285
//          .while_(condition)
 
286
//
 
287
//      While the condition (an actor) evaluates to true, statement
 
288
//      (another actor) is executed. The statement is executed at least
 
289
//      once. The result type of this is void. Note the trailing
 
290
//      underscore after do_ and the the leading dot and the trailing
 
291
//      underscore before and after .while_.
 
292
//
 
293
///////////////////////////////////////////////////////////////////////////////
 
294
template <typename DoT, typename CondT>
 
295
struct do_composite {
 
296
 
 
297
    typedef do_composite<DoT, CondT> self_t;
 
298
 
 
299
    template <typename TupleT>
 
300
    struct result { typedef void type; };
 
301
 
 
302
    do_composite(DoT const& do__, CondT const& cond_)
 
303
    :   do_(do__), cond(cond_) {}
 
304
 
 
305
    template <typename TupleT>
 
306
    void eval(TupleT const& args) const
 
307
    {
 
308
        do
 
309
            do_.eval(args);
 
310
        while (cond.eval(args));
 
311
    }
 
312
 
 
313
    DoT do_;
 
314
    CondT cond;
 
315
};
 
316
 
 
317
////////////////////////////////////
 
318
template <typename DoT>
 
319
struct do_gen2 {
 
320
 
 
321
    do_gen2(DoT const& do__)
 
322
    :   do_(do__) {}
 
323
 
 
324
    template <typename CondT>
 
325
    actor<do_composite<
 
326
        typename as_actor<DoT>::type,
 
327
        typename as_actor<CondT>::type> >
 
328
    while_(CondT const& cond) const
 
329
    {
 
330
        typedef do_composite<
 
331
            typename as_actor<DoT>::type,
 
332
            typename as_actor<CondT>::type>
 
333
        result;
 
334
 
 
335
        return result(
 
336
            as_actor<DoT>::convert(do_),
 
337
            as_actor<CondT>::convert(cond));
 
338
    }
 
339
 
 
340
    DoT do_;
 
341
};
 
342
 
 
343
////////////////////////////////////
 
344
struct do_gen {
 
345
 
 
346
    template <typename DoT>
 
347
    do_gen2<DoT>
 
348
    operator[](DoT const& do_) const
 
349
    {
 
350
        return do_gen2<DoT>(do_);
 
351
    }
 
352
};
 
353
 
 
354
do_gen const do_ = do_gen();
 
355
 
 
356
///////////////////////////////////////////////////////////////////////////////
 
357
//
 
358
//  for_composite
 
359
//
 
360
//      This statement has the form:
 
361
//
 
362
//          for_(init, condition, step)
 
363
//          [
 
364
//              statement
 
365
//          ]
 
366
//
 
367
//      Where init, condition, step and statement are all actors. init
 
368
//      is executed once before entering the for-loop. The for-loop
 
369
//      exits once condition evaluates to false. At each loop iteration,
 
370
//      step and statement is called. The result of this statement is
 
371
//      void. Note the trailing underscore after for_.
 
372
//
 
373
///////////////////////////////////////////////////////////////////////////////
 
374
template <typename InitT, typename CondT, typename StepT, typename DoT>
 
375
struct for_composite {
 
376
 
 
377
    typedef composite<InitT, CondT, StepT, DoT> self_t;
 
378
 
 
379
    template <typename TupleT>
 
380
    struct result { typedef void type; };
 
381
 
 
382
    for_composite(
 
383
        InitT const& init_,
 
384
        CondT const& cond_,
 
385
        StepT const& step_,
 
386
        DoT const& do__)
 
387
    :   init(init_), cond(cond_), step(step_), do_(do__) {}
 
388
 
 
389
    template <typename TupleT>
 
390
    void
 
391
    eval(TupleT const& args) const
 
392
    {
 
393
        for (init.eval(args); cond.eval(args); step.eval(args))
 
394
            do_.eval(args);
 
395
    }
 
396
 
 
397
    InitT init; CondT cond; StepT step; DoT do_; //  actors
 
398
};
 
399
 
 
400
//////////////////////////////////
 
401
template <typename InitT, typename CondT, typename StepT>
 
402
struct for_gen {
 
403
 
 
404
    for_gen(
 
405
        InitT const& init_,
 
406
        CondT const& cond_,
 
407
        StepT const& step_)
 
408
    :   init(init_), cond(cond_), step(step_) {}
 
409
 
 
410
    template <typename DoT>
 
411
    actor<for_composite<
 
412
        typename as_actor<InitT>::type,
 
413
        typename as_actor<CondT>::type,
 
414
        typename as_actor<StepT>::type,
 
415
        typename as_actor<DoT>::type> >
 
416
    operator[](DoT const& do_) const
 
417
    {
 
418
        typedef for_composite<
 
419
            typename as_actor<InitT>::type,
 
420
            typename as_actor<CondT>::type,
 
421
            typename as_actor<StepT>::type,
 
422
            typename as_actor<DoT>::type>
 
423
        result;
 
424
 
 
425
        return result(
 
426
            as_actor<InitT>::convert(init),
 
427
            as_actor<CondT>::convert(cond),
 
428
            as_actor<StepT>::convert(step),
 
429
            as_actor<DoT>::convert(do_));
 
430
    }
 
431
 
 
432
    InitT init; CondT cond; StepT step;
 
433
};
 
434
 
 
435
//////////////////////////////////
 
436
template <typename InitT, typename CondT, typename StepT>
 
437
inline for_gen<InitT, CondT, StepT>
 
438
for_(InitT const& init, CondT const& cond, StepT const& step)
 
439
{
 
440
    return for_gen<InitT, CondT, StepT>(init, cond, step);
 
441
}
 
442
 
 
443
}   //  namespace phoenix
 
444
 
 
445
#endif