diff --git a/doc/container.qbk b/doc/container.qbk index 571c7f0a..3ba11cb6 100644 --- a/doc/container.qbk +++ b/doc/container.qbk @@ -1949,7 +1949,7 @@ Convert a fusion sequence to a __map__. [*Semantics]: Convert a fusion sequence, `seq`, to a __map__. -[*Precondition]: The elements of the sequence are assumed to be +[*Precondition]: For non-associative sequence, the elements are assumed to be __fusion_pair__s. There may be no duplicate __fusion_pair__ key types. [heading Header] @@ -1959,9 +1959,25 @@ __fusion_pair__s. There may be no duplicate __fusion_pair__ key types. [heading Example] + // from sequence of __fusion_pair__ as_map(__make_vector__( __fusion_make_pair__('X') , __fusion_make_pair__("Men"))) + + // from associative sequence + namespace ns + { + struct x_member; + struct y_member; + } + BOOST_FUSION_DEFINE_ASSOC_STRUCT( + (ns), + point, + (int, x, ns::x_member) + (int, y, ns::y_member) + ) + ... + as_map(ns::point(123, 456)) [endsect] @@ -2150,7 +2166,7 @@ Returns the result type of __as_map__. [*Semantics]: Convert a fusion sequence, `Sequence`, to a __map__. -[*Precondition]: The elements of the sequence are assumed to be +[*Precondition]: For non-associative sequence, the elements are assumed to be __fusion_pair__s. There may be no duplicate __fusion_pair__ key types. [heading Header] @@ -2160,9 +2176,25 @@ __fusion_pair__s. There may be no duplicate __fusion_pair__ key types. [heading Example] + // from sequence of __fusion_pair__ result_of::as_map<__vector__< __fusion_pair__ , __fusion_pair__ > >::type + + // from associative sequence + namespace ns + { + struct x_member; + struct y_member; + } + BOOST_FUSION_DEFINE_ASSOC_STRUCT( + (ns), + point, + (int, x, ns::x_member) + (int, y, ns::y_member) + ) + ... + result_of::as_map::type // __map__<__fusion_pair__, __fusion_pair__ > [endsect] diff --git a/test/Jamfile b/test/Jamfile index 00c0dd41..752a798a 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -59,6 +59,7 @@ project [ run sequence/as_deque.cpp : : : : ] [ run sequence/as_list.cpp : : : : ] [ run sequence/as_map.cpp : : : : ] + [ run sequence/as_map_assoc.cpp : : : : ] [ run sequence/as_set.cpp : : : : ] [ run sequence/as_vector.cpp : : : : ] [ run sequence/boost_tuple.cpp : : : : ] diff --git a/test/sequence/as_map_assoc.cpp b/test/sequence/as_map_assoc.cpp new file mode 100644 index 00000000..bc9a6951 --- /dev/null +++ b/test/sequence/as_map_assoc.cpp @@ -0,0 +1,85 @@ +/*============================================================================= + Copyright (c) 2001-2011 Joel de Guzman + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +==============================================================================*/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace ns +{ + struct x_member; + struct y_member; + struct z_member; + + struct point + { + int x; + int y; + }; +} + +BOOST_FUSION_ADAPT_ASSOC_STRUCT( + ns::point, + (int, x, ns::x_member) + (int, y, ns::y_member) +) + +int +main() +{ + using namespace boost::fusion; + using namespace boost; + + std::cout << tuple_open('['); + std::cout << tuple_close(']'); + std::cout << tuple_delimiter(", "); + + { + ns::point p = {123, 456}; + std::cout << as_map(p) << std::endl; + } + + { + ns::point p = {123, 456}; + boost::fusion::result_of::as_map::type map(p); + std::cout << at_key(map) << std::endl; + std::cout << at_key(map) << std::endl; + BOOST_TEST(at_key(map) == 123); + BOOST_TEST(at_key(map) == 456); + } + + { + boost::fusion::result_of::as_map >::type map(make_set(1, '2')); + BOOST_TEST(at_key(map) == 1); + BOOST_TEST(at_key(map) == '2'); + } + + { + // test conversion + typedef map< + pair + , pair > + map_type; + + ns::point p = {123, 456}; + map_type m(p); + BOOST_TEST(as_vector(m) == make_vector(make_pair(123), make_pair(456))); + m = (make_vector(make_pair(123), make_pair(456))); // test assign + BOOST_TEST(as_vector(m) == make_vector(make_pair(123), make_pair(456))); + } + + return boost::report_errors(); +} +