Class: GRM::Args

Inherits:
Object
  • Object
show all
Defined in:
lib/grm.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**args) ⇒ Args

Returns a new instance of Args.



76
77
78
79
80
81
82
83
# File 'lib/grm.rb', line 76

def initialize(**args)
  @args = GRM.args_new
  @args.free = FFI["grm_args_delete"]
  @references = []
  args.each do |key, value|
    push(key, value)
  end
end

Class Method Details

.try_convert(value) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/grm.rb', line 66

def try_convert(value)
  case value
  when Hash
    new(**value)
  else
    nil
  end
end

Instance Method Details

#addressObject



142
143
144
# File 'lib/grm.rb', line 142

def address
  @args.to_i
end

#clearObject



137
138
139
140
# File 'lib/grm.rb', line 137

def clear
  GRM.args_clear(@args)
  @references.clear
end

#push(key, value) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/grm.rb', line 85

def push(key, value)
  key = key.to_s if key.is_a?(Symbol)
  case value
  when String
    GRM.args_push(@args, key, "s", :const_string, value)
  when Integer
    GRM.args_push(@args, key, "i", :int, value)
  when Float
    GRM.args_push(@args, key, "d", :double, value)
  when Args
    GRM.args_push(@args, key, "a", :voidp, value.address)
    value.to_gr.free = nil
  when Array
    case value[0]
    when String
      addresses = value.collect {|v| Fiddle::Pointer[v].to_i}
      GRM.args_push(@args, key, "nS",
                    :int, value.size,
                    :voidp, addresses.pack("J*"))
    when Integer
      GRM.args_push(@args, key, "nI",
                    :int, value.size,
                    :voidp, value.pack("i*"))
    when Float
      GRM.args_push(@args, key, "nD",
                    :int, value.size,
                    :voidp, value.pack("d*"))
    when Args
      GRM.args_push(@args, key, "nA",
                    :int, value.size,
                    :voidp, value.collect(&:address).pack("J*"))
      value.each do |v|
        v.to_gr.free = nil
      end
    else
      vs = value.collect {|v| Args.new(**v)}
      @references.concat(vs)
      GRM.args_push(@args, key, "nA",
                    :int, value.size,
                    :voidp, vs.collect(&:address).pack("J*"))
      vs.each do |v|
        v.to_gr.free = nil
      end
    end
  else
    v = Args.new(**value)
    @references << v
    GRM.args_push(@args, key, "a", :voidp, v.address)
    v.to_gr.free = nil
  end
end

#to_grObject



146
147
148
# File 'lib/grm.rb', line 146

def to_gr
  @args
end