SWIG Library

爲了幫助構建擴展模塊,SWIG附帶了支持庫,您可以在自己的接口中包括這些支持文件。 這些文件通常定義新的SWIG指令或提供實用程序功能,這些功能可用於訪問標準C和C ++庫的一部分。 本章提供對當前支持的庫文件集的參考。

8.2.2 carrays.i

該模塊定義了有助於將普通C指針包裝爲數組的宏。 該模塊不提供任何安全性或額外的包裝層-它僅提供用於創建,銷燬和修改原始C數組數據內容的功能。

%array_functions(type,name)

        Creates four functions.

type *new_name(int nelements)

       Creates a new array of objects of type type. In C, the array is allocated usingcalloc(). In C++, new [] is used.

type *delete_name(type *ary)

       Deletes an array. In C, free() is used. In C++, delete [] is used.

type name_getitem(type *ary, int index)

       Returns the value ary[index].

void name_setitem(type *ary, int index, type value)

       Assigns ary[index] = value.

使用此宏時,type可以是任何類型,並且名稱必須是目標語言中的合法標識符。 名稱不應該與接口文件中使用的任何其他名稱對應。

%array_functions()的例子:

void print_array(double x[10]) {
   int i;
   for (i = 0; i < 10; i++) {
      printf("[%d] = %g\n", i, x[i]);
   }
}

打包它,可能會這樣寫

%module example

%include "carrays.i"
%array_functions(double, doubleArray);

void print_array(double x[10]);

現在,腳本調用

a = new_doubleArray(10)           # Create an array
for i in range(0,10):
    doubleArray_setitem(a,i,2*i)  # Set a value
print_array(a)                    # Pass to C
delete_doubleArray(a)             # Destroy array

%array_class(type,name)

在基於類的接口內包裝*類型的指針。 該界面如下:

struct name {
   name(int nelements);                  // Create an array
  ~name();                               // Delete array
   type getitem(int index);              // Return item
   void setitem(int index, type value);  // Set item
   type *cast();                         // Cast to original type
   static name *frompointer(type *);     // Create class wrapper from
                                         // existing pointer
};

使用此宏時,類型被限制爲簡單的類型名稱,例如int或float。 不允許使用指針和其他複雜類型。 名稱必須是尚未使用的有效標識符。 將指針包裝爲類時,可以將其透明地傳遞給任何需要該指針的函數。

與代理類結合使用時,%array_class()宏會特別有用。 例如:

%module example
%include "carrays.i"
%array_class(double, doubleArray);

void print_array(double x[10]);

現在,腳本調用

import example
c = example.doubleArray(10)  # Create double[10]
for i in range(0,10):
    c[i] = 2*i               # Assign values
example.print_array(c)       # Pass to C

注意:這些宏不會將C數組封裝在特殊的數據結構或代理中。 沒有邊界檢查或任何形式的安全性。 如果需要,應該考慮使用特殊的數組對象而不是裸指針。

注意:%array_functions()和%array_class()不應與char或char *類型一起使用。

8.4.1 std_string.i

std_string.i庫提供了用於將C ++ std :: string對象與目標腳本語言在字符串之間進行轉換的類型映射。 例如:

%module example
%include "std_string.i"

std::string foo();
void        bar(const std::string &x);

在目標語言中

x = foo();                # Returns a string object
bar("Hello World");       # Pass string as std::string

常見問題是包含std :: string的類/結構的問題。 這可以通過定義類型映射克服。 例如:

%module example
%include "std_string.i"

%apply const std::string& {std::string* foo};

struct my_struct
{
  std::string foo;
};

目標語言中

x = my_struct();
x.foo="Hello World";      # assign with string
print x.foo;              # print as string

該模塊僅支持類型std :: string和const std :: string&。 指針和非const引用保持不變,並作爲SWIG指針返回。

該庫文件完全瞭解C ++名稱空間。 如果導出std :: string或使用typedef重命名,請確保在接口中包括這些聲明。 例如:

%module example
%include "std_string.i"

using namespace std;
typedef std::string String;
...
void foo(string s, const String &t);     // std_string typemaps still applied

 

未完待續。。。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章