SWIG 3 中文手册——10. 参数处理
10 参数处理
In Chapter 3, SWIG's treatment of basic datatypes and pointers was described. In particular, primitive types such as int and double are mapped to corresponding types in the target language. For everything else, pointers are used to refer to structures, classes, arrays, and other user-defined datatypes. However, in certain applications it is desirable to change SWIG's handling of a specific datatype. For example, you might want to return multiple values through the arguments of a function. This chapter describes some of the techniques for doing this.
第 3 章介绍了 SWIG 对基本数据类型和指针的处理。特别地,原始类型(诸如
int和double)被映射到目标语言中的相应类型。对于其他所有类型,都使用指针来引用结构体、类、数组和其他用户定义的数据类型。但是,在某些应用程序中,希望更改 SWIG 对特定数据类型的处理。例如,你可能想通过函数的参数返回多个值。本章介绍了一些执行此类操作的技术。
10.1 typemaps.i 库
This section describes the typemaps.i library file--commonly used to change certain properties of argument conversion.
本节描述
typemaps.i库文件——通常用于更改参数转换的某些属性。
10.1.1 引言
Suppose you had a C function like this:
假设你有如下 C 函数:
void add(double a, double b, double *result) {
*result = a + b;
}
From reading the source code, it is clear that the function is storing a value in the double *result parameter. However, since SWIG does not examine function bodies, it has no way to know that this is the underlying behavior.
One way to deal with this is to use the typemaps.i library file and write interface code like this:
通过阅读源代码,很明显该函数将值存储在
double *result参数中。但是,由于 SWIG 不检查函数主体,因此没有办法知道函数的底层行为。一种解决方法是使用
typemaps.i库文件,并编写如下接口代码:
// Simple example using typemaps
%module example
%include "typemaps.i"
%apply double *OUTPUT { double *result };
%inline %{
extern void add(double a, double b, double *result);
%}
The %apply directive tells SWIG that you are going to apply a special type handling rule to a type. The double *OUTPUT specification is the name of a rule that defines how to return an output value from an argument of type double *. This rule gets applied to all of the datatypes listed in curly braces--in this case double *result.
When the resulting module is created, you can now use the function like this (shown for Python):
%apply指令告诉 SWIG 你将要对某类型应用特殊的类型处理规则。double *OUTPUT规范是一个规则的名称,该规则定义了如何从类型为double *的参数返回输出值。该规则将应用于大括号中列出的所有数据类型,在这个例子中为double *result。结果模块创建后,你现在可以这样使用以下函数(针对 Python):
>>> a = add(3, 4)
>>> print a
7
>>>
In this case, you can see how the output value normally returned in the third argument has magically been transformed into a function return value. Clearly this makes the function much easier to use since it is no longer necessary to manufacture a special double * object and pass it to the function somehow.
Once a typemap has been applied to a type, it stays in effect for all future occurrences of the type and name. For example, you could write the following:
在这个例子中,你可以看到通常在第三个参数中返回的输出值如何神奇地转换为函数返回值。显然,这使函数更易于使用,因为不再需要制造特殊的
double *对象,并将其以某种方式传递给函数。一旦将类型映射应用于类型后,它对于以后所有出现的类型和名称都保持有效。例如,你可以编写以下内容:
%module example
%include "typemaps.i"
%apply double *OUTPUT { double *result };
%inline %{
extern void add(double a, double b, double *result);
extern void sub(double a, double b, double *result);
extern void mul(double a, double b, double *result);
extern void div(double a, double b, double *result);
%}
...
In this case, the double *OUTPUT rule is applied to all of the functions that follow.
Typemap transformations can even be extended to multiple return values. For example, consider this code:
在这个例子中,
double *OUTPUT规则将应用于随后的所有函数。类型映射转换甚至可以扩展为多个返回值。例如,考虑以下代码:
%include "typemaps.i"
%apply int *OUTPUT { int *width, int *height };
// Returns a pair (width, height)
void getwinsize(int winid, int *width, int *height);
In this case, the function returns multiple values, allowing it to be used like this:
在这个例子中,该函数返回多个值,从而可以像这样使用它:
>>> w, h = genwinsize(wid)
>>> print w
400
>>> print h
300
>>>
It should also be noted that although the %apply directive is used to associate typemap rules to datatypes, you can also use the rule names directly in arguments. For example, you could write this:
还应该注意,尽管已经使用
%apply指令将类型映射规则与数据类型相关联,但是你也可以直接在参数中使用规则名称。例如,你可以这样编写接口文件:
// Simple example using typemaps
%module example
%include "typemaps.i"
%{
extern void add(double a, double b, double *OUTPUT);
%}
extern void add(double a, double b, double *OUTPUT);
Typemaps stay in effect until they are explicitly deleted or redefined to something else. To clear a typemap, the %clear directive should be used. For example:
类型映射将一直有效,直到将其明确删除或重新定义为其他类型为止。要清除类型映射,应使用
%clear指令。例如:
%clear double *result; // Remove all typemaps for double *result
10.1.2 输入参数
The following typemaps instruct SWIG that a pointer really only holds a single input value:
以下类型映射告诉 SWIG,指针实际上仅保存一个输入值:
int *INPUT
short *INPUT
long *INPUT
unsigned int *INPUT
unsigned short *INPUT
unsigned long *INPUT
double *INPUT
float *INPUT
When used, it allows values to be passed instead of pointers. For example, consider this function:
使用时,它允许传递值而不是指针。例如,考虑以下函数:
double add(double *a, double *b) {
return *a + *b;
}
Now, consider this SWIG interface:
现在,考虑编写 SWIG 接口文件:
%module example
%include "typemaps.i"
...
%{
extern double add(double *, double *);
%}
extern double add(double *INPUT, double *INPUT);
When the function is used in the scripting language interpreter, it will work like this:
在脚本语言解释器中使用该函数时,它将像下面这样工作:
result = add(3, 4)
10.1.3 输出参数
The following typemap rules tell SWIG that pointer is the output value of a function. When used, you do not need to supply the argument when calling the function. Instead, one or more output values are returned.
以下类型映射规则告诉 SWIG,指针是函数的输出值。使用时,在调用函数时不需要提供参数。而是返回一个或多个输出值。
int *OUTPUT
short *OUTPUT
long *OUTPUT
unsigned int *OUTPUT
unsigned short *OUTPUT
unsigned long *OUTPUT
double *OUTPUT
float *OUTPUT
These methods can be used as shown in an earlier example. For example, if you have this C function :
可以如先前示例中所示使用这些方法。例如,如果你具有以下 C 函数:
void add(double a, double b, double *c) {
*c = a + b;
}
A SWIG interface file might look like this :
SWIG 接口文件可能如下:
%module example
%include "typemaps.i"
...
%inline %{
extern void add(double a, double b, double *OUTPUT);
%}
In this case, only a single output value is returned, but this is not a restriction. An arbitrary number of output values can be returned by applying the output rules to more than one argument (as shown previously).
If the function also returns a value, it is returned along with the argument. For example, if you had this:
在这个例子中,仅返回单个输出值,但这不是限制。通过将输出规则应用于多个参数(如上所示),可以返回任意数量的输出值。
如果函数还返回值,则将其与参数一起返回。例如,如果你有:
extern int foo(double a, double b, double *OUTPUT);
The function will return two values like this:
函数将返回两个值:
iresult, dresult = foo(3.5, 2)
10.1.4 输入 / 输出参数
When a pointer serves as both an input and output value you can use the following typemaps :
当指针既用作输入值又用作输出值时,可以使用以下类型映射:
int *INOUT
short *INOUT
long *INOUT
unsigned int *INOUT
unsigned short *INOUT
unsigned long *INOUT
double *INOUT
float *INOUT
A C function that uses this might be something like this:
有这样一个 C 函数:
void negate(double *x) {
*x = -(*x);
}
To make x function as both and input and output value, declare the function like this in an interface file :
要使
x既作为函数的输入值又作为输出值,请在接口文件中声明如下函数:
%module example
%include "typemaps.i"
...
%{
extern void negate(double *);
%}
extern void negate(double *INOUT);
Now within a script, you can simply call the function normally :
现在,在脚本中可以轻松地调用函数:
a = negate(3); # a = -3 after calling this
One subtle point of the INOUT rule is that many scripting languages enforce mutability constraints on primitive objects (meaning that simple objects like integers and strings aren't supposed to change). Because of this, you can't just modify the object's value in place as the underlying C function does in this example. Therefore, the INOUT rule returns the modified value as a new object rather than directly overwriting the value of the original input object.
Compatibility note : The INOUT rule used to be known as BOTH in earlier versions of SWIG. Backwards compatibility is preserved, but deprecated.
INOUT规则的一个细微之处是,许多脚本语言对原始对象实施了可变性约束(这意味着简单的对象,如整数和字符串,不应更改)。因此,你不能像在此示例中基础 C 函数那样就地修改对象的值。因此,INOUT规则将修改后的值作为新对象返回,而不是直接覆盖原始输入对象的值。注意兼容性:在早期版本的 SWIG 中,
INOUT规则以前被称为BOTH。已保留向后兼容性,但已弃用。
10.1.5 使用不同的名称
As previously shown, the %apply directive can be used to apply the INPUT, OUTPUT, and INOUT typemaps to different argument names. For example:
如前所示,
%apply指令可用于将INPUT、OUTPUT和INOUT类型映射应用于不同的参数名称。例如:
// Make double *result an output value
%apply double *OUTPUT { double *result };
// Make Int32 *in an input value
%apply int *INPUT { Int32 *in };
// Make long *x inout
%apply long *INOUT {long *x};
To clear a rule, the %clear directive is used:
为了清理掉规则,要使用
%clear指令:
%clear double *result;
%clear Int32 *in, long *x;
Typemap declarations are lexically scoped so a typemap takes effect from the point of definition to the end of the file or a matching %clear declaration.
类型映射声明在词法上是作用域的,因此类型映射从定义点开始到文件末尾,或匹配的
%clear声明之前均生效。
10.2 对输入值施加约束
In addition to changing the handling of various input values, it is also possible to use typemaps to apply constraints. For example, maybe you want to insure that a value is positive, or that a pointer is non-NULL. This can be accomplished including the constraints.i library file.
除了更改对各种输入值的处理之外,还可以使用类型映射来应用约束。例如,也许你想确保一个值是正数,或者一个指针是非 NULL 的。这可以通过包含
constraints.i库文件来完成。
10.2.1 简单约束的例子
The constraints library is best illustrated by the following interface file :
以下接口文件是对
constraints.i库最好的说明:
// Interface file with constraints
%module example
%include "constraints.i"
double exp(double x);
double log(double POSITIVE); // Allow only positive values
double sqrt(double NONNEGATIVE); // Non-negative values only
double inv(double NONZERO); // Non-zero values
void free(void *NONNULL); // Non-NULL pointers only
The behavior of this file is exactly as you would expect. If any of the arguments violate the constraint condition, a scripting language exception will be raised. As a result, it is possible to catch bad values, prevent mysterious program crashes and so on.
该文件的行为与你期望的完全一样。如果任何参数违反约束条件,将引发脚本语言异常。最终,可能会捕获错误的值,防止神秘的程序崩溃,等等。
10.2.2 约束方法
The following constraints are currently available
下列约束是当前可用的:
POSITIVE Any number > 0 (not zero)
NEGATIVE Any number < 0 (not zero)
NONNEGATIVE Any number >= 0
NONPOSITIVE Any number <= 0
NONZERO Nonzero number
NONNULL Non-NULL pointer (pointers only).
10.2.3 对新的数据类型应用约束
The constraints library only supports the primitive C datatypes, but it is easy to apply it to new datatypes using %apply. For example :
约束库仅支持原始 C 数据类型,但使用
%apply可以很容易地将其应用于新的数据类型。例如 :
// Apply a constraint to a Real variable
%apply Number POSITIVE { Real in };
// Apply a constraint to a pointer type
%apply Pointer NONNULL { Vector * };
The special types of "Number" and "Pointer" can be applied to any numeric and pointer variable type respectively. To later remove a constraint, the %clear directive can be used :
特殊类型的
Number和Pointer可以分别应用于任何数字和指针变量类型。为了以后删除约束,可以使用%clear指令:
%clear Real in;
%clear Vector *;
SWIG 3 中文手册——10. 参数处理的更多相关文章
- SWIG 3 中文手册——6. SWIG 和 C++
目录 6 SWIG 和 C++ 6.1 关于包装 C++ 6.2 方法 6.3 支持的 C++ 功能 6.4 命令行选项与编译 6.5.1 代理类的构造 6.5.2 代理类中的资源管理 6.5.3 语 ...
- SWIG 3 中文手册——5. SWIG 基础知识
目录 5 SWIG 基础知识 5.1 运行 SWIG 5.1.1 输入格式 5.1.2 SWIG 输出 5.1.3 注释 5.1.4 C 预处理器 5.1.5 SWIG 指令 5.1.6 解析限制 5 ...
- SWIG 3 中文手册——4. 脚本语言
目录 4 脚本语言 4.1 两种语言的概览 4.2 脚本语言如何调用 C? 4.2.1 包装器函数 4.2.2 变量链接 4.2.3 常量 4.2.4 结构体与类 4.2.5 代理类 4.3 构建脚本 ...
- SWIG 3 中文手册——1. 前言
目录 1 前言 1.1 引言 1.2 SWIG 版本 1.3 SWIG 许可证 1.4 SWIG 资源 1.5 前提要求 1.6 本手册的组织构成 1.7 如何避免阅读手册 1.8 向后兼容 1.9 ...
- SWIG 3 中文手册——9. SWIG 库
目录 9 SWIG 库 9.1 %include 指令与库搜索路径 9.2 C 数组与指针 9.2.1 cpointer.i 9.2.2 carrays.i 9.2.3 cmalloc.i 9.2.4 ...
- SWIG 3 中文手册——3. Windows 上使用 SWIG
目录 3 Windows 上使用 SWIG 后续章节 3 Windows 上使用 SWIG 暂时略过. 后续章节 <4. 脚本语言>
- SWIG 3 中文手册——2. 引言
目录 2 引言 2.1 SWIG 是什么? 2.2 为什么使用 SWIG? 2.3 一个 SWIG 示例 2.3.1 SWIG 接口文件 2.3.2 swig 命令 2.3.3 构建 Perl5 模块 ...
- Flask 中文手册 0.10 文档
Flask 中文手册 0.10 文档 欢迎使用 Flask 欢迎阅读 Flask 文档. 本文档分为几个部分.我推荐您先从 安装 开始,之后再浏览 快速入门 章节. 教程 比快速入门更详细地介绍了如何 ...
- (转)Sqoop中文手册
Sqoop中文手册 1. 概述 本文档主要对SQOOP的使用进行了说明,参考内容主要来自于Cloudera SQOOP的官方文档.为了用中文更清楚明白地描述各参数的使用含义,本文档几乎所有参数 ...
随机推荐
- 线程池技术之:ThreadPoolExecutor 源码解析
java中的所说的线程池,一般都是围绕着 ThreadPoolExecutor 来展开的.其他的实现基本都是基于它,或者模仿它的.所以只要理解 ThreadPoolExecutor, 就相当于完全理解 ...
- Spring 框架初学
一 . spring IOC, DI: 将原来有程序员建立类的对象工作交由spring来操作: 从外部来看: 将各个类以bean 的方式配在spring容器的配置文件中(application.x ...
- JDK源码之Byte类分析
一 简介 byte,即字节,由8位的二进制组成.在Java中,byte类型的数据是8位带符号的二进制数,以二进制补码表示的整数 取值范围:默认值为0,最小值为-128(-2^7);最大值是127(2^ ...
- Capslock+程序介绍
一直为编程时方向键不在盲打区域苦恼,今天接触了一个非常好的软件Capslock+. 软件特别小,一共只有九百多K,甚至不能称为软件,只能算一个很小的脚本了.但解决了我非常大的一个难题.安装好软件后可以 ...
- mac如何用quick look预览多个文件或者图片
1.先选中要查看的多个文件,然后点击 空格键 2.按住 command+return 就可以同时预览多个文件了 如果想全屏预览,则在1中,按住 option+空格键 ,然后再进行2 ,就实现全屏预览了 ...
- html作业记录
<html> <head> <title>Hello World</title> </head> <body> <!-- ...
- js笔记(4)--关于在window.onload()里面定义函数,调用函数无法执行~
由于本人学习js学不久,所以,今天刚好遇到了一个关于在window.onload里面定义函数,然后在html里面调用函数时出现错误.具体见下面: <!DOCTYPE html> <h ...
- Java使用自定义类加载器实现热部署
热部署: 热部署就是在不重启应用的情况下,当类的定义即字节码文件修改后,能够替换该Class创建的对象.一般情况下,类的加载都是由系统自带的类加载器完成,且对于同一个全限定名的java类,只能被加载一 ...
- Burpsuite Pro 2020.1最新破解版
简介 Burp Suite 是用于攻击web 应用程序的集成平台,包含了许多工具.Burp Suite为这些工具设计了许多接口,以加快攻击应用程序的过程.所有工具都共享一个请求,并能处理对应的HTTP ...
- 踩坑ThinkPHP5之模型对象返回的数据集如何转为数组
各位小伙伴们大家好,冷月今天在做项目的过程中呢,遇到了一个坑就是用tp5的模型操作数据库时,返回的是数据集而不是直接的数组.于是冷月就想办法如何将数据集转为数组.写下这篇博文,防止大家遇到这个坑时可以 ...