adds type deduction support for BOOST_FUSION_ADAPT_ASSOC_ADT.

This commit is contained in:
Damien Buhl (alias daminetreg)
2014-10-12 14:51:04 +02:00
parent 103b02fda2
commit 644d72ccfe
3 changed files with 114 additions and 25 deletions

View File

@ -2,6 +2,7 @@
Copyright (c) 2001-2009 Joel de Guzman
Copyright (c) 2007 Dan Marsden
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)
@ -35,25 +36,28 @@
#include <boost/fusion/adapted/struct/detail/value_of_data_impl.hpp>
#include <boost/fusion/adapted/adt/detail/extension.hpp>
#include <boost/fusion/adapted/adt/detail/adapt_base.hpp>
#define BOOST_FUSION_ADAPT_ASSOC_ADT_FILLER_0(A, B, C, D, E)\
((A, B, C, D, E)) BOOST_FUSION_ADAPT_ASSOC_ADT_FILLER_1
#define BOOST_FUSION_ADAPT_ASSOC_ADT_FILLER_1(A, B, C, D, E)\
((A, B, C, D, E)) BOOST_FUSION_ADAPT_ASSOC_ADT_FILLER_0
#define BOOST_FUSION_ADAPT_ASSOC_ADT_FILLER_0_END
#define BOOST_FUSION_ADAPT_ASSOC_ADT_FILLER_1_END
#include <boost/fusion/adapted/adt/detail/adapt_base_assoc_attr_filler.hpp>
#define BOOST_FUSION_ADAPT_ASSOC_ADT_C( \
TEMPLATE_PARAMS_SEQ, NAME_SEQ, IS_VIEW, I, ATTRIBUTE) \
\
BOOST_FUSION_ADAPT_ADT_C_BASE(TEMPLATE_PARAMS_SEQ,NAME_SEQ,I,ATTRIBUTE,5) \
BOOST_FUSION_ADAPT_ADT_C_BASE( \
TEMPLATE_PARAMS_SEQ, \
NAME_SEQ, \
I, \
BOOST_FUSION_ADAPT_ADT_WRAPPEDATTR(ATTRIBUTE), \
BOOST_FUSION_ADAPT_ADT_WRAPPEDATTR_SIZE(ATTRIBUTE), \
BOOST_PP_IF( \
BOOST_PP_LESS( \
BOOST_FUSION_ADAPT_ADT_WRAPPEDATTR_SIZE(ATTRIBUTE), 5) \
, 1, 0)) \
\
template< \
BOOST_FUSION_ADAPT_STRUCT_UNPACK_TEMPLATE_PARAMS(TEMPLATE_PARAMS_SEQ) \
> \
struct struct_assoc_key<BOOST_FUSION_ADAPT_STRUCT_UNPACK_NAME(NAME_SEQ), I> \
{ \
typedef BOOST_PP_TUPLE_ELEM(5, 4, ATTRIBUTE) type; \
typedef BOOST_FUSION_ADAPT_ASSOC_ADT_WRAPPEDATTR_GET_KEY(ATTRIBUTE) type;\
};
#define BOOST_FUSION_ADAPT_ASSOC_TPL_ADT( \

View File

@ -0,0 +1,61 @@
/*=============================================================================
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_ADAPTER_ADT_DETAIL_ADAPT_BASE_ASSOC_ATTR_FILLER_HPP
#define BOOST_FUSION_ADAPTER_ADT_DETAIL_ADAPT_BASE_ASSOC_ATTR_FILLER_HPP
#include <boost/config.hpp>
#include <boost/fusion/adapted/adt/detail/adapt_base_attr_filler.hpp>
#include <boost/preprocessor/control/if.hpp>
#include <boost/preprocessor/variadic/size.hpp>
#include <boost/preprocessor/empty.hpp>
#include <boost/preprocessor/facilities/is_empty.hpp>
#if BOOST_PP_VARIADICS
#define BOOST_FUSION_ADAPT_ASSOC_ADT_FILLER_0(...) \
BOOST_FUSION_ADAPT_ASSOC_ADT_WRAP_ATTR(__VA_ARGS__) \
BOOST_FUSION_ADAPT_ASSOC_ADT_FILLER_1
#define BOOST_FUSION_ADAPT_ASSOC_ADT_FILLER_1(...) \
BOOST_FUSION_ADAPT_ASSOC_ADT_WRAP_ATTR(__VA_ARGS__) \
BOOST_FUSION_ADAPT_ASSOC_ADT_FILLER_0
#define BOOST_FUSION_ADAPT_ASSOC_ADT_WRAP_ATTR(...) \
((BOOST_PP_VARIADIC_SIZE(__VA_ARGS__), (__VA_ARGS__)))
#else // BOOST_PP_VARIADICS
#define BOOST_FUSION_ADAPT_ASSOC_ADT_FILLER_0(A, B, C, D, E) \
BOOST_FUSION_ADAPT_ASSOC_ADT_WRAP_ATTR(A, B, C, D, E) \
BOOST_FUSION_ADAPT_ASSOC_ADT_FILLER_1
#define BOOST_FUSION_ADAPT_ASSOC_ADT_FILLER_1(A, B, C, D, E) \
BOOST_FUSION_ADAPT_ASSOC_ADT_WRAP_ATTR(A, B, C, D, E) \
BOOST_FUSION_ADAPT_ASSOC_ADT_FILLER_0
#define BOOST_FUSION_ADAPT_ASSOC_ADT_WRAP_ATTR(A, B, C, D, E) \
BOOST_PP_IF(BOOST_PP_IS_EMPTY(A), \
((3, (C,D,E))), \
((5, (A,B,C,D,E))) \
)
#endif // BOOST_PP_VARIADICS
#define BOOST_FUSION_ADAPT_ASSOC_ADT_FILLER_0_END
#define BOOST_FUSION_ADAPT_ASSOC_ADT_FILLER_1_END
#define BOOST_FUSION_ADAPT_ASSOC_ADT_WRAPPEDATTR_GET_KEY(ATTRIBUTE) \
BOOST_PP_TUPLE_ELEM( \
BOOST_FUSION_ADAPT_ADT_WRAPPEDATTR_SIZE(ATTRIBUTE), \
BOOST_PP_SUB(BOOST_FUSION_ADAPT_ADT_WRAPPEDATTR_SIZE(ATTRIBUTE), 1), \
BOOST_FUSION_ADAPT_ADT_WRAPPEDATTR(ATTRIBUTE))
#endif

View File

@ -26,31 +26,50 @@ namespace ns
struct y_member;
struct z_member;
struct unavailable_member;
class point
{
public:
point() : x(0), y(0) {}
point(int in_x, int in_y) : x(in_x), y(in_y) {}
point() : x(0), y(0), z(0) {}
point(int in_x, int in_y, int in_z) : x(in_x), y(in_y), z(in_z) {}
int get_x() const { return x; }
int get_y() const { return y; }
int get_z() const { return z; }
void set_x(int x_) { x = x_; }
void set_y(int y_) { y = y_; }
void set_z(int z_) { z = z_; }
private:
int x;
int y;
int z;
};
}
#if BOOST_PP_VARIADICS
BOOST_FUSION_ADAPT_ASSOC_ADT(
ns::point,
(int, int, obj.get_x(), obj.set_x(val), ns::x_member)
(int, int, obj.get_y(), obj.set_y(val), ns::y_member)
(obj.get_z(), obj.set_z(val), ns::z_member)
)
#else // BOOST_PP_VARIADICS
BOOST_FUSION_ADAPT_ASSOC_ADT(
ns::point,
(int, int, obj.get_x(), obj.set_x(val), ns::x_member)
(int, int, obj.get_y(), obj.set_y(val), ns::y_member)
(BOOST_FUSION_ADAPT_AUTO, BOOST_FUSION_ADAPT_AUTO, obj.get_z(), obj.set_z(val), ns::z_member)
)
#endif
int
main()
{
@ -62,28 +81,30 @@ main()
{
BOOST_MPL_ASSERT_NOT((traits::is_view<ns::point>));
ns::point p(123, 456);
ns::point p(123, 456, 789);
std::cout << at_c<0>(p) << std::endl;
std::cout << at_c<1>(p) << std::endl;
std::cout << at_c<2>(p) << std::endl;
std::cout << p << std::endl;
BOOST_TEST(p == make_vector(123, 456));
BOOST_TEST(p == make_vector(123, 456, 789));
at_c<0>(p) = 6;
at_c<1>(p) = 9;
BOOST_TEST(p == make_vector(6, 9));
at_c<2>(p) = 12;
BOOST_TEST(p == make_vector(6, 9, 12));
BOOST_STATIC_ASSERT(boost::fusion::result_of::size<ns::point>::value == 2);
BOOST_STATIC_ASSERT(boost::fusion::result_of::size<ns::point>::value == 3);
BOOST_STATIC_ASSERT(!boost::fusion::result_of::empty<ns::point>::value);
BOOST_TEST(front(p) == 6);
BOOST_TEST(back(p) == 9);
BOOST_TEST(back(p) == 12);
}
{
boost::fusion::vector<int, float> v1(4, 2);
ns::point v2(5, 3);
boost::fusion::vector<long, double> v3(5, 4);
boost::fusion::vector<int, float, int> v1(4, 2, 2);
ns::point v2(5, 3, 3);
boost::fusion::vector<long, double, int> v3(5, 4, 4);
BOOST_TEST(v1 < v2);
BOOST_TEST(v1 <= v2);
BOOST_TEST(v2 > v1);
@ -96,15 +117,15 @@ main()
{
// conversion from ns::point to vector
ns::point p(5, 3);
boost::fusion::vector<int, long> v(p);
ns::point p(5, 3, 3);
boost::fusion::vector<int, long, int> v(p);
v = p;
}
{
// conversion from ns::point to list
ns::point p(5, 3);
boost::fusion::list<int, long> l(p);
ns::point p(5, 3, 3);
boost::fusion::list<int, long, int> l(p);
l = p;
}
@ -119,15 +140,18 @@ main()
// assoc stuff
BOOST_MPL_ASSERT((boost::fusion::result_of::has_key<ns::point, ns::x_member>));
BOOST_MPL_ASSERT((boost::fusion::result_of::has_key<ns::point, ns::y_member>));
BOOST_MPL_ASSERT((boost::mpl::not_<boost::fusion::result_of::has_key<ns::point, ns::z_member> >));
BOOST_MPL_ASSERT((boost::fusion::result_of::has_key<ns::point, ns::z_member>));
BOOST_MPL_ASSERT((boost::mpl::not_<boost::fusion::result_of::has_key<ns::point, ns::unavailable_member> >));
BOOST_MPL_ASSERT((boost::is_same<boost::fusion::result_of::value_at_key<ns::point, ns::x_member>::type, int>));
BOOST_MPL_ASSERT((boost::is_same<boost::fusion::result_of::value_at_key<ns::point, ns::y_member>::type, int>));
ns::point p(5, 3);
ns::point p(5, 3, 1);
BOOST_TEST(at_key<ns::x_member>(p) == 5);
BOOST_TEST(at_key<ns::y_member>(p) == 3);
BOOST_TEST(at_key<ns::z_member>(p) == 1);
}
return boost::report_errors();