Boost logo

Boost Users :

From: Jeff Holle (jeff.holle_at_[hidden])
Date: 2003-05-09 14:41:44


Its time to switch your global function "myaction" into a functor.
Functors are classes that have a function call operator with the same
synopsis as your function.
The advantage is that one can pass custom data to it via a non-default
constructor.

Functors should be and sometimes must be stateless, so care must be
employeed to use references in member data.

Slava Akhmechet wrote:

> I am playing around with Spirit and I seem to run into an architecture
> problem with semantic actions. Here's a small example.
>
> class MyGrammar : public grammar<MyGrammar>
> {
> public:
> template <typename ScannerT>
> struct definition
> {
> definition(MyGrammar const& self)
> {
> TestRule = str_p("test")[&my_action];
> }
>
> rule<ScannerT> const&
> start() const { return TestRulet; };
>
> private:
> rule<ScannerT> TestRule;
> };
> };
>
> My major problem is related to the fact that I cannot pass any custom
> data to my_action function. Since my_action is a global function, it
> has no knowledge of MyGrammar and must rely on global variables.
> Ideally, I would pass self to my_action to be able to hold state. What
> is a general way to get around this problem? The documention mentions
> a way to specify member functions as semantic action actors using
> boost's bind facilities but I was unable to get it to work.
> Thanks in advance.
>
> P.S. Sorry for a fairly newbie question, I was unable to find the
> answer online or in the docs.
> P.P.S. I might have accidently double posted this. Sorry about that.
>
>
>
> *Yahoo! Groups Sponsor*
>
> <http://rd.yahoo.com/M=247865.3269369.4566997.1261774/D=egroupweb/S=1705006788:HM/A=1482387/R=0/*http://ads.x10.com/?bHlhaG9vaG0xLmRhd=1052487646%3eM=247865.3269369.4566997.1261774/D=egroupweb/S=1705006788:HM/A=1482387/R=1=1052487646%3eM=247865.3269369.4566997.1261774/D=egroupweb/S=1705006788:HM/A=1482387/R=2>
>
>
>
> Info: <http://www.boost.org>
> Wiki: <http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl>
> Unsubscribe: <mailto:boost-users-unsubscribe_at_[hidden]>
>
>
> Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service
> <http://docs.yahoo.com/info/terms/>.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net