我正在尝试为 gnucash c++ 部件构建一个 python 包装器。在QofBackend我遇到了方法const std::string && get_message ()
。在 python 中,此消息返回<Swig Object of type 'std::string *' at 0x7f4a20f5c9f0>
而不是我的设置中的字符串,因为 std::string 右值引用没有 swig 类型映射。
我并没有真正找到一个简单的解释,所以我重建了一个示例设置并深入研究了我几乎不知道的 c++。我设法将字符串输入python,但我想知道
- 如果这种 typemap(out) 方法是正确的(同样在内存和错误处理方面)。
- 转换
set_s_workaround()
也只是一种解决方法。我认为对于 gnucash,python 代码不需要设置这个值,但为了完整起见,最好还有一个typemap(in) std::string&&
和 - 摆脱
get_s_workaround
- 让 init_s_2 工作。
/* example.hpp */
#include <string>
using namespace std;
struct struct1{
string s;
const std::string&& get_s();
void set_s(string&&);
void set_s_workaround(string);
void init_s();
void init_s_2();
void print_s();
};
string conv_rvalue_string(string);
/* example.cpp */
#include<iostream>
#include"example.hpp"
using namespace std;
void
struct1::set_s (std::string&& msg)
{
s = msg;
}
std::string
conv_rvalue_string (std::string msg)
{
return msg;
}
void
struct1::set_s_workaround(std::string msg)
{
set_s ( conv_rvalue_string(msg) );
}
void
struct1::init_s ()
{
set_s("Some content");
}
void
struct1::init_s_2()
{
std::string msg {"Couldn't find "};
/* set_s( msg ); */
}
void
struct1::print_s ()
{
cout<<get_s()<<endl;
}
const std::string&&
struct1::get_s ()
{
return std::move(s);
}
/* example.i */
%module example
%include "std_string.i"
%typemap(out) std::string&& {
std::string s = *$1;
$result = SWIG_From_std_string(s);
}
%{
#include <string>
#include "example.hpp"
%}
%include "example.hpp"
#!/bin/bash
swig3.0 -c++ -shadow -python example.i
g++ -fpic -c example.hpp example.cpp example_wrap.cxx -I/usr/include/python3.7
g++ -shared example_wrap.o example.o -o _example.so
# pyexample.py
import example
s1 = example.struct1()
s1.set_s_workaround('TEST')
s1.print_s()
print(s1.get_s())
谢谢您的帮助!