Storing Different Types of Swift Objects in C++ Using New Interop

Hello Everyone,

I'm working on an app that utilizes the new C++ - Swift interop feature. I have a module called ModuleA that contains multiple Swift classes, and I need to store instances of these classes in a C++ class as a class member(to ensure ARC until the class object is deallocated). However, I want to retain the Swift class objects on the stack without directly allocating heap memory from C++.

Sample Swift Code:

public class SwiftClassA {
    public init() {}
 
    public func FuncA() -> Void {
        // Perform operations specific to SwiftClassA    }
}
 public class SwiftClassB {
    public init() {}
 
    public func FuncA() -> Void {
        // Perform operations specific to SwiftClassB
    }
}
 
// Additional Swift classes (SwiftClassC to SwiftClassN) follow a similar structure.

Sample Cpp Code:

CppClass.hpp

#include "ModuleA-Swift.h" // Include generated Swift headerclass 

CppClass {

 public:
    // Functions and declarationsprivate:
        XYZ vClassObject; // Placeholder for Any Swift class object
};
 

CppClass.cpp

#include "ModuleA-Swift.h" // Include generated Swift headervoid 

CppClass::SomeFuncA() noexcept 
{

    ModuleA::SwiftClassA obj = ModuleA::SwiftClassA::init(); // Initialize SwiftClassA object    
 vClassObject = obj; // Assign SwiftClassA object to vClassObject

}   
 void CppClass::SomeFuncB() noexcept {
    ModuleA::SwiftClassB obj = ModuleA::SwiftClassB::init(); // Initialize SwiftClassB object
    vClassObject = obj; // How do I Assign SwiftClassB object to vClassObject?
}

I'm looking for suggestions on how to efficiently store different types of Swift class objects in my C++ class while maintaining stack-based object retention and proper memory management. Any help or insights would be greatly appreciated.

Thanks,

Harshal

I don’t have any answers here, alas. But I will note that there’s a conversation going on about this right now on Swift Forums.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Hello @DTS Engineer , Although the conversation is about new interop, but the conversation is about missing generated header to use swift apis in cpp, but my problem is regarding storing different types of swift class objects in a common artefact as retained objects in cpp.

Fair enough. Still, I think you’re more likely to get traction on this issue over on Swift Forums than here.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Thank you @DTS Engineer !

I've also posted a similar query in Swift Forums. Here's the link in case someone comes across it later.

Storing Different Types of Swift Objects in C++ Using New Interop
 
 
Q