需求背景
項(xiàng)目實(shí)際優(yōu)化中會(huì)將部分較為穩(wěn)定的邏輯進(jìn)行C++化愉舔,當(dāng)邏輯較為復(fù)雜時(shí)難免出現(xiàn)使用面向?qū)ο蟮脑O(shè)計(jì)方案钢猛,但未來(lái)有可能出現(xiàn)「對(duì)C++邏輯進(jìn)行擴(kuò)展或修改,且不修改C++代碼」
需求抽象
- C++中存在TestBase轩缤、TestCppSub兩個(gè)類
- TestCppSub繼承于TestBase
- 將TestBase命迈、TestCppSub分別導(dǎo)入Python,并在Python中實(shí)例化火的、方法調(diào)用
- 在Python中聲明TestPySub1繼承于TestBase壶愤、TestPySub2繼承于TestCppSub
C++代碼:
class TestBase
{
public:
virtual void f()
{
cout<<"cpp: call TestBase f"<<endl;
}
};
class TestCppSub : public TestBase
{
public:
void f() override
{
cout << "cpp: call TestCppSub f" << endl;
}
virtual void g()
{
cout << "cpp: call TestCppSub g" << endl;
}
};
template <typename T>
class TestBaseWrapper : public T, public wrapper<T>
{
public:
void f() override
{
cout << "cpp: call TestBaseWrapper f" << endl;
if (auto f = this->get_override("f"))
{
f();
return;
}
this->T::f();
}
void py_f()
{
cout << "cpp: call TestBaseWrapper py_f" << endl;
this->T::f();
}
};
template <class W, class X1, class X2, class X3>
void bind_test_base_to_py(class_<W, X1, X2, X3>& binder)
{
binder
.def("f", &W::py_f)
;
}
template <typename T>
class TestCppSubWrapper : public TestBaseWrapper<T>
{
public:
void g() override
{
cout << "cpp: call TestCppSubWrapper g" << endl;
if (auto g = this->get_override("g"))
{
g();
return;
}
this->T::g();
}
void py_f()
{
cout << "cpp: call TestCppSubWrapper py_f" << endl;
this->T::f();
}
void py_g()
{
cout << "cpp: call TestCppSubWrapper py_g" << endl;
this->T::g();
}
};
template <class W, class X1, class X2, class X3>
void bind_test_cpp_sub_to_py(class_<W, X1, X2, X3>& binder)
{
bind_test_base_to_py<W>(binder);
binder
.def("g", &W::py_g)
;
}
void cpp_call_f(TestBase* obj)
{
obj->f();
}
void cpp_call_g(TestCppSub* obj)
{
obj->g();
}
auto test_base_binder = class_<TestBaseWrapper<TestBase>, boost::noncopyable>("TestBase", init<>());
bind_test_base_to_py(test_base_binder);
auto test_cpp_sub_binder = class_<TestCppSubWrapper<TestCppSub>, bases<TestBase>, boost::noncopyable>("TestCppSub", init<>());
bind_test_cpp_sub_to_py(test_cpp_sub_binder);
Python測(cè)試代碼:
TestBase().f()
cpp_call_f(TestBase())
cpp: call TestBaseWrapper py_f
cpp: call TestBase fcpp: call TestBaseWrapper f
cpp: call TestBase f
print isinstance(TestCppSub(), TestBase)
TestCppSub().f()
cpp_call_f(TestCppSub())
TestCppSub().g()
cpp_call_g(TestCppSub())
True
cpp: call TestCppSubWrapper py_f
cpp: call TestCppSub fcpp: call TestBaseWrapper f
cpp: call TestCppSub fcpp: call TestCppSubWrapper py_g
cpp: call TestCppSub gcpp: call TestCppSubWrapper g
cpp: call TestCppSub g
class TestPySub1(TestBase):
def f(self):
print "py: call TestPySub1 f"
print isinstance(TestPySub1(), TestBase)
TestPySub1().f()
cpp_call_f(TestPySub1())
True
py: call TestPySub1 f
cpp: call TestBaseWrapper f
py: call TestPySub1 f
class TestPySub2(TestCppSub):
def f(self):
print "py: call TestPySub2 f"
def g(self):
print "py: call TestPySub2 g"
print isinstance(TestPySub2(), TestBase)
print isinstance(TestPySub2(), TestCppSub)
TestPySub2().f()
cpp_call_f(TestPySub2())
TestPySub2().g()
cpp_call_g(TestPySub2())
True
True
py: call TestPySub2 f
cpp: call TestBaseWrapper f
py: call TestPySub2 fpy: call TestPySub2 g
cpp: call TestCppSubWrapper g
py: call TestPySub2 g