constrained_ops.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. #include <boost/proto/proto.hpp>
  10. #include <boost/test/unit_test.hpp>
  11. using namespace boost;
  12. typedef proto::terminal<int>::type term;
  13. struct equation;
  14. struct addition:
  15. proto::or_
  16. <
  17. proto::terminal<proto::_>,
  18. proto::plus<addition, addition>
  19. >
  20. {};
  21. struct equation:
  22. proto::or_
  23. <
  24. proto::equal_to<addition, addition>
  25. >
  26. {};
  27. template<class Expr>
  28. struct extension;
  29. struct my_domain:
  30. proto::domain
  31. <
  32. proto::pod_generator<extension>,
  33. equation,
  34. proto::default_domain
  35. >
  36. {};
  37. template<class Expr>
  38. struct lhs_extension;
  39. struct my_lhs_domain:
  40. proto::domain
  41. <
  42. proto::pod_generator<lhs_extension>,
  43. addition,
  44. my_domain
  45. >
  46. {};
  47. template<class Expr>
  48. struct rhs_extension;
  49. struct my_rhs_domain:
  50. proto::domain
  51. <
  52. proto::pod_generator<rhs_extension>,
  53. addition,
  54. my_domain
  55. >
  56. {};
  57. template<class Expr>
  58. struct extension
  59. {
  60. BOOST_PROTO_BASIC_EXTENDS(
  61. Expr
  62. , extension<Expr>
  63. , my_domain
  64. )
  65. void test() const
  66. {}
  67. };
  68. template<class Expr>
  69. struct lhs_extension
  70. {
  71. BOOST_PROTO_BASIC_EXTENDS(
  72. Expr
  73. , lhs_extension<Expr>
  74. , my_lhs_domain
  75. )
  76. };
  77. template<class Expr>
  78. struct rhs_extension
  79. {
  80. BOOST_PROTO_BASIC_EXTENDS(
  81. Expr
  82. , rhs_extension<Expr>
  83. , my_rhs_domain
  84. )
  85. };
  86. void test_constrained_ops()
  87. {
  88. lhs_extension<term> const i = {};
  89. rhs_extension<term> const j = {};
  90. proto::assert_matches_not<equation>(i); // false
  91. proto::assert_matches_not<equation>(j); // false
  92. proto::assert_matches_not<equation>(i + i); // false
  93. proto::assert_matches_not<equation>(j + j); // false
  94. #if 0
  95. proto::assert_matches_not<equation>(i + j); // compile error (by design)
  96. proto::assert_matches_not<equation>(j + i); // compile error (by design)
  97. #endif
  98. proto::assert_matches<equation>(i == j); // true
  99. proto::assert_matches<equation>(i == j + j); // true
  100. proto::assert_matches<equation>(i + i == j); // true
  101. proto::assert_matches<equation>(i + i == j + j); // true
  102. }
  103. using namespace boost::unit_test;
  104. ///////////////////////////////////////////////////////////////////////////////
  105. // init_unit_test_suite
  106. //
  107. test_suite* init_unit_test_suite( int argc, char* argv[] )
  108. {
  109. test_suite *test = BOOST_TEST_SUITE("test constrained EDSLs");
  110. test->add(BOOST_TEST_CASE(&test_constrained_ops));
  111. return test;
  112. }