Skip to content

Commit ef8c34e

Browse files
authored
Merge pull request #62412 from hyp/eng/import_ref_docx2
[docs][interop] give a more specific example of how to use import_ref…
2 parents 5d3232f + d6e8358 commit ef8c34e

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

docs/CppInteroperability/UserManual.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,35 @@ The following document explains how C++ APIs are imported into Swift and is targ
66

77
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.
88

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:
18+
19+
```c++
20+
#define SWIFT_CXX_REF_MYREFOBJECT \
21+
__attribute__((swift_attr("import_reference"))) \
22+
__attribute__((swift_attr("retain:incRef"))) \
23+
__attribute__((swift_attr("release:decRef")))
24+
25+
class SWIFT_CXX_REF_MYREFOBJECT MyReferenceObject {
26+
private:
27+
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+
938
## Owned Types
1039
1140
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

Comments
 (0)