升压蟒蛇,比较原始的指针托管指针? [英] Boost python, compare raw pointer to managed pointer?
问题描述
所以我有一个的std ::地图<关键字类型,性病:: shared_ptr的<值类型>>
使用暴露到蟒蛇 map_indexing_suite
。
在在code我存到值类型
使用原始指针在地图对象,的ValueType * $引用其他地方C $ C>,因为这些容器没有自己的
值类型
对象,地图一样。
我的问题是,我怎么能生指针暴露在蟒蛇在某种程度上,它可以比较的参照共享指针?事情是这样的:
valueRef = getRawReference()
在MYMAP X:
如果x.data()== valueRef:
打印匹配
找到自己的答案。
首先定义了两个方法:
布尔EQ(的std :: shared_ptr的<&值类型GT; LHS,值类型* RHS)
{
返回lhs.get()== RHS;
}布尔NEQ(的std :: shared_ptr的<&值类型GT; LHS,值类型* RHS)
{
返回lhs.get()= RHS!;
}
然后在您的BOOST_PYTHON_MODULE:
BP :: DEF(getRawReference,getRawReference,BP :: return_value_policy< BP :: reference_existing_object>())BP :: class_<值类型,性病:: shared_ptr的<&值类型GT;>(值类型)
.DEF(__ eq__当量)
.DEF(__ neq__,NEQ);
So I have an std::map<KeyType, std::shared_ptr<ValueType>>
exposed up to python using map_indexing_suite
.
In other places in the code I store references to the ValueType
objects in the map using raw pointers, ValueType*
, because those containers don't own the ValueType
objects, the map does.
My question is, how can I expose the raw pointer to python in a way it can compare that reference to the shared pointer? Something like this:
valueRef = getRawReference()
for x in myMap:
if x.data() == valueRef:
print "match"
Found the answer myself.
First define two methods:
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;
}
Then in your 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);
这篇关于升压蟒蛇,比较原始的指针托管指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!