python - 将自定义代码添加到 SWIG 包装器
问题描述
我正在使用 SWIG 在 C++ 和 Python 之间进行接口。我想在生成的包装器中添加一些代码,以在结构中设置成员变量。我发现的最接近的是:
%allowexception TAxis::Min;
%exception TAxis::Min
%{
$action
do_something();
%}
struct TAxis
{
double Min;
double Max;
};
但是,当 TAxis::Min 被读取和写入时,这将调用 do_something()。我希望仅在写入 TAxis::Min 时调用它。任何建议表示赞赏。
解决方案
我会回答我自己的问题。下面创建一个宏VAR_WITH_SET_CODE,可以用来在变量设置前后做一些事情。
%define VAR_WITH_SET_CODE(class, type, member, pre_code, post_code)
%extend class
{
type member;
}
%{
#define class ## _ ## member ## _get(self) self->member
#define class ## _ ## member ## _set(self, value) {pre_code} self->member = value; {post_code}
%}
%enddef
VAR_WITH_SET_CODE(TAxis, double, Min, do_before();, do_after();)
VAR_WITH_SET_CODE(TAxis, double, Max, do_before();, do_after();)
struct TAxis
{
//double Min;
//double Max;
};
推荐阅读
- npm - 在html中设置javascript包目录
- sql-server - 如果“日期字段”错误,我们可以自动输入默认日期吗
- css - 需要避免与 justify-content 之间的间距:子 div flex wrap 后最后一个完整行的空格
- c - 如何在 C 中将 printf "d\xe9j\xe0 vu" 打印为 "Déjà vu"?
- codeigniter - 在codeigniter中获取所有超过24小时的记录
- css-animations - 重新打开应用程序时如何防止动画重复?
- javascript - 如何在 loDash 函数中使用变量
- java - 扩展后如何在父类中获取 T 类型?
- javascript - 如何在 Reactjs 中使用未声明的变量
- node.js - 使用 through2-map 使用 nodejs 制作的服务器中的“结束后写入”错误