1///////////////////////////////////////////////////////////////////////////////
2// constrained_ops.cpp
3//
4// Copyright 2010 Thomas Heller
5// Copyright 2011 Eric Niebler
6//
7// Distributed under the Boost Software License, Version 1.0. (See accompanying
8// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9
10#include <boost/proto/proto.hpp>
11#include <boost/test/unit_test.hpp>
12
13using namespace boost;
14
15typedef proto::terminal<int>::type term;
16
17struct equation;
18
19struct addition:
20 proto::or_
21 <
22 proto::terminal<proto::_>,
23 proto::plus<addition, addition>
24 >
25{};
26
27struct equation:
28 proto::or_
29 <
30 proto::equal_to<addition, addition>
31 >
32{};
33
34template<class Expr>
35struct extension;
36
37struct my_domain:
38 proto::domain
39 <
40 proto::pod_generator<extension>,
41 equation,
42 proto::default_domain
43 >
44{};
45
46template<class Expr>
47struct lhs_extension;
48
49struct my_lhs_domain:
50 proto::domain
51 <
52 proto::pod_generator<lhs_extension>,
53 addition,
54 my_domain
55 >
56{};
57
58template<class Expr>
59struct rhs_extension;
60
61struct my_rhs_domain:
62 proto::domain
63 <
64 proto::pod_generator<rhs_extension>,
65 addition,
66 my_domain
67 >
68{};
69
70template<class Expr>
71struct extension
72{
73 BOOST_PROTO_BASIC_EXTENDS(
74 Expr
75 , extension<Expr>
76 , my_domain
77 )
78
79 void test() const
80 {}
81};
82
83template<class Expr>
84struct lhs_extension
85{
86 BOOST_PROTO_BASIC_EXTENDS(
87 Expr
88 , lhs_extension<Expr>
89 , my_lhs_domain
90 )
91};
92
93template<class Expr>
94struct rhs_extension
95{
96 BOOST_PROTO_BASIC_EXTENDS(
97 Expr
98 , rhs_extension<Expr>
99 , my_rhs_domain
100 )
101};
102
103void test_constrained_ops()
104{
105 lhs_extension<term> const i = {};
106 rhs_extension<term> const j = {};
107
108 proto::assert_matches_not<equation>(i); // false
109 proto::assert_matches_not<equation>(j); // false
110 proto::assert_matches_not<equation>(i + i); // false
111 proto::assert_matches_not<equation>(j + j); // false
112#if 0
113 proto::assert_matches_not<equation>(i + j); // compile error (by design)
114 proto::assert_matches_not<equation>(j + i); // compile error (by design)
115#endif
116 proto::assert_matches<equation>(i == j); // true
117 proto::assert_matches<equation>(i == j + j); // true
118 proto::assert_matches<equation>(i + i == j); // true
119 proto::assert_matches<equation>(i + i == j + j); // true
120}
121
122using namespace boost::unit_test;
123///////////////////////////////////////////////////////////////////////////////
124// init_unit_test_suite
125//
126test_suite* init_unit_test_suite( int argc, char* argv[] )
127{
128 test_suite *test = BOOST_TEST_SUITE("test constrained EDSLs");
129 test->add(BOOST_TEST_CASE(&test_constrained_ops));
130 return test;
131}
132

source code of boost/libs/proto/test/constrained_ops.cpp