From 0d7ad9f6e28443ea54a76ea20af8474472345f47 Mon Sep 17 00:00:00 2001 From: "Damien Buhl (alias daminetreg)" Date: Sun, 26 Jan 2014 17:31:01 +0100 Subject: [PATCH] FEATURE: BOOST_FUSION_AUTO_ADAPT_STRUCT_NAMED that does not require specifying the types of the fields. --- include/boost/fusion/adapted/struct.hpp | 2 + .../struct/auto_adapt_struct_named.hpp | 25 ++++ test/Jamfile | 1 + test/sequence/auto_adapt_struct_named.cpp | 137 ++++++++++++++++++ 4 files changed, 165 insertions(+) create mode 100644 include/boost/fusion/adapted/struct/auto_adapt_struct_named.hpp create mode 100644 test/sequence/auto_adapt_struct_named.cpp diff --git a/include/boost/fusion/adapted/struct.hpp b/include/boost/fusion/adapted/struct.hpp index 3f159038..5a45b596 100644 --- a/include/boost/fusion/adapted/struct.hpp +++ b/include/boost/fusion/adapted/struct.hpp @@ -14,7 +14,9 @@ #include #include #include +#include #include +#include #include #include #include diff --git a/include/boost/fusion/adapted/struct/auto_adapt_struct_named.hpp b/include/boost/fusion/adapted/struct/auto_adapt_struct_named.hpp new file mode 100644 index 00000000..a09662b7 --- /dev/null +++ b/include/boost/fusion/adapted/struct/auto_adapt_struct_named.hpp @@ -0,0 +1,25 @@ +/*============================================================================= + Copyright (c) 2001-2007 Joel de Guzman + Copyright (c) 2009-2010 Hartmut Kaiser + Copyright (c) 2010-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_STRUCT_NAMED_HPP +#define BOOST_FUSION_ADAPTED_STRUCT_AUTO_ADAPT_STRUCT_NAMED_HPP + +#include +#include + +#define BOOST_FUSION_AUTO_ADAPT_STRUCT_NAMED(WRAPPED_TYPE, NAME, ATTRIBUTES) \ + BOOST_FUSION_ADAPT_STRUCT_NAMED( \ + WRAPPED_TYPE,NAME, \ + BOOST_PP_SEQ_FOR_EACH( \ + BOOST_FUSION_AUTO_ADAPT_STRUCT_FILLER, WRAPPED_TYPE, ATTRIBUTES) \ + ) + +#endif diff --git a/test/Jamfile b/test/Jamfile index 1e58b054..c5bfdf6a 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -140,6 +140,7 @@ project [ run sequence/adapt_assoc_tpl_adt.cpp : : : : ] [ run sequence/adapt_assoc_tpl_struct.cpp : : : : ] [ run sequence/adapt_struct_named.cpp : : : : ] + [ run sequence/auto_adapt_struct_named.cpp : : : : ] [ run sequence/adapt_struct.cpp : : : : ] [ run sequence/auto_adapt_struct.cpp : : : : ] [ run sequence/adapt_tpl_adt.cpp : : : : ] diff --git a/test/sequence/auto_adapt_struct_named.cpp b/test/sequence/auto_adapt_struct_named.cpp new file mode 100644 index 00000000..dfa10006 --- /dev/null +++ b/test/sequence/auto_adapt_struct_named.cpp @@ -0,0 +1,137 @@ +/*============================================================================= + Copyright (c) 2001-2007 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 +#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 point + { + int x; + int y; + }; +} + +// this creates a fusion view: boost::fusion::adapted::point +BOOST_FUSION_AUTO_ADAPT_STRUCT_NAMED( + ns::point, point, + (x) + (y) +) + +// this creates a fusion view: ns1::s1 +struct s { int m; }; +BOOST_FUSION_ADAPT_STRUCT_NAMED_NS(s, (ns1), s1, (int, m)) + +int +main() +{ + using namespace boost::fusion; + using namespace boost; + + std::cout << tuple_open('['); + std::cout << tuple_close(']'); + std::cout << tuple_delimiter(", "); + + { + BOOST_MPL_ASSERT((traits::is_view)); + ns::point basep = {123, 456}; + adapted::point p(basep); + + 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 p = {5, 3}; + adapted::point v2(p); + + 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 adapted::point to vector + ns::point basep = {5, 3}; + adapted::point p(basep); + fusion::vector v(p); + v = p; + } + + { + // conversion from adapted::point to list + ns::point basep = {5, 3}; + adapted::point p(basep); + fusion::list l(p); + l = p; + } + + { // begin/end + using namespace boost::fusion; + using boost::is_same; + + typedef boost::fusion::result_of::begin::type b; + typedef boost::fusion::result_of::end::type e; + // this fails + BOOST_MPL_ASSERT((is_same::type, e>)); + } + + + { + 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(); +} +