Merge pull request #63 from Flast/develop

Add tests and documents
This commit is contained in:
Joel de Guzman
2015-03-16 08:43:34 +08:00
4 changed files with 138 additions and 4 deletions

View File

@ -1949,7 +1949,7 @@ Convert a fusion sequence to a __map__.
[*Semantics]: Convert a fusion sequence, `seq`, 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. __fusion_pair__s. There may be no duplicate __fusion_pair__ key types.
[heading Header] [heading Header]
@ -1959,9 +1959,25 @@ __fusion_pair__s. There may be no duplicate __fusion_pair__ key types.
[heading Example] [heading Example]
// from sequence of __fusion_pair__
as_map(__make_vector__( as_map(__make_vector__(
__fusion_make_pair__<int>('X') __fusion_make_pair__<int>('X')
, __fusion_make_pair__<double>("Men"))) , __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__. [*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. __fusion_pair__s. There may be no duplicate __fusion_pair__ key types.
[heading Header] [heading Header]
@ -2160,9 +2176,25 @@ __fusion_pair__s. There may be no duplicate __fusion_pair__ key types.
[heading Example] [heading Example]
// from sequence of __fusion_pair__
result_of::as_map<__vector__< result_of::as_map<__vector__<
__fusion_pair__<int, char> __fusion_pair__<int, char>
, __fusion_pair__<double, std::string> > >::type , __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]

View File

@ -59,6 +59,7 @@ project
[ run sequence/as_deque.cpp : : : : ] [ run sequence/as_deque.cpp : : : : ]
[ run sequence/as_list.cpp : : : : ] [ run sequence/as_list.cpp : : : : ]
[ run sequence/as_map.cpp : : : : ] [ run sequence/as_map.cpp : : : : ]
[ run sequence/as_map_assoc.cpp : : : : ]
[ run sequence/as_set.cpp : : : : ] [ run sequence/as_set.cpp : : : : ]
[ run sequence/as_vector.cpp : : : : ] [ run sequence/as_vector.cpp : : : : ]
[ run sequence/boost_tuple.cpp : : : : ] [ run sequence/boost_tuple.cpp : : : : ]

View 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();
}

View File

@ -38,6 +38,11 @@ struct copy_all
} }
}; };
struct abstract
{
virtual void foo() = 0;
};
int int
main() main()
{ {
@ -54,7 +59,8 @@ main()
{ {
typedef map< typedef map<
pair<int, char> pair<int, char>
, pair<double, std::string> > , pair<double, std::string>
, pair<abstract, int> >
map_type; map_type;
BOOST_MPL_ASSERT((traits::is_associative<map_type>)); BOOST_MPL_ASSERT((traits::is_associative<map_type>));
@ -62,23 +68,29 @@ main()
map_type m( map_type m(
make_pair<int>('X') make_pair<int>('X')
, make_pair<double>("Men")); , make_pair<double>("Men")
, make_pair<abstract>(2));
std::cout << at_key<int>(m) << std::endl; std::cout << at_key<int>(m) << std::endl;
std::cout << at_key<double>(m) << std::endl; std::cout << at_key<double>(m) << std::endl;
std::cout << at_key<abstract>(m) << std::endl;
BOOST_TEST(at_key<int>(m) == 'X'); BOOST_TEST(at_key<int>(m) == 'X');
BOOST_TEST(at_key<double>(m) == "Men"); BOOST_TEST(at_key<double>(m) == "Men");
BOOST_TEST(at_key<abstract>(m) == 2);
BOOST_STATIC_ASSERT(( BOOST_STATIC_ASSERT((
boost::is_same<boost::fusion::result_of::value_at_key<map_type, int>::type, char>::value)); boost::is_same<boost::fusion::result_of::value_at_key<map_type, int>::type, char>::value));
BOOST_STATIC_ASSERT(( BOOST_STATIC_ASSERT((
boost::is_same<boost::fusion::result_of::value_at_key<map_type, double>::type, std::string>::value)); boost::is_same<boost::fusion::result_of::value_at_key<map_type, double>::type, std::string>::value));
BOOST_STATIC_ASSERT((
boost::is_same<boost::fusion::result_of::value_at_key<map_type, abstract>::type, int>::value));
std::cout << m << std::endl; std::cout << m << std::endl;
BOOST_STATIC_ASSERT((boost::fusion::result_of::has_key<map_type, int>::value)); BOOST_STATIC_ASSERT((boost::fusion::result_of::has_key<map_type, int>::value));
BOOST_STATIC_ASSERT((boost::fusion::result_of::has_key<map_type, double>::value)); BOOST_STATIC_ASSERT((boost::fusion::result_of::has_key<map_type, double>::value));
BOOST_STATIC_ASSERT((boost::fusion::result_of::has_key<map_type, abstract>::value));
BOOST_STATIC_ASSERT((!boost::fusion::result_of::has_key<map_type, std::string>::value)); BOOST_STATIC_ASSERT((!boost::fusion::result_of::has_key<map_type, std::string>::value));
std::cout << deref_data(begin(m)) << std::endl; std::cout << deref_data(begin(m)) << std::endl;
@ -86,15 +98,19 @@ main()
BOOST_TEST(deref_data(begin(m)) == 'X'); BOOST_TEST(deref_data(begin(m)) == 'X');
BOOST_TEST(deref_data(fusion::next(begin(m))) == "Men"); BOOST_TEST(deref_data(fusion::next(begin(m))) == "Men");
BOOST_TEST(deref_data(fusion::next(next(begin(m)))) == 2);
BOOST_STATIC_ASSERT((boost::is_same<boost::fusion::result_of::key_of<boost::fusion::result_of::begin<map_type>::type>::type, int>::value)); BOOST_STATIC_ASSERT((boost::is_same<boost::fusion::result_of::key_of<boost::fusion::result_of::begin<map_type>::type>::type, int>::value));
BOOST_STATIC_ASSERT((boost::is_same<boost::fusion::result_of::key_of<boost::fusion::result_of::next<boost::fusion::result_of::begin<map_type>::type>::type>::type, double>::value)); BOOST_STATIC_ASSERT((boost::is_same<boost::fusion::result_of::key_of<boost::fusion::result_of::next<boost::fusion::result_of::begin<map_type>::type>::type>::type, double>::value));
BOOST_STATIC_ASSERT((boost::is_same<boost::fusion::result_of::key_of<boost::fusion::result_of::next<boost::fusion::result_of::next<boost::fusion::result_of::begin<map_type>::type>::type>::type>::type, abstract>::value));
BOOST_STATIC_ASSERT((boost::is_same<boost::fusion::result_of::value_of_data<boost::fusion::result_of::begin<map_type>::type>::type, char>::value)); BOOST_STATIC_ASSERT((boost::is_same<boost::fusion::result_of::value_of_data<boost::fusion::result_of::begin<map_type>::type>::type, char>::value));
BOOST_STATIC_ASSERT((boost::is_same<boost::fusion::result_of::value_of_data<boost::fusion::result_of::next<boost::fusion::result_of::begin<map_type>::type>::type>::type, std::string>::value)); BOOST_STATIC_ASSERT((boost::is_same<boost::fusion::result_of::value_of_data<boost::fusion::result_of::next<boost::fusion::result_of::begin<map_type>::type>::type>::type, std::string>::value));
BOOST_STATIC_ASSERT((boost::is_same<boost::fusion::result_of::value_of_data<boost::fusion::result_of::next<boost::fusion::result_of::next<boost::fusion::result_of::begin<map_type>::type>::type>::type>::type, int>::value));
// Test random access interface. // Test random access interface.
pair<int, char> a = at_c<0>(m); (void) a; pair<int, char> a = at_c<0>(m); (void) a;
pair<double, std::string> b = at_c<1>(m); pair<double, std::string> b = at_c<1>(m);
pair<abstract, int> c = at_c<2>(m);
} }
// iterators & random access interface. // iterators & random access interface.