Update of /cvsroot/boost/boost/libs/xpressive/proto/example
In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv28117
Modified Files:
Jamfile.v2
Added Files:
rgb.cpp
Log Message:
add rgb example, ported from PETE docs
--- NEW FILE: rgb.cpp ---
///////////////////////////////////////////////////////////////////////////////
// Copyright 2007 Eric Niebler. 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)
//
// This is a simple example of doing arbitrary type manipulations with proto
// transforms. It takes some expression involving primiary colors and combines
// the colors according to arbitrary rules. It is a port of the RGB example
// from PETE (http://www.codesourcery.com/pooma/download.html).
#include <iostream>
#include <boost/xpressive/proto/proto.hpp>
#include <boost/xpressive/proto/transform/arg.hpp>
#include <boost/xpressive/proto/transform/compose.hpp>
using namespace boost::proto;
struct RedTag
{
friend std::ostream &operator <<(std::ostream &sout, RedTag)
{
return sout << "This expression is red.";
}
};
struct BlueTag
{
friend std::ostream &operator <<(std::ostream &sout, BlueTag)
{
return sout << "This expression is blue.";
}
};
struct GreenTag
{
friend std::ostream &operator <<(std::ostream &sout, GreenTag)
{
return sout << "This expression is green.";
}
};
typedef terminal<RedTag>::type Red;
typedef terminal<BlueTag>::type Blue;
typedef terminal<GreenTag>::type 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 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<_>
>
>
>
{};
template<typename Expr>
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;
}
int main()
{
printColor(Red() + Green());
printColor(Red() + Green() + Blue());
printColor(Red() + (Green() + Blue()));
return 0;
}
Index: Jamfile.v2
===================================================================
RCS file: /cvsroot/boost/boost/libs/xpressive/proto/example/Jamfile.v2,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- Jamfile.v2 17 Apr 2007 22:36:44 -0000 1.3
+++ Jamfile.v2 23 Jun 2007 01:38:40 -0000 1.4
@@ -27,3 +27,13 @@
:
lazy_vector.cpp
;
+
+exe tarray
+ :
+ tarray.cpp
+ ;
+
+exe rgb
+ :
+ rgb.cpp
+ ;
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Boost-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/boost-cvs