Spirit: Assigning elements of a closure member.

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Spirit: Assigning elements of a closure member.

Mark Williams-3

Is there any standard way, in a semantic action, to assign individual
elements of a struct that is set as a member of a closure? For example
suppose I have:-

struct foo {
  int a, b, c;
  std::string d;
  float e;
};

And I have this as member1 in my closure, which is then attached to a rule
that parses each of those struct member elements seperately, is there then a
standard way within the semantic actions in my rule to assign to foo.d, say,
without modifying the other values? It seems that my only option is to
either write my own functors to set each member seperately, or have the
individual elements as seperate members of my closure.

I kind of want to do something like this:
myrule = int_p[assign_a(myrule.m1.a)] >> int_p[assign_a(myrule.m1.b)];
But myrule.m1 is an actor, not an instance of foo.

Is there a way to do this with boost::bind perhaps?

I want to use closures since my rules need to be reentrant.

Mark



_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: Spirit: Assigning elements of a closure member.

Mark Williams-3

Having failed to find this elsewhere in boost, or in spirit, I rolled my
own - and it turned out to be a lot simpler than I expected:-

   struct assign_member_impl
   {
      template <typename Container, typename Member, typename Item>
      struct result
      {
         typedef void type;
      };
      template <typename Container, typename Member, typename Item>
      void operator()(Container& c, Member &m, Item const& item) const
      {
         c.*m = item;
      }
   };
   function<assign_member_impl> const assign_member_a =
assign_member_impl();

In a semantic action, we can then do something like this:-
  myrule = int_p[assign_member_a(myrule.m1, &foo::a, arg1)];


Mark



"Mark Williams" <[hidden email]> wrote in message
news:e10t9b$86l$[hidden email]...

>
> Is there any standard way, in a semantic action, to assign individual
> elements of a struct that is set as a member of a closure? For example
> suppose I have:-
>
> struct foo {
>  int a, b, c;
>  std::string d;
>  float e;
> };
>
> And I have this as member1 in my closure, which is then attached to a rule
> that parses each of those struct member elements seperately, is there then
> a standard way within the semantic actions in my rule to assign to foo.d,
> say, without modifying the other values? It seems that my only option is
> to either write my own functors to set each member seperately, or have the
> individual elements as seperate members of my closure.
>
> I kind of want to do something like this:
> myrule = int_p[assign_a(myrule.m1.a)] >> int_p[assign_a(myrule.m1.b)];
> But myrule.m1 is an actor, not an instance of foo.
>
> Is there a way to do this with boost::bind perhaps?
>
> I want to use closures since my rules need to be reentrant.
>
> Mark



_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users