基于boostpython,C++父類、子類同時導入Python并分別被繼承的最佳實踐

需求背景

項目實際優(yōu)化中會將部分較為穩(wěn)定的邏輯進行C++化,當邏輯較為復雜時難免出現(xiàn)使用面向?qū)ο蟮脑O計方案,但未來有可能出現(xiàn)「對C++邏輯進行擴展或修改,且不修改C++代碼」

需求抽象

  • C++中存在TestBase、TestCppSub兩個類
  • TestCppSub繼承于TestBase
  • 將TestBase、TestCppSub分別導入Python,并在Python中實例化、方法調(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測試代碼:

TestBase().f()
cpp_call_f(TestBase())

cpp: call TestBaseWrapper py_f
cpp: call TestBase f

cpp: 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 f

cpp: call TestBaseWrapper f
cpp: call TestCppSub f

cpp: call TestCppSubWrapper py_g
cpp: call TestCppSub g

cpp: 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 f

py: call TestPySub2 g

cpp: call TestCppSubWrapper g
py: call TestPySub2 g

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容