Python对象以本地C ++指针 [英] python object to native c++ pointer

查看:136
本文介绍了Python对象以本地C ++指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林的想法玩弄周围使用Python作为嵌入式脚本语言的一个项目IM工作,并已得到了大多数事情工作。但是我不能似乎能够给一个Python扩展对象转换回本地C ++指针。

Im toying around with the idea to use python as an embedded scripting language for a project im working on and have got most things working. However i cant seem to be able to convert a python extended object back into a native c++ pointer.

因此​​,这是我的类:

So this is my class:

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);
}

和蟒蛇code:

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()

现在我可以调用第一个做这个罚款的功能:

Now i can call the first to functions fine by doing this:

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]() );

然而,当我尝试和转换阿尔法类回其基类(上面最后一行)我得到一个提升错误:

However when i try and convert the Alpha class back to its base class (last line above) i get an boost error:

TypeError: No registered converter was able to extract a C++ pointer to type class CGEPYGameMode from this Python object of type Alpha

我已经做了很多在网络上搜索的,但无法工作,如何阿尔法对象转换为基类指针。我可以把它作为一个对象,而是把它作为一个指针,以便一些非蟒蛇知道code可以使用它。任何想法?

I have done alot of searching on the net but cant work out how to convert the Alpha object into its base class pointer. I could leave it as an object but rather have it as a pointer so some non python aware code can use it. Any ideas?

推荐答案

从Python的C ++ mailling名单感谢斯蒂芬,我缺少

Thanks to Stefan from the python c++ mailling list, i was missing

super(Alpha, self).__init__()

从构造函数调用这意味着它从未父类。认为这将被自动的:D

from the constructor call meaning it never made the parent class. Thought this would of been automatic :D

唯一的其他问题,我不得不被保存新的类实例作为一个全局变量,否则它得到了清理,因为它出去的范围。

Only other issue i had was saving the new class instance as a global var otherwise it got cleaned up as it went out of scope.

所以,现在开心

这篇关于Python对象以本地C ++指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆