Re: [Boost-users] fusion struct adapt macro, construct the struct from a fusion vector

----- Original Message -----
From: "Hicham Mouline"
What about:
template <typename Seq> params(Seq const& seq, typename boost::enable_if< fusion::traits::is_sequence<Seq>
::type* = NULL);
?
Fantastic.
Here is the code I have (plz see below), but it seems there are issues with const-ness that I don't understand.
Following my previous attempt, replacing the functor applied by foreach to
the zipped sequence by:
struct assignelt {
template <typename T>
void operator()( const vector2

On 1/20/2010 2:05 AM, Hicham Mouline wrote:
Following my previous attempt, replacing the functor applied by foreach to the zipped sequence by:
struct assignelt {
template <typename T> void operator()( const vector2
& seqpairs ) const { typedef vector2 pair_t; const_cast (at_c<0, const pair_t>(seqpairs)) = at_c<1, const pair_t>(seqpairs); } }; works. I can now construct my struct params from an equivalent fusion sequence. This constructor is identical for all my params struct. I have structs params1 to params100
Is there a way to factorise this ctor and not repeat its definition in the 100 struct params? Is this possible with a base struct templated on the derived struct?
Wow. 100! Anyway, I've seem to lost context already. Pardon me, I'm not following this thread well. Could you rephrase the problem again in simpler terms? Regards, -- Joel de Guzman http://www.boostpro.com http://spirit.sf.net http://www.facebook.com/djowel Meet me at BoostCon http://www.boostcon.com/home http://www.facebook.com/boostcon

----- Original Message -----
From: "Joel de Guzman"
On 1/20/2010 2:05 AM, Hicham Mouline wrote:
Following my previous attempt, replacing the functor applied by foreach to the zipped sequence by:
struct assignelt {
template <typename T> void operator()( const vector2
& seqpairs ) const { typedef vector2 pair_t; const_cast (at_c<0, const pair_t>(seqpairs)) = at_c<1, const pair_t>(seqpairs); } }; works. I can now construct my struct params from an equivalent fusion sequence. This constructor is identical for all my params struct. I have structs params1 to params100
Is there a way to factorise this ctor and not repeat its definition in the 100 struct params? Is this possible with a base struct templated on the derived struct?
Wow. 100! Anyway, I've seem to lost context already. Pardon me, I'm not following this thread well. Could you rephrase the problem again in simpler terms?
Regards, Yes, something in that order. We are testing systems and we don't know which will give the best results.
With your and Hartmut's help, I wrote a constructor for my struct pararms that takes a fusion sequence as an argument: template<typename Seq> params( const Seq& seq, ... ) { for_each( zip(*this, seq), assignelt() ); } params is adapted to be a fusion sequence. assignelt() is the functor that assigns from seq to params with the help of the zipped sequence Now, my question was whether the ctor could be refactored because it is identical for all the 100 params structs. Regards,

On 1/20/2010 11:43 AM, Hicham Mouline wrote:
Yes, something in that order. We are testing systems and we don't know which will give the best results.
With your and Hartmut's help, I wrote a constructor for my struct pararms that takes a fusion sequence as an argument:
template<typename Seq> params( const Seq& seq, ... ) { for_each( zip(*this, seq), assignelt() ); }
params is adapted to be a fusion sequence. assignelt() is the functor that assigns from seq to params with the help of the zipped sequence
Now, my question was whether the ctor could be refactored because it is identical for all the 100 params structs.
So you have 100 similar structs that all want to to have this constructor? But why? Why not simply work on fusion::vectors instead of adapting structs? Regards, -- Joel de Guzman http://www.boostpro.com http://spirit.sf.net http://www.facebook.com/djowel Meet me at BoostCon http://www.boostcon.com/home http://www.facebook.com/boostcon

----- Original Message -----
From: "Joel de Guzman"
On 1/20/2010 11:43 AM, Hicham Mouline wrote:
Yes, something in that order. We are testing systems and we don't know which will give the best results.
With your and Hartmut's help, I wrote a constructor for my struct pararms that takes a fusion sequence as an argument:
template<typename Seq> params( const Seq& seq, ... ) { for_each( zip(*this, seq), assignelt() ); }
params is adapted to be a fusion sequence. assignelt() is the functor that assigns from seq to params with the help of the zipped sequence
Now, my question was whether the ctor could be refactored because it is identical for all the 100 params structs.
So you have 100 similar structs that all want to to have this constructor? But why? Why not simply work on fusion::vectors instead of adapting structs?
Regards,
Because I wish to leave the machinery of fusion and spirit not very visible to the user. The user only works with the structs, aggregates of parameters. Parsing into those structs from input streams is what drove me to use those spirit and fusion. Ideally, the user would define the struct, and hop, I would generate the parser for them at compile time(the questions for this are in spirit users mailing list), fill up the struct for them at runtime (I do the parsing for them), then in the rest of their code, they would use just the struct directly. But maybe I am going about this in a wrong way? The user's direct is defined in a header file, some translation units would deal just with the struct raw. Other translation units would deal with the fusion/spirit parsing machinery. Maybe some adapt macro a la FUSION_ADAPT could add the ctor and its definition, or something like deriving from a common base class? I'd love to explain more fully my application but I find it hard to do with an email and easier in an realtime medium... rds,

On 1/20/2010 6:31 PM, Hicham Mouline wrote:
----- Original Message ----- From: "Joel de Guzman"
To: Sent: Wednesday, January 20, 2010 10:55 AM Subject: Re: [Boost-users] fusion struct adapt macro, construct the struct from a fusion vector On 1/20/2010 11:43 AM, Hicham Mouline wrote:
Yes, something in that order. We are testing systems and we don't know which will give the best results.
With your and Hartmut's help, I wrote a constructor for my struct pararms that takes a fusion sequence as an argument:
template<typename Seq> params( const Seq& seq, ... ) { for_each( zip(*this, seq), assignelt() ); }
params is adapted to be a fusion sequence. assignelt() is the functor that assigns from seq to params with the help of the zipped sequence
Now, my question was whether the ctor could be refactored because it is identical for all the 100 params structs.
So you have 100 similar structs that all want to to have this constructor? But why? Why not simply work on fusion::vectors instead of adapting structs?
Regards,
Because I wish to leave the machinery of fusion and spirit not very visible to the user. The user only works with the structs, aggregates of parameters. Parsing into those structs from input streams is what drove me to use those spirit and fusion.
Ideally, the user would define the struct, and hop, I would generate the parser for them at compile time(the questions for this are in spirit users mailing list), fill up the struct for them at runtime (I do the parsing for them), then in the rest of their code, they would use just the struct directly.
But maybe I am going about this in a wrong way?
The user's direct is defined in a header file, some translation units would deal just with the struct raw. Other translation units would deal with the fusion/spirit parsing machinery. Maybe some adapt macro a la FUSION_ADAPT could add the ctor and its definition, or something like deriving from a common base class?
I'd love to explain more fully my application but I find it hard to do with an email and easier in an realtime medium...
Ok, understood. Well, then I suggest that you don't mess with the structs
and leave them alone. The goal of FUSION_ADAPT is non-intrusive adaptation.
Instead, if you control the assignment to your structs from fusion vectors
anyway (which I imagine is the case because as you said, your users do
not have to deal with fusion vectors), then I suggest a generic free function
assign instead of decorating all your structs:
template

Joel de Guzman wrote:
Or, if you can wait a bit, I do intend to write an intrusive version of the FUSION_ADAPT_STRUCT which also generates the struct along with the constructors and assignment to/from fusion sequences. I dunno, perhaps you can persuade a fusion guru to write the code (Christopher)? :-)
This isn't all of the above, but the attached worked for me as of Boost
1.35.0...
#include

On 1/21/2010 5:24 AM, Nat Goodspeed wrote:
Joel de Guzman wrote:
Or, if you can wait a bit, I do intend to write an intrusive version of the FUSION_ADAPT_STRUCT which also generates the struct along with the constructors and assignment to/from fusion sequences. I dunno, perhaps you can persuade a fusion guru to write the code (Christopher)? :-)
This isn't all of the above, but the attached worked for me as of Boost 1.35.0...
Aha! Someone took the challenge :-) I should post more challenges like this. There are lots of bits and pieces that I'd appreciate help on. This is one of 'em. Anyway... my hesitation is that the struct is placed in global namespace. Is it still usable when the macro invoked in a specific namespace? Regards, -- Joel de Guzman http://www.boostpro.com http://spirit.sf.net http://www.facebook.com/djowel Meet me at BoostCon http://www.boostcon.com/home http://www.facebook.com/boostcon

----- Original Message -----
From: "Joel de Guzman"
Ok, understood. Well, then I suggest that you don't mess with the structs and leave them alone. The goal of FUSION_ADAPT is non-intrusive adaptation. Instead, if you control the assignment to your structs from fusion vectors anyway (which I imagine is the case because as you said, your users do not have to deal with fusion vectors), then I suggest a generic free function assign instead of decorating all your structs:
template
void assign( Struct& v, const Seq& seq ) { ... } The free function is what I went with and it works perfectly fine with my different params structs. Thanks,
participants (3)
-
Hicham Mouline
-
Joel de Guzman
-
Nat Goodspeed