Files
swift-mirror/test/Interop/Cxx/stdlib/Inputs/std-unique-ptr.h
Gabor Horvath 7474a51691 [cxx-interop] Avoid copies when accessing pointee
Previously, we would get two copies, one accessing the pointee and one
when we pass the pointee as a method as the implicit self argument.
These copies are unsafe as they might introduce slicing. When
addressable paramaters features are enabled, we no longer make these
copies for the standard STL types. Custom smart pointers can replicate
this by making the lifetime dependency between the implicit object
parameter and the returned reference of operator* explicit via a
lifetime annotation.

rdar://154213694&128293252&112690482
2025-06-25 17:09:55 +01:00

85 lines
2.0 KiB
C++

#ifndef TEST_INTEROP_CXX_STDLIB_INPUTS_STD_UNIQUE_PTR_H
#define TEST_INTEROP_CXX_STDLIB_INPUTS_STD_UNIQUE_PTR_H
#include <memory>
#include <string>
struct NonCopyable {
NonCopyable(int x) : x(x) {}
NonCopyable(const NonCopyable &) = delete;
NonCopyable(NonCopyable &&other) : x(other.x) { other.x = -123; }
int method(int y) const { return x * y; }
int mutMethod(int y) {
x = y;
return y;
}
int x;
};
struct NonCopyableDerived: public NonCopyable {
NonCopyableDerived(int x) : NonCopyable(x) {}
};
inline std::shared_ptr<NonCopyable> getNonCopyableSharedPtr() { return std::make_shared<NonCopyableDerived>(42); }
inline std::unique_ptr<NonCopyable> getNonCopyableUniquePtr() { return std::make_unique<NonCopyableDerived>(42); }
std::unique_ptr<int> makeInt() {
return std::make_unique<int>(42);
}
std::unique_ptr<std::string> makeString() {
return std::make_unique<std::string>("Unique string");
}
std::unique_ptr<int[]> makeArray() {
int *array = new int[3];
array[0] = 1;
array[1] = 2;
array[2] = 3;
return std::unique_ptr<int[]>(array);
}
static bool dtorCalled = false;
struct HasDtor {
HasDtor() = default;
#if __is_target_os(windows)
// On windows, force this type to be address-only.
HasDtor(const HasDtor &other);
#endif
~HasDtor() {
dtorCalled = true;
}
private:
int x;
};
std::unique_ptr<HasDtor> makeDtor() {
return std::make_unique<HasDtor>();
}
std::shared_ptr<int> makeIntShared() { return std::make_unique<int>(42); }
std::shared_ptr<std::string> makeStringShared() {
return std::make_unique<std::string>("Shared string");
}
static int copies = 0;
struct CountCopies {
CountCopies() = default;
CountCopies(const CountCopies& other) { ++copies; }
~CountCopies() {}
int getCopies() const { return copies; }
void method() {}
void constMethod() const {}
int field = 42;
};
inline std::unique_ptr<CountCopies> getCopyCountedUniquePtr() { return std::make_unique<CountCopies>(); }
#endif // TEST_INTEROP_CXX_STDLIB_INPUTS_STD_UNIQUE_PTR_H