forked from fanchy/ffpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.cpp
More file actions
198 lines (165 loc) · 5.14 KB
/
example.cpp
File metadata and controls
198 lines (165 loc) · 5.14 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#ifdef _WIN32
#include <cstdlib>
#endif
#include "ffpython.h"
#define TestGuard(X, Y) \
printf("-------%s begin---------\n", X); \
try { \
Y; \
} \
catch(exception& e_) \
{ \
printf("exception<%s>\n", e_.what()); \
} \
printf("-------%s end-----------\n", X); \
printf("\n");
void test_base(ffpython_t& ffpython)
{
printf("sys.version=%s\n", ffpython.get_global_var<string>("sys", "version").c_str());
ffpython.set_global_var("fftest", "global_var", "OhNice");
printf("fftest.global_var=%s\n", ffpython.get_global_var<string>("fftest", "global_var").c_str());
printf("time.asctime=%s\n", ffpython.call<string>("time", "asctime").c_str());
int a1 = 100; float a2 = 3.14f; string a3 = "OhWell";
ffpython.call<void>("fftest", "test_base", a1, a2, a3);
}
void test_stl(ffpython_t& ffpython)
{
vector<int> a1;a1.push_back(100);a1.push_back(200);
list<string> a2; a2.push_back("Oh");a2.push_back("Nice");
vector<list<string> > a3;a3.push_back(a2);
ffpython.call<bool>("fftest", "test_stl", a1, a2, a3);
}
void test_return_stl(ffpython_t& ffpython)
{
typedef map<string, list<vector<int> > > ret_t;
ret_t val = ffpython.call<ret_t>("fftest", "test_return_stl");
}
static int print_val(int a1, double a2, const string& a3, const vector<double>& a4)
{
printf("%s[%d,%f,%s,%d]\n", __FUNCTION__, a1, a2, a3.c_str(), a4.size());
return 0;
}
struct ops_t
{
static list<int> return_stl()
{
list<int> ret;ret.push_back(1024);
printf("%s\n", __FUNCTION__);
return ret;
}
};
void test_reg_function( ffpython_t& ffpython)
{
ffpython.call<void>("fftest", "test_reg_function");
}
class foo_t
{
public:
foo_t(int v_):m_value(v_)
{
printf("%s\n", __FUNCTION__);
}
virtual ~foo_t()
{
printf("%s<%d>\n", __FUNCTION__, m_value);
}
int get_value() const { return m_value; }
void set_value(int v_) { m_value = v_; }
void test_stl(map<string, list<int> >& v_)
{
printf("%s\n", __FUNCTION__);
}
int m_value;
};
class dumy_t: public foo_t
{
public:
dumy_t(int v_):foo_t(v_)
{
printf("%s\n", __FUNCTION__);
}
~dumy_t()
{
printf("%s\n", __FUNCTION__);
}
void dump()
{
printf("%s\n", __FUNCTION__);
}
};
static foo_t* obj_test(dumy_t* p)
{
printf("%s\n", __FUNCTION__);
return p;
}
void test_register_base_class(ffpython_t& ffpython)
{
ffpython.reg_class<foo_t, PYCTOR(int)>("foo_t")
.reg(&foo_t::get_value, "get_value")
.reg(&foo_t::set_value, "set_value")
.reg(&foo_t::test_stl, "test_stl")
.reg_property(&foo_t::m_value, "m_value");
ffpython.reg_class<dumy_t, PYCTOR(int)>("dumy_t", "dumy_t class inherit foo_t ctor <int>", "foo_t")
.reg(&dumy_t::dump, "dump");
ffpython.reg(&obj_test, "obj_test")
.reg(&print_val, "print_val")
.reg(&ops_t::return_stl, "return_stl");
ffpython.init("ext2");
ffpython.call<void>("fftest", "test_register_base_class");
};
void test_register_inherit_class(ffpython_t& ffpython)
{
foo_t* p = ffpython.call<foo_t*>("fftest", "test_register_inherit_class");
if( p )
{
printf( "p's value is %d\n", p->m_value );
p->set_value( 3333 );
printf( "p's value is %d\n", p->m_value );
}
};
void test_cpp_obj_to_py(ffpython_t& ffpython)
{
foo_t tmp_foo(2013);
vector<foo_t*> vt;
vt.push_back(&tmp_foo);
ffpython.call<void>("fftest", "test_cpp_obj_to_py", &tmp_foo);
printf("test_cpp_obj_to_py changed m_value=%d\n", tmp_foo.m_value);
ffpython.call<void>("fftest", "test_cpp_obj_to_py_ext", vt);
}
void test_cpp_obj_py_obj(ffpython_t& ffpython)
{
dumy_t tmp_foo(2013);
foo_t* p = ffpython.call<foo_t*>("fftest", "test_cpp_obj_py_obj", &tmp_foo);
if( p )
printf( "p's value is %d\n", p->m_value );
// p = NULL;
}
void test_py_class_lambda(ffpython_t& ffpython)
{
PyObject* pobj = ffpython.call<PyObject*>("fftest", "test_cpp_obj_return_py_obj");
ffpython.obj_call<void>(pobj, "sayHi", 1, string("soNice"));
PyObject* pFunc= ffpython.call<PyObject*>("fftest", "test_cpp_obj_return_py_lambda");
ffpython.call_lambda<void>(pFunc, 112233);
Py_XDECREF(pFunc);
Py_XDECREF(pobj);
}
int main(int argc, char* argv[])
{
Py_Initialize();
ffpython_t::add_path("./");
ffpython_t ffpython;//("ext2");
TestGuard("test_register_base_class", test_register_base_class(ffpython));
TestGuard("test_base", test_base(ffpython));
TestGuard("test_stl", test_stl(ffpython));
TestGuard("test_reg_function", test_reg_function(ffpython));
TestGuard("test_register_inherit_class", test_register_inherit_class(ffpython));
TestGuard("test_cpp_obj_to_py", test_cpp_obj_to_py(ffpython));
TestGuard("test_cpp_obj_py_obj", test_cpp_obj_py_obj(ffpython));
TestGuard("test_py_class_lambda", test_py_class_lambda(ffpython));
#ifdef _WIN32
system("pause");
#endif
Py_Finalize();
printf("main exit...\n");
return 0;
}