-
Notifications
You must be signed in to change notification settings - Fork 5
/
gobject.cr
83 lines (71 loc) · 2.52 KB
/
gobject.cr
1
2
3
4
5
6
7
8
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
module Vips
class GObject
protected def initialize(@handle : LibVips::GObject*)
end
# Connects a `callback` to a signal on this object.
# The callback will be triggered every time this signal is issued on this instance.
def signal_connect(signal : String, callback : Proc, data : Pointer(Void) = Pointer(Void).null) : LibVips::Gulong
if (cb = callback.as?(Image::EvalProc))
em = LibVips::EvalSignal.new { |imgptr, progressptr, data|
next if imgptr.null? || progressptr.null?
img = Image.new(imgptr)
progress = progressptr.value
cb.call(img, progress)
}
callback = em
end
LibVips.g_signal_connect_data(@handle, signal,
callback.pointer,
data,
nil,
LibVips::GConnectFlags::After).tap do |ret|
raise VipsException.new("Couldn't connect signal #{signal}") if ret == 0
end
end
# Disconnects a handler from this object
def signal_disconnect(handler_id : LibVips::Gulong)
LibVips.g_signal_handler_disconnect(@handle, handler_id) unless handler_id == 0
end
# Disconnects all handlers from this object that match `func` and `data`
def signal_disconnect(func : Proc, data : Void* = Pointer(Void).null)
LibVips.g_signal_handlers_disconnect_matched(@handle,
LibVips::GSignalMatchType::GSignalMatchFunc |
LibVips::GSignalMatchType::GSignalMatchData,
0, 0, Pointer(LibVips::GClosure).null, nil, data)
end
# Disconnects all handlers from this object that match
def signal_disconnect(data : LibVips::Gpointer)
LibVips.g_signal_handlers_disconnect_matched(@handle,
LibVips::GSignalMatchType::GSignalMatchData,
0, 0, Pointer(LibVips::GClosure).null, nil, data)
end
# Decreases the reference count of object.
# When its reference count drops to 0, its memory is freed.
def release_handle
LibVips.g_object_unref(@handle) unless @handle.null?
true
end
# Increases the reference count of object
def object_ref
LibVips.g_object_ref(@handle)
end
# Get the reference count of object.
def ref_count
@handle.value.ref_count
end
def get(name : String, gval : GValue)
LibVips.g_object_get_property(@handle, name, gval)
gval.get
end
def set(name : String, gval : GValue)
LibVips.g_object_set_property(@handle, name, gval)
end
# :nodoc:
def handle
@handle
end
def finalize
LibVips.g_object_unref(@handle)
end
end
end