Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/spirit/phoenix/example/fundamental/sample6.cpp @ 12

Last change on this file since 12 was 12, checked in by landauf, 17 years ago

added boost

File size: 2.6 KB
Line 
1/*=============================================================================
2    Phoenix V1.2.1
3    Copyright (c) 2001-2003 Joel de Guzman
4
5    Use, modification and distribution is subject to the Boost Software
6    License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7    http://www.boost.org/LICENSE_1_0.txt)
8==============================================================================*/
9#include <vector>
10#include <algorithm>
11#include <iostream>
12#include <boost/spirit/phoenix/operators.hpp>
13#include <boost/spirit/phoenix/primitives.hpp>
14#include <boost/spirit/phoenix/composite.hpp>
15#include <boost/spirit/phoenix/special_ops.hpp>
16
17using namespace std;
18using namespace phoenix;
19
20//////////////////////////////////
21template <typename CondT, typename TrueT, typename FalseT>
22struct if_else_composite {
23
24    typedef if_else_composite<CondT, TrueT, FalseT> self_t;
25
26    template <typename TupleT>
27    struct result {
28
29        typedef typename higher_rank<
30            typename actor_result<TrueT, TupleT>::plain_type,
31            typename actor_result<FalseT, TupleT>::plain_type
32        >::type type;
33    };
34
35    if_else_composite(
36        CondT const& cond_, TrueT const& true__, FalseT const& false__)
37    :   cond(cond_), true_(true__), false_(false__) {}
38
39    template <typename TupleT>
40    typename actor_result<self_t, TupleT>::type
41    eval(TupleT const& args) const
42    {
43        return cond.eval(args) ? true_.eval(args) : false_.eval(args);
44    }
45
46    CondT cond; TrueT true_; FalseT false_; //  actors
47};
48
49//////////////////////////////////
50template <typename CondT, typename TrueT, typename FalseT>
51inline actor<if_else_composite<
52    typename as_actor<CondT>::type,
53    typename as_actor<TrueT>::type,
54    typename as_actor<FalseT>::type> >
55if_else_(CondT const& cond, TrueT const& true_, FalseT const& false_)
56{
57    typedef if_else_composite<
58        typename as_actor<CondT>::type,
59        typename as_actor<TrueT>::type,
60        typename as_actor<FalseT>::type>
61    result;
62
63    return result(
64        as_actor<CondT>::convert(cond),
65        as_actor<TrueT>::convert(true_),
66        as_actor<FalseT>::convert(false_));
67}
68
69//////////////////////////////////
70int
71main()
72{
73    int init[] = { 2, 10, 4, 5, 1, 6, 8, 3, 9, 7 };
74    vector<int> c(init, init + 10);
75    typedef vector<int>::iterator iterator;
76
77    //  Print all contents of an stl container c and
78    //  prefix " is odd" or " is even" appropriately.
79
80    for_each(c.begin(), c.end(),
81        cout
82            << arg1
83            << if_else_(arg1 % 2 == 1, " is odd", " is even")
84            << val('\n')
85    );
86
87    return 0;
88}
Note: See TracBrowser for help on using the repository browser.