mirror of
https://github.com/boostorg/fusion.git
synced 2025-07-21 08:12:39 +02:00
Update documents and tests for #8270 from Jamboree.
https://svn.boost.org/trac/boost/ticket/8270
see also: b36426c8e3
This commit is contained in:
committed by
Kohei Takahashi
parent
52eb8768f2
commit
d03f79b6fd
@ -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,10 +1959,26 @@ __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__<int>('X')
|
||||
, __fusion_make_pair__<double>("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]
|
||||
|
||||
[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,10 +2176,26 @@ __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__<int, char>
|
||||
, __fusion_pair__<double, std::string> > >::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<ns::point>::type // __map__<__fusion_pair__<ns::x_member, int>, __fusion_pair__<ns::y_member, int> >
|
||||
|
||||
[endsect]
|
||||
|
||||
[endsect]
|
||||
|
@ -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 : : : : ]
|
||||
|
85
test/sequence/as_map_assoc.cpp
Normal file
85
test/sequence/as_map_assoc.cpp
Normal file
@ -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 <boost/detail/lightweight_test.hpp>
|
||||
#include <boost/fusion/adapted/struct/adapt_assoc_struct.hpp>
|
||||
#include <boost/fusion/container/vector/vector.hpp>
|
||||
#include <boost/fusion/adapted/mpl.hpp>
|
||||
#include <boost/fusion/container/generation/make_vector.hpp>
|
||||
#include <boost/fusion/container/map/convert.hpp>
|
||||
#include <boost/fusion/container/generation/make_set.hpp>
|
||||
#include <boost/fusion/container/vector/convert.hpp>
|
||||
#include <boost/fusion/sequence/comparison/equal_to.hpp>
|
||||
#include <boost/fusion/sequence/intrinsic/at_key.hpp>
|
||||
#include <boost/fusion/sequence/io/out.hpp>
|
||||
#include <boost/fusion/support/pair.hpp>
|
||||
|
||||
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<ns::point>::type map(p);
|
||||
std::cout << at_key<ns::x_member>(map) << std::endl;
|
||||
std::cout << at_key<ns::y_member>(map) << std::endl;
|
||||
BOOST_TEST(at_key<ns::x_member>(map) == 123);
|
||||
BOOST_TEST(at_key<ns::y_member>(map) == 456);
|
||||
}
|
||||
|
||||
{
|
||||
boost::fusion::result_of::as_map<set<int, char> >::type map(make_set(1, '2'));
|
||||
BOOST_TEST(at_key<int>(map) == 1);
|
||||
BOOST_TEST(at_key<char>(map) == '2');
|
||||
}
|
||||
|
||||
{
|
||||
// test conversion
|
||||
typedef map<
|
||||
pair<ns::x_member, int>
|
||||
, pair<ns::y_member, int> >
|
||||
map_type;
|
||||
|
||||
ns::point p = {123, 456};
|
||||
map_type m(p);
|
||||
BOOST_TEST(as_vector(m) == make_vector(make_pair<ns::x_member>(123), make_pair<ns::y_member>(456)));
|
||||
m = (make_vector(make_pair<ns::x_member>(123), make_pair<ns::y_member>(456))); // test assign
|
||||
BOOST_TEST(as_vector(m) == make_vector(make_pair<ns::x_member>(123), make_pair<ns::y_member>(456)));
|
||||
}
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
||||
|
Reference in New Issue
Block a user