- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在带有 MSVC2010 的 Windows 上使用 Boost.Python 1.54,我在从 python 将指向一个类的指针存储到第二个类中并检索它时遇到问题。它似乎以某种方式改变了数据类型。
这是我的类(class):
typedef unsigned int uint_t;
struct classA {
int intval;
unsigned int bitfield_member:1;
};
struct Collection {
classA * class_a_ptr;
};
下面是我如何将它们暴露给 python(其中一些代码最初是由 Py++ 自动生成的,但从那时起我就手动编辑了它):
#include <boost/python.hpp>
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/return_value_policy.hpp>
#include <boost/python/manage_new_object.hpp>
namespace bp = boost::python;
struct Collection_wrapper : Collection, bp::wrapper< Collection > {
Collection_wrapper(Collection const & arg )
: Collection( arg )
, bp::wrapper< Collection >(){
// copy constructor
}
Collection_wrapper()
: Collection()
, bp::wrapper< Collection >(){
// null constructor
}
static ::classA * get_class_a_ptr(Collection const & inst ){
return inst.class_a_ptr;
}
static void set_class_a_ptr( Collection & inst, ::classA * new_value ){
inst.class_a_ptr = new_value;
}
};
struct classA_wrapper : classA, bp::wrapper< classA > {
classA_wrapper(classA const & arg )
: classA( arg )
, bp::wrapper< classA >(){
// copy constructor
}
classA_wrapper()
: classA()
, bp::wrapper< classA >(){
// null constructor
}
::uint_t get_bitfield_member() const {
return bitfield_member;
}
void set_bitfield_member( ::uint_t new_value ){
bitfield_member = new_value;
}
};
BOOST_PYTHON_MODULE(render_lib_ext)
{
using namespace bp;
{ //::Collection
typedef bp::class_< Collection_wrapper > Collection_exposer_t;
Collection_exposer_t Collection_exposer = Collection_exposer_t( "Collection" );
bp::scope Collection_scope( Collection_exposer );
// original version, fails
Collection_exposer.add_property( "class_a_ptr"
, bp::make_function( (::classA * (*)( ::Collection const & ))(&Collection_wrapper::get_class_a_ptr), bp::return_internal_reference< >() )
, bp::make_function( (void (*)( ::Collection &,::classA * ))(&Collection_wrapper::set_class_a_ptr), bp::with_custodian_and_ward_postcall< 1, 2 >() ) );
}
{ //::classA
typedef bp::class_< classA_wrapper > classA_exposer_t;
classA_exposer_t classA_exposer = classA_exposer_t( "classA" );
bp::scope classA_scope( classA_exposer );
classA_exposer.def_readwrite( "intval", &classA::intval );
classA_exposer.add_property( "bitfield_member"
, (::uint_t ( classA_wrapper::* )( ) const)(&classA_wrapper::get_bitfield_member)
, (void ( classA_wrapper::* )( ::uint_t ) )(&classA_wrapper::set_bitfield_member) );
}
}
下面是运行它的 python 测试:
import unittest
import render_lib_ext as RL
class TestRenderLib(unittest.TestCase):
def test_globals(self):
coll=RL.Collection()
g = RL.classA()
g.intval=9801;
self.assertEqual(9801, g.intval)
coll.class_a_ptr = g # store pointer in collection
geg = coll.class_a_ptr # retrieve it
self.assertEqual(0, g.bitfield_member) # works
self.assertEqual(0, geg.bitfield_member) # fails with ArgumentError (type error)
self.assertEqual(9801, geg.intval) # fails! Is it not the same object?
它在第一个“失败”行出现此错误而失败:
Traceback (most recent call last):
File "test2.py", line 18, in test_globals
self.assertEqual(0, geg.bitfield_member) # fails with ArgumentError (type error)
ArgumentError: Python argument types in
None.None(classA)
did not match C++ signature:
None(struct classA_wrapper {lvalue})
这对我来说似乎很奇怪,因为 classA_wrapper 扩展了 classA。我究竟做错了什么?有没有不同的方法来做到这一点?我在 python 和 c++ 方面非常有经验,但这是我第一次涉足 Boost.Python。
最佳答案
暴露给 classA
上的 bitfield_member
属性的仿函数需要显式接受它们操作的实例。它相当于 property()
Python 中的方法,其中 fget 和 fset 接受 self
参数。因此,将 bitfield_member
getter 和 setter 函数更改为静态的,并接受 classA&
作为它们的第一个参数。
// ...
struct classA_wrapper: ...
{
// ...
static ::uint_t get_bitfield_member(classA& self)
{
return self.bitfield_member;
}
static void set_bitfield_member(classA& self, ::uint_t new_value)
{
self.bitfield_member = new_value;
}
};
BOOST_PYTHON_MODULE(...)
{
namespace python = boost::python;
// ...
python::class_< classA_wrapper >("classA")
.def_readwrite("intval", &classA::intval)
.add_property("bitfield_member",
&classA_wrapper::get_bitfield_member,
&classA_wrapper::set_bitfield_member)
;
}
}
虽然get_bitfield_member
和set_bitfield_member
在原代码中是成员函数,但是class_a_ptr
返回的PythonclassA
对象似乎没有完全初始化其底层 C++ 类型。这可能是 Boost.Python API 中未定义行为的结果。
问题没有出现在其他地方,因为:
Collection.class_a_ptr
属性的 fget 和 fset 显式接受实例参数。classA.intval
属性使用 def_readwrite
,它将隐式创建 fget 和 fset 接受实例make_getter/make_setter
.这是一个基于原始代码的完整示例:
#include <boost/python.hpp>
typedef unsigned int uint_t;
struct classA
{
int intval;
unsigned int bitfield_member:1;
};
struct Collection
{
classA * class_a_ptr;
};
namespace python = boost::python;
struct Collection_wrapper
: Collection, python::wrapper<Collection>
{
Collection_wrapper() {}
Collection_wrapper(const Collection& self)
: Collection(self)
{}
static ::classA* get_class_a_ptr(const Collection& self)
{
return self.class_a_ptr;
}
static void set_class_a_ptr(Collection& self, ::classA * new_value)
{
self.class_a_ptr = new_value;
}
};
struct classA_wrapper
: classA, python::wrapper<classA>
{
classA_wrapper() {}
classA_wrapper(const classA& self)
: classA(self)
{}
static ::uint_t get_bitfield_member(const classA& self)
{
return self.bitfield_member;
}
static void set_bitfield_member(classA& self, ::uint_t new_value)
{
self.bitfield_member = new_value;
}
};
BOOST_PYTHON_MODULE(example)
{
python::class_<Collection_wrapper>("Collection")
.add_property("class_a_ptr",
python::make_function(&Collection_wrapper::get_class_a_ptr,
python::return_internal_reference<>()),
python::make_function(&Collection_wrapper::set_class_a_ptr,
python::with_custodian_and_ward_postcall<1, 2>()))
;
python::class_<classA_wrapper>("classA")
.def_readwrite("intval", &classA::intval)
.add_property("bitfield_member",
&classA_wrapper::get_bitfield_member,
&classA_wrapper::set_bitfield_member)
;
}
及其用法:
>>> import example
>>> collection = example.Collection()
>>> a = example.classA()
>>> a.intval = 9801
>>> print a.intval
9801
>>> collection.class_a_ptr = a
>>> same_a = collection.class_a_ptr
>>> a.bitfield_member = 0
>>> print a.bitfield_member
0
>>> print same_a.bitfield_member
0
>>> same_a.bitfield_member = 1
>>> print a.bitfield_member
1
>>> print same_a.bitfield_member
1
关于c++ - 在 Boost.Python : wrong type 中的类中存储和检索指针时出现 ArgumentError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19461274/
我需要将文本放在 中在一个 Div 中,在另一个 Div 中,在另一个 Div 中。所以这是它的样子: #document Change PIN
奇怪的事情发生了。 我有一个基本的 html 代码。 html,头部, body 。(因为我收到了一些反对票,这里是完整的代码) 这是我的CSS: html { backgroun
我正在尝试将 Assets 中的一组图像加载到 UICollectionview 中存在的 ImageView 中,但每当我运行应用程序时它都会显示错误。而且也没有显示图像。 我在ViewDidLoa
我需要根据带参数的 perl 脚本的输出更改一些环境变量。在 tcsh 中,我可以使用别名命令来评估 perl 脚本的输出。 tcsh: alias setsdk 'eval `/localhome/
我使用 Windows 身份验证创建了一个新的 Blazor(服务器端)应用程序,并使用 IIS Express 运行它。它将显示一条消息“Hello Domain\User!”来自右上方的以下 Ra
这是我的方法 void login(Event event);我想知道 Kotlin 中应该如何 最佳答案 在 Kotlin 中通配符运算符是 * 。它指示编译器它是未知的,但一旦知道,就不会有其他类
看下面的代码 for story in book if story.title.length < 140 - var story
我正在尝试用 C 语言学习字符串处理。我写了一个程序,它存储了一些音乐轨道,并帮助用户检查他/她想到的歌曲是否存在于存储的轨道中。这是通过要求用户输入一串字符来完成的。然后程序使用 strstr()
我正在学习 sscanf 并遇到如下格式字符串: sscanf("%[^:]:%[^*=]%*[*=]%n",a,b,&c); 我理解 %[^:] 部分意味着扫描直到遇到 ':' 并将其分配给 a。:
def char_check(x,y): if (str(x) in y or x.find(y) > -1) or (str(y) in x or y.find(x) > -1):
我有一种情况,我想将文本文件中的现有行包含到一个新 block 中。 line 1 line 2 line in block line 3 line 4 应该变成 line 1 line 2 line
我有一个新项目,我正在尝试设置 Django 调试工具栏。首先,我尝试了快速设置,它只涉及将 'debug_toolbar' 添加到我的已安装应用程序列表中。有了这个,当我转到我的根 URL 时,调试
在 Matlab 中,如果我有一个函数 f,例如签名是 f(a,b,c),我可以创建一个只有一个变量 b 的函数,它将使用固定的 a=a1 和 c=c1 调用 f: g = @(b) f(a1, b,
我不明白为什么 ForEach 中的元素之间有多余的垂直间距在 VStack 里面在 ScrollView 里面使用 GeometryReader 时渲染自定义水平分隔线。 Scrol
我想知道,是否有关于何时使用 session 和 cookie 的指南或最佳实践? 什么应该和什么不应该存储在其中?谢谢! 最佳答案 这些文档很好地了解了 session cookie 的安全问题以及
我在 scipy/numpy 中有一个 Nx3 矩阵,我想用它制作一个 3 维条形图,其中 X 轴和 Y 轴由矩阵的第一列和第二列的值、高度确定每个条形的 是矩阵中的第三列,条形的数量由 N 确定。
假设我用两种不同的方式初始化信号量 sem_init(&randomsem,0,1) sem_init(&randomsem,0,0) 现在, sem_wait(&randomsem) 在这两种情况下
我怀疑该值如何存储在“WORD”中,因为 PStr 包含实际输出。? 既然Pstr中存储的是小写到大写的字母,那么在printf中如何将其给出为“WORD”。有人可以吗?解释一下? #include
我有一个 3x3 数组: var my_array = [[0,1,2], [3,4,5], [6,7,8]]; 并想获得它的第一个 2
我意识到您可以使用如下方式轻松检查焦点: var hasFocus = true; $(window).blur(function(){ hasFocus = false; }); $(win
我是一名优秀的程序员,十分优秀!