|
14 | 14 | // template<class Pointer = void, class Smart, class... Args>
|
15 | 15 | // auto inout_ptr(Smart& s, Args&&... args); // since c++23
|
16 | 16 |
|
| 17 | +#include <cassert> |
17 | 18 | #include <memory>
|
18 | 19 |
|
19 |
| -int main(int, char**) { |
| 20 | +#include "../types.h" |
| 21 | + |
| 22 | +// Test helpers. |
| 23 | + |
| 24 | +void replace_int_p(int** pp) { |
| 25 | + assert(**pp == 90); |
| 26 | + delete *pp; |
| 27 | + *pp = new int{84}; |
| 28 | +} |
| 29 | + |
| 30 | +void replace_int_p_with_nullptr(int** pp) { |
| 31 | + assert(**pp == 90); |
| 32 | + delete *pp; |
| 33 | + *pp = nullptr; |
| 34 | +} |
| 35 | + |
| 36 | +void replace_nullptr_with_int_p(int** pp) { |
| 37 | + assert(*pp == nullptr); |
| 38 | + *pp = new int{84}; |
| 39 | +} |
| 40 | + |
| 41 | +void replace_int_void_p(void** pp) { |
| 42 | + assert(*(static_cast<int*>(*pp)) == 90); |
| 43 | + delete static_cast<int*>(*pp); |
| 44 | + *pp = new int{84}; |
| 45 | +} |
| 46 | + |
| 47 | +void replace_int_void_p_with_nullptr(void** pp) { |
| 48 | + assert(*(static_cast<int*>(*pp)) == 90); |
| 49 | + delete static_cast<int*>(*pp); |
| 50 | + *pp = nullptr; |
| 51 | +} |
| 52 | + |
| 53 | +void replace_nullptr_with_int_void_p(void** pp) { |
| 54 | + assert(*pp == nullptr); |
| 55 | + *pp = new int{84}; |
| 56 | +} |
| 57 | + |
| 58 | +void replace_SomeInt_p(SomeInt** pp) { |
| 59 | + auto si = **pp; |
| 60 | + assert(si.value == 90); |
| 61 | + delete static_cast<SomeInt*>(*pp); |
| 62 | + *pp = new SomeInt{9084}; |
| 63 | +} |
| 64 | + |
| 65 | +void replace_SomeInt_void_p(void** pp) { |
| 66 | + assert(reinterpret_cast<SomeInt*>(*pp)->value == 90); |
| 67 | + delete static_cast<SomeInt*>(*pp); |
| 68 | + *pp = reinterpret_cast<void*>(new SomeInt{9084}); |
| 69 | +} |
| 70 | + |
| 71 | +// Test `std::inout_ptr()` function. |
| 72 | + |
| 73 | +void test_raw_ptr() { |
| 74 | + { |
| 75 | + auto rPtr = new int{90}; |
| 76 | + |
| 77 | + replace_int_p(std::inout_ptr<int*>(rPtr)); |
| 78 | + assert(*rPtr == 84); |
| 79 | + |
| 80 | + delete rPtr; |
| 81 | + } |
| 82 | + { |
| 83 | + auto rPtr = new int{90}; |
| 84 | + |
| 85 | + replace_int_p_with_nullptr(std::inout_ptr<int*>(rPtr)); |
| 86 | + assert(rPtr == nullptr); |
| 87 | + } |
| 88 | + { |
| 89 | + int* rPtr = nullptr; |
| 90 | + |
| 91 | + replace_nullptr_with_int_p(std::inout_ptr<int*>(rPtr)); |
| 92 | + assert(*rPtr == 84); |
| 93 | + delete rPtr; |
| 94 | + } |
| 95 | + { |
| 96 | + auto rPtr = new int{90}; |
| 97 | + |
| 98 | + replace_int_void_p(std::inout_ptr<int*>(rPtr)); |
| 99 | + assert(*rPtr == 84); |
| 100 | + delete rPtr; |
| 101 | + } |
| 102 | + { |
| 103 | + auto rPtr = new int{90}; |
| 104 | + |
| 105 | + replace_int_void_p_with_nullptr(std::inout_ptr<int*>(rPtr)); |
| 106 | + assert(rPtr == nullptr); |
| 107 | + } |
| 108 | + { |
| 109 | + int* rPtr = nullptr; |
| 110 | + |
| 111 | + replace_nullptr_with_int_void_p(std::inout_ptr<int*>(rPtr)); |
| 112 | + assert(*rPtr == 84); |
| 113 | + delete rPtr; |
| 114 | + } |
| 115 | + { |
| 116 | + auto* rPtr = new SomeInt{90}; |
| 117 | + |
| 118 | + replace_SomeInt_p(std::inout_ptr(rPtr)); |
| 119 | + assert(rPtr->value == 9084); |
| 120 | + delete rPtr; |
| 121 | + } |
| 122 | + { |
| 123 | + auto* rPtr = new SomeInt{90}; |
| 124 | + |
| 125 | + replace_SomeInt_void_p(std::inout_ptr<SomeInt*>(rPtr)); |
| 126 | + assert(rPtr->value == 9084); |
| 127 | + delete rPtr; |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +void test_unique_ptr() { |
| 132 | + { |
| 133 | + auto uPtr = std::make_unique<int>(90); |
| 134 | + |
| 135 | + replace_int_p(std::inout_ptr(uPtr)); |
| 136 | + assert(*uPtr == 84); |
| 137 | + } |
20 | 138 | {
|
21 | 139 | std::unique_ptr<int> uPtr;
|
22 | 140 |
|
23 |
| - auto inoutUPtr1 = std::inout_ptr(uPtr); |
24 |
| - (void)inoutUPtr1; |
25 |
| - auto inoutUPtr2 = std::inout_ptr<int*>(uPtr); |
26 |
| - (void)inoutUPtr2; |
| 141 | + replace_nullptr_with_int_p(std::inout_ptr(uPtr)); |
| 142 | + assert(*uPtr == 84); |
27 | 143 | }
|
28 | 144 | {
|
29 |
| - auto deleter = [](auto* p) { delete p; }; |
30 |
| - std::unique_ptr<int, decltype(deleter)> uPtr; |
| 145 | + auto uPtr = std::make_unique<int>(90); |
31 | 146 |
|
32 |
| - auto inoutUPtr1 = std::inout_ptr(uPtr, deleter); |
33 |
| - (void)inoutUPtr1; |
34 |
| - auto inoutUPtr2 = std::inout_ptr<int*>(uPtr, deleter); |
35 |
| - (void)inoutUPtr2; |
| 147 | + replace_int_void_p(std::inout_ptr(uPtr)); |
| 148 | + assert(*uPtr == 84); |
36 | 149 | }
|
| 150 | + { |
| 151 | + std::unique_ptr<int> uPtr; |
| 152 | + |
| 153 | + replace_nullptr_with_int_void_p(std::inout_ptr(uPtr)); |
| 154 | + assert(*uPtr == 84); |
| 155 | + } |
| 156 | + { |
| 157 | + auto uPtr = std::make_unique<SomeInt>(90); |
| 158 | + |
| 159 | + replace_SomeInt_p(std::inout_ptr(uPtr)); |
| 160 | + assert(uPtr->value == 9084); |
| 161 | + } |
| 162 | + { |
| 163 | + auto uPtr = std::make_unique<SomeInt>(90); |
| 164 | + |
| 165 | + replace_SomeInt_void_p(std::inout_ptr<SomeInt*>(uPtr)); |
| 166 | + assert(uPtr->value == 9084); |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +void test_custom_ptr() { |
| 171 | + // ConstructiblePtr |
| 172 | + { |
| 173 | + ConstructiblePtr<int> cPtr(new int{90}); |
| 174 | + |
| 175 | + replace_int_p(std::inout_ptr(cPtr)); |
| 176 | + assert(cPtr == 84); |
| 177 | + } |
| 178 | + // ResettablePtr |
| 179 | + { |
| 180 | + ResettablePtr<int> rPtr(new int{90}); |
| 181 | + |
| 182 | + replace_int_p(std::inout_ptr(rPtr)); |
| 183 | + assert(rPtr == 84); |
| 184 | + } |
| 185 | + // NonConstructiblePtr |
| 186 | + { |
| 187 | + NonConstructiblePtr<int> nPtr; |
| 188 | + nPtr.reset(new int{90}); |
| 189 | + |
| 190 | + replace_int_p(std::inout_ptr(nPtr)); |
| 191 | + assert(nPtr == 84); |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +int main(int, char**) { |
| 196 | + test_raw_ptr(); |
| 197 | + test_unique_ptr(); |
| 198 | + test_custom_ptr(); |
37 | 199 |
|
38 | 200 | return 0;
|
39 | 201 | }
|
0 commit comments