【问题标题】:Convert type into python将类型转换为python
【发布时间】:2011-03-31 13:51:37
【问题描述】:

我正在使用 boost python。我已经导出了一些函数,它在参数中采用 CL_DomElement 类。现在,当我运行应用程序时,我有:

TypeError: No to_python (by-value) converter found for C++ type: CL_DomElement

那么代码呢。我已经导出了在参数中使用函数指针的函数。代码如下:

typedef boost::function<boost::shared_ptr<Object> (CL_DomElement*, std::string& desc)> Parser;
void registerParser(std::string type, Parser p);

struct ParserProxy
{
    bp::object callable;

    ParserProxy(bp::object callable)
    : callable(callable)
    { }

    boost::shared_ptr<Object> operator()(CL_DomElement* elem, std::string& desc)
    {
        bp::object obj = callable(elem, desc);
        return bp::extract<boost::shared_ptr<Object> >(obj);
    }
};

void registerParserByProxy(std::string type, bp::object callable)
{
    registerParser(type, ParserProxy(callable));
}

// In some boost.python module
bp::def("RegisterParser", registerParserByProxy);

我以这种方式注册它(在 python 中):

class TestObj(Object):
    @staticmethod
    def ParseTestObj(node, desc):
        print 'Parser is called!'
# Register parser
RegisterParser("testobj", TestObj.ParseTestObj)

它成功注册,我检查了我的映射(注册解析器将传递的键→值添加到 std::map 中),那里一切正常(添加了新值)。现在我想调用传递的指针:

boost::shared_ptr<Object> TypesManager::parseObject(CL_DomElement* objectTag, const std::string &type, std::string &desc)
{
    return (getParser(type))(objectTag, desc);
}

getParser 从 std::map 返回函数指针,键为 type


所以,据我了解,通过课程 CL_DomElement 有问题。但我在我的模块中做了:

bp::class_<CL_DomElement>("CL_DomElement");

我认为这不应阻止我描述的此类错误。那么,怎么了?

【问题讨论】:

    标签: c++ python boost types export


    【解决方案1】:
    猜你喜欢
    • 2016-06-07
    • 1970-01-01
    • 2017-05-30
    • 2022-11-18
    • 1970-01-01
    • 2012-03-16
    • 2018-05-06
    • 2020-07-31
    • 1970-01-01
    相关资源
    最近更新 更多