| 1 | /*============================================================================= |
| 2 | Copyright (c) 2001-2011 Joel de Guzman |
| 3 | |
| 4 | Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | ==============================================================================*/ |
| 7 | #include <boost/detail/lightweight_test.hpp> |
| 8 | #include <boost/fusion/adapted/struct/adapt_assoc_struct.hpp> |
| 9 | #include <boost/fusion/container/vector/vector.hpp> |
| 10 | #include <boost/fusion/adapted/mpl.hpp> |
| 11 | #include <boost/fusion/container/generation/make_vector.hpp> |
| 12 | #include <boost/fusion/container/map/convert.hpp> |
| 13 | #include <boost/fusion/container/generation/make_set.hpp> |
| 14 | #include <boost/fusion/container/vector/convert.hpp> |
| 15 | #include <boost/fusion/sequence/comparison/equal_to.hpp> |
| 16 | #include <boost/fusion/sequence/intrinsic/at_key.hpp> |
| 17 | #include <boost/fusion/sequence/io/out.hpp> |
| 18 | #include <boost/fusion/support/pair.hpp> |
| 19 | |
| 20 | namespace ns |
| 21 | { |
| 22 | struct x_member; |
| 23 | struct y_member; |
| 24 | struct z_member; |
| 25 | |
| 26 | struct point |
| 27 | { |
| 28 | int x; |
| 29 | int y; |
| 30 | }; |
| 31 | } |
| 32 | |
| 33 | BOOST_FUSION_ADAPT_ASSOC_STRUCT( |
| 34 | ns::point, |
| 35 | (int, x, ns::x_member) |
| 36 | (int, y, ns::y_member) |
| 37 | ) |
| 38 | |
| 39 | int |
| 40 | main() |
| 41 | { |
| 42 | using namespace boost::fusion; |
| 43 | using namespace boost; |
| 44 | |
| 45 | std::cout << tuple_open('['); |
| 46 | std::cout << tuple_close(']'); |
| 47 | std::cout << tuple_delimiter(", "); |
| 48 | |
| 49 | { |
| 50 | ns::point p = {123, 456}; |
| 51 | std::cout << as_map(p) << std::endl; |
| 52 | } |
| 53 | |
| 54 | { |
| 55 | ns::point p = {123, 456}; |
| 56 | boost::fusion::result_of::as_map<ns::point>::type map(p); |
| 57 | std::cout << at_key<ns::x_member>(map) << std::endl; |
| 58 | std::cout << at_key<ns::y_member>(map) << std::endl; |
| 59 | BOOST_TEST(at_key<ns::x_member>(map) == 123); |
| 60 | BOOST_TEST(at_key<ns::y_member>(map) == 456); |
| 61 | } |
| 62 | |
| 63 | { |
| 64 | boost::fusion::result_of::as_map<set<int, char> >::type map(make_set(1, '2')); |
| 65 | BOOST_TEST(at_key<int>(map) == 1); |
| 66 | BOOST_TEST(at_key<char>(map) == '2'); |
| 67 | } |
| 68 | |
| 69 | { |
| 70 | // test conversion |
| 71 | typedef map< |
| 72 | pair<ns::x_member, int> |
| 73 | , pair<ns::y_member, int> > |
| 74 | map_type; |
| 75 | |
| 76 | ns::point p = {123, 456}; |
| 77 | map_type m(p); |
| 78 | BOOST_TEST(as_vector(m) == make_vector(make_pair<ns::x_member>(123), make_pair<ns::y_member>(456))); |
| 79 | m = (make_vector(make_pair<ns::x_member>(123), make_pair<ns::y_member>(456))); // test assign |
| 80 | BOOST_TEST(as_vector(m) == make_vector(make_pair<ns::x_member>(123), make_pair<ns::y_member>(456))); |
| 81 | } |
| 82 | |
| 83 | return boost::report_errors(); |
| 84 | } |
| 85 | |