简体   繁体   English

boost.python字符串管理

[英]boost.python string management

How does boost.python handle std::string member variables? boost.python如何处理std :: string成员变量? Specifically, if I have a class and its boost.python wrapper 具体来说,如果我有一个类及其boost.python包装器

class A {
public: 
    std::string name;
};
class_<A>("AWrapper")
.def_readwrite("name", &A::name);

how is the correspondence between std::string object and PyObject it(string) representing is handled: is boost::python::object(ie PyObject) created which stores pointer to the string, thus this object servers as a proxy? 如何处理std :: string对象和它表示的PyObject之间的对应关系:创建了boost :: python :: object(即PyObject)来存储指向字符串的指针,因此该对象作为代理服务器? Thus, each time I want to retrieve string member's value, PyString object is created? 因此,每次我想检索字符串成员的值时,都会创建PyString对象吗? Can interning mechanism be applied to such string objects? 可以将interning机制应用于此类字符串对象吗?

Short answer: your suspicions and insight about the behaviour is mostly correct, boost-python will interpret your configuration to produce a simple and inefficient behaviour, however it provides support mechanisms to override and extend this. 简短的答案:您对行为的怀疑和见解基本上是正确的,boost-python会解释您的配置以产生简单且效率低下的行为,但是它提供了支持机制来覆盖和扩展此行为。 This may be a lot of work. 这可能是很多工作。

Long answer: 长答案:

is boost::python::object(ie PyObject) created which stores pointer to the string, thus this object servers as a proxy? 是boost :: python :: object(即PyObject)创建的,它存储指向字符串的指针,因此该对象服务器作为代理?

No. 没有。

Python-strings are immutable and by default (with your configuration) boost-python will convert the std::string into a new python string (str). Python字符串是不可变的,默认情况下(使用您的配置)boost-python会将std :: string转换为新的python字符串(str)。 So there is no proxying going on. 因此,没有代理正在进行。

Thus, each time I want to retrieve string member's value, PyString object is created? 因此,每次我想检索字符串成员的值时,都会创建PyString对象吗?

Yes. 是。 The following assertion will fail, where I attempt to verify that the python references to the strings have the same python object id. 以下断言将失败,在此我将尝试验证对字符串的python引用具有相同的python对象id。

input = 'some value'
sut = AWrapper()
sut.name = input
assert id(sut.name) == id(input)

Can interning mechanism be applied to such string objects? 可以将interning机制应用于此类字符串对象吗?

For example, if you can enable caching of the created python string-object for a given c++ string, to then return a previously created python string-object when available? 例如,如果您可以为给定的c ++字符串启用对创建的python字符串对象的缓存,然后在可用时返回先前创建的python字符串对象? The answer is also yes, it is possible. 答案是肯定的,这是可能的。 The boost-python concepts are call policies or custom converters . boost-python概念是调用策略自定义转换器 (Both are not part of the tutorial for boost-python.) (两者都不是boost-python教程的一部分。)

You would either extend every def_readwrite call with an appropriate call policy, or overwrite the general built-in converter for std::string to/from PyString , to introduce this theoretical cache. 你要么延长每次def_readwrite呼叫与适当的呼叫政策,或覆盖一般内置转换器std::string从到/ PyString ,引进这一理论缓存。

Keep in mind, python strings are immutable and std::string is mutable. 请记住,python字符串是不可变的,而std :: string是可变的。 So you will have issues when the member is changed from the C++ side. 因此,当从C ++端更改成员时,您将遇到问题。 Additionally, such hooks will also cost performance at runtime when the code handles every single get and set operation. 此外,当代码处理每个单独的get和set操作时,此类挂钩也会在运行时降低性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM