File: nothrow_swap.cpp

package info (click to toggle)
boost1.42 1.42.0-4
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 277,864 kB
  • ctags: 401,076
  • sloc: cpp: 1,235,659; xml: 74,142; ansic: 41,313; python: 26,756; sh: 11,840; cs: 2,118; makefile: 655; perl: 494; yacc: 456; asm: 353; csh: 6
file content (60 lines) | stat: -rw-r--r-- 1,373 bytes parent folder | download | duplicates (6)
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
// 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/test/minimal.hpp>
#include <boost/function.hpp>

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 test_main(int, char* [])
{
  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 0;
}