[karma] using attributes out of order

Hello, I was fairly familiar with Spirit 1, but I'm now making my first foray into Spirit 2. I'm trying to write something with karma's attribute system. Say I've got a fusion adapted struct: struct s { int foo; double bar; std::string baz; }; The Spirit docs give plenty of examples like: int_ << double_ << string But I can't find any docs showing how to output the members in a different order, like: double_ << string << int_ Is there a recommended way to achieve this? Thanks! -- Cory Nelson

On Fri, Jan 15, 2010 at 3:17 AM, Cory Nelson
Hello,
I was fairly familiar with Spirit 1, but I'm now making my first foray into Spirit 2. I'm trying to write something with karma's attribute system. Say I've got a fusion adapted struct:
struct s { int foo; double bar; std::string baz; };
The Spirit docs give plenty of examples like:
int_ << double_ << string
But I can't find any docs showing how to output the members in a different order, like:
double_ << string << int_
Is there a recommended way to achieve this? Thanks!
It is generally better to ask these questions on the Spirit mailing list. However, three immediate answers to that from what I can see right off the bat. First: When you specify you struct as the BOOST_FUSION_ADAPT_STRUCT or whatever it is called, you can specify your order in that. Second: You can use Fusion to operate over the struct to give things a different order. Third: You can use actors, unsure if this code is correct, but it is like this: double_[_2] << string[_3] << int_[_1]

I was fairly familiar with Spirit 1, but I'm now making my first foray into Spirit 2. I'm trying to write something with karma's attribute system. Say I've got a fusion adapted struct:
struct s { int foo; double bar; std::string baz; };
The Spirit docs give plenty of examples like:
int_ << double_ << string
But I can't find any docs showing how to output the members in a different order, like:
double_ << string << int_
Is there a recommended way to achieve this? Thanks!
It is generally better to ask these questions on the Spirit mailing list.
However, three immediate answers to that from what I can see right off the bat.
First: When you specify you struct as the BOOST_FUSION_ADAPT_STRUCT or whatever it is called, you can specify your order in that.
That's the preferred method, indeed.
Second: You can use Fusion to operate over the struct to give things a different order.
Look for fusion::nview, which allows to create a fusion view of another fusion sequence/view while defining the order of the elements.
Third: You can use actors, unsure if this code is correct, but it is like this: double_[_2] << string[_3] << int_[_1]
This syntax is actually: using boost::phoenix::at_c; double_[_1 = at_c<1>(_val)] << string[_1 = at_c<2>(_val)] << int_[_1 = at_c<0>(_val)] Which is much more than you ever want to write :-P (and additionally, it's much more inefficient than the 2 solutions above). There is a forth method involving Spirit's customization points, but that's merely academic... Regards Hartmut --------------- Meet me at BoostCon www.boostcon.com
participants (3)
-
Cory Nelson
-
Hartmut Kaiser
-
OvermindDL1