I have a Class defined in C++, I want to pass the instance of class from C++ to Swift as a reference type. By default swift maps C++ classes as value types but we can change this behavior by using SWIFT_IMMORTAL_REFERENCE annotation mentioned here. The example mentioned here is of Singelton class but I have a usecase where i require more than one instance.
Cpp Class Skeleton.
class Cpp {
public:
void Print () noexcept;
void SetValue (int pValue) noexcept;
// Method which is Invoked by Swift.
static Cpp& ReturnObj () noexcept;
private:
int vValue;
} SWIFT_IMMORTAL_REFERENCE;
Definition of Return Obj
Cpp&
Cpp::ReturnObj () noexcept {
static Cpp obj;
return obj;
}
Swift Co
var obj : Cpp = Cpp.ReturnObj()
withUnsafeBytes(of: &obj) {(pointer : UnsafeRawBufferPointer) in
print (pointer)
print (pointer.baseAddress!)
}
Output
Address Printed by C++
0x100008000
Address Printed by Swift
0x00007ff7bfeff108
So from the above observation copy is passed.
How to do pass by reference then?
Post
Replies
Boosts
Views
Activity
I have a Usecase where I want to pass user-defined swift structure instance from Swift to C++ as argument to the C++ Function.
In the documentation it's mentioned that swift exposes these structures to c++.
Swift Structure.
public struct MyStruct {
public init (_ pValue : Int) {
uValue = pValue
}
public var uValue : Int
}
I am able to Create Instance in C++ .
Code
void
CppClass::CreateSwiftStruct ()
{
Interop::MyStruct my_struct = Interop::MyStruct::init (20);
}
But when I define a C++ Function which takes Interop::MyStruct as argument then that function doesn't get exposed to swift, so i am not able to call it.
Skeleton For CppClass
class CppClass {
static void PassStruct (Interop::MyStruct pStruct);
static void Test ();
}
Here PassStruct Method doesn't get exposed to C++ but Test does.
How can I pass Struct Instance in swift to C++ Function as In Param?
I have a struct defined in Swift, i want to pass it's instance pointer from swift to C++. When I am trying to directly return Typed Pointer from Swift Function to C++, the function doesn't get expose to C++.
Code which i have tried.
// Defined Structure
public struct MyStruct {
public init (_ pValue : Int) {
uValue = pValue
}
public var uValue : Int
}
var my_struct = MyStruct(20)
// Function which returns Struct Pointer to C++
// When I return typed pointer this function doesn't get exposed to C++
public func PassStructPointer () -> UnsafeMutablePointer<MyStruct> {
withUnsafeMutablePointer(to: &my_struct) { pointer in
return pointer
}
}
But when I pass UnsafeRawMutablePointer instead of type pointer then the function does get expose to C++
var my_struct = MyStruct(20)
// This get expose to C++.
public func PassStructPointer () -> UnsafeMutableRawPointer {
return withUnsafeMutableBytes(of: &my_struct) { pointer in
return pointer.baseAddress!
}
}
Can we not pass typed pointer of the types defined by us?
I am trying to use the swift type UnsafeMutablePointer directly in C++. According to the documentation mentioned, swift expose this type to C++. But I am not able to use it .
void
GetPointerFromSwift () {
// Calls a swift function to get a pointer.
swift::UnsafeMutablePointer<swit::Int> x = Interop::GetPointer ()
}
I have a use case where I want to return reference from Swift Function just like we can do in C++.
This is How we do it in C++:
int & ReturnIntRef () noexcept
{
static int a = 4;
return a;
}
Do we have equivalent of this in Swift ?
When i instantiate a structure defined in swift in C++ and then i pass it to swift function as a IN param, it is passed as a constant value to the function. Here the same structure instance is passed or a copy is created?
When we pass a input to swift function it is passed as a constant, so does copy gets created or not here?
public func Test (_ pValue : Int) {
print (pValue)
}
let x : Int = 2
Test (x)
I have a usecase, where I have Data instance as Data is Value type copy will be created on assignment. I want to prevent copying for which I was using this Initializer of Data. Will it prevent copying?.
I have a use-case where I want to pass the user defined swift structure instantiated in swift and then pass it to a C++ Function as a Input Param. Is there a way to do that?
Hi, I was trying to understand how swift manages it memory just wanted to verify my understanding on it.
For Value Types i.e. Struct ARC (Automatic Reference Counting) is not there, Memory is Managed/Confined on the basis of scope of that Variable. And For Struct whenver we do assignment a Copy is been created.
For Classes, Swift Manages Memroy with the help of ARC i.e. whenever I create a instance of class its reference count get increased and when we assign same instance to new variable then it also result in increment of Reference Count. The Memory will get deallocated when all the variables pointing to that object are no longer in use.
I am using withUnsafeMutablePointer to get raw pointer from Data. Once I get a Pointer I follow these steps:
I create a Wrapper using that Pointer.
After that I increase it ARC and pass it as a opaque to C++
When I was on First Step if my thread get suspended and after some time if it resumes then is there a possibility that the Memory will get freed due to ARC.
Adding basic Code Flow depicting what i am doing.
public class DataHolder {
public init () {}
public var data_wrapper : Data?
}
func InternalReceiveHandler (_ pContent : Data?) -> Void {
var holder : DataHolder = DataHolder.init ()
withUnsafeMutablePointer (to : &pContent) { data_pointer in
holder.data_wrapper = Data.init (noBytesCopy : data_pointer, count : no_of_bytes, deallocator : .none)
return Unmanaged.passRetained (holder).toOpaque ()
}
}
Is there a possibility that when I am creating the wrapper my thread get suspended and when it get resumed the Memory the pointer was pointing can be freed by ARC.
I want to get the network-name (domain-name) on my Mac-Machine. Where iin the Settings does this domain name gets configured. I refer to this page which talks about computer name and host name, I could find where my hostname is present (Settings-&gt;General-&gt;Sharing-&gt;local host name) but not anything related to the network-name (local -domain) .
Even try to fetch this info using the linux api to getdomainname, api call succeeded but it returns Nothing.
#include &lt;iostream&gt;
#include &lt;unistd.h&gt;
#include &lt;limits.h&gt;
#include &lt;cstring&gt;
int main() {
char domainname[255];
// Get the domain name
if (getdomainname(domainname, 255) != 0) {
std::cout &lt;&lt; "Error getting domain name" &lt;&lt; std::endl;
return 1;
}
std::cout &lt;&lt; "Domain name: " &lt;&lt; domainname &lt;&lt; std::endl;
return 0;
}
Output
Domain name:
I even came across Search-Domains, Does it have anything to do with the network-name (domain name of the machine)?
I have a use-case were I want to use the the FQDN (Fully Qualified Domain Name) in IOS-Device, which can be used to connect to a Device instead of using the IP-Address. FQDN will be consisting of the machine-name or host-name (Most common term) and the domain-name of the network i.e network-name (local domain assigned to that device). Which IOS Api can be used Here?