|
Boost-Commit : |
From: eric_at_[hidden]
Date: 2007-11-11 18:26:01
Author: eric_niebler
Date: 2007-11-11 18:26:01 EST (Sun, 11 Nov 2007)
New Revision: 41021
URL: http://svn.boost.org/trac/boost/changeset/41021
Log:
port rgb example to proto3
Text files modified:
branches/proto/v3/libs/xpressive/proto3/example/rgb.cpp | 72 +++++++++++++++++++++++----------------
1 files changed, 42 insertions(+), 30 deletions(-)
Modified: branches/proto/v3/libs/xpressive/proto3/example/rgb.cpp
==============================================================================
--- branches/proto/v3/libs/xpressive/proto3/example/rgb.cpp (original)
+++ branches/proto/v3/libs/xpressive/proto3/example/rgb.cpp 2007-11-11 18:26:01 EST (Sun, 11 Nov 2007)
@@ -11,9 +11,7 @@
#include <iostream>
#include <boost/xpressive/proto3/proto.hpp>
-#include <boost/xpressive/proto3/transform/arg.hpp>
-#include <boost/xpressive/proto3/transform/apply.hpp>
-#include <boost/xpressive/proto3/transform/compose.hpp>
+#include <boost/xpressive/proto3/transform.hpp>
using namespace boost::proto;
struct RedTag
@@ -40,35 +38,49 @@
}
};
-typedef terminal<RedTag>::type Red;
-typedef terminal<BlueTag>::type Blue;
-typedef terminal<GreenTag>::type Green;
+typedef terminal<RedTag>::type RedT;
+typedef terminal<BlueTag>::type BlueT;
+typedef terminal<GreenTag>::type GreenT;
+
+struct Red;
+struct Blue;
+struct Green;
///////////////////////////////////////////////////////////////////////////////
// A transform that produces new colors according to some arbitrary rules:
// red & green give blue, red & blue give green, blue and green give red.
+struct Red
+ : or_<
+ plus<Green, Blue>
+ , plus<Blue, Green>
+ , plus<Red, Red>
+ , terminal<RedTag>
+ >
+{};
+
+struct Green
+ : or_<
+ plus<Red, Blue>
+ , plus<Blue, Red>
+ , plus<Green, Green>
+ , terminal<GreenTag>
+ >
+{};
+
+struct Blue
+ : or_<
+ plus<Red, Green>
+ , plus<Green, Red>
+ , plus<Blue, Blue>
+ , terminal<BlueTag>
+ >
+{};
+
struct RGB
: or_<
- // leave terminals as they are
- terminal<_>
- , transform::compose<
- // Match binary nodes, convert left and right to terminals
- plus<RGB, RGB>
- // Forward resulting binary expression to the following transform
- , or_<
- // Green + Blue -> Red
- transform::always<plus<Green, Blue>, Red>
- , transform::always<plus<Blue, Green>, Red>
- // Red + Green -> Blue
- , transform::always<plus<Red, Green>, Blue>
- , transform::always<plus<Green, Red>, Blue>
- // Red + Blue -> Green
- , transform::always<plus<Red, Blue>, Green>
- , transform::always<plus<Blue, Red>, Green>
- // else (both same color), select the left operand
- , transform::left<_>
- >
- >
+ case_< Red, RedTag() >
+ , case_< Blue, BlueTag() >
+ , case_< Green, GreenTag() >
>
{};
@@ -76,14 +88,14 @@
void printColor(Expr const & expr)
{
int i = 0; // dummy state and visitor parameter, not used
- std::cout << arg(RGB::call(expr, i, i)) << std::endl;
+ std::cout << RGB::call(expr, i, i) << std::endl;
}
int main()
{
- printColor(Red() + Green());
- printColor(Red() + Green() + Blue());
- printColor(Red() + (Green() + Blue()));
+ printColor(RedT() + GreenT());
+ printColor(RedT() + GreenT() + BlueT());
+ printColor(RedT() + (GreenT() + BlueT()));
return 0;
}
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk