将构造函数添加到 boost::python vector_indexing_suite 公开类
我想添加一个构造函数,这样我就可以做到这一点(我的字节在字符串中,因为我使用的是 python 2.6 和 2.7):
import myboostpymodule
d = 'serialised representation of a vector of some c++ objects'
vec = myboostpymodule.MyVectorType(d)
我重载向量构造函数以接受字符串,该字符串将包含序列化数据,而 MyVectorType 是使用 boost::python::vector_indexing_suite
公开:
namespace bp = boost::python;
bp::class_< std::vector<SomeType> >("MyVectorType")
.def(bp::vector_indexing_suite< std::vector<SomeType> >());
- 是否可以在不包装(通过子类化和 boost::python::wrapper<...>)向量来添加构造函数的情况下执行此操作?
- 我可以告诉 boost python 使用静态方法(或普通函数)作为构造函数吗?
重要的是我可以使用构造函数(而不是其他函数)从 python 创建向量。
<子> 我这样做实际上是因为我想让我的向量的 python 实例可pickle,并且我的所有 c++ 类型都已经具有明确定义的序列化(包括 std::vector<...>
),因此使用它来一次 pickle 整个向量似乎是明智的,而不是让 python 独立 pickle 每个元素1。
1它似乎可能是一个更高性能的解决方案
I'd like to add a constructor so I can do this (my bytes are in strings because I'm using python 2.6 and 2.7):
import myboostpymodule
d = 'serialised representation of a vector of some c++ objects'
vec = myboostpymodule.MyVectorType(d)
Where I overload the vector constructor to accept a string, which will contain serialised data, and MyVectorType is exposed with boost::python::vector_indexing_suite
:
namespace bp = boost::python;
bp::class_< std::vector<SomeType> >("MyVectorType")
.def(bp::vector_indexing_suite< std::vector<SomeType> >());
- Is it possible to do this without wrapping (via subclassing and boost::python::wrapper<...>) the vector to add a constructor?
- Can I tell boost python to use a static method (or a plain function) as the constructor?
It's important that I can create the vectors from python using the constructor (not some other function).
I'm actually doing this because I'd like to make python instances of my vectors pickleable, and all my c++ types already have well-defined serialisation (including std::vector<...>
), so it seems sensible to use this to pickle whole vectors at once, instead of allowing python to pickle each element independently1.
1and it seems likely to be a higher-performance solution
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不认为使用 boost::python::wrapper 会对您有帮助,因为您需要使用构造函数。我查看了 boost::python::vector_indexing_suite 定义,我认为您实际上可以定义自己的构造函数。
boost::python 中有一个函数可以指定您自己的命名构造函数。它通常用于实现返回
shared_ptr
的工厂函数,但看起来像 Python 中的常规构造函数。您可以查看 http://wiki.python.org/moin/boost.python/HowTo#named_constructors_.2BAC8_factories_.28as_Python_initializers.29 例如。如果复制列表并不昂贵,我认为您可以简单地执行以下操作:
但是,如果复制成本昂贵,您应该能够将其与链接中描述的shared_ptr工厂方法结合起来。
I don't think using
boost::python::wrapper
will help you, since you'll need to use the constructor. I looked atboost::python::vector_indexing_suite
definition and I think you can in fact define your own constructor.There's a function in boost::python to specify your own named constructors. It is usually used to implement factory functions that return
shared_ptr
, but look like regular constructors from python. You can look at http://wiki.python.org/moin/boost.python/HowTo#named_constructors_.2BAC8_factories_.28as_Python_initializers.29 for an example of this.If copying your list is not expensive, I think you can simply do this:
However, if copying is expensive, you should be able to combine it with the shared_ptr factory approach described in the link.