-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
c++ ffi and RAII #3592
Comments
When I changed
|
Found the ugly workaround: ####################################################################################################
# RAII test for C++ FFI
{.passC: "-std=c++11".}
{.emit: """
#include <memory>
#include <iostream>
class TestObj;
typedef std::shared_ptr<TestObj> TestObjRef;
class TestObj {
private:
TestObj(const TestObj&) = delete;
TestObj() {}
public:
void sayHello() {
std::cout << "Hello from cpp" << std::endl;
}
static TestObjRef mkRef() {
return std::shared_ptr<TestObj>(new TestObj);
}
};
""".}
type
TestObj {.importcpp: "TestObj".} = object
TestObjRef {.importcpp: "TestObjRef".} = object
proc mkRef(): TestObjRef {.importcpp: "TestObj::mkRef".}
proc sayHello(o: TestObj) {.importcpp: "sayHello".}
proc get(o: TestObjRef): ptr TestObj =
{.emit: """
return `o`.get();
""".}
{.experimental.}
when isMainModule:
echo "Hello from nim"
mkRef().get.sayHello() |
No, it doesn't. 'importcpp' cannot ever work for structural types ("ptr", "ref", etc.). The compiler should complain though. |
This issue has been automatically marked as stale because it has not had recent activity. If you think it is still a valid issue, write a comment below; otherwise it will be closed. Thank you for your contributions. |
The original bug disappeared, but the origin issue remains. ####################################################################################################
# RAII test for C++ FFI
{.passC: "-std=c++11".}
{.emit: """
#include <memory>
#include <iostream>
class TestObj;
typedef std::shared_ptr<TestObj> TestObjRef;
class TestObj {
public:
void sayHello() {
std::cout << "Hello from cpp" << std::endl;
}
static TestObjRef mkRef() {
return std::make_shared<TestObj>();
}
};
""".}
type
TestObj {.importcpp: "TestObj".} = object
TestObjRef {.importcpp: "TestObjRef".} = ptr TestObj
proc mkRef(): TestObjRef {.importcpp: "TestObj::mkRef".}
proc sayHello(o: TestObj) {.importcpp: "sayHello".}
when isMainModule:
echo "Hello from nim"
var x = mkRef()
x[].sayHello()
|
this works: #include <memory>
#include <iostream>
class TestObj;
typedef std::shared_ptr<TestObj> TestObjRef;
class TestObj {
public:
void sayHello() {
std::cout << "Hello from cpp" << std::endl;
}
static TestObjRef mkRef() {
return std::make_shared<TestObj>();
}
}; when defined case5:
{.passC: "-std=c++11".}
type
TestObj {.importcpp: "TestObj", header: "t11664c.h".} = object
shared_ptr[T] {.importcpp: "std::shared_ptr", header:"<memory>".} = object
TestObjRef = shared_ptr[TestObj]
proc `[]`[T](a: shared_ptr[T]): T {.importcpp: "(*#)", header: "<memory>".}
proc mkRef(): TestObjRef {.importcpp: "TestObj::mkRef", header: "t11664c.h".}
proc sayHello(o: TestObj) {.importcpp: "sayHello", header: "t11664c.h".}
when isMainModule:
echo "Hello from nim"
var x = mkRef()
doAssert x is TestObjRef
let x2 = x[]
doAssert x2 is TestObj
x[].sayHello()
we need more examples like that in manual or (preferably) some other dedicated section of manual dedicated to interop TODO before closing this
(alternatively, these items can be moved to some dedicated issue tracking them) |
Tried to make an example of c++ ffi and raii:
, but the import for this line is broken:
The compiler generates this definition in cpp code:
But it must use imported symbol, like this:
The text was updated successfully, but these errors were encountered: