|
| 1 | +#include <swift/bridging> |
| 2 | + |
| 3 | +struct RefCountedBox { |
| 4 | + int value; |
| 5 | + int refCount = 1; |
| 6 | + |
| 7 | + RefCountedBox(int value) : value(value) {} |
| 8 | + |
| 9 | + void doRetain() { refCount++; } |
| 10 | + void doRelease() { refCount--; } |
| 11 | +} SWIFT_SHARED_REFERENCE(.doRetain, .doRelease); |
| 12 | + |
| 13 | +struct DerivedRefCountedBox : RefCountedBox { |
| 14 | + int secondValue = 1; |
| 15 | + DerivedRefCountedBox(int value, int secondValue) |
| 16 | + : RefCountedBox(value), secondValue(secondValue) {} |
| 17 | +}; |
| 18 | + |
| 19 | +// MARK: Retain in a base type, release in derived |
| 20 | + |
| 21 | +struct BaseHasRetain { |
| 22 | + mutable int refCount = 1; |
| 23 | + void doRetainInBase() const { refCount++; } |
| 24 | +}; |
| 25 | + |
| 26 | +struct DerivedHasRelease : BaseHasRetain { |
| 27 | + int value; |
| 28 | + DerivedHasRelease(int value) : value(value) {} |
| 29 | + |
| 30 | + void doRelease() const { refCount--; } |
| 31 | +} SWIFT_SHARED_REFERENCE(.doRetainInBase, .doRelease); |
| 32 | + |
| 33 | +// MARK: Retain in a base type, release in templated derived |
| 34 | + |
| 35 | +template <typename T> |
| 36 | +struct TemplatedDerivedHasRelease : BaseHasRetain { |
| 37 | + T value; |
| 38 | + TemplatedDerivedHasRelease(T value) : value(value) {} |
| 39 | + |
| 40 | + void doReleaseTemplated() const { refCount--; } |
| 41 | +} SWIFT_SHARED_REFERENCE(.doRetainInBase, .doReleaseTemplated); |
| 42 | + |
| 43 | +using TemplatedDerivedHasReleaseFloat = TemplatedDerivedHasRelease<float>; |
| 44 | +using TemplatedDerivedHasReleaseInt = TemplatedDerivedHasRelease<int>; |
| 45 | + |
| 46 | +// MARK: Retain/release in CRTP base type |
| 47 | + |
| 48 | +template <typename Derived> |
| 49 | +struct CRTPBase { |
| 50 | + mutable int refCount = 1; |
| 51 | + void crtpRetain() const { refCount++; } |
| 52 | + void crtpRelease() const { refCount--; } |
| 53 | +} SWIFT_SHARED_REFERENCE(.crtpRetain, .crtpRelease); |
| 54 | + |
| 55 | +struct CRTPDerived : CRTPBase<CRTPDerived> { |
| 56 | + int value; |
| 57 | + CRTPDerived(int value) : value(value) {} |
| 58 | +}; |
| 59 | + |
| 60 | +// MARK: Virtual retain and release |
| 61 | + |
| 62 | +struct VirtualRetainRelease { |
| 63 | + int value; |
| 64 | + mutable int refCount = 1; |
| 65 | + VirtualRetainRelease(int value) : value(value) {} |
| 66 | + |
| 67 | + virtual void doRetainVirtual() const { refCount++; } |
| 68 | + virtual void doReleaseVirtual() const { refCount--; } |
| 69 | + virtual ~VirtualRetainRelease() = default; |
| 70 | +} SWIFT_SHARED_REFERENCE(.doRetainVirtual, .doReleaseVirtual); |
| 71 | + |
| 72 | +struct DerivedVirtualRetainRelease : VirtualRetainRelease { |
| 73 | + DerivedVirtualRetainRelease(int value) : VirtualRetainRelease(value) {} |
| 74 | + |
| 75 | + mutable bool calledDerived = false; |
| 76 | + void doRetainVirtual() const override { refCount++; calledDerived = true; } |
| 77 | + void doReleaseVirtual() const override { refCount--; } |
| 78 | +}; |
| 79 | + |
| 80 | +// MARK: Pure virtual retain and release |
| 81 | + |
| 82 | +struct PureVirtualRetainRelease { |
| 83 | + int value; |
| 84 | + mutable int refCount = 1; |
| 85 | + PureVirtualRetainRelease(int value) : value(value) {} |
| 86 | + |
| 87 | + virtual void doRetainPure() const = 0; |
| 88 | + virtual void doReleasePure() const = 0; |
| 89 | + virtual ~PureVirtualRetainRelease() = default; |
| 90 | +} SWIFT_SHARED_REFERENCE(.doRetainPure, .doReleasePure); |
| 91 | + |
| 92 | +struct DerivedPureVirtualRetainRelease : PureVirtualRetainRelease { |
| 93 | + mutable int refCount = 1; |
| 94 | + |
| 95 | + DerivedPureVirtualRetainRelease(int value) : PureVirtualRetainRelease(value) {} |
| 96 | + void doRetainPure() const override { refCount++; } |
| 97 | + void doReleasePure() const override { refCount--; } |
| 98 | +}; |
| 99 | + |
| 100 | +// MARK: Static retain/release |
| 101 | +#ifdef INCORRECT |
| 102 | +struct StaticRetainRelease { |
| 103 | +// expected-error@-1 {{specified retain function '.staticRetain' is a static function; expected an instance function}} |
| 104 | +// expected-error@-2 {{specified release function '.staticRelease' is a static function; expected an instance function}} |
| 105 | + int value; |
| 106 | + int refCount = 1; |
| 107 | + |
| 108 | + StaticRetainRelease(int value) : value(value) {} |
| 109 | + |
| 110 | + static void staticRetain(StaticRetainRelease* o) { o->refCount++; } |
| 111 | + static void staticRelease(StaticRetainRelease* o) { o->refCount--; } |
| 112 | +} SWIFT_SHARED_REFERENCE(.staticRetain, .staticRelease); |
| 113 | + |
| 114 | +struct DerivedStaticRetainRelease : StaticRetainRelease { |
| 115 | +// expected-error@-1 {{cannot find retain function '.staticRetain' for reference type 'DerivedStaticRetainRelease'}} |
| 116 | +// expected-error@-2 {{cannot find release function '.staticRelease' for reference type 'DerivedStaticRetainRelease'}} |
| 117 | + int secondValue = 1; |
| 118 | + DerivedStaticRetainRelease(int value, int secondValue) |
| 119 | + : StaticRetainRelease(value), secondValue(secondValue) {} |
| 120 | +}; |
| 121 | +#endif |
0 commit comments