-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproto_transforms.cpp
78 lines (63 loc) · 1.96 KB
/
proto_transforms.cpp
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
#include <boost/assert.hpp>
#include <boost/proto/proto.hpp>
using namespace boost;
namespace eigen_proto
{
/// Simple grammar that only allows addition of integers
struct add_ints_grammar :
proto::plus< proto::terminal<int>, proto::terminal<int> >
{
};
/// We need a functor that we can call to do the actual evaluation
struct evaluate_plus : proto::callable
{
typedef int result_type;
int operator()(const int a, const int b) const
{
return a + b;
}
};
/// Now we can create a transform that can evaluate our expressions:
struct add_ints_transform :
proto::when<
add_ints_grammar,
evaluate_plus(proto::_value(proto::_left), proto::_value(proto::_right))
>
{
};
/// We can also do this simpler, combining different types of arithmetic
/// and using the proto default transform
struct calculator_transform :
proto::or_
<
proto::when< proto::terminal<proto::_>, proto::_value >, // Replace terminals with their value
proto::when<proto::or_ // When we have +, - * or / ...
<
proto::plus<calculator_transform, calculator_transform>,
proto::minus<calculator_transform, calculator_transform>,
proto::multiplies<calculator_transform, calculator_transform>,
proto::divides<calculator_transform, calculator_transform>
>,
proto::_default<calculator_transform> > // ... Do what C++ would do
>
{
};
}
/// Demonstrate some basic proto expression properties
int main(void)
{
using namespace eigen_proto;
using proto::lit;
// Create an integer
int i = 1;
// Evaluate the expression using our transform
const int result = add_ints_transform()(lit(i) + 2);
BOOST_ASSERT(result == 3);
std::cout << "i + 2 = " << result << std::endl;
// The calculator is much more powerful:
int a = 2; double b = 0.5; unsigned int c = 3;
const double result2 = calculator_transform()((lit(a) + lit(c)) * lit(b));
BOOST_ASSERT(result2 == 2.5);
std::cout << "lit(a) + lit(c)) * lit(b) = " << result2 << std::endl;
return 0;
}