| 1 | /*============================================================================= |
|---|---|
| 2 | Copyright (c) 2017 Paul Fultz II |
| 3 | mutable.cpp |
| 4 | Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | ==============================================================================*/ |
| 7 | #include <boost/hof/mutable.hpp> |
| 8 | #include <boost/hof/lazy.hpp> |
| 9 | #include <boost/hof/detail/move.hpp> |
| 10 | #include <memory> |
| 11 | #include "test.hpp" |
| 12 | |
| 13 | struct mutable_fun |
| 14 | { |
| 15 | int x; |
| 16 | mutable_fun() noexcept |
| 17 | : x(1) |
| 18 | {} |
| 19 | |
| 20 | int operator()(int i) noexcept |
| 21 | { |
| 22 | x+=i; |
| 23 | return x; |
| 24 | } |
| 25 | }; |
| 26 | |
| 27 | #if BOOST_HOF_HAS_NOEXCEPT_DEDUCTION |
| 28 | BOOST_HOF_TEST_CASE() |
| 29 | { |
| 30 | static_assert(noexcept(boost::hof::mutable_(mutable_fun())(3)), "noexcept mutable_"); |
| 31 | } |
| 32 | #endif |
| 33 | |
| 34 | BOOST_HOF_TEST_CASE() |
| 35 | { |
| 36 | BOOST_HOF_TEST_CHECK(boost::hof::mutable_(mutable_fun())(3) == 4); |
| 37 | } |
| 38 | |
| 39 | BOOST_HOF_TEST_CASE() |
| 40 | { |
| 41 | auto mut_fun = mutable_fun(); |
| 42 | auto by_5 = boost::hof::lazy(boost::hof::mutable_(std::ref(t&: mut_fun)))(5); |
| 43 | BOOST_HOF_TEST_CHECK(by_5() == 6); |
| 44 | BOOST_HOF_TEST_CHECK(by_5() == 11); |
| 45 | } |
| 46 | |
| 47 | struct mutable_move_fun |
| 48 | { |
| 49 | std::unique_ptr<int> x; |
| 50 | mutable_move_fun() : x(new int(1)) |
| 51 | {} |
| 52 | |
| 53 | int operator()(int i) |
| 54 | { |
| 55 | *x+=i; |
| 56 | return *x; |
| 57 | } |
| 58 | }; |
| 59 | |
| 60 | BOOST_HOF_TEST_CASE() |
| 61 | { |
| 62 | BOOST_HOF_TEST_CHECK(boost::hof::mutable_(mutable_move_fun())(3) == 4); |
| 63 | } |
| 64 | |
| 65 | BOOST_HOF_TEST_CASE() |
| 66 | { |
| 67 | auto mut_fun = mutable_move_fun(); |
| 68 | auto by_5 = boost::hof::lazy(boost::hof::mutable_(boost::hof::move(x&: mut_fun)))(5); |
| 69 | BOOST_HOF_TEST_CHECK(by_5() == 6); |
| 70 | BOOST_HOF_TEST_CHECK(by_5() == 11); |
| 71 | } |
| 72 | |
| 73 |
