即時通訊的想法是使用python作爲嵌入式腳本語言,用於一個項目即時通訊工作,並獲得大部分工作。但是我似乎無法將python擴展對象轉換回原生C++指針。python對象本地C++指針
所以這是我的課:
class CGEGameModeBase
{
public:
virtual void FunctionCall()=0;
virtual const char* StringReturn()=0;
};
class CGEPYGameMode : public CGEGameModeBase, public boost::python::wrapper<CGEPYGameMode>
{
public:
virtual void FunctionCall()
{
if (override f = this->get_override("FunctionCall"))
f();
}
virtual const char* StringReturn()
{
if (override f = this->get_override("StringReturn"))
return f();
return "FAILED TO CALL";
}
};
加速包裝:
BOOST_PYTHON_MODULE(GEGameMode)
{
class_<CGEGameModeBase, boost::noncopyable>("CGEGameModeBase", no_init);
class_<CGEPYGameMode, bases<CGEGameModeBase> >("CGEPYGameMode", no_init)
.def("FunctionCall", &CGEPYGameMode::FunctionCall)
.def("StringReturn", &CGEPYGameMode::StringReturn);
}
和Python代碼:
import GEGameMode
def Ident():
return "Alpha"
def NewGamePlay():
return "NewAlpha"
def NewAlpha():
import GEGameMode
import GEUtil
class Alpha(GEGameMode.CGEPYGameMode):
def __init__(self):
print "Made new Alpha!"
def FunctionCall(self):
GEUtil.Msg("This is function test Alpha!")
def StringReturn(self):
return "This is return test Alpha!"
return Alpha()
現在我可以調用第一個地精功能做這個:
const char* ident = extract< const char* >(GetLocalDict()["Ident"]());
const char* newgameplay = extract< const char* >(GetLocalDict()["NewGamePlay"]());
printf("Loading Script: %s\n", ident);
CGEPYGameMode* m_pGameMode = extract< CGEPYGameMode* >(GetLocalDict()[newgameplay]());
但是當我嘗試和轉換阿爾法類回其基類(上面最後一行)我得到一個增壓誤差:
TypeError: No registered converter was able to extract a C++ pointer to type class CGEPYGameMode from this Python object of type Alpha
我已經做了很多在網絡上搜索,但不能工作如何將Alpha對象轉換爲其基類指針。我可以把它作爲一個對象,而是把它作爲一個指針,這樣一些非python感知的代碼就可以使用它。有任何想法嗎?
這將是確定的,但我希望腳本擴展,這使得這個不太好 – Lodle 2009-08-31 10:50:21