~ubuntu-branches/ubuntu/quantal/ruby1.9.1/quantal

« back to all changes in this revision

Viewing changes to ext/fiddle/lib/fiddle/closure.rb

  • Committer: Bazaar Package Importer
  • Author(s): Lucas Nussbaum
  • Date: 2011-09-24 19:16:17 UTC
  • mfrom: (1.1.8 upstream) (13.1.7 experimental)
  • Revision ID: james.westby@ubuntu.com-20110924191617-o1qz4rcmqjot8zuy
Tags: 1.9.3~rc1-1
* New upstream release: 1.9.3 RC1.
  + Includes load.c fixes. Closes: #639959.
* Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
module Fiddle
2
2
  class Closure
 
3
 
 
4
    # the C type of the return of the FFI closure
3
5
    attr_reader :ctype
 
6
 
 
7
    # arguments of the FFI closure
4
8
    attr_reader :args
5
9
 
 
10
    # Extends Fiddle::Closure to allow for building the closure in a block
6
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
      #
7
34
      def initialize ctype, args, abi = Fiddle::Function::DEFAULT, &block
8
35
        super(ctype, args, abi)
9
36
        @block = block
10
37
      end
11
38
 
 
39
      # Calls the constructed BlockCaller, with +args+
 
40
      #
 
41
      # For an example see Fiddle::Closure::BlockCaller.new
 
42
      #
12
43
      def call *args
13
44
        @block.call(*args)
14
45
      end