https://github.com/shoyamanishi/swift-objc-cpp-interoperability
This is a tiny sample project to show a technique for the two-way communication between one Swift object and another in C++ via Objective-C.
https://github.com/shoyamanishi/swift-objc-cpp-interoperability
cpp interoperability objective-c swift
Last synced: 3 months ago
JSON representation
This is a tiny sample project to show a technique for the two-way communication between one Swift object and another in C++ via Objective-C.
- Host: GitHub
- URL: https://github.com/shoyamanishi/swift-objc-cpp-interoperability
- Owner: ShoYamanishi
- License: cc0-1.0
- Created: 2023-01-14T18:49:09.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-01-14T19:04:24.000Z (over 3 years ago)
- Last Synced: 2025-02-06T20:35:27.504Z (over 1 year ago)
- Topics: cpp, interoperability, objective-c, swift
- Language: Swift
- Homepage:
- Size: 206 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Swift-ObjC-Cpp Interoperability
This is a tiny sample project to show a technique for the two-way communication between one **Swift** object and another in **C++** via Objective-C.
# Background and Motivation
The *bridging header* makes it possible for Swift objects to access Obj-C objects. And *[Project]-Swift.h* precompiled header makes it possible for Obj-C objects to access Swift objects, whose classes and functions are makred by `@objc`. The interoperability between Obj-C and C++ are well defined, though it is a bit cumbersome, if you want to completely isolate the C++ code from the Obj-C++.
It becomes a bit tricky when you want to achieve the communication in both ways, as you have to cope with some cyclic definition situations with some forward declarations. And it is probably worth making a project for my own future reference and others.
# Overview
* `SwiftEnd.swift` : The Swift class that communicates with `CppEnd`. It instantiates and owns `Shim`.
* `CppEnd.{hpp,cpp}` : The C++ class that commnicates with `SwiftEnd`. It is instantiated by `Shim`. It has a reference to `ReverseShim`.
* `Shim.{h,mm}` : The shim in Obj-C class. It instantiates and owns `CppEnd` and `ReverseShim`. It has a reference to `SwiftEnd`.
* `ReverseShim.{h,mm}` : Another small shim in C++ class with some Obj-C code inits implementation. It has a reference to `Shim`. It facilitates the communication from `CppEnd` to `Shim`.
# Call Sequence in the Sample Program.
The `SwiftEnd` object is instantiated in `App` and it is pushed to SwiftUI as an environment object.
It is picked up in `ContentView`. The button in the view calls `SwiftEnd`'s `callCpp()` function.
1. ContentView calls SwiftEnd.callToCpp()
2. SwiftEnd's callCpp() calls [ Shim callToCPP ].
3. Shim calls CppEnd::receiveFromSwift().
4. CppEnd' receiveFromSwift() in turn calls ReverseShim::callToSwift().
5. ReverseShim's callToSwift() calls [ Shim callToSwift ].
6. Shim's callToSwift calls SwiftEnd.receiveFromCpp().
This call chain makes a roundtrip from SwiftEnd to CppEnd and back to SwiftEnd.
