| 1 | // Copyright (C) 2016-2018 T. Zachary Laine |
| 2 | // |
| 3 | // Distributed under the Boost Software License, Version 1.0. (See |
| 4 | // accompanying file LICENSE_1_0.txt or copy at |
| 5 | // http://www.boost.org/LICENSE_1_0.txt) |
| 6 | #include <boost/yap/expression.hpp> |
| 7 | |
| 8 | #include <boost/mpl/assert.hpp> |
| 9 | |
| 10 | #include <boost/core/lightweight_test.hpp> |
| 11 | |
| 12 | |
| 13 | template<typename T> |
| 14 | using term = boost::yap::terminal<boost::yap::expression, T>; |
| 15 | |
| 16 | template<typename T> |
| 17 | using ref = boost::yap::expression_ref<boost::yap::expression, T>; |
| 18 | |
| 19 | namespace yap = boost::yap; |
| 20 | namespace bh = boost::hana; |
| 21 | |
| 22 | |
| 23 | template<boost::yap::expr_kind Kind, typename Tuple> |
| 24 | struct user_expr |
| 25 | { |
| 26 | static boost::yap::expr_kind const kind = Kind; |
| 27 | |
| 28 | Tuple elements; |
| 29 | }; |
| 30 | |
| 31 | BOOST_YAP_USER_BINARY_OPERATOR(plus, user_expr, user_expr) |
| 32 | |
| 33 | template<typename T> |
| 34 | using user_term = boost::yap::terminal<user_expr, T>; |
| 35 | |
| 36 | template<typename T> |
| 37 | using user_ref = boost::yap::expression_ref<user_expr, T>; |
| 38 | |
| 39 | |
| 40 | int main() |
| 41 | { |
| 42 | { |
| 43 | term<double> unity = {{1.0}}; |
| 44 | using plus_expr_type = yap::expression< |
| 45 | yap::expr_kind::plus, |
| 46 | bh::tuple<ref<term<double> &>, term<int>>>; |
| 47 | |
| 48 | { |
| 49 | plus_expr_type plus_expr = unity + term<int>{{1}}; |
| 50 | BOOST_MPL_ASSERT((std::is_same< |
| 51 | decltype(yap::left(std::move(plus_expr))), |
| 52 | ref<term<double> &> &&>)); |
| 53 | } |
| 54 | |
| 55 | { |
| 56 | plus_expr_type plus_expr = unity + term<int>{{1}}; |
| 57 | BOOST_MPL_ASSERT((std::is_same< |
| 58 | decltype(yap::left(plus_expr)), |
| 59 | ref<term<double> &> &>)); |
| 60 | } |
| 61 | |
| 62 | { |
| 63 | plus_expr_type const plus_expr = unity + term<int>{{1}}; |
| 64 | BOOST_MPL_ASSERT((std::is_same< |
| 65 | decltype(yap::left(plus_expr)), |
| 66 | ref<term<double> &> const &>)); |
| 67 | } |
| 68 | |
| 69 | { |
| 70 | term<double> const unity = {{1.0}}; |
| 71 | using plus_expr_type = yap::expression< |
| 72 | yap::expr_kind::plus, |
| 73 | bh::tuple<ref<term<double> const &>, term<int>>>; |
| 74 | |
| 75 | { |
| 76 | plus_expr_type plus_expr = unity + term<int>{{1}}; |
| 77 | BOOST_MPL_ASSERT((std::is_same< |
| 78 | decltype(yap::left(std::move(plus_expr))), |
| 79 | ref<term<double> const &> &&>)); |
| 80 | } |
| 81 | |
| 82 | { |
| 83 | plus_expr_type plus_expr = unity + term<int>{{1}}; |
| 84 | BOOST_MPL_ASSERT((std::is_same< |
| 85 | decltype(yap::left(plus_expr)), |
| 86 | ref<term<double> const &> &>)); |
| 87 | } |
| 88 | |
| 89 | { |
| 90 | plus_expr_type const plus_expr = unity + term<int>{{1}}; |
| 91 | BOOST_MPL_ASSERT((std::is_same< |
| 92 | decltype(yap::left(plus_expr)), |
| 93 | ref<term<double> const &> const &>)); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | { |
| 98 | term<double> unity = {{1.0}}; |
| 99 | using plus_expr_type = yap::expression< |
| 100 | yap::expr_kind::plus, |
| 101 | bh::tuple<ref<term<double> &>, term<int>>>; |
| 102 | plus_expr_type plus_expr = unity + term<int>{{1}}; |
| 103 | |
| 104 | using plus_plus_expr_type = yap::expression< |
| 105 | yap::expr_kind::plus, |
| 106 | bh::tuple<ref<plus_expr_type &>, term<int>>>; |
| 107 | |
| 108 | { |
| 109 | plus_plus_expr_type plus_plus_expr = plus_expr + term<int>{{1}}; |
| 110 | ref<plus_expr_type &> plus_expr_ref = |
| 111 | bh::front(plus_plus_expr.elements); |
| 112 | BOOST_MPL_ASSERT((std::is_same< |
| 113 | decltype(yap::left(std::move(plus_expr_ref))), |
| 114 | ref<term<double> &> &>)); |
| 115 | } |
| 116 | |
| 117 | { |
| 118 | plus_plus_expr_type plus_plus_expr = plus_expr + term<int>{{1}}; |
| 119 | ref<plus_expr_type &> plus_expr_ref = |
| 120 | bh::front(plus_plus_expr.elements); |
| 121 | BOOST_MPL_ASSERT((std::is_same< |
| 122 | decltype(yap::left(plus_expr_ref)), |
| 123 | ref<term<double> &> &>)); |
| 124 | } |
| 125 | |
| 126 | { |
| 127 | plus_plus_expr_type plus_plus_expr = plus_expr + term<int>{{1}}; |
| 128 | ref<plus_expr_type &> const plus_expr_ref = |
| 129 | bh::front(plus_plus_expr.elements); |
| 130 | BOOST_MPL_ASSERT((std::is_same< |
| 131 | decltype(yap::left(plus_expr_ref)), |
| 132 | ref<term<double> &> &>)); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | { |
| 137 | term<double> unity = {{1.0}}; |
| 138 | using plus_expr_type = yap::expression< |
| 139 | yap::expr_kind::plus, |
| 140 | bh::tuple<ref<term<double> &>, term<int>>>; |
| 141 | plus_expr_type const plus_expr = unity + term<int>{{1}}; |
| 142 | |
| 143 | using plus_plus_expr_type = yap::expression< |
| 144 | yap::expr_kind::plus, |
| 145 | bh::tuple<ref<plus_expr_type const &>, term<int>>>; |
| 146 | |
| 147 | { |
| 148 | plus_plus_expr_type plus_plus_expr = plus_expr + term<int>{{1}}; |
| 149 | ref<plus_expr_type const &> plus_expr_ref = |
| 150 | bh::front(plus_plus_expr.elements); |
| 151 | BOOST_MPL_ASSERT((std::is_same< |
| 152 | decltype(yap::left(std::move(plus_expr_ref))), |
| 153 | ref<term<double> &> const &>)); |
| 154 | } |
| 155 | |
| 156 | { |
| 157 | plus_plus_expr_type plus_plus_expr = plus_expr + term<int>{{1}}; |
| 158 | ref<plus_expr_type const &> plus_expr_ref = |
| 159 | bh::front(plus_plus_expr.elements); |
| 160 | BOOST_MPL_ASSERT((std::is_same< |
| 161 | decltype(yap::left(plus_expr_ref)), |
| 162 | ref<term<double> &> const &>)); |
| 163 | } |
| 164 | |
| 165 | { |
| 166 | plus_plus_expr_type plus_plus_expr = plus_expr + term<int>{{1}}; |
| 167 | ref<plus_expr_type const &> const plus_expr_ref = |
| 168 | bh::front(plus_plus_expr.elements); |
| 169 | BOOST_MPL_ASSERT((std::is_same< |
| 170 | decltype(yap::left(plus_expr_ref)), |
| 171 | ref<term<double> &> const &>)); |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | { |
| 177 | user_term<double> unity = {.elements: {1.0}}; |
| 178 | using plus_expr_type = user_expr< |
| 179 | yap::expr_kind::plus, |
| 180 | bh::tuple<user_ref<user_term<double> &>, user_term<int>>>; |
| 181 | |
| 182 | { |
| 183 | plus_expr_type plus_expr = unity + user_term<int>{.elements: {1}}; |
| 184 | BOOST_MPL_ASSERT((std::is_same< |
| 185 | decltype(yap::left(std::move(plus_expr))), |
| 186 | user_ref<user_term<double> &> &&>)); |
| 187 | } |
| 188 | |
| 189 | { |
| 190 | plus_expr_type plus_expr = unity + user_term<int>{.elements: {1}}; |
| 191 | BOOST_MPL_ASSERT((std::is_same< |
| 192 | decltype(yap::left(plus_expr)), |
| 193 | user_ref<user_term<double> &> &>)); |
| 194 | } |
| 195 | |
| 196 | { |
| 197 | plus_expr_type const plus_expr = unity + user_term<int>{.elements: {1}}; |
| 198 | BOOST_MPL_ASSERT((std::is_same< |
| 199 | decltype(yap::left(plus_expr)), |
| 200 | user_ref<user_term<double> &> const &>)); |
| 201 | } |
| 202 | |
| 203 | { |
| 204 | user_term<double> const unity = {.elements: {1.0}}; |
| 205 | using plus_expr_type = user_expr< |
| 206 | yap::expr_kind::plus, |
| 207 | bh::tuple<user_ref<user_term<double> const &>, user_term<int>>>; |
| 208 | |
| 209 | { |
| 210 | plus_expr_type plus_expr = unity + user_term<int>{.elements: {1}}; |
| 211 | BOOST_MPL_ASSERT((std::is_same< |
| 212 | decltype(yap::left(std::move(plus_expr))), |
| 213 | user_ref<user_term<double> const &> &&>)); |
| 214 | } |
| 215 | |
| 216 | { |
| 217 | plus_expr_type plus_expr = unity + user_term<int>{.elements: {1}}; |
| 218 | BOOST_MPL_ASSERT((std::is_same< |
| 219 | decltype(yap::left(plus_expr)), |
| 220 | user_ref<user_term<double> const &> &>)); |
| 221 | } |
| 222 | |
| 223 | { |
| 224 | plus_expr_type const plus_expr = unity + user_term<int>{.elements: {1}}; |
| 225 | BOOST_MPL_ASSERT( |
| 226 | (std::is_same< |
| 227 | decltype(yap::left(plus_expr)), |
| 228 | user_ref<user_term<double> const &> const &>)); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | { |
| 233 | user_term<double> unity = {.elements: {1.0}}; |
| 234 | using plus_expr_type = user_expr< |
| 235 | yap::expr_kind::plus, |
| 236 | bh::tuple<user_ref<user_term<double> &>, user_term<int>>>; |
| 237 | plus_expr_type plus_expr = unity + user_term<int>{.elements: {1}}; |
| 238 | |
| 239 | using plus_plus_expr_type = user_expr< |
| 240 | yap::expr_kind::plus, |
| 241 | bh::tuple<user_ref<plus_expr_type &>, user_term<int>>>; |
| 242 | |
| 243 | { |
| 244 | plus_plus_expr_type plus_plus_expr = |
| 245 | plus_expr + user_term<int>{.elements: {1}}; |
| 246 | user_ref<plus_expr_type &> plus_expr_ref = |
| 247 | bh::front(plus_plus_expr.elements); |
| 248 | BOOST_MPL_ASSERT((std::is_same< |
| 249 | decltype(yap::left(std::move(plus_expr_ref))), |
| 250 | user_ref<user_term<double> &> &>)); |
| 251 | } |
| 252 | |
| 253 | { |
| 254 | plus_plus_expr_type plus_plus_expr = |
| 255 | plus_expr + user_term<int>{.elements: {1}}; |
| 256 | user_ref<plus_expr_type &> plus_expr_ref = |
| 257 | bh::front(plus_plus_expr.elements); |
| 258 | BOOST_MPL_ASSERT((std::is_same< |
| 259 | decltype(yap::left(plus_expr_ref)), |
| 260 | user_ref<user_term<double> &> &>)); |
| 261 | } |
| 262 | |
| 263 | { |
| 264 | plus_plus_expr_type plus_plus_expr = |
| 265 | plus_expr + user_term<int>{.elements: {1}}; |
| 266 | user_ref<plus_expr_type &> const plus_expr_ref = |
| 267 | bh::front(plus_plus_expr.elements); |
| 268 | BOOST_MPL_ASSERT((std::is_same< |
| 269 | decltype(yap::left(plus_expr_ref)), |
| 270 | user_ref<user_term<double> &> &>)); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | { |
| 275 | user_term<double> unity = {.elements: {1.0}}; |
| 276 | using plus_expr_type = user_expr< |
| 277 | yap::expr_kind::plus, |
| 278 | bh::tuple<user_ref<user_term<double> &>, user_term<int>>>; |
| 279 | plus_expr_type const plus_expr = unity + user_term<int>{.elements: {1}}; |
| 280 | |
| 281 | using plus_plus_expr_type = user_expr< |
| 282 | yap::expr_kind::plus, |
| 283 | bh::tuple<user_ref<plus_expr_type const &>, user_term<int>>>; |
| 284 | |
| 285 | { |
| 286 | plus_plus_expr_type plus_plus_expr = |
| 287 | plus_expr + user_term<int>{.elements: {1}}; |
| 288 | user_ref<plus_expr_type const &> plus_expr_ref = |
| 289 | bh::front(plus_plus_expr.elements); |
| 290 | BOOST_MPL_ASSERT((std::is_same< |
| 291 | decltype(yap::left(std::move(plus_expr_ref))), |
| 292 | user_ref<user_term<double> &> const &>)); |
| 293 | } |
| 294 | |
| 295 | { |
| 296 | plus_plus_expr_type plus_plus_expr = |
| 297 | plus_expr + user_term<int>{.elements: {1}}; |
| 298 | user_ref<plus_expr_type const &> plus_expr_ref = |
| 299 | bh::front(plus_plus_expr.elements); |
| 300 | BOOST_MPL_ASSERT((std::is_same< |
| 301 | decltype(yap::left(plus_expr_ref)), |
| 302 | user_ref<user_term<double> &> const &>)); |
| 303 | } |
| 304 | |
| 305 | { |
| 306 | plus_plus_expr_type plus_plus_expr = |
| 307 | plus_expr + user_term<int>{.elements: {1}}; |
| 308 | user_ref<plus_expr_type const &> const plus_expr_ref = |
| 309 | bh::front(plus_plus_expr.elements); |
| 310 | BOOST_MPL_ASSERT((std::is_same< |
| 311 | decltype(yap::left(plus_expr_ref)), |
| 312 | user_ref<user_term<double> &> const &>)); |
| 313 | } |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | #ifndef _MSC_VER // Tsk, tsk. |
| 318 | { |
| 319 | using term_t = term<int>; |
| 320 | constexpr auto expr = term_t{13} + term_t{42}; |
| 321 | constexpr auto result1 = expr.left().value(); |
| 322 | constexpr auto result2 = yap::value(x: left(expr)); |
| 323 | (void)result1; |
| 324 | (void)result2; |
| 325 | } |
| 326 | #endif |
| 327 | |
| 328 | return boost::report_errors(); |
| 329 | } |
| 330 | |