Skip to content

Commit 3575ad6

Browse files
committed
Improve naming for gtype
1 parent 22b0b90 commit 3575ad6

File tree

2 files changed

+38
-35
lines changed

2 files changed

+38
-35
lines changed

src/vips/gvalue.lua

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ gvalue.blob_type = gobject_lib.g_type_from_name("VipsBlob")
5656
gvalue.band_format_type = gobject_lib.g_type_from_name("VipsBandFormat")
5757
gvalue.blend_mode_type = version.at_least(8, 6) and gobject_lib.g_type_from_name("VipsBlendMode") or 0
5858

59+
-- gvalue.*_type can be of type cdata or number depending on the OS and Lua version
60+
-- gtypes as returned by vips_lib can also be of type cdata or number
61+
-- cdata and number are not comparable with Standard Lua (using luaffi-tkl)
5962
gvalue.comparable_type = type(gvalue.gdouble_type) == "number" and
6063
function(gtype) return tonumber(gtype) end or
6164
function(gtype) return gtype end
@@ -86,27 +89,27 @@ gvalue.init = function(gv, gtype)
8689
end
8790

8891
gvalue.set = function(gv, value)
89-
local gtype_raw = gv.gtype
90-
local gtype = gvalue.comparable_type(gtype_raw)
91-
local fundamental = gobject_lib.g_type_fundamental(gtype_raw)
92+
local gtype = gv.gtype
93+
local gtype_comp = gvalue.comparable_type(gtype)
94+
local fundamental = gobject_lib.g_type_fundamental(gtype)
9295

93-
if gtype == gvalue.gbool_type then
96+
if gtype_comp == gvalue.gbool_type then
9497
gobject_lib.g_value_set_boolean(gv, value)
95-
elseif gtype == gvalue.gint_type then
98+
elseif gtype_comp == gvalue.gint_type then
9699
gobject_lib.g_value_set_int(gv, value)
97-
elseif gtype == gvalue.gdouble_type then
100+
elseif gtype_comp == gvalue.gdouble_type then
98101
gobject_lib.g_value_set_double(gv, value)
99102
elseif fundamental == gvalue.genum_type then
100-
gobject_lib.g_value_set_enum(gv, gvalue.to_enum(gtype_raw, value))
103+
gobject_lib.g_value_set_enum(gv, gvalue.to_enum(gtype, value))
101104
elseif fundamental == gvalue.gflags_type then
102105
gobject_lib.g_value_set_flags(gv, value)
103-
elseif gtype == gvalue.gstr_type then
106+
elseif gtype_comp == gvalue.gstr_type then
104107
gobject_lib.g_value_set_string(gv, value)
105-
elseif gtype == gvalue.refstr_type then
108+
elseif gtype_comp == gvalue.refstr_type then
106109
gobject_lib.vips_value_set_ref_string(gv, value)
107-
elseif gtype == gvalue.image_type then
110+
elseif gtype_comp == gvalue.image_type then
108111
gobject_lib.g_value_set_object(gv, value.vimage)
109-
elseif gtype == gvalue.array_int_type then
112+
elseif gtype_comp == gvalue.array_int_type then
110113
if type(value) == "number" then
111114
value = { value }
112115
end
@@ -115,7 +118,7 @@ gvalue.set = function(gv, value)
115118
local a = ffi.new(gvalue.int_arr_typeof, n, value)
116119

117120
vips_lib.vips_value_set_array_int(gv, a, n)
118-
elseif gtype == gvalue.array_double_type then
121+
elseif gtype_comp == gvalue.array_double_type then
119122
if type(value) == "number" then
120123
value = { value }
121124
end
@@ -124,7 +127,7 @@ gvalue.set = function(gv, value)
124127
local a = ffi.new(gvalue.double_arr_typeof, n, value)
125128

126129
vips_lib.vips_value_set_array_double(gv, a, n)
127-
elseif gtype == gvalue.array_image_type then
130+
elseif gtype_comp == gvalue.array_image_type then
128131
if Image.is_Image(value) then
129132
value = { value }
130133
end
@@ -139,7 +142,7 @@ gvalue.set = function(gv, value)
139142
-- the gvalue needs a set of refs to own
140143
gobject_lib.g_object_ref(a[i])
141144
end
142-
elseif gtype == gvalue.blob_type then
145+
elseif gtype_comp == gvalue.blob_type then
143146
-- we need to set the blob to a copy of the lua string that vips
144147
-- can own
145148
local n = #value
@@ -153,27 +156,27 @@ gvalue.set = function(gv, value)
153156
vips_lib.vips_value_set_blob(gv, glib_lib.g_free, buf, n)
154157
end
155158
else
156-
error("unsupported gtype for set " .. gvalue.type_name(gtype_raw))
159+
error("unsupported gtype for set " .. gvalue.type_name(gtype))
157160
end
158161
end
159162

160163
gvalue.get = function(gv)
161-
local gtype_raw = gv.gtype
162-
local gtype = gvalue.comparable_type(gtype_raw)
163-
local fundamental = gobject_lib.g_type_fundamental(gtype_raw)
164+
local gtype = gv.gtype
165+
local gtype_comp = gvalue.comparable_type(gtype)
166+
local fundamental = gobject_lib.g_type_fundamental(gtype)
164167

165168
local result
166169

167-
if gtype == gvalue.gbool_type then
170+
if gtype_comp == gvalue.gbool_type then
168171
result = gobject_lib.g_value_get_boolean(gv)
169-
elseif gtype == gvalue.gint_type then
172+
elseif gtype_comp == gvalue.gint_type then
170173
result = gobject_lib.g_value_get_int(gv)
171-
elseif gtype == gvalue.gdouble_type then
174+
elseif gtype_comp == gvalue.gdouble_type then
172175
result = gobject_lib.g_value_get_double(gv)
173176
elseif fundamental == gvalue.genum_type then
174177
local enum_value = gobject_lib.g_value_get_enum(gv)
175178

176-
local cstr = vips_lib.vips_enum_nick(gtype_raw, enum_value)
179+
local cstr = vips_lib.vips_enum_nick(gtype, enum_value)
177180

178181
if cstr == ffi.NULL then
179182
error("value not in enum")
@@ -182,21 +185,21 @@ gvalue.get = function(gv)
182185
result = ffi.string(cstr)
183186
elseif fundamental == gvalue.gflags_type then
184187
result = gobject_lib.g_value_get_flags(gv)
185-
elseif gtype == gvalue.gstr_type then
188+
elseif gtype_comp == gvalue.gstr_type then
186189
local cstr = gobject_lib.g_value_get_string(gv)
187190

188191
if cstr ~= ffi.NULL then
189192
result = ffi.string(cstr)
190193
else
191194
result = nil
192195
end
193-
elseif gtype == gvalue.refstr_type then
196+
elseif gtype_comp == gvalue.refstr_type then
194197
local psize = ffi.new(gvalue.psize_typeof, 1)
195198

196199
local cstr = vips_lib.vips_value_get_ref_string(gv, psize)
197200

198201
result = ffi.string(cstr, tonumber(psize[0]))
199-
elseif gtype == gvalue.image_type then
202+
elseif gtype_comp == gvalue.image_type then
200203
-- g_value_get_object() will not add a ref ... that is
201204
-- held by the gvalue
202205
local vo = gobject_lib.g_value_get_object(gv)
@@ -208,7 +211,7 @@ gvalue.get = function(gv)
208211
gobject_lib.g_object_ref(vimage)
209212

210213
result = Image.new(vimage)
211-
elseif gtype == gvalue.array_int_type then
214+
elseif gtype_comp == gvalue.array_int_type then
212215
local pint = ffi.new(gvalue.pint_typeof, 1)
213216

214217
local array = vips_lib.vips_value_get_array_int(gv, pint)
@@ -217,15 +220,15 @@ gvalue.get = function(gv)
217220
result[i + 1] = array[i]
218221
end
219222

220-
elseif gtype == gvalue.array_double_type then
223+
elseif gtype_comp == gvalue.array_double_type then
221224
local pint = ffi.new(gvalue.pint_typeof, 1)
222225

223226
local array = vips_lib.vips_value_get_array_double(gv, pint)
224227
result = {}
225228
for i = 0, pint[0] - 1 do
226229
result[i + 1] = array[i]
227230
end
228-
elseif gtype == gvalue.array_image_type then
231+
elseif gtype_comp == gvalue.array_image_type then
229232
local pint = ffi.new(gvalue.pint_typeof, 1)
230233

231234
local array = vips_lib.vips_value_get_array_image(gv, pint)
@@ -240,14 +243,14 @@ gvalue.get = function(gv)
240243

241244
result[i + 1] = Image.new(vimage)
242245
end
243-
elseif gtype == gvalue.blob_type then
246+
elseif gtype_comp == gvalue.blob_type then
244247
local psize = ffi.new(gvalue.psize_typeof, 1)
245248

246249
local array = vips_lib.vips_value_get_blob(gv, psize)
247250

248251
result = ffi.string(array, tonumber(psize[0]))
249252
else
250-
error("unsupported gtype for get " .. gvalue.type_name(gtype_raw))
253+
error("unsupported gtype for get " .. gvalue.type_name(gtype))
251254
end
252255

253256
return result

src/vips/voperation.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,17 @@ end
7272

7373
voperation.set = function(self, name, flags, match_image, value)
7474
local vob = self:vobject()
75-
local gtype_raw = vob:get_typeof(name)
76-
local gtype = gvalue.comparable_type(gtype_raw)
75+
local gtype = vob:get_typeof(name)
76+
local gtype_comp = gvalue.comparable_type(gtype)
7777

7878
-- if the object wants an image and we have a constant, imageize it
7979
--
8080
-- if the object wants an image array, imageize any constants in the
8181
-- array
8282
if match_image then
83-
if gtype == gvalue.image_type then
83+
if gtype_comp == gvalue.image_type then
8484
value = match_image:imageize(value)
85-
elseif gtype == gvalue.array_image_type then
85+
elseif gtype_comp == gvalue.array_image_type then
8686
for i = 1, #value do
8787
value[i] = match_image:imageize(value[i])
8888
end
@@ -96,7 +96,7 @@ voperation.set = function(self, name, flags, match_image, value)
9696
value = value:copy():copy_memory()
9797
end
9898

99-
return vob:set_type(name, value, gtype_raw)
99+
return vob:set_type(name, value, gtype)
100100
end
101101

102102
-- this is slow ... call as little as possible

0 commit comments

Comments
 (0)