You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In case of the `takeSharedObject` function, the compiler will automatically insert calls to retain and release for `x` as necessary to satisfy the semantics of
1279
-
owned/guaranteed calling conventions. The C++ callers must guarantee that `x` is alive for the duration of the call.
1280
-
Note that functions returning a shared reference type such as `returnSharedObject` transfer the ownership to the caller.
1281
-
The C++ caller of this function is responsible for releasing the object.
1282
-
1283
-
If a C++ shared reference type is passed as an non-const argument to a C++ API from Swift, the Swift compiler guarantees that the passed value would be alive, and retains the ownership of the value. In other words, the argument is passed as +0 and there is no transfer of ownership.
1258
+
### Calling conventions when passing shared reference types from Swift to C++
1259
+
If a C++ shared reference type is passed as an argument to a C++ API from Swift, the Swift compiler guarantees that the passed value would be alive, and retains the ownership of the value. In other words, the argument is passed as +0 and there is no transfer of ownership.
1284
1260
The C++ function is responsible for ensuring that the value pointed to by the parameter is alive during and at the end of the call. The C++ function should not assume it has ownership of the value and should do necessary retain operations if it is needs to take ownership.
1261
+
1285
1262
If the argument is an inout (non-const reference) as shown below:
Similar to value types, casting an instance of a derived reference type to a
1289
+
base reference type, or vice versa, is not yet supported by Swift.
1290
+
1291
+
If a reference type has virtual methods, you can call those methods from Swift.
1292
+
This includes pure virtual methods.
1293
+
1294
+
### Exposing C++ Shared Reference Types back from Swift
1295
+
1296
+
C++ can call into Swift APIs that take or return C++ Shared Reference Types. Objects of these types are always created on the C++ side,
1297
+
but their references can be passed back and forth between Swift and C++. This section explains the conventions of incrementing and decrementing
1298
+
the reference counts when passing such references across the language boundaries. Consider the following Swift APIs:
1299
+
1300
+
```swift
1301
+
public func takeSharedObject(_ x : SharedObject) { ... }
1302
+
1303
+
public func returnSharedObject() -> SharedObject { ... }
1304
+
```
1305
+
1306
+
In case of the `takeSharedObject` function, the compiler will automatically insert calls to retain and release for `x` as necessary to satisfy the semantics of
1307
+
owned/guaranteed calling conventions. The C++ callers must guarantee that `x` is alive for the duration of the call.
1308
+
Note that functions returning a shared reference type such as `returnSharedObject` transfer the ownership to the caller.
1309
+
The C++ caller of this function is responsible for releasing the object.
1310
+
1309
1311
### Unsafe Reference Types
1310
1312
1311
1313
The `SWIFT_UNSAFE_REFERENCE` annotation macro has the same effect as `SWIFT_IMMORTAL_REFERENCE`
0 commit comments