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 对基本数据类型和指针的处理。特别地,原始类型(诸如 intdouble)被映射到目标语言中的相应类型。对于其他所有类型,都使用指针来引用结构体、类、数组和其他用户定义的数据类型。但是,在某些应用程序中,希望更改 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 指令可用于将 INPUTOUTPUTINOUT 类型映射应用于不同的参数名称。例如:

// 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 :

特殊类型的 NumberPointer 可以分别应用于任何数字和指针变量类型。为了以后删除约束,可以使用 %clear 指令:

%clear Real in;
%clear Vector *;

SWIG 3 中文手册——10. 参数处理的更多相关文章

  1. 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 语 ...

  2. 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 ...

  3. 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 构建脚本 ...

  4. 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 ...

  5. 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 ...

  6. SWIG 3 中文手册——3. Windows 上使用 SWIG

    目录 3 Windows 上使用 SWIG 后续章节 3 Windows 上使用 SWIG 暂时略过. 后续章节 <4. 脚本语言>

  7. 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 模块 ...

  8. Flask 中文手册 0.10 文档

    Flask 中文手册 0.10 文档 欢迎使用 Flask 欢迎阅读 Flask 文档. 本文档分为几个部分.我推荐您先从 安装 开始,之后再浏览 快速入门 章节. 教程 比快速入门更详细地介绍了如何 ...

  9. (转)Sqoop中文手册

    Sqoop中文手册 1.     概述 本文档主要对SQOOP的使用进行了说明,参考内容主要来自于Cloudera SQOOP的官方文档.为了用中文更清楚明白地描述各参数的使用含义,本文档几乎所有参数 ...

随机推荐

  1. ReactNative---android系统中Image组件无默认图片问题

    react native的Image组件通过网络地址加载图片的时候,若加载失败iOS有默认图片等属性,但安卓没有:但可以通过其他方式来实现: {Platform.OS == 'android'?< ...

  2. Java博客专栏

    1. Java23种设计模式 2. JVM虚拟机 3. 设计模式6大原则 4. Java代码性能优化总结 5. 三种代理 6. iText操作PDF 7. 解析XML的4种方式 8. 面向对象思想 9 ...

  3. Oracle数据库安装与卸载

    一.下载俩个压缩包,同时选中解压到一个文件夹中 二.点击step.exe(win10可能弹出不满足环境要求,选择是就行了) 三.把接收更新勾掉不需要 四.选择创建和配置数据库 五.选择服务器类 六.选 ...

  4. tomcat 持久区溢出

    知识点:堆内存设置(JVM堆内存)java的堆内存分为两块:permantspace(持久带) 和 heap spaceOOM1.年老代溢出,表现为:java.lang.OutOfMemoryErro ...

  5. oracle和mysql区别

    1.本质的区别.oracle是对象关系数据库管理系统,简称ordbms.mysql是开源关系数据库关系系统,简称rdbms.Oracle是收费的.mysql是开源.免费的. 2.数据库安全性.myql ...

  6. centos 配置自动启动(nginx为例)

    [Unit] Description=nginx After=network.target [Service] Type=forking ExecStart=/usr/local/nginx/sbin ...

  7. 使用IntelliJ IDEA创建Maven工程

    1. 新建项目(点击“+ Create New Project”): 2. 选择Maven,不必选择项目骨架,直接点击Next 按钮即可: 3. 输入项目名称,选择项目位置,输入组织名称,模块名称,项 ...

  8. Python中的hashable(散列)

    Python文档中的解释: 一个对象是可散列的,那么在它的生命周期中它的hash 值是不变的. 可散列的对象需要2个方法:__hash__()方法和__eq__()方法.两个可散列的对象相等,那么它们 ...

  9. Codeforces_540_C

    http://codeforces.com/problemset/problem/540/C 简单bfs,注意结束条件. #include<iostream> #include<cs ...

  10. RocketChat:开源聊天软件部署

    1. 简介 RocketChat是一款优秀的开源聊天软件.支持各种平台,IOS.Android.Web.Mac.Windows以及Linux,安装部署简单,功能简单易用,目前部分功能还在完善当中,特别 ...