所以我有一个 std::map<KeyType, std::shared_ptr<ValueType>>
使用 map_indexing_suite
暴露给 python .
在代码的其他地方,我存储了对 ValueType
的引用 map 中的对象使用原始指针,ValueType*
,因为这些容器不拥有 ValueType
对象, map 。
我的问题是,我怎样才能以一种可以将该引用与共享指针进行比较的方式将原始指针暴露给 python?像这样:
valueRef = getRawReference()
for x in myMap:
if x.data() == valueRef:
print "match"
最佳答案
我自己找到了答案。
首先定义两个方法:
bool eq(std::shared_ptr<ValueType> lhs, ValueType* rhs)
{
return lhs.get() == rhs;
}
bool neq(std::shared_ptr<ValueType> lhs, ValueType* rhs)
{
return lhs.get() != rhs;
}
然后在您的 BOOST_PYTHON_MODULE 中:
bp::def("getRawReference", getRawReference, bp::return_value_policy<bp::reference_existing_object>())
bp::class_<ValueType, std::shared_ptr<ValueType>>("ValueType")
.def("__eq__", eq)
.def("__neq__", neq);
关于c++ - Boost python,将原始指针与托管指针进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10612218/