[boost-users][fusion] nested map access

Hello, I'm trying out some ideas with boost.fusion maps. Here is a small example: namespace fields { struct general { struct first_name ; struct last_name ; struct age ; } ; struct address { struct street ; struct postal_code ; struct country ; } ; } typedef fusion::map < fusion::pair< fields::general::first_name, std::string >, fusion::pair< fields::general::last_name, std::string >, fusion::pair< fields::general::age, int >
general_person ;
typedef fusion::map < fusion::pair< fields::address::street, std::string >, fusion::pair< fields::address::postal_code, std::string >, fusion::pair< fields::address::country, std::string >
address_person ;
typedef fusion::map < fusion::pair< fields::general, general_person >, fusion::pair< fields::address, address_person >
person ;
int main() { using namespace fields ; general_person gp = fusion::make_map< general::first_name, general::last_name, general::age >( "John", "Doe", 99 ) ; address_person ap = fusion::make_map< address::street, address::postal_code, address::country >( "Foo street 1", "12345", "Barland" ) ; person a_person = fusion::make_map< general, address >( gp, ap ) ; std::cout << "Age: " << fusion::at_key< general::age >( fusion::at_key< general >( a_person ) ) << std::endl ; return 0 ; } I'm not happy with the last line that retrieve the value of a specific pair inside a nested map. I was wondering whether this could benefit from some MPL trick to make it more readable. I was thinking about something like the following: get_value< general, age >( a_person ). My questions are: 1) Is MPL the right tool to transform a call to get_value< general, age >( a_person ) into fusion::at_key< general::age >( fusion::at_key< general >( a_person ) ) 2) If it is, could someone provide me some help on how to achieve such a result? Thank you for your help

AMDG Istvan Buki wrote:
1) Is MPL the right tool to transform a call to get_value< general, age >( a_person ) into fusion::at_key< general::age >( fusion::at_key< general >( a_person ) )
No not really.
2) If it is, could someone provide me some help on how to achieve such a result?
Here's a modified form of your example. I couldn't get the variadic
syntax to compile, so
there is a numeral tacked onto the end of the function name.
#include
general_person ;
typedef fusion::map < fusion::pair< fields::address::street, std::string >, fusion::pair< fields::address::postal_code, std::string >, fusion::pair< fields::address::country, std::string >
address_person ;
typedef fusion::map < fusion::pair< fields::general, general_person >, fusion::pair< fields::address, address_person >
person ;
int main() { using namespace fields ; general_person gp = fusion::make_map< general::first_name, general::last_name, general::age >( "John", "Doe", 99 ) ; address_person ap = fusion::make_map< address::street, address::postal_code, address::country >( "Foo street 1", "12345", "Barland" ) ; person a_person = fusion::make_map< general, address >( gp, ap ) ; std::cout << "Age: " // << fusion::at_key< general::age >( fusion::at_key< general >( a_person ) ) << at_key2< general, general::age>( a_person ) << std::endl ; return 0 ; } In Christ, Steven Watanabe

Hello Steven,
thank you very much for your nice and clean solution.
May I ask you to post the variadic version as it is, even if it doesn't
compile. I'll try to find how to make it work. It will give me a good
starting point to learn more about these techniques.
Istvan
On Mon, Jun 2, 2008 at 7:07 PM, Steven Watanabe
AMDG
Istvan Buki wrote:
1) Is MPL the right tool to transform a call to get_value< general, age >( a_person ) into fusion::at_key< general::age >( fusion::at_key< general >( a_person ) )
No not really.
2) If it is, could someone provide me some help on how to achieve such a result?
Here's a modified form of your example. I couldn't get the variadic syntax to compile, so there is a numeral tacked onto the end of the function name.
#include
#include #include #include #include <string> #include <iostream> namespace result_of { template
struct at_key1 : boost::fusion::result_of::at_key {}; template struct at_key2 : boost::fusion::result_of::at_key ::type>::type, K1> {}; } template
typename result_of::at_key1 ::type at_key1(Map& m) { return(boost::fusion::at_key<K0>(m)); } template
typename result_of::at_key2 ::type at_key2(Map& m) { return(boost::fusion::at_key<K1>(boost::fusion::at_key<K0>(m))); } namespace fusion = boost::fusion;
namespace fields { struct general { struct first_name ; struct last_name ; struct age ; } ;
struct address { struct street ; struct postal_code ; struct country ; } ; }
typedef fusion::map < fusion::pair< fields::general::first_name, std::string >, fusion::pair< fields::general::last_name, std::string >, fusion::pair< fields::general::age, int >
general_person ;
typedef fusion::map < fusion::pair< fields::address::street, std::string >, fusion::pair< fields::address::postal_code, std::string >, fusion::pair< fields::address::country, std::string >
address_person ;
typedef fusion::map < fusion::pair< fields::general, general_person >, fusion::pair< fields::address, address_person >
person ;
int main() { using namespace fields ;
general_person gp = fusion::make_map< general::first_name, general::last_name, general::age >( "John", "Doe", 99 ) ;
address_person ap = fusion::make_map< address::street, address::postal_code, address::country >( "Foo street 1", "12345", "Barland" ) ;
person a_person = fusion::make_map< general, address >( gp, ap ) ;
std::cout << "Age: " // << fusion::at_key< general::age >( fusion::at_key< general >( a_person ) ) << at_key2< general, general::age>( a_person ) << std::endl ;
return 0 ; }
In Christ, Steven Watanabe
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

AMDG Istvan Buki wrote:
thank you very much for your nice and clean solution. May I ask you to post the variadic version as it is, even if it doesn't compile. I'll try to find how to make it work. It will give me a good starting point to learn more about these techniques.
Just take the numbers off of the functions at_key1 and at_key2, so that they are overloaded. I suppose that variadic is not really the right term for this... In Christ, Steven Watanabe

ok, I understand now. Indeed, "variadic" was misleading.
Thanks for your help.
On Tue, Jun 3, 2008 at 3:14 PM, Steven Watanabe
AMDG
Istvan Buki wrote:
thank you very much for your nice and clean solution. May I ask you to post the variadic version as it is, even if it doesn't compile. I'll try to find how to make it work. It will give me a good starting point to learn more about these techniques.
Just take the numbers off of the functions at_key1 and at_key2, so that they are overloaded. I suppose that variadic is not really the right term for this...
In Christ, Steven Watanabe
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
Istvan Buki
-
Steven Watanabe