Skip to content

Commit 2c31db8

Browse files
committed
修改ffpython.h编码格式,修改测试代码
1 parent 55bfaaa commit 2c31db8

3 files changed

Lines changed: 55 additions & 31 deletions

File tree

‎example.cpp‎

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,17 @@
55

66
#include "ffpython.h"
77

8-
#define TestGuard(X, Y) printf("-------%s begin-----------\n", X);try {Y;}catch(exception& e_){printf("exception<%s>\n", e_.what());}\
9-
printf("-------%s end-----------\n", X);
8+
#define TestGuard(X, Y) \
9+
printf("-------%s begin---------\n", X); \
10+
try { \
11+
Y; \
12+
} \
13+
catch(exception& e_) \
14+
{ \
15+
printf("exception<%s>\n", e_.what()); \
16+
} \
17+
printf("-------%s end-----------\n", X); \
18+
printf("\n");
1019

1120
void test_base(ffpython_t& ffpython)
1221
{
@@ -33,9 +42,9 @@ void test_return_stl(ffpython_t& ffpython)
3342
ret_t val = ffpython.call<ret_t>("fftest", "test_return_stl");
3443
}
3544

36-
static int print_val(int a1, int a2, const string& a3, const vector<double>& a4)
45+
static int print_val(int a1, double a2, const string& a3, const vector<double>& a4)
3746
{
38-
//printf("%s[%d,%f,%s,%d]\n", __FUNCTION__, a1, a2, a3.c_str(), a4.size());
47+
printf("%s[%d,%f,%s,%d]\n", __FUNCTION__, a1, a2, a3.c_str(), a4.size());
3948
return 0;
4049
}
4150
struct ops_t
@@ -48,12 +57,8 @@ struct ops_t
4857
}
4958
};
5059

51-
void test_reg_function()
60+
void test_reg_function( ffpython_t& ffpython)
5261
{
53-
ffpython_t ffpython;//("ext1");
54-
ffpython.reg(&print_val, "print_val")
55-
.reg(&ops_t::return_stl, "return_stl");
56-
ffpython.init("ext1");
5762
ffpython.call<void>("fftest", "test_reg_function");
5863
}
5964

@@ -66,7 +71,7 @@ class foo_t
6671
}
6772
virtual ~foo_t()
6873
{
69-
printf("%s\n", __FUNCTION__);
74+
printf("%s<%d>\n", __FUNCTION__, m_value);
7075
}
7176
int get_value() const { return m_value; }
7277
void set_value(int v_) { m_value = v_; }
@@ -112,15 +117,24 @@ void test_register_base_class(ffpython_t& ffpython)
112117
ffpython.reg_class<dumy_t, PYCTOR(int)>("dumy_t", "dumy_t class inherit foo_t ctor <int>", "foo_t")
113118
.reg(&dumy_t::dump, "dump");
114119

115-
ffpython.reg(obj_test, "obj_test");
120+
ffpython.reg(&obj_test, "obj_test")
121+
.reg(&print_val, "print_val")
122+
.reg(&ops_t::return_stl, "return_stl");
116123

117124
ffpython.init("ext2");
118125
ffpython.call<void>("fftest", "test_register_base_class");
119126
};
120127

121128
void test_register_inherit_class(ffpython_t& ffpython)
122129
{
123-
ffpython.call<void>("fftest", "test_register_inherit_class");
130+
foo_t* p = ffpython.call<foo_t*>("fftest", "test_register_inherit_class");
131+
132+
if( p )
133+
{
134+
printf( "p's value is %d\n", p->m_value );
135+
p->set_value( 3333 );
136+
printf( "p's value is %d\n", p->m_value );
137+
}
124138
};
125139

126140
void test_cpp_obj_to_py(ffpython_t& ffpython)
@@ -137,8 +151,12 @@ void test_cpp_obj_py_obj(ffpython_t& ffpython)
137151
{
138152
dumy_t tmp_foo(2013);
139153

140-
//foo_t* p = ffpython.call<foo_t*>("fftest", "test_cpp_obj_py_obj", &tmp_foo);
141-
//p = NULL;
154+
foo_t* p = ffpython.call<foo_t*>("fftest", "test_cpp_obj_py_obj", &tmp_foo);
155+
156+
if( p )
157+
printf( "p's value is %d\n", p->m_value );
158+
159+
// p = NULL;
142160
}
143161

144162
void test_py_class_lambda(ffpython_t& ffpython)
@@ -160,10 +178,10 @@ int main(int argc, char* argv[])
160178
ffpython_t::add_path("./");
161179
ffpython_t ffpython;//("ext2");
162180

181+
TestGuard("test_register_base_class", test_register_base_class(ffpython));
163182
TestGuard("test_base", test_base(ffpython));
164183
TestGuard("test_stl", test_stl(ffpython));
165-
TestGuard("test_reg_function", test_reg_function());
166-
TestGuard("test_register_base_class", test_register_base_class(ffpython));
184+
TestGuard("test_reg_function", test_reg_function(ffpython));
167185

168186
TestGuard("test_register_inherit_class", test_register_inherit_class(ffpython));
169187
TestGuard("test_cpp_obj_to_py", test_cpp_obj_to_py(ffpython));

‎ffpython.h‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#ifndef __FFPYTHON_H_
1+
#ifndef __FFPYTHON_H_
22
#define __FFPYTHON_H_
33

44
#include <Python.h>
@@ -1169,7 +1169,7 @@ class ffpython_t
11691169
for (size_t i = 0; i < m_all_pyclass.size(); ++i)
11701170
{
11711171
m_all_pyclass[i].static_pytype_info->mod_name = m_mod_name;
1172-
if (false == m_all_pyclass[i].inherit_name.empty())//! ���ڻ���
1172+
if (false == m_all_pyclass[i].inherit_name.empty())//! ???????
11731173
{
11741174
pyclass_regigster_tool_t* inherit_class = get_pyclass_info_by_name(m_all_pyclass[i].inherit_name);
11751175
assert(inherit_class && "base class must be registed");
@@ -1297,7 +1297,7 @@ class ffpython_t
12971297
"import %s\n"
12981298
"class %s(object):\n"
12991299
"\t'''%s'''\n"
1300-
"\tdef __init__(self, %s assign_obj_ = 0):\n"//! ����init����
1300+
"\tdef __init__(self, %s assign_obj_ = 0):\n"//! ????init????
13011301
"\t\t'''%s'''\n"
13021302
"\t\tif True == isinstance(assign_obj_, %s):\n"
13031303
"\t\t\tself.obj = assign_obj_\n"
@@ -1315,7 +1315,7 @@ class ffpython_t
13151315

13161316
string gen_class_str = buff;
13171317
SAFE_SPRINTF(buff, sizeof(buff),
1318-
"\tdef delete(self):\n"//! ����init����
1318+
"\tdef delete(self):\n"//! ????init????
13191319
"\t\t'''delete obj'''\n"
13201320
"\t\tself.obj.delete()\n");
13211321
gen_class_str += buff;
@@ -2044,7 +2044,7 @@ struct pytype_traits_t<map<T, R> >
20442044
static const char* get_typename() { return "map";}
20452045
};
20462046

2047-
//! ��ȡpython�����ķ���ֵ,������
2047+
//! ???python??????????,??????
20482048
template<typename T>
20492049
class pytype_tool_impl_t: public pytype_tool_t
20502050
{
@@ -2898,7 +2898,7 @@ struct pyext_func_traits_t<RET (*)(ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7, ARG
28982898
}
28992899
};
29002900

2901-
//! ��������pyclass ��ʼ������
2901+
//! ????????pyclass ?????????
29022902
template <typename CLASS_TYPE>
29032903
struct pyclass_ctor_tool_t<CLASS_TYPE, int(*)()>
29042904
{

‎fftest.py‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1+
import ext2
12

3+
class tstSubClass(ext2.foo_t):
4+
def __init__(self,data):
5+
super(tstSubClass,self).__init__(data)
6+
def showValue(self):
7+
print 'tstSubClass:', self.m_value
28

39
def test_base(a1, a2, a3):
410
print('test_base', a1, a2, a3)
@@ -15,13 +21,11 @@ def test_return_stl():
1521
return ret
1622

1723
def test_reg_function():
18-
import ext1
19-
ext1.print_val(123, 45.6 , "----789---", [3.14])
20-
ret = ext1.return_stl()
24+
ext2.print_val(123, 45.6 , "----789---", [3.14])
25+
ret = ext2.return_stl()
2126
print('test_reg_function', ret)
2227

2328
def test_register_base_class():
24-
import ext2
2529
foo = ext2.foo_t(20130426)
2630
print("test_register_base_class get_val:", foo.get_value())
2731
foo.set_value(778899)
@@ -30,22 +34,24 @@ def test_register_base_class():
3034
print('test_register_base_class test_register_base_class', foo)
3135

3236
def test_register_inherit_class():
33-
import ext2
3437
dumy = ext2.dumy_t(20130426)
3538
print("test_register_inherit_class get_val:", dumy.get_value())
3639
dumy.set_value(778899)
3740
print("test_register_inherit_class get_val:", dumy.get_value(), dumy.m_value)
3841
dumy.test_stl({"key": [11,22,33] })
3942
dumy.dump()
4043
print('test_register_inherit_class', dumy)
44+
a = tstSubClass(222)
45+
a.showValue()
46+
return a
47+
4148

4249
def test_cpp_obj_to_py_ext(foo):
4350
print('test_cpp_obj_to_py_ext', len(foo))
4451
for k in range(0, len(foo)):
4552
print('test_cpp_obj_to_py_ext', k, foo[k].m_value)
4653

4754
def test_cpp_obj_to_py(foo):
48-
import ext2
4955
print("test_cpp_obj_to_py get_val:", foo.get_value())
5056
foo.set_value(778899)
5157
print("test_cpp_obj_to_py get_val:", foo.get_value(), foo.m_value)
@@ -54,16 +60,16 @@ def test_cpp_obj_to_py(foo):
5460
print('test_cpp_obj_to_py test_register_base_class', foo)
5561

5662
def test_cpp_obj_py_obj(dumy):
57-
import ext2
5863
print("test_cpp_obj_py_obj get_val:", dumy.get_value())
5964
dumy.set_value(778899)
6065
print("test_cpp_obj_py_obj get_val:", dumy.get_value(), dumy.m_value)
6166
dumy.test_stl({"key": [11,22,33] })
6267
dumy.dump()
6368
ext2.obj_test(dumy)
69+
dumy.m_value = 100
6470
print('test_cpp_obj_py_obj', dumy)
65-
66-
return dumy
71+
a = ext2.dumy_t(2000)
72+
return a
6773

6874
class pyclass_t:
6975
def __init__(self):

0 commit comments

Comments
 (0)