1 // Boost.TypeErasure library
3 // Copyright 2012 Steven Watanabe
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)
11 #include <boost/type_erasure/any.hpp>
12 #include <boost/type_erasure/builtin.hpp>
13 #include <boost/type_erasure/member.hpp>
14 #include <boost/mpl/vector.hpp>
16 #define BOOST_TEST_MAIN
17 #include <boost/test/unit_test.hpp>
19 using namespace boost::type_erasure
;
21 BOOST_TYPE_ERASURE_MEMBER((ns
)(ns2
)(has_fun
), fun
, 0)
24 explicit model(int v
) : val(v
) {}
25 int f1() { return val
; }
26 int f1(int i
) { return val
+ i
; }
30 BOOST_TYPE_ERASURE_MEMBER((global_has_f1_0
), f1
, 0)
32 BOOST_AUTO_TEST_CASE(test_global_has_f1_0
) {
33 typedef ::boost::mpl::vector
<
34 global_has_f1_0
<int()>,
35 copy_constructible
<> > concept_type
;
37 any
<concept_type
> x(m
);
38 BOOST_CHECK_EQUAL(x
.f1(), 10);
41 BOOST_AUTO_TEST_CASE(test_global_has_f1_0_ref
) {
42 typedef ::boost::mpl::vector
<
43 global_has_f1_0
<int()>,
44 copy_constructible
<> > concept_type
;
46 const any
<concept_type
, _self
&> x(m
);
47 BOOST_CHECK_EQUAL(x
.f1(), 10);
50 BOOST_TYPE_ERASURE_MEMBER((ns1
)(ns2
)(ns_has_f1_0
), f1
, 0)
52 BOOST_AUTO_TEST_CASE(test_ns_has_f1_0
) {
53 typedef ::boost::mpl::vector
<
54 ns1::ns2::ns_has_f1_0
<int()>,
55 copy_constructible
<> > concept_type
;
57 any
<concept_type
> x(m
);
58 BOOST_CHECK_EQUAL(x
.f1(), 10);
62 explicit model_const(int v
) : val(v
) {}
63 int f1() const { return val
; }
64 int f1(int i
) const { return val
+ i
; }
68 BOOST_AUTO_TEST_CASE(test_global_has_f1_0_const
) {
69 typedef ::boost::mpl::vector
<
70 ns1::ns2::ns_has_f1_0
<int(), const _self
>,
71 copy_constructible
<> > concept_type
;
73 any
<concept_type
> x(m
);
74 BOOST_CHECK_EQUAL(x
.f1(), 10);
77 BOOST_AUTO_TEST_CASE(test_global_has_f1_0_const_ref
) {
78 typedef ::boost::mpl::vector
<
79 ns1::ns2::ns_has_f1_0
<int(), const _self
>,
80 copy_constructible
<> > concept_type
;
82 const any
<concept_type
, _self
&> x(m
);
83 BOOST_CHECK_EQUAL(x
.f1(), 10);
86 BOOST_AUTO_TEST_CASE(test_global_has_f1_0_const_cref
) {
87 typedef ::boost::mpl::vector
<
88 ns1::ns2::ns_has_f1_0
<int(), const _self
>,
89 copy_constructible
<> > concept_type
;
91 const any
<concept_type
, const _self
&> x(m
);
92 BOOST_CHECK_EQUAL(x
.f1(), 10);
95 BOOST_AUTO_TEST_CASE(test_global_has_f1_0_void
) {
96 typedef ::boost::mpl::vector
<
97 global_has_f1_0
<void()>,
98 copy_constructible
<> > concept_type
;
100 any
<concept_type
> x(m
);
104 BOOST_TYPE_ERASURE_MEMBER((global_has_f1_1
), f1
, 1)
106 BOOST_AUTO_TEST_CASE(test_global_has_f1_overload
) {
107 typedef ::boost::mpl::vector
<
108 global_has_f1_0
<int()>,
109 global_has_f1_1
<int(int)>,
110 copy_constructible
<> > concept_type
;
112 any
<concept_type
> x(m
);
113 BOOST_CHECK_EQUAL(x
.f1(), 10);
114 BOOST_CHECK_EQUAL(x
.f1(5), 15);
117 BOOST_AUTO_TEST_CASE(test_global_has_f1_overload_const
) {
118 typedef ::boost::mpl::vector
<
119 global_has_f1_0
<int(), const _self
>,
120 global_has_f1_1
<int(int), const _self
>,
121 copy_constructible
<> > concept_type
;
123 any
<concept_type
> x(m
);
124 BOOST_CHECK_EQUAL(x
.f1(), 10);
125 BOOST_CHECK_EQUAL(x
.f1(5), 15);
128 struct model_overload_const_non_const
{
129 int f1() { return 1; }
130 int f1() const { return 2; }
133 BOOST_AUTO_TEST_CASE(test_global_has_f1_overload_const_non_const
) {
134 typedef ::boost::mpl::vector
<
135 global_has_f1_0
<int(), _self
>,
136 global_has_f1_0
<int(), const _self
>,
137 copy_constructible
<> > concept_type
;
138 model_overload_const_non_const m
;
139 any
<concept_type
> x1(m
);
140 BOOST_CHECK_EQUAL(x1
.f1(), 1);
141 const any
<concept_type
> x2(m
);
142 BOOST_CHECK_EQUAL(x2
.f1(), 2);
145 BOOST_AUTO_TEST_CASE(test_global_has_f1_overload_const_non_const_ref
) {
146 typedef ::boost::mpl::vector
<
147 global_has_f1_0
<int(), const _self
>, // FIXME: This is order sensitive
148 global_has_f1_0
<int(), _self
>,
149 copy_constructible
<> > concept_type
;
150 model_overload_const_non_const m
;
151 any
<concept_type
, _self
&> x1(m
);
152 BOOST_CHECK_EQUAL(x1
.f1(), 1);
153 const any
<concept_type
, _self
&> x2(m
);
154 BOOST_CHECK_EQUAL(x2
.f1(), 1);
157 BOOST_AUTO_TEST_CASE(test_global_has_f1_overload_const_non_const_cref
) {
158 typedef ::boost::mpl::vector
<
159 global_has_f1_0
<int(), _self
>,
160 global_has_f1_0
<int(), const _self
>,
161 copy_constructible
<> > concept_type
;
162 model_overload_const_non_const m
;
163 any
<concept_type
, const _self
&> x1(m
);
164 BOOST_CHECK_EQUAL(x1
.f1(), 2);
165 const any
<concept_type
, const _self
&> x2(m
);
166 BOOST_CHECK_EQUAL(x2
.f1(), 2);
169 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
171 BOOST_AUTO_TEST_CASE(test_global_has_f1_rv
) {
172 typedef ::boost::mpl::vector
<
173 global_has_f1_1
<int(int&&)>,
174 copy_constructible
<> > concept_type
;
176 any
<concept_type
> x(m
);
177 BOOST_CHECK_EQUAL(x
.f1(5), 15);
180 BOOST_AUTO_TEST_CASE(test_global_has_f1_rv_const
) {
181 typedef ::boost::mpl::vector
<
182 global_has_f1_1
<int(int&&), const _self
>,
183 copy_constructible
<> > concept_type
;
185 const any
<concept_type
> x(m
);
186 BOOST_CHECK_EQUAL(x
.f1(5), 15);
192 #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES) && \
193 !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && \
194 !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && \
195 !defined(BOOST_NO_CXX11_DECLTYPE) && \
196 !BOOST_WORKAROUND(BOOST_MSVC, == 1800)
199 BOOST_TYPE_ERASURE_MEMBER(f1
);
202 BOOST_AUTO_TEST_CASE(test_simple_member_0
) {
203 typedef ::boost::mpl::vector
<
205 copy_constructible
<> > concept_type
;
207 any
<concept_type
> x(m
);
208 BOOST_CHECK_EQUAL(x
.f1(), 10);
211 BOOST_AUTO_TEST_CASE(test_simple_member_1
) {
212 typedef ::boost::mpl::vector
<
213 ns3::has_f1
<int(int)>,
214 copy_constructible
<> > concept_type
;
216 any
<concept_type
> x(m
);
217 BOOST_CHECK_EQUAL(x
.f1(5), 15);
221 BOOST_TYPE_ERASURE_MEMBER(has_f1_ex
, f1
);
224 BOOST_AUTO_TEST_CASE(test_named_member_1
) {
225 typedef ::boost::mpl::vector
<
226 ns3::has_f1_ex
<int(int)>,
227 copy_constructible
<> > concept_type
;
229 any
<concept_type
> x(m
);
230 BOOST_CHECK_EQUAL(x
.f1(5), 15);
233 BOOST_AUTO_TEST_CASE(test_named_member_1_const
) {
234 typedef ::boost::mpl::vector
<
235 ns3::has_f1_ex
<int(int) const>,
236 copy_constructible
<> > concept_type
;
238 const any
<concept_type
> x(m
);
239 BOOST_CHECK_EQUAL(x
.f1(5), 15);