-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraii.nim
47 lines (34 loc) · 898 Bytes
/
raii.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
####################################################################################################
# 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()