1module Fiddle
2  class Closure
3
4    # the C type of the return of the FFI closure
5    attr_reader :ctype
6
7    # arguments of the FFI closure
8    attr_reader :args
9
10    # Extends Fiddle::Closure to allow for building the closure in a block
11    class BlockCaller < Fiddle::Closure
12
13      # == Description
14      #
15      # Construct a new BlockCaller object.
16      #
17      # * +ctype+ is the C type to be returned
18      # * +args+ are passed the callback
19      # * +abi+ is the abi of the closure
20      #
21      # If there is an error in preparing the +ffi_cif+ or +ffi_prep_closure+,
22      # then a RuntimeError will be raised.
23      #
24      # == Example
25      #
26      #   include Fiddle
27      #
28      #   cb = Closure::BlockCaller.new(TYPE_INT, [TYPE_INT]) do |one|
29      #     one
30      #   end
31      #
32      #   func = Function.new(cb, [TYPE_INT], TYPE_INT)
33      #
34      def initialize ctype, args, abi = Fiddle::Function::DEFAULT, &block
35        super(ctype, args, abi)
36        @block = block
37      end
38
39      # Calls the constructed BlockCaller, with +args+
40      #
41      # For an example see Fiddle::Closure::BlockCaller.new
42      #
43      def call *args
44        @block.call(*args)
45      end
46    end
47  end
48end
49