forked from jmarrec/Cpp_Swig_Ruby_Python_MCVE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Person.i
46 lines (36 loc) · 1.02 KB
/
Person.i
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
#ifndef PERSON_I
#define PERSON_I
%module mylib
%include <stl.i>
%include <std_string.i>
%{
#include <sstream>
#include <Person.hpp>
%}
%include <Person.hpp>
%extend Test::Person {
// Use the overloaded operator<< for string representation
std::string __str__() {
std::ostringstream os;
os << *$self;
return os.str();
}
#ifdef SWIGRUBY
// get an integral representation of the pointer that is this Person
inline long long __toInt() {
std::clog << "original pointer: " << $self << '\n';
const auto result = reinterpret_cast<long long>($self);
std::clog << "toInt from C++ " << result << '\n';
return result;
}
#endif
#ifdef SWIGPYTHON
// take the integer from toInt and reinterpret_cast it back into a Person *, then return that as a reference
static inline Test::Person& _fromInt(long long i) {
auto *ptr = reinterpret_cast<Test::Person *>(i);
std::clog << "Reclaimed pointer: " << ptr << '\n';
return *ptr;
}
#endif
};
#endif //PERSON_I