检查转换器是否已注册

时间:2012-03-27 11:00:02

标签: converter boost-python

我有几个模块为某些简单类型定义转换器(例如,作为std::vector<int>的整数列表);它们是独立模块的一部分,但有时它们都在一个脚本中使用,这导致

RuntimeWarning: to-Python converter for std::vector<int, std::allocator<int> > already registered; second conversion method ignored.

如何检查某些类型的转换器是否已定义并跳过第二次注册?

2 个答案:

答案 0 :(得分:8)

boost::python::type_info info = boost::python::type_id<YourType>(); 
const boost::python::converter::registration* reg = boost::python::converter::registry::query(info); 
if (reg == NULL)  {
  //register YourType 
} else if ((*reg).m_to_python == NULL) {
  //register YourType 
}

请注意,您还需要检查((*reg).m_to_python == NULL),否则您在某些体系结构中冒险注册不会作为默认构造函数发生,因为已调用注册为YourType分配NULL转换器。在这种情况下,query(info)会返回空注册的地址。

答案 1 :(得分:6)

你可以查询注册表,所以像这样(未经测试)..

#include <boost/python/converter/registry.hpp>

boost::python::type_info info = boost::python::type_id<YourType>();
boost::python::converter::registration* reg = boost::python::converter::registry::query(info);
if (reg == NULL)
{
  //registry YourType
}