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
Copy file name to clipboardExpand all lines: docs/CppInteroperability/UserManual.md
+29Lines changed: 29 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -6,6 +6,35 @@ The following document explains how C++ APIs are imported into Swift and is targ
6
6
7
7
Reference types have reference semantics and object identity. A reference type is a pointer (or “reference”) to some object which means there is a layer of indirection. When a reference type is copied, the pointer’s value is copied rather than the object’s storage. This means reference types can be used to represent non-copyable types in C++. Any C++ APIs that use reference types must have at least one layer of indirection to the type (a pointer or reference). Currently reference types must be immortal (never deallocated) or have manually managed lifetimes. You can specify a type has reference semantics by using the `import_reference` swift attribute.
8
8
9
+
### Importing custom reference counted types into Swift
10
+
11
+
C++ types that have their own reference count and custom retain/release operations
12
+
can be bridged over as reference types into Swift. Swift's automatic reference counting (ARC)
13
+
will automatically retain and release them using their C++ reference counting operations.
14
+
15
+
The `import_reference` swift attribute can be applied with the appropriate `retain` and `release` attributes
16
+
to import a type with such semantics as a reference type into Swift.
17
+
The example below illustrates how this can be done for the C++ `MyReferenceObject` class:
int referenceCount; // the custom reference count.
28
+
};
29
+
30
+
/// Increment the reference count for the given object.
31
+
void incRef(MyReferenceObject *object);
32
+
33
+
/// Decrement the reference count for the given object. When it reaches zero,
34
+
/// the object is deallocated.
35
+
void decRef(MyReferenceObject *object);
36
+
```
37
+
9
38
## Owned Types
10
39
11
40
Owned types “own” some storage which can be copied and destroyed. An owned type must be copyable and destructible. The copy constructor must copy any storage that is owned by the type and the destructor must destroy that storage. Copies and destroys must balance out and these operations must not have side effects. Examples of owned types include `std::vector` and `std::string`. The Swift compiler will assume that any types which do not contain pointers are owned types. You can also specify a type is an owned type by using the `import_owned` swift attribute.
0 commit comments