[Top] [Contents] [Index] [ ? ]

c-wrapper リファレンスマニュアル

本ドキュメントはc-wrapperのリファレンスマニュアルです。


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1. c-wrapper

c-wrapperとは、CのライブラリをGaucheから使えるようにするためのモジュールです。


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.1 Load a shared library

Function: c-load-library file-or-list &keyword

共有ライブラリを読み込みます。 file-or-list でリストを渡すと複数のライブラリが読み込まれます。 また拡張子を省略した場合、環境に応じた拡張子 (.so や .dylib など ) が付加されます。

キーワード引数には以下のものを与えることができます。

:option option-string

ldに与えるオプションのような形式で共有ライブラリや読み込みパスの指定を行います。現状使えるのは以下のオプションです。

-llibrary

共有ライブラリ library を読み込みます。例えば、-lc のように指定すると libc が読み込まれます。

-Ldir

共有ライブラリのサーチリストにディレクトリ dir を追加します。

Function: c-ld option-string

(c-load-library '() :option option-string) と同じです。この関数は pkg-config などのようなリンカのフラグを返すコマンドと組み合わせて使うことを想定しています。

 
(c-ld (process-output->string "pkg-config Wand --libs"))

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.2 Load a header file

Macro: c-include file-or-list &keyword

指定されたヘッダファイルを読み込み、関数・グローバル変数・定数・型定義・マクロをカレントモジュールに定義します。

keyword 引数には以下のものを指定することができます。

:include-dirs

インクルードパスを指定します。文字列もしくは文字列のリストが指定可能です。

:option

Cプリプロセッサに渡すオプションを指定します。文字列が指定可能です。

:import

ヘッダファイルから読み込むシンボルを指定します。 引数として、シンボル、文字列、正規表現、手続きと、それらのリストが指定可能です。 手続きが渡された場合は、シンボルが見つかるたびにヘッダファイルのパス名とシンボル名を引数として、その手続きが呼び出されます。 手続きが#fを返した場合そのシンボルは無視され、それ以外の値の場合にシンボルが定義されます。

:export?

定義したシンボルをexportするかどうかを指定します。 #tの場合は定義したシンボルに対し自動的にexportが実行されます。#fの場合は何もしません。

:compiled-lib

cwcompileコマンドにより作成されるパーズ済みの共有ライブラリの名前を指定します。 ここで指定された共有ライブラリが存在する場合、ヘッダファイルをパーズする代わりにその共有ライブラリを読み込みます。共有ライブラリが存在しない場合は、ヘッダファイルのパーズが行われます。

ヘッダファイル読み込み時に、以下の情報を収集し関数や変数などを定義していきます。

関数のプロトタイプ宣言

関数名と同名のシンボルで関数が定義されます。なおこの関数は引数に対してcastを戻り値に対してscm-castを適用します。

外部変数定義

変数名と同名のシンボルで外部変数を参照するオブジェクトが定義されます。

enum

定数名と同名のシンボルで定数が定義されます。

struct

構造体のクラスを定義します。このクラスは(c-struct tagname)で参照することができます。

union

共用体のクラスを定義します。このクラスは(c-union tagname)で参照することができます。

typedef

<typename>というシンボルでCの型に対応するクラスを定義します。

オブジェクト形式 (objectlike) のマクロ

マクロの内容が式であり、使用されている識別子がすべて宣言済みであり、かつ副作用を及ぼす式でない場合に、マクロ名と同名のシンボルで値が定義されます。なお、C言語でのマクロとは意味が異なり、この値はヘッダファイル読み込み時点での値であり、マクロ使用時点での値ではありません。

関数形式 (functionlike) のマクロ

マクロの内容が式もしくは文であり使用されている識別子がすべて宣言済みの場合にのみ、マクロ名と同名のシンボルでマクロが定義されます。

Macro: c-load file-or-list &keyword

指定されたヘッダファイルとライブラリを読み込み、関数・グローバル変数・定数・型定義・マクロをカレントモジュールに定義します。 c-load-libraryc-includeを一度に行うものです。

keyword 引数には以下のものを指定することができます。

:cflags
:cppflags
:ldflags
:libs

ヘッダファイルのパーズとライブラリのロードで使うための各種フラグを指定します。:cppflagsはプリプロセッサに渡され、:ldflags, :libsはライブラリのロードで使用されます。これらのフラグはcwcompileでも使われます。

:cflags-cmd
:cppflags-cmd
:ldflags-cmd
:libs-cmd

各種フラグを取得するためのコマンドを指定します。例えば、gtkのヘッダファイルをパーズするときにpkg-configを使って以下のようにオプションを指定することができます。

 
(c-load "gtk/gtk/h"
        :cppflags-cmd "pkg-config gtk+-2.0 --cflags-only-I"
        :cflags-cmd   "pkg-config gtk+-2.0 --cflags-only-other"
        :libs-cmd     "pkg-config gtk+-2.0 --libs"
        :compiled-lib "gtklib")
:import

ヘッダファイルから読み込むシンボルを指定します。 引数として、シンボル、文字列、正規表現、手続きと、それらのリストが指定可能です。 手続きが渡された場合は、シンボルが見つかるたびにヘッダファイルのパス名とシンボル名を引数として、その手続きが呼び出されます。 手続きが#fを返した場合そのシンボルは無視され、それ以外の値の場合にシンボルが定義されます。

:export?

定義したシンボルをexportするかどうかを指定します。 #tの場合は定義したシンボルに対し自動的にexportが実行されます。#fの場合は何もしません。

:compiled-lib

cwcompileコマンドにより作成されるパーズ済みの共有ライブラリの名前を指定します。 ここで指定された共有ライブラリが存在する場合、ヘッダファイルをパーズする代わりにその共有ライブラリを読み込みます。共有ライブラリが存在しない場合は、ヘッダファイルのパーズが行われます。


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.3 Classes and functions about C types

Class: <c-type>

Cの型に対応したクラスの基底クラスです。

Class: <c-type-meta>

Cの型に対応したクラスのメタクラスです。Cの型のクラスはすべて <c-type-meta> のインスタンスとなります。

Class: <c-char>
Class: <c-uchar>
Class: <c-short>
Class: <c-ushort>
Class: <c-int>
Class: <c-uint>
Class: <c-long>
Class: <c-ulong>
Class: <c-longlong>
Class: <c-ulonglong>
Class: <c-float>
Class: <c-double>

C言語の算術型 (Arithmetic types) に対応するクラスです。 make を使ってインスタンスを作成することができます。

Class: <c-value-meta>

C言語の算術型 (Arithmetic types) に対応するクラスのメタクラスです。

Method: ref (obj <c-value-meta>)

obj の値を返します。

Method: (setter ref) (obj <c-value-meta>) value

obj に値 value を設定します。

Class: <c-void>

C言語の void 型に対応するクラスです。

Class: <c-ptr>

C言語のポインタ型に対応する抽象クラスです。実際にポインタ型を使用する場合は、後述する ptr を使用して <c-ptr> の具象クラスを作成する必要があります。

Method: ptr (class <c-type-meta>)

class で指定された型のポインタ型に対応するクラスを生成します。このクラスは <c-ptr> のサブクラスとなります。make を使って作成したクラスのインスタンスを作成することができます。

Method: ptr (obj <c-type>)

obj のポインタオブジェクトを返します。

Class: <c-array>

C言語の配列に対応する抽象クラスです。実際に配列を使用する場合は、後述する c-array を使用して、<c-array> の具象クラスを作成する必要があります。

また <c-array><sequence> を継承しており、シーケンスフレームワーク (gauche.sequence モジュール) で定義されている操作を使用することができます。

Function: c-array (class <c-type-meta>) size

class 型で大きさ size の配列クラスを生成します。このクラスは <c-array> のサブクラスとなります。make を使って作成したクラスのインスタンスを作成することができます。

Class: <c-struct>

C言語の構造体に対応する抽象クラスです。

Class: <c-union>

C言語の共用体に対応する抽象クラスです。

Macro: c-struct tagname

tagnameという名前の構造体クラスを返します。

Macro: c-union tagname

tagnameという名前の共用体クラスを返します。

Method: ref (obj <c-struct>) member
Method: ref (obj <c-union>) member

構造体もしくは共用体のメンバ member の値を返します。メンバが算術型 (Arithmetic types) の場合は <real> もしくは <integer> へ変換された値が返ります。

Method: (setter ref) (obj <c-struct>) member value
Method: (setter ref) (obj <c-union>) member value

構造体もしくは共用体のメンバ member へ値をセットします。

Class: <c-func-ptr>

C言語の関数ポインタに対応する抽象クラスです。

Method: deref ptrobj

ポインタオブジェクト ptrobj の参照を返します。

Method: c-sizeof (class <c-type-meta>)
Method: c-sizeof (obj <c-type>)

classもしくはobjのサイズを返します。

Method: cast class obj

obj から class 型のインスタンスを生成します。生成のルールは以下の通りです。

class

objの型

説明

<c-value> を継承したクラス

<real>

obj と同じ値を持つ class のインスタンスを生成します

<c-value> を継承したクラス

<boolean>

obj#t の時は 1、#f の時は 0 の値を持つ class のインスタンスを生成します。

<c-ptr> または <c-func-ptr>

<integer>

obj のアドレスを持つポインタを生成します

<c-ptr> または <c-func-ptr>

<c-ptr> または <c-func-ptr>

obj と同じアドレスを持つポインタを生成します

<c-ptr> または <c-func-ptr>

<c-array>

obj の先頭アドレスを持つポインタを生成します。

<c-ptr>

<string>

Cの文字列を生成し、その先頭アドレスを持つポインタを生成します。

<c-ptr>

<uvector>

obj の先頭アドレスを持つポインタを生成します。

<c-ptr>

<sequence>

obj と同じ内容の配列を生成し、その先頭アドレスを持つポインタを生成します。

<c-array>

<sequence>

obj と同じ内容の配列を生成します。

<c-array>

<c-ptr>

ポインタが指すメモリ領域を配列と見なして class のインスタンスを生成します。

<c-func-ptr>

<procedure>

obj を実行する関数のポインタを生成します。

<integer> または <real>

<c-value>

obj と同じ値の数値を返します。

<boolean>

<c-value> または <real>

値が 0 のとき #f が、そうでなければ #t が返ります。

<integer>

<c-ptr> または <c-func-ptr>

ポインタのアドレスが返ります。

<integer>

<c-array>

配列の先頭アドレスが返ります。

<string>

<c-ptr> または <c-func-ptr>

ポインタが指し示す領域が0終端された文字列と見なし、文字列を生成します。

<string>

<c-array>

配列の内容が0終端された文字列と見なし、文字列を生成します。

<collection>

<c-array>

配列の内容を持つ class のインスタンスを生成します。各要素の値は scm-cast でキャストされます。

Function: scm-cast obj

obj<c-value> 型である時は数値を返します。そうでなければ obj をそのまま返します。

Function: make-null-ptr

NULLポインタを返します。

Function: null-ptr? obj

obj がNULLポインタであれば #t が、そうでなければ #f が返されます。

Function: register-finalizer! ptrobj proc

ptrobj にファイナライザ proc を登録します。ptrobj がGCされるときに proc が呼ばれます。

Function: unregister-finalizer! ptrobj

ptrobj のファイナライザを削除します。

Function: finalize! ptrobj

ptrobj のファイナライザを実行します。これにより ptrobj のファイナライザは削除されます。


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2. objc-wrapper

objc-wrapper とは Objective-C のライブラリを Gauche から使えるようにするためのモジュールです。


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2.1 Load a framework library

Function: c-load-framework framework

フレームワークライブラリ framework を読み込みます。

 
(c-load-framework "Foundation")

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2.2 Load an Objective-C header file

Function: Macro c-include file-or-list &keyword

objc-wrapper モジュールの c-include では Objective-C のクラス定義やメソッド定義の拡張が行われています。

Objective-C のクラス Foo がヘッダファイルで定義されていた場合、 Foo<Foo> の2つのシンボルが定義されます。Foo<c-struct:objc_objcect> 型のオブジェクトでクラス Foo そのものを指しています。<Foo><id> のエイリアスとなります。


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2.3 Functions about Objective-C objects

Macro: define-objc-class class-name super-class-name

super-class-nameを継承したObjective-Cのクラスclass-nameを定義します。

Macro: define-objc-method objc-class ret-type (message-keyword varspec ...) body ...

メッセージキーワードがmessage-keywordであり戻り値の型がret-typeであるようなobjc-classのメソッドを定義します。varspecは(変数名 変数型)のリストを指定します。

bodyではselfsuperの2つの変数が使えます。それぞれ、レシーバオブジェクト、レシーバをスーパクラスにキャストしたオブジェクトを指しています。

 
(define-objc-method MyFooClass <id> [:doSomething (v <c-int>)]
  [super :doSomething v]
  [self :foo v]
  (bar v) ;; The result of (bar v) is the return value of this method.
  )
Method: object-apply (obj <c-struct:objc_object>) selector-or-args ...

obj のメソッドを呼び出します。

 
[[NSString :alloc] :init]

[[NSString :startWithCString ( "Hello, world") :encoding NSASCIIStringEncoding]]
Function: @ str

Objective-C文字列を返します。

Function: @selector method-name

メソッド名 method-name のセレクタを返します。


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

A. Function and Syntax Index

Jump to:   (   @  
C   D   F   M   N   O   P   R   S   U  
Index Entry Section

(
(setter1.3 Classes and functions about C types
(setter1.3 Classes and functions about C types
(setter1.3 Classes and functions about C types

@
@2.3 Functions about Objective-C objects
@selector2.3 Functions about Objective-C objects

C
c-array1.3 Classes and functions about C types
c-include1.2 Load a header file
c-ld1.1 Load a shared library
c-load1.2 Load a header file
c-load-framework2.1 Load a framework library
c-load-library1.1 Load a shared library
c-sizeof1.3 Classes and functions about C types
c-sizeof1.3 Classes and functions about C types
c-struct1.3 Classes and functions about C types
c-union1.3 Classes and functions about C types
cast1.3 Classes and functions about C types

D
define-objc-class2.3 Functions about Objective-C objects
define-objc-method2.3 Functions about Objective-C objects
deref1.3 Classes and functions about C types

F
finalize!1.3 Classes and functions about C types

M
Macro2.2 Load an Objective-C header file
make-null-ptr1.3 Classes and functions about C types

N
null-ptr?1.3 Classes and functions about C types

O
object-apply2.3 Functions about Objective-C objects

P
ptr1.3 Classes and functions about C types
ptr1.3 Classes and functions about C types

R
ref1.3 Classes and functions about C types
ref1.3 Classes and functions about C types
ref1.3 Classes and functions about C types
register-finalizer!1.3 Classes and functions about C types

S
scm-cast1.3 Classes and functions about C types

U
unregister-finalizer!1.3 Classes and functions about C types

Jump to:   (   @  
C   D   F   M   N   O   P   R   S   U  

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

B. Class Index

For readability, the surrounding < and > are stripped off.

Jump to:   C  
Index Entry Section

C
c-char1.3 Classes and functions about C types
c-double1.3 Classes and functions about C types
c-float1.3 Classes and functions about C types
c-int1.3 Classes and functions about C types
c-long1.3 Classes and functions about C types
c-longlong1.3 Classes and functions about C types
c-short1.3 Classes and functions about C types
c-uchar1.3 Classes and functions about C types
c-uint1.3 Classes and functions about C types
c-ulong1.3 Classes and functions about C types
c-ulonglong1.3 Classes and functions about C types
c-ushort1.3 Classes and functions about C types
c-value-meta1.3 Classes and functions about C types

Jump to:   C  

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

C. Variable Index


[Top] [Contents] [Index] [ ? ]

Table of Contents


[Top] [Contents] [Index] [ ? ]

About This Document

This document was generated by KOGURO, Naoki on December, 26 2006 using texi2html 1.76.

The buttons in the navigation panels have the following meaning:

Button Name Go to From 1.2.3 go to
[ < ] Back previous section in reading order 1.2.2
[ > ] Forward next section in reading order 1.2.4
[ << ] FastBack beginning of this chapter or previous chapter 1
[ Up ] Up up section 1.2
[ >> ] FastForward next chapter 2
[Top] Top cover (top) of document  
[Contents] Contents table of contents  
[Index] Index index  
[ ? ] About about (help)  

where the Example assumes that the current position is at Subsubsection One-Two-Three of a document of the following structure:


This document was generated by KOGURO, Naoki on December, 26 2006 using texi2html 1.76.