1// Boost.TypeErasure library
2//
3// Copyright 2012 Steven Watanabe
4//
5// Distributed under the Boost Software License Version 1.0. (See
6// accompanying file LICENSE_1_0.txt or copy at
7// http://www.boost.org/LICENSE_1_0.txt)
8//
9// $Id$
10
11#include <boost/type_erasure/any.hpp>
12#include <boost/type_erasure/builtin.hpp>
13#include <boost/type_erasure/free.hpp>
14#include <boost/mpl/vector.hpp>
15
16#define BOOST_TEST_MAIN
17#include <boost/test/unit_test.hpp>
18
19using namespace boost::type_erasure;
20
21struct model {
22 explicit model(int v) : val(v) {}
23 int val;
24};
25
26int f1(model& m) { return m.val; }
27int f1(model& m, int i) { return m.val + i; }
28
29BOOST_TYPE_ERASURE_FREE((global_has_f1_1), f1, 1)
30
31BOOST_AUTO_TEST_CASE(test_global_has_f1_1) {
32 typedef ::boost::mpl::vector<
33 global_has_f1_1<int(_self&)>,
34 copy_constructible<> > concept_type;
35 model m(10);
36 any<concept_type> x(m);
37 BOOST_CHECK_EQUAL(f1(x), 10);
38}
39
40BOOST_TYPE_ERASURE_FREE((ns1)(ns2)(ns_has_f1_1), f1, 1)
41
42BOOST_AUTO_TEST_CASE(test_ns_has_f1_1) {
43 typedef ::boost::mpl::vector<
44 ns1::ns2::ns_has_f1_1<int(_self&)>,
45 copy_constructible<> > concept_type;
46 model m(10);
47 any<concept_type> x(m);
48 BOOST_CHECK_EQUAL(f1(x), 10);
49}
50
51struct model_const {
52 explicit model_const(int v) : val(v) {}
53 int val;
54};
55
56int f1(const model_const& m) { return m.val; }
57int f1(const model_const& m, int i) { return m.val + i; }
58
59BOOST_AUTO_TEST_CASE(test_global_has_f1_1_const) {
60 typedef ::boost::mpl::vector<
61 ns1::ns2::ns_has_f1_1<int(const _self&)>,
62 copy_constructible<> > concept_type;
63 model_const m(10);
64 const any<concept_type> x(m);
65 BOOST_CHECK_EQUAL(f1(x), 10);
66}
67
68BOOST_AUTO_TEST_CASE(test_global_has_f1_1_void) {
69 typedef ::boost::mpl::vector<
70 global_has_f1_1<void(_self&)>,
71 copy_constructible<> > concept_type;
72 model m(10);
73 any<concept_type> x(m);
74 f1(t&: x);
75}
76
77BOOST_TYPE_ERASURE_FREE((global_has_f1_2), f1, 2)
78
79BOOST_AUTO_TEST_CASE(test_global_has_f1_overload) {
80 typedef ::boost::mpl::vector<
81 global_has_f1_1<int(_self&)>,
82 global_has_f1_2<int(_self&, int)>,
83 copy_constructible<> > concept_type;
84 model m(10);
85 any<concept_type> x(m);
86 BOOST_CHECK_EQUAL(f1(x), 10);
87 BOOST_CHECK_EQUAL(f1(x, 5), 15);
88}
89
90BOOST_AUTO_TEST_CASE(test_global_has_f1_overload_const) {
91 typedef ::boost::mpl::vector<
92 global_has_f1_1<int(const _self&)>,
93 global_has_f1_2<int(const _self&, int)>,
94 copy_constructible<> > concept_type;
95 model_const m(10);
96 const any<concept_type> x(m);
97 BOOST_CHECK_EQUAL(f1(x), 10);
98 BOOST_CHECK_EQUAL(f1(x, 5), 15);
99}
100
101#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
102
103BOOST_AUTO_TEST_CASE(test_global_has_f1_rv) {
104 typedef ::boost::mpl::vector<
105 global_has_f1_2<int(_self&&, int&&)>,
106 copy_constructible<> > concept_type;
107 model_const m(10);
108 any<concept_type> x(m);
109 BOOST_CHECK_EQUAL(f1(std::move(x), 5), 15);
110}
111
112#endif
113
114#if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES) && \
115 !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && \
116 !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && \
117 !defined(BOOST_NO_CXX11_DECLTYPE) && \
118 !BOOST_WORKAROUND(BOOST_MSVC, == 1800)
119
120namespace ns3 {
121BOOST_TYPE_ERASURE_FREE(f1)
122}
123
124BOOST_AUTO_TEST_CASE(test_simple_free_1) {
125 typedef ::boost::mpl::vector<
126 ns3::has_f1<int(_self&)>,
127 copy_constructible<> > concept_type;
128 model m(10);
129 any<concept_type> x(m);
130 BOOST_CHECK_EQUAL(f1(x), 10);
131}
132
133namespace ns3 {
134BOOST_TYPE_ERASURE_FREE(has_f1_ex, f1)
135}
136
137BOOST_AUTO_TEST_CASE(test_named_free_1) {
138 typedef ::boost::mpl::vector<
139 ns3::has_f1_ex<int(_self&)>,
140 copy_constructible<> > concept_type;
141 model m(10);
142 any<concept_type> x(m);
143 BOOST_CHECK_EQUAL(f1(x), 10);
144}
145
146#endif
147

source code of boost/libs/type_erasure/test/test_free.cpp