本文共 1652 字,大约阅读时间需要 5 分钟。
我正在考虑使用python作为一个嵌入式脚本语言的想法,我正在努力的项目,并使大多数工作.但是我似乎无法将python扩展对象转换回本机c指针.
所以这是我的班级:
class CGEGameModeBase
{
public:
virtual void FunctionCall()=0;
virtual const char* StringReturn()=0;
};
class CGEPYGameMode : public CGEGameModeBase, public boost::python::wrapper
{
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", no_init);
class_ >("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
", ident);
CGEPYGameMode* m_pGameMode = extract< CGEPYGameMode* >( GetLocalDict()[newgameplay]() );
但是,当我尝试将Alpha类转换回其基类(上面的最后一行)时,我得到一个提升错误:
TypeError: No registered converter was able to extract a C++ pointer to type class CGEPYGameMode from this Python object of type Alpha
我已经在网上做了很多搜索,但无法解决如何将Alpha对象转换为其基类指针.我可以把它作为一个对象,而不是它作为一个指针,所以一些非python意识的代码可以使用它.有任何想法吗?
转载地址:https://blog.csdn.net/weixin_39849239/article/details/111453092 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!