summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/function/test/nothrow_swap.cpp
blob: 8b1b6dc83d4893a19135c2d078265c76a61582f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Boost.Function library

//  Copyright Douglas Gregor 2008. Use, modification and
//  distribution is subject to the Boost Software License, Version
//  1.0. (See accompanying file LICENSE_1_0.txt or copy at
//  http://www.boost.org/LICENSE_1_0.txt)

// For more information, see http://www.boost.org

#include <boost/function.hpp>
#include <boost/core/lightweight_test.hpp>

#define BOOST_CHECK BOOST_TEST

struct tried_to_copy { };

struct MaybeThrowOnCopy {
  MaybeThrowOnCopy(int value = 0) : value(value) { }

  MaybeThrowOnCopy(const MaybeThrowOnCopy& other) : value(other.value) {
    if (throwOnCopy)
      throw tried_to_copy();
  }

  MaybeThrowOnCopy& operator=(const MaybeThrowOnCopy& other) {
    if (throwOnCopy)
      throw tried_to_copy();
    value = other.value;
    return *this;
  }

  int operator()() { return value; }

  int value;

  // Make sure that this function object doesn't trigger the
  // small-object optimization in Function.
  float padding[100];

  static bool throwOnCopy;
};

bool MaybeThrowOnCopy::throwOnCopy = false;

int main()
{
  boost::function0<int> f;
  boost::function0<int> g;

  MaybeThrowOnCopy::throwOnCopy = false;
  f = MaybeThrowOnCopy(1);
  g = MaybeThrowOnCopy(2);
  BOOST_CHECK(f() == 1);
  BOOST_CHECK(g() == 2);

  MaybeThrowOnCopy::throwOnCopy = true;
  f.swap(g);
  BOOST_CHECK(f() == 2);
  BOOST_CHECK(g() == 1);
  
  return boost::report_errors();
}