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 *;
