Module: Interface

Included in:
InterfaceCollectionOut, InterfaceComposedOut, InterfaceSingleOut
Defined in:
lib/white_gold/abi/interface/interface.rb

Class Method Summary collapse

Class Method Details

.abi_const_string_method_suffix(str) ⇒ Object



5
6
7
# File 'lib/white_gold/abi/interface/interface.rb', line 5

def self.abi_const_string_method_suffix str
  return str.gsub(/([A-Z])/, '_\1').gsub(/::/, '__').downcase
end

.fiddle_type(type) ⇒ Object



96
97
98
99
100
101
# File 'lib/white_gold/abi/interface/interface.rb', line 96

def self.fiddle_type type
  if type == Integer || type == Boolean then Fiddle::TYPE_INT
  elsif type == Float then Fiddle::TYPE_FLOAT
  else Fiddle::TYPE_VOIDP
  end
end

.from(packer, unpacker) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/white_gold/abi/interface/interface.rb', line 107

def self.from packer, unpacker
  case unpacker
  when Array then InterfaceComposedOut
  when Range then InterfaceCollectionOut
  else InterfaceSingleOut
  end.from packer, unpacker
end

.packer_fiddle_type(type) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/white_gold/abi/interface/interface.rb', line 66

def self.packer_fiddle_type type
  if type == Integer || type == Boolean then Fiddle::TYPE_INT
  elsif type == Float then Fiddle::TYPE_FLOAT
  elsif type == String then Fiddle::TYPE_CONST_STRING
  else Fiddle::TYPE_VOIDP
  end
end

.parse_packer(packer) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/white_gold/abi/interface/interface.rb', line 9

def self.parse_packer packer
  case packer
  when Proc, Reducer
    packer
  when Array
    packers = packer.map{ parse_packer _1 }
    proc do |host, *a|
      ai = a.each
      packers.map do |packer|
        case packer
        when Proc then packer.call host, *ai.next
        when nil then ai.next
        when Reducer then packer.call host, *(0..packer.reduction).map{ ai.next }
        else packer
        end
      end.flatten
    end
  when Symbol
    if packer.to_s.start_with? "_"
      parse_packer "abi_pack#{packer}".to_sym
    else
      proc do |host, *a|
        host.send packer, *a
      end
    end
  when Class, Module, Enum
    parse_packer "abi_pack#{abi_const_string_method_suffix packer.name}".to_sym
  when String
    parse_packer "abi_pack#{abi_const_string_method_suffix packer}".to_sym
  when Range
    subpacker = parse_packer packer.min
    fiddle_type = packer_fiddle_type packer.min
    if packer.exclude_end?
      proc do |host, *a|
        it = a.each
        block_caller = Fiddle::Closure::BlockCaller.new(fiddle_type, [0]) do
          subpacker.call host, it.next
        rescue StopIteration
          subpacker.call host, nil
        end
        block_caller
      end
    else
      proc do |host, *a|
        it = a.each
        block_caller = Fiddle::Closure::BlockCaller.new(fiddle_type, [0]) do
          subpacker.call host, it.next
        end
        [a.size, block_caller]
      end
    end
  when nil
    proc{}
  else raise "Unknown packer type #{packer.class}"
  end
end

.parse_unpacker(unpacker) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/white_gold/abi/interface/interface.rb', line 74

def self.parse_unpacker unpacker
  case unpacker
  when Proc
    unpacker
  when Symbol
    if unpacker.to_s.start_with? "_"
      parse_unpacker "abi_unpack#{unpacker}".to_sym
    else
      proc do |host, *a|
        host.send unpacker, *a
      end
    end
  when Class, Module, Enum
    parse_unpacker "abi_unpack#{abi_const_string_method_suffix unpacker.name}".to_sym
  when String
    parse_unpacker "abi_unpack#{abi_const_string_method_suffix unpacker}".to_sym
  when nil
    nil
  else raise "Unknown unpacker type #{unpacker.class}"
  end
end