expression_function.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. #include <boost/test/minimal.hpp>
  8. template<typename T>
  9. using term = boost::yap::terminal<boost::yap::expression, T>;
  10. template<typename T>
  11. using ref = boost::yap::expression_ref<boost::yap::expression, T>;
  12. namespace yap = boost::yap;
  13. namespace bh = boost::hana;
  14. int test_main(int, char * [])
  15. {
  16. {
  17. term<int> number = {{42}};
  18. auto fn = yap::make_expression_function(number);
  19. auto fn_copy = fn;
  20. BOOST_CHECK(fn() == 42);
  21. BOOST_CHECK(fn_copy() == 42);
  22. yap::value(number) = 21;
  23. BOOST_CHECK(fn() == 21);
  24. BOOST_CHECK(fn_copy() == 21);
  25. }
  26. {
  27. term<int> number = {{42}};
  28. auto fn = yap::make_expression_function(std::move(number));
  29. auto fn_copy = fn;
  30. BOOST_CHECK(fn() == 42);
  31. BOOST_CHECK(fn_copy() == 42);
  32. yap::value(number) = 21;
  33. BOOST_CHECK(fn() == 42);
  34. BOOST_CHECK(fn_copy() == 42);
  35. }
  36. {
  37. term<std::unique_ptr<int>> number = {
  38. {std::unique_ptr<int>(new int(42))}};
  39. auto fn = yap::make_expression_function(std::move(number));
  40. BOOST_CHECK(*fn() == 42);
  41. auto fn_2 = std::move(fn);
  42. BOOST_CHECK(*fn_2() == 42);
  43. yap::value(number) = std::unique_ptr<int>(new int(21));
  44. BOOST_CHECK(*fn_2() == 42);
  45. }
  46. return 0;
  47. }