Boost logo

Boost-Commit :

From: joel_at_[hidden]
Date: 2008-04-17 20:55:12


Author: djowel
Date: 2008-04-17 20:55:12 EDT (Thu, 17 Apr 2008)
New Revision: 44525
URL: http://svn.boost.org/trac/boost/changeset/44525

Log:
added example on various ways to attach actions
Added:
   trunk/libs/spirit/example/qi/actions.cpp (contents, props changed)
Text files modified:
   trunk/libs/spirit/example/qi/Jamfile | 1 +
   1 files changed, 1 insertions(+), 0 deletions(-)

Modified: trunk/libs/spirit/example/qi/Jamfile
==============================================================================
--- trunk/libs/spirit/example/qi/Jamfile (original)
+++ trunk/libs/spirit/example/qi/Jamfile 2008-04-17 20:55:12 EDT (Thu, 17 Apr 2008)
@@ -6,6 +6,7 @@
 #==============================================================================
 project spirit-qi-example ;
 
+exe actions : actions.cpp ;
 exe sum : sum.cpp ;
 exe complex_number : complex_number.cpp ;
 exe employee : employee.cpp ;

Added: trunk/libs/spirit/example/qi/actions.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/spirit/example/qi/actions.cpp 2008-04-17 20:55:12 EDT (Thu, 17 Apr 2008)
@@ -0,0 +1,51 @@
+#include <iostream>
+#include <boost/spirit/include/qi.hpp>
+#include <boost/lambda/lambda.hpp>
+#include <boost/bind.hpp>
+
+// Presented are various ways to attach semantic actions
+// * Using plain function pointers
+// * Using plain simple function objects
+// * Using plain boost.bind
+// * Using plain boost.lambda
+
+using namespace boost::spirit;
+
+void write(int& i)
+{
+ std::cout << i << std::endl;
+}
+
+struct write_action
+{
+ void operator()(int& i, unused_type, unused_type) const
+ {
+ std::cout << i << std::endl;
+ }
+};
+
+int main()
+{
+ char const *s1 = "{42}", *e1 = s1 + std::strlen(s1);
+
+ { // example using plain functions
+
+ qi::parse(s1, e1, '{' >> int_[&write] >> '}');
+ }
+
+ { // example using simple function objects
+
+ qi::parse(s1, e1, '{' >> int_[write_action()] >> '}');
+ }
+
+ { // example using boost.bind
+
+ qi::parse(s1, e1, '{' >> int_[boost::bind(&write, _1)] >> '}');
+ }
+
+ { // example using boost.lambda
+
+ using boost::lambda::_1;
+ qi::parse(s1, e1, '{' >> int_[std::cout << _1 << '\n'] >> '}');
+ }}
+


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