summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/contract/test/old/copyable_traits.cpp
blob: 3792c6fea12e2cc77ddcd888dbb0587640daf65a (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright (C) 2008-2018 Lorenzo Caminiti
// Distributed under the Boost Software License, Version 1.0 (see accompanying
// file LICENSE_1_0.txt or a copy at http://www.boost.org/LICENSE_1_0.txt).
// See: http://www.boost.org/doc/libs/release/libs/contract/doc/html/index.html

// Test specializations of old value copy type traits.

#include <boost/contract/old.hpp>
#include <boost/type_traits.hpp>
#include <boost/noncopyable.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <cassert>

template<typename T>
void f(T& x) {
    // No OLDOF here so C++11 not required for this test.
    boost::contract::old_ptr_if_copyable<T> old_x = boost::contract::make_old(
            boost::contract::copy_old() ? x : boost::contract::null_old());
}

// Test copyable type but...
struct w {
    w() {}
    w(w const&) { BOOST_TEST(false); } // Test this doesn't get copied.
};

// ...never copy old values for type `w` (because its copy is too expensive).
namespace boost { namespace contract {
    template<>
    struct is_old_value_copyable<w> : boost::false_type {};
} } // namespace

// Test non-copyable type but...
struct p : private boost::noncopyable { // Non-copyable via Boost.
    static bool copied;
    p() : num_(new int(0)) {}
    ~p() { delete num_; }
private:
    int* num_;
    friend struct boost::contract::old_value_copy<p>;
};
bool p::copied = false;

// ...still copy old values for type `p` (using a deep copy).
namespace boost { namespace contract {
    template<>
    struct old_value_copy<p> {
        explicit old_value_copy(p const& old) {
            *old_.num_ = *old.num_; // Deep copy pointed value.
            p::copied = true;
        }

        p const& old() const { return old_; }

    private:
        p old_;
    };
    
    template<>
    struct is_old_value_copyable<p> : boost::true_type {};
} } // namespace

// Non-copyable type so...
struct n {
    n() {}
private:
    n(n const&); // Non-copyable (but not via Boost).
};

// ...specialize `boost::is_copy_constructible` (no need for this on C++11).
namespace boost { namespace contract {
    template<>
    struct is_old_value_copyable<n> : boost::false_type {};
} } // namespace

int main() {
    // NOTE: No test::detail::counter below because that is for copyable types.

    {
        int x; // Test has copy ctor so copy olds.
        f(x);
    }
    
    {
        w x; // Test has copy ctor but never copy olds (see TEST(...) above).
        f(x);
    }

    p::copied = false;
    {
        p x; // Test no copy ctor but still old copies.
        f(x);
    }
    #ifndef BOOST_CONTRACT_NO_OLDS
        BOOST_TEST(p::copied);
    #else
        BOOST_TEST(!p::copied);
    #endif

    {
        n x; // Test no copy ctor so no old copies.
        f(x);
    }
  
    return boost::report_errors();
}