line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
// Copyright Catch2 Authors |
3
|
|
|
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. |
4
|
|
|
|
|
|
|
// (See accompanying file LICENSE_1_0.txt or copy at |
5
|
|
|
|
|
|
|
// https://www.boost.org/LICENSE_1_0.txt) |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
// SPDX-License-Identifier: BSL-1.0 |
8
|
|
|
|
|
|
|
#ifndef CATCH_UNIQUE_PTR_HPP_INCLUDED |
9
|
|
|
|
|
|
|
#define CATCH_UNIQUE_PTR_HPP_INCLUDED |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
#include |
12
|
|
|
|
|
|
|
#include |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
#include |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
#if defined(__clang__) && defined(__has_attribute) |
17
|
|
|
|
|
|
|
# if __has_attribute(trivial_abi) |
18
|
|
|
|
|
|
|
# define TRIVIAL_ABI [[clang::trivial_abi]] |
19
|
|
|
|
|
|
|
# endif |
20
|
|
|
|
|
|
|
#endif |
21
|
|
|
|
|
|
|
#if !defined(TRIVIAL_ABI) |
22
|
|
|
|
|
|
|
# define TRIVIAL_ABI |
23
|
|
|
|
|
|
|
#endif |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
namespace Catch { |
26
|
|
|
|
|
|
|
namespace Detail { |
27
|
|
|
|
|
|
|
/** |
28
|
|
|
|
|
|
|
* A reimplementation of `std::unique_ptr` for improved compilation performance |
29
|
|
|
|
|
|
|
* |
30
|
|
|
|
|
|
|
* Does not support arrays nor custom deleters, but has trivial ABI |
31
|
|
|
|
|
|
|
* when supposed by the compiler. |
32
|
|
|
|
|
|
|
*/ |
33
|
|
|
|
|
|
|
template |
34
|
|
|
|
|
|
|
class TRIVIAL_ABI unique_ptr { |
35
|
|
|
|
|
|
|
T* m_ptr; |
36
|
|
|
|
|
|
|
public: |
37
|
|
|
|
|
|
|
constexpr unique_ptr(std::nullptr_t = nullptr): |
38
|
|
|
|
|
|
|
m_ptr{} |
39
|
|
|
|
|
|
|
{} |
40
|
|
|
|
|
|
|
explicit constexpr unique_ptr(T* ptr): |
41
|
|
|
|
|
|
|
m_ptr(ptr) |
42
|
|
|
|
|
|
|
{} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
template ::value>> |
45
|
|
|
|
|
|
|
unique_ptr(unique_ptr&& from): |
46
|
|
|
|
|
|
|
m_ptr(from.release()) |
47
|
|
|
|
|
|
|
{} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
template ::value>> |
50
|
|
|
|
|
|
|
unique_ptr& operator=(unique_ptr&& from) { |
51
|
|
|
|
|
|
|
reset(from.release()); |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
return *this; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
unique_ptr(unique_ptr const&) = delete; |
57
|
|
|
|
|
|
|
unique_ptr& operator=(unique_ptr const&) = delete; |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
unique_ptr(unique_ptr&& rhs) noexcept: |
60
|
|
|
|
|
|
|
m_ptr(rhs.m_ptr) { |
61
|
|
|
|
|
|
|
rhs.m_ptr = nullptr; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
unique_ptr& operator=(unique_ptr&& rhs) noexcept { |
64
|
|
|
|
|
|
|
reset(rhs.release()); |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
return *this; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
0
|
|
|
|
|
|
~unique_ptr() { |
70
|
0
|
0
|
|
|
|
|
delete m_ptr; |
71
|
0
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
T& operator*() { |
74
|
|
|
|
|
|
|
assert(m_ptr); |
75
|
|
|
|
|
|
|
return *m_ptr; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
T const& operator*() const { |
78
|
|
|
|
|
|
|
assert(m_ptr); |
79
|
|
|
|
|
|
|
return *m_ptr; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
T* operator->() noexcept { |
82
|
|
|
|
|
|
|
assert(m_ptr); |
83
|
|
|
|
|
|
|
return m_ptr; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
T const* operator->() const noexcept { |
86
|
|
|
|
|
|
|
assert(m_ptr); |
87
|
|
|
|
|
|
|
return m_ptr; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
T* get() { return m_ptr; } |
91
|
|
|
|
|
|
|
T const* get() const { return m_ptr; } |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
void reset(T* ptr = nullptr) { |
94
|
|
|
|
|
|
|
delete m_ptr; |
95
|
|
|
|
|
|
|
m_ptr = ptr; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
T* release() { |
99
|
|
|
|
|
|
|
auto temp = m_ptr; |
100
|
|
|
|
|
|
|
m_ptr = nullptr; |
101
|
|
|
|
|
|
|
return temp; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
explicit operator bool() const { |
105
|
|
|
|
|
|
|
return m_ptr; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
friend void swap(unique_ptr& lhs, unique_ptr& rhs) { |
109
|
|
|
|
|
|
|
auto temp = lhs.m_ptr; |
110
|
|
|
|
|
|
|
lhs.m_ptr = rhs.m_ptr; |
111
|
|
|
|
|
|
|
rhs.m_ptr = temp; |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
}; |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
//! Specialization to cause compile-time error for arrays |
116
|
|
|
|
|
|
|
template |
117
|
|
|
|
|
|
|
class unique_ptr; |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
template |
120
|
|
|
|
|
|
|
unique_ptr make_unique(Args&&... args) { |
121
|
|
|
|
|
|
|
return unique_ptr(new T(CATCH_FORWARD(args)...)); |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
} // end namespace Detail |
126
|
|
|
|
|
|
|
} // end namespace Catch |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
#endif // CATCH_UNIQUE_PTR_HPP_INCLUDED |