@@ -10,52 +10,59 @@ class GValue < FFI::ManagedStruct
10
10
layout :gtype , :GType ,
11
11
:data , [ :ulong_long , 2 ]
12
12
13
- def self . release ( ptr )
14
- Vips ::log "GLib::GValue::release ptr = #{ ptr } "
15
- GLib ::g_value_unset ( ptr )
13
+ def self . release ptr
14
+ # Vips::log "GLib::GValue::release ptr = #{ptr}"
15
+ GLib ::g_value_unset ptr
16
16
end
17
17
18
18
G_FREE_CALLBACK = Proc . new do |ptr |
19
19
Vips ::g_free ptr
20
20
end
21
21
22
22
def self . alloc
23
- # we seem to need the extra cast, I'm not sure why
23
+ # allocate memory
24
24
memory = FFI ::MemoryPointer . new GValue
25
+
26
+ # make this alloc autorelease ... we mustn't release in
27
+ # GValue::release, since we are used to wrap GValue pointers
28
+ # made by other people
25
29
pointer = FFI ::Pointer . new GValue , memory
26
- GValue . new ( pointer )
30
+
31
+ # ... and wrap in a GValue
32
+ GValue . new pointer
27
33
end
28
34
29
- def init ( gtype )
30
- GLib ::g_value_init ( self , gtype )
35
+ def init gtype
36
+ GLib ::g_value_init self , gtype
31
37
end
32
38
33
- def set ( value )
34
- Vips ::log "GLib::GValue.set: value = #{ value } "
39
+ def set value
40
+ # Vips::log "GLib::GValue.set: value = #{value}"
35
41
36
42
gtype = self [ :gtype ]
37
43
fundamental = GLib ::g_type_fundamental gtype
38
44
39
45
case gtype
40
46
when GBOOL_TYPE
41
47
GLib ::g_value_set_boolean self , ( value ? 1 : 0 )
48
+
42
49
when GINT_TYPE
43
50
GLib ::g_value_set_int self , value
51
+
44
52
when GDOUBLE_TYPE
45
53
GLib ::g_value_set_double self , value
54
+
46
55
when GSTR_TYPE
47
56
# set_string takes a copy, no lifetime worries
48
57
GLib ::g_value_set_string self , value
49
- when Vips ::IMAGE_TYPE
50
- # g_value_set_object() will add an extra ref
51
- GLib ::g_value_set_object self , value
52
- GLib ::g_object_unref value
58
+
53
59
when Vips ::ARRAY_INT_TYPE
54
60
value = [ value ] if not value . is_a? Array
55
61
56
62
Vips ::vips_value_set_array_int self , nil , value . length
57
63
ptr = Vips ::vips_value_get_array_int self , nil
58
64
ptr . write_array_of_int32 value
65
+
59
66
when Vips ::ARRAY_DOUBLE_TYPE
60
67
value = [ value ] if not value . is_a? Array
61
68
@@ -66,6 +73,7 @@ def set(value)
66
73
ptr = Vips ::vips_value_get_array_double self , nil
67
74
68
75
ptr . write_array_of_double value
76
+
69
77
when Vips ::ARRAY_IMAGE_TYPE
70
78
value = [ value ] if not value . is_a? Array
71
79
@@ -75,15 +83,18 @@ def set(value)
75
83
76
84
# the gvalue needs a ref on each of the images
77
85
value . each { |image | GLib ::g_object_ref image }
86
+
78
87
when Vips ::BLOB_TYPE
79
- ptr = GLib ::g_malloc value . length
80
- ptr . write_bytes ( value )
81
- Vips ::vips_value_set_blob self ,
82
- G_FREE_CALLBACK , ptr , value . length
88
+ len = value . length
89
+ ptr = GLib ::g_malloc len
90
+ ptr . write_bytes value
91
+ Vips ::vips_value_set_blob self , G_FREE_CALLBACK , ptr , len
92
+
83
93
else
84
94
case fundamental
85
95
when GFLAGS_TYPE
86
96
GLib ::g_value_set_flags self , value
97
+
87
98
when GENUM_TYPE
88
99
value = value . to_s if value . is_a? Symbol
89
100
@@ -96,6 +107,12 @@ def set(value)
96
107
end
97
108
98
109
GLib ::g_value_set_enum self , value
110
+
111
+ when GOBJECT_TYPE
112
+ # g_value_set_object() will add an extra ref
113
+ GLib ::g_value_set_object self , value
114
+ GLib ::g_object_unref value
115
+
99
116
else
100
117
raise Vips ::Error , "unimplemented gtype for set: #{ gtype } "
101
118
end
@@ -110,25 +127,27 @@ def get
110
127
case gtype
111
128
when GBOOL_TYPE
112
129
result = GLib ::g_value_get_boolean ( self ) != 0 ? true : false
130
+
113
131
when GINT_TYPE
114
- result = GLib ::g_value_get_int ( self )
132
+ result = GLib ::g_value_get_int self
133
+
115
134
when GDOUBLE_TYPE
116
- result = GLib ::g_value_get_double ( self )
135
+ result = GLib ::g_value_get_double self
136
+
117
137
when GSTR_TYPE
118
138
# FIXME do we need to strdup here?
119
139
result = GLib ::g_value_get_string self
120
- when Vips ::IMAGE_TYPE
121
- # g_value_get_object() does not add a ref
122
- obj = GLib ::g_value_get_object self
123
- result = Vips ::Image . new obj
140
+
124
141
when Vips ::ARRAY_INT_TYPE
125
142
len = Vips ::IntStruct . new
126
143
array = Vips ::vips_value_get_array_int self , len
127
144
result = array . get_array_of_int32 0 , len [ :value ]
145
+
128
146
when Vips ::ARRAY_DOUBLE_TYPE
129
147
len = Vips ::IntStruct . new
130
148
array = Vips ::vips_value_get_array_double self , len
131
149
result = array . get_array_of_double 0 , len [ :value ]
150
+
132
151
when Vips ::ARRAY_IMAGE_TYPE
133
152
len = Vips ::IntStruct . new
134
153
array = Vips ::vips_value_get_array_image self , len
@@ -137,29 +156,39 @@ def get
137
156
GLib ::g_object_ref pointer
138
157
Vips ::Image . new pointer
139
158
end
159
+
140
160
when Vips ::BLOB_TYPE
141
161
len = Vips ::SizeStruct . new
142
162
array = Vips ::vips_value_get_blob self , len
143
163
result = array . get_bytes 0 , len [ :value ]
164
+
144
165
else
145
166
case fundamental
146
167
when GFLAGS_TYPE
147
- result = GLib ::g_value_get_flags ( self )
168
+ result = GLib ::g_value_get_flags self
169
+
148
170
when GENUM_TYPE
149
- enum_value = GLib ::g_value_get_enum ( self )
171
+ enum_value = GLib ::g_value_get_enum self
150
172
# this returns a static string, no need to worry about
151
173
# lifetime
152
174
enum_name = Vips ::vips_enum_nick self [ :gtype ] , enum_value
153
175
if enum_name == nil
154
176
raise Vips ::Error
155
177
end
156
178
result = enum_name . to_sym
179
+
180
+ when GOBJECT_TYPE
181
+ # g_value_get_object() does not add a ref
182
+ obj = GLib ::g_value_get_object self
183
+ result = Vips ::Image . new obj
184
+
157
185
else
158
186
raise Vips ::Error , "unimplemented gtype for get: #{ gtype } "
187
+
159
188
end
160
189
end
161
190
162
- Vips ::log "GLib::GValue.get: result = #{ result } "
191
+ # Vips::log "GLib::GValue.get: result = #{result}"
163
192
164
193
return result
165
194
end
0 commit comments