class DL::Function
クラスの継承リスト: DL::Function < Object < Kernel < BasicObject
要約
DL::CFunc のラッパークラスです。
特異メソッド
new(cfunc, argtypes) -> DL::Function
new(cfunc, argtypes) {|*arg| ... } -> DL::Function
-
指定された DL::CFunc オブジェクト cfunc をラップする DL::Function オブジェクトを生成して返します。
- [PARAM] cfunc:
- DL::CFunc オブジェクトを指定します。
- [PARAM] argtypes:
-
cfunc の引数の型をDLに定義された定数の配列で指定します。
unsigned char など unsigned を指定したい場合は - を付けて
- TYPE_CHAR
を使います。
例:
require 'dl/import' h = DL::Handle.new addr = h.sym('strncmp') cfunc = DL::CFunc.new(addr, DL::TYPE_INT) func = DL::Function.new(cfunc, [DL::TYPE_VOIDP, DL::TYPE_VOIDP, DL::TYPE_INT]) p func.call("abcd", "abcz", 3) #=> 0
[SEE_ALSO] DL::TYPE_INT
インスタンスメソッド
bind {|*args| ... }
-
自身に指定されたブロックをバインドします。 自身に対して call メソッドが呼ばれたとき、生成時に与えられた DL::CFunc オブジェクトではなく このブロックが呼ばれます。
例:
require 'dl/import' cfunc = DL::CFunc.new(0, DL::TYPE_INT) func = DL::Function.new(cfunc, [DL::TYPE_INT]) func.bind{|i| i + 3 } p func.call(2) #=> 5
call(*arg) -> DL::CPtr | Integer | Float | nil
call(*arg) {|*v| ... } -> DL::CPtr | Integer | Float | nil
-
自身が表す関数を呼び、返り値を Ruby のオブジェクトに変換して返します。
自身の返り値の型がポインタである場合、返り値を DL::CPtr オブジェクトにして返します。 返り値の型が double か float である場合は、返り値を Ruby の Float オブジェクトに変換して返します。 返り値の型が void の場合は nil を返します。それ以外の場合は整数として返します。
ブロックを与えた場合、引数の中にバインドされていない DL::Function オブジェクトが もしあれば与えられたブロックをバインドし、その後上と同じように関数を呼びます。
- [PARAM] arg:
- 関数に与える引数を指定します。引数の型がポインタである場合は、 DL::CPtr オブジェクト、DL::Function オブジェクト、IO オブジェクト、 整数、文字列のいずれかを指定します。特に文字列を与えた場合は、 文字列はポインタが指すメモリ領域として扱われます。 それ以外の場合は、Integer か Float を与えます。
例:
require 'dl/import' h = DL::Handle.new addr = h.sym('fread') cfunc = DL::CFunc.new(addr, DL::TYPE_INT) func = DL::Function.new(cfunc, [DL::TYPE_VOIDP, DL::TYPE_LONG, DL::TYPE_LONG, DL::TYPE_VOIDP]) io = File.open('s.txt') s = " " * 8 func.call(s, 4, 1, io) p s #=> "hoge "
例:
require 'dl/import' h = DL::Handle.new addr = h.sym('strdup') cfunc = DL::CFunc.new(addr, DL::TYPE_VOIDP) func = DL::Function.new(cfunc, [DL::TYPE_VOIDP]) s = 'abc' ptr = func.call(s) p ptr.to_s #=> "abc"
name -> String
-
自身が表す関数の名前を文字列で返します。
require 'dl/import' h = DL::Handle.new addr = h.sym('strncmp') cfunc = DL::CFunc.new(addr, DL::TYPE_INT, 'strncmp') func = DL::Function.new(cfunc, [DL::TYPE_VOIDP, DL::TYPE_VOIDP, DL::TYPE_INT]) p func.name #=> "strncmp"
to_i -> Integer
-
自身が表す関数のアドレスを整数で返します。
require 'dl/import' h = DL::Handle.new addr = h.sym('strncmp') cfunc = DL::CFunc.new(addr, DL::TYPE_INT) func = DL::Function.new(cfunc, [DL::TYPE_VOIDP, DL::TYPE_VOIDP, DL::TYPE_INT]) p func.to_i #=> 3084587680
unbind -> ()
-
自身から関数をアンバインドします。
class DL::Function