From f7b14aee35cbf25a7728eb8b334c2ad8df893c8d Mon Sep 17 00:00:00 2001 From: "Damien Buhl (alias daminetreg)" Date: Tue, 11 Feb 2014 23:37:53 +0100 Subject: [PATCH] FEATURE: BOOST_FUSION_AUTO_ADAPT_ASSOC_STRUCT now allows defining a Random Access Sequence and an Associative sequence without providing the type. --- .../struct/auto_adapt_assoc_struct.hpp | 44 ++++++ test/Jamfile | 1 + test/sequence/auto_adapt_assoc_struct.cpp | 141 ++++++++++++++++++ 3 files changed, 186 insertions(+) create mode 100644 include/boost/fusion/adapted/struct/auto_adapt_assoc_struct.hpp create mode 100644 test/sequence/auto_adapt_assoc_struct.cpp diff --git a/include/boost/fusion/adapted/struct/auto_adapt_assoc_struct.hpp b/include/boost/fusion/adapted/struct/auto_adapt_assoc_struct.hpp new file mode 100644 index 00000000..0b069de1 --- /dev/null +++ b/include/boost/fusion/adapted/struct/auto_adapt_assoc_struct.hpp @@ -0,0 +1,44 @@ +/*============================================================================= + Copyright (c) 2001-2007 Joel de Guzman + Copyright (c) 2007 Dan Marsden + Copyright (c) 2009-2011 Christopher Schmidt + Copyright (c) 2013-2014 Damien Buhl + + 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) +==============================================================================*/ + +#ifndef BOOST_FUSION_ADAPTED_STRUCT_AUTO_ADAPT_ASSOC_STRUCT_HPP +#define BOOST_FUSION_ADAPTED_STRUCT_AUTO_ADAPT_ASSOC_STRUCT_HPP + +#include +#include + +#include + +#include +#include + +#define BOOST_FUSION_AUTO_ADAPT_ASSOC_STRUCT_FILLER_0(A, B) \ + ((A, B)) BOOST_FUSION_AUTO_ADAPT_ASSOC_STRUCT_FILLER_1 +#define BOOST_FUSION_AUTO_ADAPT_ASSOC_STRUCT_FILLER_1(A, B) \ + ((A, B)) BOOST_FUSION_AUTO_ADAPT_ASSOC_STRUCT_FILLER_0 +#define BOOST_FUSION_AUTO_ADAPT_ASSOC_STRUCT_FILLER_0_END +#define BOOST_FUSION_AUTO_ADAPT_ASSOC_STRUCT_FILLER_1_END + +#define BOOST_FUSION_AUTO_ADAPT_ASSOC_STRUCT_TYPE_DEDUCER(r, NAME, ATTRIBUTE) \ + (BOOST_TYPEOF(NAME::BOOST_PP_TUPLE_ELEM(2, 0, ATTRIBUTE)), \ + BOOST_PP_TUPLE_ELEM(2, 0, ATTRIBUTE), \ + BOOST_PP_TUPLE_ELEM(2, 1, ATTRIBUTE)) + +#define BOOST_FUSION_AUTO_ADAPT_ASSOC_STRUCT(NAME, ATTRIBUTES) \ + BOOST_FUSION_ADAPT_ASSOC_STRUCT(NAME, \ + BOOST_PP_SEQ_FOR_EACH( \ + BOOST_FUSION_AUTO_ADAPT_ASSOC_STRUCT_TYPE_DEDUCER, \ + NAME, \ + BOOST_PP_CAT( \ + BOOST_FUSION_AUTO_ADAPT_ASSOC_STRUCT_FILLER_0 ATTRIBUTES,_END) \ + ) \ + ) + +#endif diff --git a/test/Jamfile b/test/Jamfile index c5bfdf6a..f21f753d 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -137,6 +137,7 @@ project [ run sequence/adapt_assoc_adt.cpp : : : : ] [ run sequence/adapt_assoc_struct_named.cpp : : : : ] [ run sequence/adapt_assoc_struct.cpp : : : : ] + [ run sequence/auto_adapt_assoc_struct.cpp : : : : ] [ run sequence/adapt_assoc_tpl_adt.cpp : : : : ] [ run sequence/adapt_assoc_tpl_struct.cpp : : : : ] [ run sequence/adapt_struct_named.cpp : : : : ] diff --git a/test/sequence/auto_adapt_assoc_struct.cpp b/test/sequence/auto_adapt_assoc_struct.cpp new file mode 100644 index 00000000..177df20b --- /dev/null +++ b/test/sequence/auto_adapt_assoc_struct.cpp @@ -0,0 +1,141 @@ +/*============================================================================= + Copyright (c) 2001-2007 Joel de Guzman + Copyright (c) 2005-2007 Dan Marsden + + 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 +#include +#include +#include +#include +#include +#include +#include +#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_AUTO_ADAPT_ASSOC_STRUCT( + ns::point, + (x, ns::x_member) + (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(", "); + + { + BOOST_MPL_ASSERT_NOT((traits::is_view)); + ns::point p = {123, 456}; + + std::cout << at_c<0>(p) << std::endl; + std::cout << at_c<1>(p) << std::endl; + std::cout << p << std::endl; + BOOST_TEST(p == make_vector(123, 456)); + + at_c<0>(p) = 6; + at_c<1>(p) = 9; + BOOST_TEST(p == make_vector(6, 9)); + + BOOST_STATIC_ASSERT(boost::fusion::result_of::size::value == 2); + BOOST_STATIC_ASSERT(!boost::fusion::result_of::empty::value); + + BOOST_TEST(front(p) == 6); + BOOST_TEST(back(p) == 9); + } + + { + fusion::vector v1(4, 2); + ns::point v2 = {5, 3}; + fusion::vector v3(5, 4); + BOOST_TEST(v1 < v2); + BOOST_TEST(v1 <= v2); + BOOST_TEST(v2 > v1); + BOOST_TEST(v2 >= v1); + BOOST_TEST(v2 < v3); + BOOST_TEST(v2 <= v3); + BOOST_TEST(v3 > v2); + BOOST_TEST(v3 >= v2); + } + + { + // conversion from ns::point to vector + ns::point p = {5, 3}; + fusion::vector v(p); + v = p; + } + + { + // conversion from ns::point to list + ns::point p = {5, 3}; + fusion::list l(p); + l = p; + } + + { + // assoc stuff + BOOST_MPL_ASSERT((boost::fusion::result_of::has_key)); + BOOST_MPL_ASSERT((boost::fusion::result_of::has_key)); + BOOST_MPL_ASSERT((mpl::not_ >)); + + BOOST_MPL_ASSERT((boost::is_same::type, int>)); + BOOST_MPL_ASSERT((boost::is_same::type, int>)); + + ns::point p = {5, 3}; + + BOOST_TEST(at_key(p) == 5); + BOOST_TEST(at_key(p) == 3); + } + + { + BOOST_MPL_ASSERT((mpl::is_sequence)); + BOOST_MPL_ASSERT((boost::is_same< + boost::fusion::result_of::value_at_c::type + , mpl::front::type>)); + } + + return boost::report_errors(); +} +