2007-04-02 04:34:24 +00:00
|
|
|
/*=============================================================================
|
|
|
|
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 <boost/detail/lightweight_test.hpp>
|
2007-10-24 02:32:28 +00:00
|
|
|
#include <boost/fusion/adapted/struct/adapt_struct.hpp>
|
2007-04-02 04:34:24 +00:00
|
|
|
#include <boost/fusion/sequence/intrinsic/at.hpp>
|
|
|
|
#include <boost/fusion/sequence/intrinsic/size.hpp>
|
|
|
|
#include <boost/fusion/sequence/intrinsic/empty.hpp>
|
|
|
|
#include <boost/fusion/sequence/intrinsic/front.hpp>
|
|
|
|
#include <boost/fusion/sequence/intrinsic/back.hpp>
|
2009-12-15 13:30:02 +00:00
|
|
|
#include <boost/fusion/sequence/intrinsic/value_at.hpp>
|
2007-04-02 04:34:24 +00:00
|
|
|
#include <boost/fusion/sequence/io/out.hpp>
|
2007-10-24 02:32:28 +00:00
|
|
|
#include <boost/fusion/container/vector/vector.hpp>
|
|
|
|
#include <boost/fusion/container/list/list.hpp>
|
2007-11-06 12:13:52 +00:00
|
|
|
#include <boost/fusion/container/generation/make_vector.hpp>
|
2007-10-24 02:32:28 +00:00
|
|
|
#include <boost/fusion/container/vector/convert.hpp>
|
2007-04-02 04:34:24 +00:00
|
|
|
#include <boost/fusion/sequence/comparison/equal_to.hpp>
|
|
|
|
#include <boost/fusion/sequence/comparison/not_equal_to.hpp>
|
|
|
|
#include <boost/fusion/sequence/comparison/less.hpp>
|
|
|
|
#include <boost/fusion/sequence/comparison/less_equal.hpp>
|
|
|
|
#include <boost/fusion/sequence/comparison/greater.hpp>
|
|
|
|
#include <boost/fusion/sequence/comparison/greater_equal.hpp>
|
2009-12-15 13:30:02 +00:00
|
|
|
#include <boost/fusion/mpl.hpp>
|
2007-04-02 04:34:24 +00:00
|
|
|
#include <boost/fusion/support/is_view.hpp>
|
2009-12-15 13:30:02 +00:00
|
|
|
#include <boost/mpl/front.hpp>
|
|
|
|
#include <boost/mpl/is_sequence.hpp>
|
2007-04-02 04:34:24 +00:00
|
|
|
#include <boost/mpl/assert.hpp>
|
2010-07-31 00:17:34 +00:00
|
|
|
#include <boost/static_assert.hpp>
|
2007-04-02 04:34:24 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
|
2015-04-07 18:46:38 +02:00
|
|
|
namespace namespaced_type {
|
|
|
|
typedef int integer;
|
|
|
|
}
|
|
|
|
|
2007-04-02 04:34:24 +00:00
|
|
|
namespace ns
|
|
|
|
{
|
|
|
|
struct point
|
|
|
|
{
|
|
|
|
int x;
|
|
|
|
int y;
|
2015-04-07 18:46:38 +02:00
|
|
|
namespaced_type::integer z;
|
2007-04-02 04:34:24 +00:00
|
|
|
};
|
2010-07-31 00:17:34 +00:00
|
|
|
|
|
|
|
#if !BOOST_WORKAROUND(__GNUC__,<4)
|
|
|
|
struct point_with_private_attributes
|
|
|
|
{
|
|
|
|
friend struct boost::fusion::extension::access;
|
|
|
|
|
|
|
|
private:
|
|
|
|
int x;
|
|
|
|
int y;
|
2014-06-11 01:43:57 +02:00
|
|
|
int z;
|
2010-07-31 00:17:34 +00:00
|
|
|
|
|
|
|
public:
|
2014-06-11 01:43:57 +02:00
|
|
|
point_with_private_attributes(int x, int y, int z):x(x),y(y),z(z)
|
2010-07-31 00:17:34 +00:00
|
|
|
{}
|
|
|
|
};
|
|
|
|
#endif
|
2014-11-12 11:13:06 +08:00
|
|
|
|
|
|
|
struct foo
|
|
|
|
{
|
|
|
|
int x;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct bar
|
|
|
|
{
|
|
|
|
foo foo_;
|
|
|
|
int y;
|
|
|
|
};
|
2015-04-26 21:14:17 +02:00
|
|
|
|
|
|
|
|
|
|
|
// Testing non-constexpr compatible types
|
|
|
|
struct employee {
|
|
|
|
std::string name;
|
|
|
|
std::string nickname;
|
|
|
|
|
|
|
|
employee(std::string name, std::string nickname)
|
|
|
|
: name(name), nickname(nickname)
|
|
|
|
{}
|
|
|
|
};
|
2007-04-02 04:34:24 +00:00
|
|
|
}
|
|
|
|
|
2014-06-11 01:43:57 +02:00
|
|
|
#if BOOST_PP_VARIADICS
|
|
|
|
|
|
|
|
BOOST_FUSION_ADAPT_STRUCT(
|
|
|
|
ns::point,
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
z
|
|
|
|
)
|
|
|
|
|
|
|
|
# if !BOOST_WORKAROUND(__GNUC__,<4)
|
|
|
|
BOOST_FUSION_ADAPT_STRUCT(
|
|
|
|
ns::point_with_private_attributes,
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
z
|
|
|
|
)
|
|
|
|
# endif
|
|
|
|
|
|
|
|
struct s { int m; };
|
|
|
|
BOOST_FUSION_ADAPT_STRUCT(s, m)
|
|
|
|
|
2015-02-02 07:06:52 +01:00
|
|
|
BOOST_FUSION_ADAPT_STRUCT(
|
|
|
|
ns::bar,
|
|
|
|
foo_.x, // test that adapted members can actually be expressions
|
2015-03-31 08:18:46 +02:00
|
|
|
(auto , y)
|
2015-02-02 07:06:52 +01:00
|
|
|
)
|
|
|
|
|
2015-04-26 21:14:17 +02:00
|
|
|
BOOST_FUSION_ADAPT_STRUCT(
|
|
|
|
ns::employee,
|
|
|
|
name,
|
|
|
|
nickname
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2014-06-11 01:43:57 +02:00
|
|
|
#else // BOOST_PP_VARIADICS
|
|
|
|
|
|
|
|
BOOST_FUSION_ADAPT_STRUCT(
|
|
|
|
ns::point,
|
|
|
|
(int, x)
|
2015-04-07 18:46:38 +02:00
|
|
|
(auto, y)
|
|
|
|
(namespaced_type::integer, z)
|
2014-06-11 01:43:57 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
# if !BOOST_WORKAROUND(__GNUC__,<4)
|
|
|
|
BOOST_FUSION_ADAPT_STRUCT(
|
|
|
|
ns::point_with_private_attributes,
|
|
|
|
(int, x)
|
|
|
|
(int, y)
|
2015-03-31 08:18:46 +02:00
|
|
|
(auto, z)
|
2014-06-11 01:43:57 +02:00
|
|
|
)
|
|
|
|
# endif
|
|
|
|
|
|
|
|
struct s { int m; };
|
2015-03-31 08:18:46 +02:00
|
|
|
BOOST_FUSION_ADAPT_STRUCT(s, (auto, m))
|
2007-04-02 04:34:24 +00:00
|
|
|
|
2015-02-02 07:06:52 +01:00
|
|
|
BOOST_FUSION_ADAPT_STRUCT(
|
|
|
|
ns::bar,
|
2015-03-31 08:18:46 +02:00
|
|
|
(auto, foo_.x) // test that adapted members can actually be expressions
|
|
|
|
(BOOST_FUSION_ADAPT_AUTO, y) // Mixing auto & BOOST_FUSION_ADAPT_AUTO
|
|
|
|
// to test backward compatibility
|
2015-02-02 07:06:52 +01:00
|
|
|
)
|
2007-04-02 04:34:24 +00:00
|
|
|
|
2015-04-26 21:14:17 +02:00
|
|
|
BOOST_FUSION_ADAPT_STRUCT(
|
|
|
|
ns::employee,
|
2015-04-29 15:58:41 +02:00
|
|
|
(std::string, name)
|
2015-04-26 21:14:17 +02:00
|
|
|
(BOOST_FUSION_ADAPT_AUTO, nickname)
|
|
|
|
)
|
|
|
|
|
2010-07-31 00:17:34 +00:00
|
|
|
#endif
|
|
|
|
|
2015-05-06 08:21:02 +02:00
|
|
|
struct empty_struct {};
|
2015-05-15 09:09:10 +02:00
|
|
|
BOOST_FUSION_ADAPT_STRUCT(empty_struct,)
|
2015-05-06 08:21:02 +02:00
|
|
|
|
2007-04-02 04:34:24 +00:00
|
|
|
int
|
|
|
|
main()
|
|
|
|
{
|
|
|
|
using namespace boost::fusion;
|
|
|
|
using namespace boost;
|
2014-06-14 11:40:51 +02:00
|
|
|
using ns::point;
|
2007-04-02 04:34:24 +00:00
|
|
|
|
|
|
|
std::cout << tuple_open('[');
|
|
|
|
std::cout << tuple_close(']');
|
|
|
|
std::cout << tuple_delimiter(", ");
|
|
|
|
|
|
|
|
{
|
2014-06-11 01:43:57 +02:00
|
|
|
BOOST_MPL_ASSERT_NOT((traits::is_view<point>));
|
2018-08-03 00:24:10 +03:00
|
|
|
BOOST_STATIC_ASSERT(!traits::is_view<point>::value);
|
2014-06-11 01:43:57 +02:00
|
|
|
point p = {123, 456, 789};
|
2007-04-02 04:34:24 +00:00
|
|
|
|
|
|
|
std::cout << at_c<0>(p) << std::endl;
|
|
|
|
std::cout << at_c<1>(p) << std::endl;
|
2014-06-11 01:43:57 +02:00
|
|
|
std::cout << at_c<2>(p) << std::endl;
|
2007-04-02 04:34:24 +00:00
|
|
|
std::cout << p << std::endl;
|
2014-06-11 01:43:57 +02:00
|
|
|
BOOST_TEST(p == make_vector(123, 456, 789));
|
2007-04-02 04:34:24 +00:00
|
|
|
|
|
|
|
at_c<0>(p) = 6;
|
|
|
|
at_c<1>(p) = 9;
|
2014-06-11 01:43:57 +02:00
|
|
|
at_c<2>(p) = 12;
|
2014-06-04 01:36:15 +02:00
|
|
|
BOOST_TEST(p == make_vector(6, 9, 12));
|
2007-04-02 04:34:24 +00:00
|
|
|
|
2014-06-11 01:43:57 +02:00
|
|
|
BOOST_STATIC_ASSERT(boost::fusion::result_of::size<point>::value == 3);
|
|
|
|
BOOST_STATIC_ASSERT(!boost::fusion::result_of::empty<point>::value);
|
2007-04-02 04:34:24 +00:00
|
|
|
|
|
|
|
BOOST_TEST(front(p) == 6);
|
2014-06-11 01:43:57 +02:00
|
|
|
BOOST_TEST(back(p) == 12);
|
2007-04-02 04:34:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-02-16 01:02:58 +09:00
|
|
|
vector<int, float, int> v1(4, 2.f, 2);
|
2014-06-11 01:43:57 +02:00
|
|
|
point v2 = {5, 3, 3};
|
2016-02-16 01:02:58 +09:00
|
|
|
vector<long, double, int> v3(5, 4., 4);
|
2007-04-02 04:34:24 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2014-06-11 01:43:57 +02:00
|
|
|
// conversion from point to vector
|
|
|
|
point p = {5, 3, 3};
|
|
|
|
vector<int, long, int> v(p);
|
2007-04-02 04:34:24 +00:00
|
|
|
v = p;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2014-06-11 01:43:57 +02:00
|
|
|
// conversion from point to list
|
|
|
|
point p = {5, 3, 3};
|
2014-06-25 00:25:14 +02:00
|
|
|
list<int, long, int> l(p);
|
2007-04-02 04:34:24 +00:00
|
|
|
l = p;
|
|
|
|
}
|
|
|
|
|
2007-12-19 10:33:39 +00:00
|
|
|
{ // begin/end
|
|
|
|
using namespace boost::fusion;
|
|
|
|
using boost::is_same;
|
|
|
|
|
2011-08-17 18:53:56 +00:00
|
|
|
typedef boost::fusion::result_of::begin<s>::type b;
|
|
|
|
typedef boost::fusion::result_of::end<s>::type e;
|
2007-12-19 10:33:39 +00:00
|
|
|
// this fails
|
2011-08-17 18:53:56 +00:00
|
|
|
BOOST_MPL_ASSERT((is_same<boost::fusion::result_of::next<b>::type, e>));
|
2007-12-19 10:33:39 +00:00
|
|
|
}
|
|
|
|
|
2009-12-15 13:30:02 +00:00
|
|
|
{
|
|
|
|
BOOST_MPL_ASSERT((mpl::is_sequence<ns::point>));
|
|
|
|
BOOST_MPL_ASSERT((boost::is_same<
|
2011-08-17 18:53:56 +00:00
|
|
|
boost::fusion::result_of::value_at_c<ns::point,0>::type
|
2009-12-15 13:30:02 +00:00
|
|
|
, mpl::front<ns::point>::type>));
|
|
|
|
}
|
|
|
|
|
2010-07-31 00:17:34 +00:00
|
|
|
#if !BOOST_WORKAROUND(__GNUC__,<4)
|
|
|
|
{
|
2014-06-11 01:43:57 +02:00
|
|
|
ns::point_with_private_attributes p(123, 456, 789);
|
2010-07-31 00:17:34 +00:00
|
|
|
|
|
|
|
std::cout << at_c<0>(p) << std::endl;
|
|
|
|
std::cout << at_c<1>(p) << std::endl;
|
2014-06-11 01:43:57 +02:00
|
|
|
std::cout << at_c<2>(p) << std::endl;
|
2010-07-31 00:17:34 +00:00
|
|
|
std::cout << p << std::endl;
|
2014-06-11 01:43:57 +02:00
|
|
|
BOOST_TEST(p == make_vector(123, 456, 789));
|
2010-07-31 00:17:34 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-11-12 11:13:06 +08:00
|
|
|
{
|
2016-02-16 01:02:58 +09:00
|
|
|
fusion::vector<int, float> v1(4, 2.f);
|
2015-01-21 01:13:19 +09:00
|
|
|
ns::bar v2 = {{5}, 3};
|
2014-11-12 11:13:06 +08:00
|
|
|
BOOST_TEST(v1 < v2);
|
|
|
|
BOOST_TEST(v1 <= v2);
|
|
|
|
BOOST_TEST(v2 > v1);
|
|
|
|
BOOST_TEST(v2 >= v1);
|
|
|
|
}
|
|
|
|
|
2015-04-26 21:14:17 +02:00
|
|
|
{
|
|
|
|
ns::employee emp("John Doe", "jdoe");
|
|
|
|
std::cout << at_c<0>(emp) << std::endl;
|
|
|
|
std::cout << at_c<1>(emp) << std::endl;
|
|
|
|
|
|
|
|
fusion::vector<std::string, std::string> v1("John Doe", "jdoe");
|
|
|
|
BOOST_TEST(emp == v1);
|
|
|
|
}
|
|
|
|
|
2007-04-02 04:34:24 +00:00
|
|
|
return boost::report_errors();
|
|
|
|
}
|
|
|
|
|