Using Karma true_

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

Using Karma true_

Bill Buklis-2
I was trying to extend the karma example using &true_ in the "Complex -
Fully Integrated" example. But, I can't yet figure out how to add
additional elements that are unrelated. If I try to put any fields
before &true_, the compiler complains that it doesn't match the struct.
Parentheses around the true_ section do not work either.

Basically let's say I have a structure with 7 ints. I want to always
output the first two and the last two. The middle 3 are only output if
element 3 is non-zero. For example let's say I want to output a csv line
with a structure like this:

struct testdata
{
     int a,b,c,d,e,f,g;
};

If c is non-zero then the output should be (where the letters represent
the appropriate number):

a,b,c,d,e,f,g

Otherwise:

a,b,,,f,g

I tried setting up a fusion adapt struct like so:

BOOST_FUSION_ADAPT_ADT(
     testdata,
     (int, int, obj.a,)
     (int, int, obj.b,)
     (bool, bool, obj.c != 0,)
     (int, int, obj.c,)
     (int, int, obj.d,)
     (int, int, obj.e,)
     (int, int, obj.f,)
     (int, int, obj.g,)

// Does not work (where sink is templated output and data is const
testdata&):
generate_delimited(sink,
     (
         int_ << int_ <<
         (&true_ << int_ << int_ << int_ | omit[int_] << omit[int_] <<
omit[int_]) <<
         int_ << int_
     ),
     ',',
     data);

What's the correct way to handle this?

Notes: I used generate_delimited here only to avoid repeating ','
everywhere. If it works better using plain generate, fine with me. I
notice that it usually always ends with a trailing delimiter. In my
actual use case this is fine, because the last field is always blank
anyway and doesn't need to be included in the generator.

Thanks,

--
Bill


------------------------------------------------------------------------------
Want excitement?
Manually upgrade your production database.
When you want reliability, choose Perforce
Perforce version control. Predictably reliable.
http://pubads.g.doubleclick.net/gampad/clk?id=157508191&iu=/4140/ostg.clktrk
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: Using Karma true_

Bill Buklis-2
> I was trying to extend the karma example using &true_ in the "Complex -
> Fully Integrated" example. But, I can't yet figure out how to add
> additional elements that are unrelated. If I try to put any fields
> before &true_, the compiler complains that it doesn't match the struct.
> Parentheses around the true_ section do not work either.
>
> Basically let's say I have a structure with 7 ints. I want to always
> output the first two and the last two. The middle 3 are only output if
> element 3 is non-zero. For example let's say I want to output a csv line
> with a structure like this:
>
> struct testdata
> {
>       int a,b,c,d,e,f,g;
> };
>
> If c is non-zero then the output should be (where the letters represent
> the appropriate number):
>
> a,b,c,d,e,f,g
>
> Otherwise:
>
> a,b,,,f,g
>
> I tried setting up a fusion adapt struct like so:
>
> BOOST_FUSION_ADAPT_ADT(
>       testdata,
>       (int, int, obj.a,)
>       (int, int, obj.b,)
>       (bool, bool, obj.c != 0,)
>       (int, int, obj.c,)
>       (int, int, obj.d,)
>       (int, int, obj.e,)
>       (int, int, obj.f,)
>       (int, int, obj.g,)
>
> // Does not work (where sink is templated output and data is const
> testdata&):
> generate_delimited(sink,
>       (
>           int_ << int_ <<
>           (&true_ << int_ << int_ << int_ | omit[int_] << omit[int_] <<
> omit[int_]) <<
>           int_ << int_
>       ),
>       ',',
>       data);
>
> What's the correct way to handle this?
>
> Notes: I used generate_delimited here only to avoid repeating ','
> everywhere. If it works better using plain generate, fine with me. I
> notice that it usually always ends with a trailing delimiter. In my
> actual use case this is fine, because the last field is always blank
> anyway and doesn't need to be included in the generator.
>
> Thanks,
>

And one more related question: how do you properly have more than one
conditional section? Using the example above for instance, how would you
output fields a, b and c only if b !=0, d, e and f only if e != 0, g -
with the fusion adapted struct updated to match, of course?

Thanks,

--
Bill


------------------------------------------------------------------------------
Want excitement?
Manually upgrade your production database.
When you want reliability, choose Perforce.
Perforce version control. Predictably reliable.
http://pubads.g.doubleclick.net/gampad/clk?id=157508191&iu=/4140/ostg.clktrk
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general