@@ -6,7 +6,7 @@ immutable GValue
6
6
field3:: Uint64
7
7
GValue () = new (0 ,0 ,0 )
8
8
end
9
- typealias GV Union (Mutable{GValue}, Vector{GValue}, Ptr{GValue})
9
+ typealias GV Union (Mutable{GValue}, Ptr{GValue})
10
10
Base. zero (:: Type{GValue} ) = GValue ()
11
11
function gvalue {T} (:: Type{T} )
12
12
v = mutable (GValue ())
21
21
function gvalues (xs... )
22
22
v = zeros (GValue, length (xs))
23
23
for (i,x) in enumerate (xs)
24
+ T = typeof (x)
24
25
gv = mutable (v,i)
25
- gv[] = typeof (x) # init type
26
- gv[] = x # init value
26
+ gv[] = T # init type
27
+ gv[T ] = x # init value
27
28
end
28
29
finalizer (v, (v)-> for i = 1 : length (v)
29
30
ccall ((:g_value_unset ,libgobject),Void,(Ptr{GValue},),pointer (v,i))
30
31
end )
31
32
v
32
33
end
33
34
35
+ function setindex! (dest:: GV , src:: GV )
36
+ bool (ccall ((:g_value_transform ,libgobject),Cint,(Ptr{GValue},Ptr{GValue}),src,dest))
37
+ src
38
+ end
39
+
40
+ setindex! (:: Type{Void} ,v:: GV ) = v
41
+ setindex! (v:: GLib.GV , x) = setindex! (v, x, typeof (x))
42
+ setindex! (gv:: GV , x, i:: Int ) = setindex! (mutable (gv,i), x)
43
+
44
+ getindex {T} (gv:: GV , i:: Int , :: Type{T} ) = getindex (mutable (gv,i), T)
45
+ getindex (gv:: GV , i:: Int ) = getindex (mutable (gv,i))
46
+ getindex (v:: GV ,i:: Int , :: Type{Void} ) = nothing
47
+
34
48
# let
35
49
# global make_gvalue, getindex
36
50
function make_gvalue (pass_x,as_ctype,to_gtype,with_id,allow_reverse:: Bool = true ,fundamental:: Bool = false )
@@ -44,32 +58,39 @@ function make_gvalue(pass_x,as_ctype,to_gtype,with_id,allow_reverse::Bool=true,f
44
58
ccall ((:g_value_init ,GLib. libgobject),Void,(Ptr{GLib. GValue},Csize_t), v, $ with_id)
45
59
v
46
60
end
47
- function Base. setindex! {T<:$pass_x} (v:: GLib.GV , x:: T )
48
- $ (if to_gtype == :string ; :(x = GLib. bytestring (x)) end )
49
- $ (if to_gtype == :pointer || to_gtype == :boxed ; :(x = GLib. mutable (x)) end )
61
+ function Base. setindex! {T<:$pass_x} (v:: GLib.GV , x, :: Type{T} )
62
+ $ ( if to_gtype == :string
63
+ :(x = GLib. bytestring (x))
64
+ elseif to_gtype == :pointer || to_gtype == :boxed
65
+ :(x = GLib. mutable (x))
66
+ elseif to_gtype == :gtype
67
+ :(x = GLib. g_type (x))
68
+ end )
50
69
ccall (($ (string (" g_value_set_" ,to_gtype)),GLib. libgobject),Void,(Ptr{GLib. GValue},$ as_ctype), v, x)
51
70
if isa (v, GLib. MutableTypes. MutableX)
52
71
finalizer (v, (v:: GLib.MutableTypes.MutableX )-> ccall ((:g_value_unset ,GLib. libgobject),Void,(Ptr{GLib. GValue},), v))
53
72
end
54
73
v
55
74
end
56
75
end )
57
- if to_gtype == :static_string
58
- to_gtype = :string
59
- end
76
+ end
77
+ if to_gtype == :static_string
78
+ to_gtype = :string
79
+ end
80
+ if pass_x != = None
60
81
eval (current_module (),quote
61
82
function Base. getindex {T<:$pass_x} (v:: GLib.GV ,:: Type{T} )
62
83
x = ccall (($ (string (" g_value_get_" ,to_gtype)),GLib. libgobject),$ as_ctype,(Ptr{GLib. GValue},), v)
63
- $ (if to_gtype == :string ; :(x = GLib. bytestring (x)) end )
64
- $ (if pass_x == Symbol; :(x = symbol (x)) end )
84
+ $ ( if to_gtype == :string
85
+ :(x = GLib. bytestring (x))
86
+ elseif pass_x == Symbol
87
+ :(x = symbol (x))
88
+ end )
65
89
return Base. convert (T,x)
66
90
end
67
91
end )
68
92
end
69
93
if fundamental || allow_reverse
70
- if to_gtype == :static_string
71
- to_gtype = :string
72
- end
73
94
fn = eval (current_module (),quote
74
95
function (v:: GLib.GV )
75
96
x = ccall (($ (string (" g_value_get_" ,to_gtype)),GLib. libgobject),$ as_ctype,(Ptr{GLib. GValue},), v)
@@ -86,53 +107,42 @@ function make_gvalue(pass_x,as_ctype,to_gtype,with_id,allow_reverse::Bool=true,f
86
107
end
87
108
end
88
109
const gvalue_types = {}
89
- const fundamental_ids = tuple (Int[g_type_from_name (name) for (name,c,j,f) in fundamental_types]. .. )
90
110
const fundamental_fns = tuple (Function[make_gvalue (juliatype, ctype, g_value_fn, fundamental_ids[i], false , true ) for
91
111
(i,(name, ctype, juliatype, g_value_fn)) in enumerate (fundamental_types)]. .. )
92
- make_gvalue (Symbol, Ptr{Uint8}, :static_string , :gstring_id , false )
112
+ make_gvalue (Symbol, Ptr{Uint8}, :static_string , :(g_type (String)), false )
113
+ make_gvalue (Type, GType, :gtype , (:g_gtype ,:libgobject ))
93
114
94
115
function getindex (gv:: Union(Mutable{GValue}, Ptr{GValue}) )
95
- g_type = unsafe_load (gv). g_type
96
- if g_type == 0
116
+ gtyp = unsafe_load (gv). g_type
117
+ if gtyp == 0
97
118
error (" Invalid GValue type" )
98
119
end
99
- if g_type == gvoid_id
120
+ if gtyp == g_type (Void)
100
121
return nothing
101
122
end
102
123
# first pass: fast loop for fundamental types
103
124
for (i,id) in enumerate (fundamental_ids)
104
- if id == g_type # if g_type == id
125
+ if id == gtyp # if g_type == id
105
126
return fundamental_fns[i](gv)
106
127
end
107
128
end
108
129
# second pass: user defined (sub)types
109
130
for (typ, typefn, getfn) in gvalue_types
110
- if bool (ccall ((:g_type_is_a ,libgobject),Cint,(Int,Int),g_type ,typefn ())) # if g_type <: expr()
131
+ if bool (ccall ((:g_type_is_a ,libgobject),Cint,(Int,Int),gtyp ,typefn ())) # if gtyp <: expr()
111
132
return getfn (gv)
112
133
end
113
134
end
114
135
# last pass: check for derived fundamental types (which have not been overridden by the user)
115
136
for (i,id) in enumerate (fundamental_ids)
116
- if bool (ccall ((:g_type_is_a ,libgobject),Cint,(Int,Int),g_type ,id)) # if g_type <: id
137
+ if bool (ccall ((:g_type_is_a ,libgobject),Cint,(Int,Int),gtyp ,id)) # if gtyp <: id
117
138
return fundamental_fns[i](gv)
118
139
end
119
140
end
120
- typename = g_type_name (g_type )
141
+ typename = g_type_name (gtyp )
121
142
error (" Could not convert GValue of type $typename to Julia type" )
122
143
end
123
144
# end
124
145
125
- function setindex! (dest:: GV , src:: GV )
126
- bool (ccall ((:g_value_transform ,libgobject),Cint,(Ptr{GValue},Ptr{GValue}),src,dest))
127
- src
128
- end
129
-
130
- setindex! (:: Type{Void} ,v:: GV ) = v
131
- setindex! (gv:: GV , i:: Int , x) = setindex! (mutable (gv,i), x)
132
- getindex {T} (gv:: GV , i:: Int , :: Type{T} ) = getindex (mutable (gv,i), T)
133
- getindex (gv:: Union(Mutable{GValue}, Ptr{GValue}) , i:: Int ) = getindex (mutable (gv,i))
134
- getindex (v:: GV ,i:: Int ,:: Type{Void} ) = nothing
135
-
136
146
function getindex {T} (w:: GObject , name:: Union(String,Symbol) , :: Type{T} )
137
147
v = gvalue (T)
138
148
ccall ((:g_object_get_property ,libgobject), Void,
@@ -167,13 +177,12 @@ function show(io::IO, w::GObject)
167
177
const READABLE= 1
168
178
if (param. flags& 1 )== READABLE &&
169
179
bool (ccall ((:g_value_type_transformable ,libgobject),Cint,
170
- (Int,Int),param. value_type,gstring_id ))
180
+ (Int,Int),param. value_type,g_type (String) ))
171
181
ccall ((:g_object_get_property ,libgobject), Void,
172
182
(Ptr{GObject}, Ptr{Uint8}, Ptr{GValue}), w, param. name, v)
173
183
str = ccall ((:g_value_get_string ,libgobject),Ptr{Uint8},(Ptr{GValue},), v)
174
184
value = (str == C_NULL ? " NULL" : bytestring (str))
175
- ccall ((:g_value_reset ,libgobject),Ptr{Void},(Ptr{GValue},), v)
176
- if param. value_type == gstring_id && str != C_NULL
185
+ if param. value_type == g_type (String) && str != C_NULL
177
186
print (io," =\" " ,value,' "' )
178
187
else
179
188
print (io,' =' ,value)
194
203
# instance_size::Cuint
195
204
# GTypeQuery() = new(0,0,0,0)
196
205
# end
197
- # function gsizeof(g_type )
206
+ # function gsizeof(gtyp )
198
207
# q = mutable(GTypeQuery)
199
- # ccall((:g_type_query,libgobject),Void,(Int,Ptr{GTypeQuery},),g_type ,q)
208
+ # ccall((:g_type_query,libgobject),Void,(Int,Ptr{GTypeQuery},),gtyp ,q)
200
209
# q[].instance_size
201
210
# end
0 commit comments