Class: FFI::Tcl::EvalResult

Inherits:
Object show all
Defined in:
lib/ffi-tk/ffi/tcl/eval_result.rb

Overview

This whole class feels very awkward, maybe it should be merged with Obj.

Constant Summary collapse

TYPES =
{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.guess(interp, obj, fallback = nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ffi-tk/ffi/tcl/eval_result.rb', line 31

def self.guess(interp, obj, fallback = nil)
  obj = Obj.new(obj) unless obj.respond_to?(:type)
  type = TYPES[obj.type.to_i]

  case type
  when :list
    to_list(interp, obj)
  when :string, :pixel, :cmdName
    to_string(interp, obj)
  when :int
    to_int(interp, obj)
  when :double
    to_double(interp, obj)
  else
    if fallback
      __send__(fallback, interp, obj)
    else
      raise "Unknown type: %p" % [type] if type
      new(interp, obj)
    end
  end
end

.map_list_core(interp, obj, &block) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ffi-tk/ffi/tcl/eval_result.rb', line 71

def self.map_list_core(interp, obj, &block)
  objc_ptr = MemoryPointer.new(:int)
  objv_ptr = MemoryPointer.new(:pointer)

  if Tcl.list_obj_get_elements(interp, obj, objc_ptr, objv_ptr) == 0
    objv_ptr.get_pointer(0).
      read_array_of_pointer(objc_ptr.get_int(0)).
      map(&block)
  else
    panic(interp, 'Tcl_ListObjGetElements')
  end
end

.panic(interp, function) ⇒ Object



111
112
113
114
115
116
117
118
119
# File 'lib/ffi-tk/ffi/tcl/eval_result.rb', line 111

def self.panic(interp, function)
  message = guess(interp, Obj.new(Tcl.get_obj_result(interp))).to_s

  if message.empty?
    raise 'Failure during call of: %p' % [function]
  else
    raise '%s during call of: %p' % [message, function]
  end
end

.reset_types(interp) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ffi-tk/ffi/tcl/eval_result.rb', line 7

def self.reset_types(interp)
  TYPES.clear
  list = Tcl.new_list_obj(0, nil)
  Tcl.append_all_obj_types(interp, list)

  objc_ptr = MemoryPointer.new(:int)
  objv_ptr = MemoryPointer.new(:pointer)

  string_length_ptr = MemoryPointer.new(:int)

  if Tcl.list_obj_get_elements(interp, list, objc_ptr, objv_ptr) == 0
    array_ptr = objv_ptr.get_pointer(0)
    array_length = objc_ptr.get_int(0)
    array = array_ptr.read_array_of_pointer(array_length)
    array.each do |type_ptr|
      name = Tcl.get_string_from_obj(type_ptr, string_length_ptr)
      type = Tcl.get_obj_type(name)
      TYPES[type.to_i] = name.to_sym
    end
  else
    panic 'Tcl_ListObjGetElements'
  end
end

.to_boolean(interp, obj) ⇒ Object



84
85
86
87
88
89
90
91
92
# File 'lib/ffi-tk/ffi/tcl/eval_result.rb', line 84

def self.to_boolean(interp, obj)
  boolean_pointer = MemoryPointer.new(:int)

  if Tcl.get_boolean_from_obj(interp, obj, boolean_pointer) == 0
    boolean_pointer.get_int(0) == 1
  else
    panic(interp, 'Tcl_GetBooleanFromObj')
  end
end

.to_double(interp, obj) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/ffi-tk/ffi/tcl/eval_result.rb', line 54

def self.to_double(interp, obj)
  double_pointer = MemoryPointer.new(:double)

  if Tcl.get_double_from_obj(interp, obj, double_pointer) == 0
    double_pointer.get_double(0)
  else
    raise "Couldn't get double from %p" % [obj]
  end
end

.to_int(interp, obj) ⇒ Object



94
95
96
97
98
99
100
101
102
# File 'lib/ffi-tk/ffi/tcl/eval_result.rb', line 94

def self.to_int(interp, obj)
  int_pointer = MemoryPointer.new(:int)

  if Tcl.get_int_from_obj(interp, obj, int_pointer) == 0
    int_pointer.get_int(0)
  else
    panic(interp, 'Tcl_GetIntFromObj')
  end
end

.to_list(interp, obj) ⇒ Object



64
65
66
67
68
69
# File 'lib/ffi-tk/ffi/tcl/eval_result.rb', line 64

def self.to_list(interp, obj)
  map_list_core(interp, obj) do |pointer|
    value = guess(interp, pointer, :to_string)
    block_given? ? yield(value) : value
  end
end

.to_string(interp, obj) ⇒ Object



104
105
106
107
108
109
# File 'lib/ffi-tk/ffi/tcl/eval_result.rb', line 104

def self.to_string(interp, obj)
  length_pointer = MemoryPointer.new(:int)

  string = Tcl.get_string_from_obj(obj, length_pointer)
  string.force_encoding(Encoding.default_external)
end

Instance Method Details

#inspectObject



164
165
166
# File 'lib/ffi-tk/ffi/tcl/eval_result.rb', line 164

def inspect
  "#<EvalResult #{to_s}>"
end

#to_a(&block) ⇒ Object



121
122
123
# File 'lib/ffi-tk/ffi/tcl/eval_result.rb', line 121

def to_a(&block)
  self.class.to_list(interp, obj, &block)
end

#to_a?(&block) ⇒ Boolean

Returns:

  • (Boolean)


125
126
127
128
# File 'lib/ffi-tk/ffi/tcl/eval_result.rb', line 125

def to_a?(&block)
  value = self.class.to_list(interp, obj, &block)
  value.empty? ? nil : value
end

#to_booleanObject



156
157
158
# File 'lib/ffi-tk/ffi/tcl/eval_result.rb', line 156

def to_boolean
  self.class.to_boolean(interp, obj)
end

#to_fObject



143
144
145
# File 'lib/ffi-tk/ffi/tcl/eval_result.rb', line 143

def to_f
  self.class.to_double(interp, obj)
end

#to_iObject



139
140
141
# File 'lib/ffi-tk/ffi/tcl/eval_result.rb', line 139

def to_i
  self.class.to_int(interp, obj)
end

#to_sObject



147
148
149
# File 'lib/ffi-tk/ffi/tcl/eval_result.rb', line 147

def to_s
  self.class.to_string(interp, obj)
end

#to_s?Boolean

Returns:

  • (Boolean)


151
152
153
154
# File 'lib/ffi-tk/ffi/tcl/eval_result.rb', line 151

def to_s?
  value = self.class.to_string(interp, obj)
  value.empty? ? nil : value
end

#to_symObject



130
131
132
# File 'lib/ffi-tk/ffi/tcl/eval_result.rb', line 130

def to_sym
  self.class.to_string(interp, obj).to_sym
end

#to_sym?Boolean

Returns:

  • (Boolean)


134
135
136
137
# File 'lib/ffi-tk/ffi/tcl/eval_result.rb', line 134

def to_sym?
  value = self.class.to_string(interp, obj).to_sym
  value.empty? ? nil : value.to_sym
end

#to_tclObject



160
161
162
# File 'lib/ffi-tk/ffi/tcl/eval_result.rb', line 160

def to_tcl
  to_s.to_tcl
end