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