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 函数:

  1. void add(double a, double b, double *result) {
  2. *result = a + b;
  3. }

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 库文件,并编写如下接口代码:

  1. // Simple example using typemaps
  2. %module example
  3. %include "typemaps.i"
  4. %apply double *OUTPUT { double *result };
  5. %inline %{
  6. extern void add(double a, double b, double *result);
  7. %}

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

  1. >>> a = add(3, 4)
  2. >>> print a
  3. 7
  4. >>>

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 * 对象,并将其以某种方式传递给函数。

一旦将类型映射应用于类型后,它对于以后所有出现的类型和名称都保持有效。例如,你可以编写以下内容:

  1. %module example
  2. %include "typemaps.i"
  3. %apply double *OUTPUT { double *result };
  4. %inline %{
  5. extern void add(double a, double b, double *result);
  6. extern void sub(double a, double b, double *result);
  7. extern void mul(double a, double b, double *result);
  8. extern void div(double a, double b, double *result);
  9. %}
  10. ...

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 规则将应用于随后的所有函数。

类型映射转换甚至可以扩展为多个返回值。例如,考虑以下代码:

  1. %include "typemaps.i"
  2. %apply int *OUTPUT { int *width, int *height };
  3. // Returns a pair (width, height)
  4. void getwinsize(int winid, int *width, int *height);

In this case, the function returns multiple values, allowing it to be used like this:

在这个例子中,该函数返回多个值,从而可以像这样使用它:

  1. >>> w, h = genwinsize(wid)
  2. >>> print w
  3. 400
  4. >>> print h
  5. 300
  6. >>>

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 指令将类型映射规则与数据类型相关联,但是你也可以直接在参数中使用规则名称。例如,你可以这样编写接口文件:

  1. // Simple example using typemaps
  2. %module example
  3. %include "typemaps.i"
  4. %{
  5. extern void add(double a, double b, double *OUTPUT);
  6. %}
  7. 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 指令。例如:

  1. %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,指针实际上仅保存一个输入值:

  1. int *INPUT
  2. short *INPUT
  3. long *INPUT
  4. unsigned int *INPUT
  5. unsigned short *INPUT
  6. unsigned long *INPUT
  7. double *INPUT
  8. float *INPUT

When used, it allows values to be passed instead of pointers. For example, consider this function:

使用时,它允许传递值而不是指针。例如,考虑以下函数:

  1. double add(double *a, double *b) {
  2. return *a + *b;
  3. }

Now, consider this SWIG interface:

现在,考虑编写 SWIG 接口文件:

  1. %module example
  2. %include "typemaps.i"
  3. ...
  4. %{
  5. extern double add(double *, double *);
  6. %}
  7. extern double add(double *INPUT, double *INPUT);

When the function is used in the scripting language interpreter, it will work like this:

在脚本语言解释器中使用该函数时,它将像下面这样工作:

  1. 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,指针是函数的输出值。使用时,在调用函数时不需要提供参数。而是返回一个或多个输出值。

  1. int *OUTPUT
  2. short *OUTPUT
  3. long *OUTPUT
  4. unsigned int *OUTPUT
  5. unsigned short *OUTPUT
  6. unsigned long *OUTPUT
  7. double *OUTPUT
  8. float *OUTPUT

These methods can be used as shown in an earlier example. For example, if you have this C function :

可以如先前示例中所示使用这些方法。例如,如果你具有以下 C 函数:

  1. void add(double a, double b, double *c) {
  2. *c = a + b;
  3. }

A SWIG interface file might look like this :

SWIG 接口文件可能如下:

  1. %module example
  2. %include "typemaps.i"
  3. ...
  4. %inline %{
  5. extern void add(double a, double b, double *OUTPUT);
  6. %}

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:

在这个例子中,仅返回单个输出值,但这不是限制。通过将输出规则应用于多个参数(如上所示),可以返回任意数量的输出值。

如果函数还返回值,则将其与参数一起返回。例如,如果你有:

  1. extern int foo(double a, double b, double *OUTPUT);

The function will return two values like this:

函数将返回两个值:

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

当指针既用作输入值又用作输出值时,可以使用以下类型映射:

  1. int *INOUT
  2. short *INOUT
  3. long *INOUT
  4. unsigned int *INOUT
  5. unsigned short *INOUT
  6. unsigned long *INOUT
  7. double *INOUT
  8. float *INOUT

A C function that uses this might be something like this:

有这样一个 C 函数:

  1. void negate(double *x) {
  2. *x = -(*x);
  3. }

To make x function as both and input and output value, declare the function like this in an interface file :

要使 x 既作为函数的输入值又作为输出值,请在接口文件中声明如下函数:

  1. %module example
  2. %include "typemaps.i"
  3. ...
  4. %{
  5. extern void negate(double *);
  6. %}
  7. extern void negate(double *INOUT);

Now within a script, you can simply call the function normally :

现在,在脚本中可以轻松地调用函数:

  1. 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 类型映射应用于不同的参数名称。例如:

  1. // Make double *result an output value
  2. %apply double *OUTPUT { double *result };
  3. // Make Int32 *in an input value
  4. %apply int *INPUT { Int32 *in };
  5. // Make long *x inout
  6. %apply long *INOUT {long *x};

To clear a rule, the %clear directive is used:

为了清理掉规则,要使用 %clear 指令:

  1. %clear double *result;
  2. %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 库最好的说明:

  1. // Interface file with constraints
  2. %module example
  3. %include "constraints.i"
  4. double exp(double x);
  5. double log(double POSITIVE); // Allow only positive values
  6. double sqrt(double NONNEGATIVE); // Non-negative values only
  7. double inv(double NONZERO); // Non-zero values
  8. 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

下列约束是当前可用的:

  1. POSITIVE Any number > 0 (not zero)
  2. NEGATIVE Any number < 0 (not zero)
  3. NONNEGATIVE Any number >= 0
  4. NONPOSITIVE Any number <= 0
  5. NONZERO Nonzero number
  6. 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 可以很容易地将其应用于新的数据类型。例如 :

  1. // Apply a constraint to a Real variable
  2. %apply Number POSITIVE { Real in };
  3. // Apply a constraint to a pointer type
  4. %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 指令:

  1. %clear Real in;
  2. %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. Python3-ORM-Sqlalchemy

    目录: ORM介绍 sqlalchemy安装 sqlalchemy基本使用 多外键关联 多对多关系 表结构设计作业 1. ORM介绍 orm英文全称object relational mapping, ...

  2. mysql--->mysql慢查询

    简介 > 开启慢查询日志,可以让MySQL记录下查询超过指定时间的语句,通过定位分析性能的瓶颈,才能更好的优化数据库系统的性能 参数及命令说明 查看慢查询是否开启和日志存储地址 show var ...

  3. Git基础常用功能

    一.安装 具体查看 安装Git 二.使用 基础知识 工作区(Workspace):就是你在电脑里能看到的项目目录. 暂存区(Index / Stage):临时存放更改的地方,使用命令"git ...

  4. spring cloud oauth2搭建认证中心与资源中心

    一 认证中心搭建 添加依赖,如果使用spring cloud的话,不管哪个服务都只需要这一个封装好的依赖即可 <dependency> <groupId>org.springf ...

  5. mysql 记录一次内存清理

    摘自:https://blog.csdn.net/wyzxg/article/details/7279986/ 摘要:Linux对内存的管理与Windows不同,free小并不是说内存不够用了,应该看 ...

  6. 阿里云Redis性能测试结果(1个集合存300万数据,查询能几秒返回结果)

    现状: 1.买了一台主从的阿里云Redis,内存就1GB. 2.查询了阿里云的帮助,没有找到性能相关的说明, 有的也是4GB版本的并发性能 3.提工单问客服 一个集合里有300万数据,单次查询性能大概 ...

  7. CassandraAppender - distributed logging,分布式软件logback-appender

    农历年最后一场scala-meetup听刘颖分享专业软件开发经验,大受启发.突然意识到一直以来都没有完全按照任何标准的开发规范做事.诚然,在做技术调研和学习的过程中不会对规范操作有什么严格要求,一旦技 ...

  8. Codeforces 924 A Tritonic Iridescence(暴力集合交集、相等)

    题目链接:点击打开链接 There is a rectangular grid of n rows of m initially-white cells each. Arkady performed ...

  9. Kittenblock画笔基础,移动留下痕迹的蝴蝶,图形化编程经验分享

    Kittenblock画笔基础,移动留下痕迹的蝴蝶,图形化编程经验分享 跟很多学生聊过,很多学生不是不努力,只是找不到感觉.有一点不可否认,同样在一个教室上课,同样是一个老师讲授,学习效果迥然不同.关 ...

  10. 在Thinkphp3.1中使用Mongo的具体操作

    最近研究Mongo项目都是用TP开发的,先介绍下Mongo在TP3.1中的用法 首先要确保你的PHP环境中已经安装好Mongo扩展,在实际项目中大多数都是Mysql数据库为主的,那么如何添加一个Mon ...