Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 1 | // Boost.Function library |
| 2 | |
| 3 | // Copyright Douglas Gregor 2008. Use, modification and |
| 4 | // distribution is subject to the Boost Software License, Version |
| 5 | // 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
| 6 | // http://www.boost.org/LICENSE_1_0.txt) |
| 7 | |
| 8 | // For more information, see http://www.boost.org |
| 9 | |
| 10 | #include <boost/test/minimal.hpp> |
| 11 | #include <boost/function.hpp> |
| 12 | |
| 13 | struct tried_to_copy { }; |
| 14 | |
| 15 | struct MaybeThrowOnCopy { |
| 16 | MaybeThrowOnCopy(int value = 0) : value(value) { } |
| 17 | |
| 18 | MaybeThrowOnCopy(const MaybeThrowOnCopy& other) : value(other.value) { |
| 19 | if (throwOnCopy) |
| 20 | throw tried_to_copy(); |
| 21 | } |
| 22 | |
| 23 | MaybeThrowOnCopy& operator=(const MaybeThrowOnCopy& other) { |
| 24 | if (throwOnCopy) |
| 25 | throw tried_to_copy(); |
| 26 | value = other.value; |
| 27 | return *this; |
| 28 | } |
| 29 | |
| 30 | int operator()() { return value; } |
| 31 | |
| 32 | int value; |
| 33 | |
| 34 | // Make sure that this function object doesn't trigger the |
| 35 | // small-object optimization in Function. |
| 36 | float padding[100]; |
| 37 | |
| 38 | static bool throwOnCopy; |
| 39 | }; |
| 40 | |
| 41 | bool MaybeThrowOnCopy::throwOnCopy = false; |
| 42 | |
| 43 | int test_main(int, char* []) |
| 44 | { |
| 45 | ndnboost::function0<int> f; |
| 46 | ndnboost::function0<int> g; |
| 47 | |
| 48 | MaybeThrowOnCopy::throwOnCopy = false; |
| 49 | f = MaybeThrowOnCopy(1); |
| 50 | g = MaybeThrowOnCopy(2); |
| 51 | BOOST_CHECK(f() == 1); |
| 52 | BOOST_CHECK(g() == 2); |
| 53 | |
| 54 | MaybeThrowOnCopy::throwOnCopy = true; |
| 55 | f.swap(g); |
| 56 | BOOST_CHECK(f() == 2); |
| 57 | BOOST_CHECK(g() == 1); |
| 58 | |
| 59 | return 0; |
| 60 | } |