@@ -6,7 +6,7 @@ immutable GValue
66 field3:: Uint64
77 GValue () = new (0 ,0 ,0 )
88end
9- typealias GV Union (Mutable{GValue}, Vector{GValue}, Ptr{GValue})
9+ typealias GV Union (Mutable{GValue}, Ptr{GValue})
1010Base. zero (:: Type{GValue} ) = GValue ()
1111function gvalue {T} (:: Type{T} )
1212 v = mutable (GValue ())
2121function gvalues (xs... )
2222 v = zeros (GValue, length (xs))
2323 for (i,x) in enumerate (xs)
24+ T = typeof (x)
2425 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
2728 end
2829 finalizer (v, (v)-> for i = 1 : length (v)
2930 ccall ((:g_value_unset ,libgobject),Void,(Ptr{GValue},),pointer (v,i))
3031 end )
3132 v
3233end
3334
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+
3448# let
3549# global make_gvalue, getindex
3650function 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
4458 ccall ((:g_value_init ,GLib. libgobject),Void,(Ptr{GLib. GValue},Csize_t), v, $ with_id)
4559 v
4660 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 )
5069 ccall (($ (string (" g_value_set_" ,to_gtype)),GLib. libgobject),Void,(Ptr{GLib. GValue},$ as_ctype), v, x)
5170 if isa (v, GLib. MutableTypes. MutableX)
5271 finalizer (v, (v:: GLib.MutableTypes.MutableX )-> ccall ((:g_value_unset ,GLib. libgobject),Void,(Ptr{GLib. GValue},), v))
5372 end
5473 v
5574 end
5675 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
6081 eval (current_module (),quote
6182 function Base. getindex {T<:$pass_x} (v:: GLib.GV ,:: Type{T} )
6283 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 )
6589 return Base. convert (T,x)
6690 end
6791 end )
6892 end
6993 if fundamental || allow_reverse
70- if to_gtype == :static_string
71- to_gtype = :string
72- end
7394 fn = eval (current_module (),quote
7495 function (v:: GLib.GV )
7596 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
86107 end
87108end
88109const gvalue_types = {}
89- const fundamental_ids = tuple (Int[g_type_from_name (name) for (name,c,j,f) in fundamental_types]. .. )
90110const fundamental_fns = tuple (Function[make_gvalue (juliatype, ctype, g_value_fn, fundamental_ids[i], false , true ) for
91111 (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 ))
93114
94115function 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
97118 error (" Invalid GValue type" )
98119 end
99- if g_type == gvoid_id
120+ if gtyp == g_type (Void)
100121 return nothing
101122 end
102123 # first pass: fast loop for fundamental types
103124 for (i,id) in enumerate (fundamental_ids)
104- if id == g_type # if g_type == id
125+ if id == gtyp # if g_type == id
105126 return fundamental_fns[i](gv)
106127 end
107128 end
108129 # second pass: user defined (sub)types
109130 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()
111132 return getfn (gv)
112133 end
113134 end
114135 # last pass: check for derived fundamental types (which have not been overridden by the user)
115136 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
117138 return fundamental_fns[i](gv)
118139 end
119140 end
120- typename = g_type_name (g_type )
141+ typename = g_type_name (gtyp )
121142 error (" Could not convert GValue of type $typename to Julia type" )
122143end
123144# end
124145
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-
136146function getindex {T} (w:: GObject , name:: Union(String,Symbol) , :: Type{T} )
137147 v = gvalue (T)
138148 ccall ((:g_object_get_property ,libgobject), Void,
@@ -167,13 +177,12 @@ function show(io::IO, w::GObject)
167177 const READABLE= 1
168178 if (param. flags& 1 )== READABLE &&
169179 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) ))
171181 ccall ((:g_object_get_property ,libgobject), Void,
172182 (Ptr{GObject}, Ptr{Uint8}, Ptr{GValue}), w, param. name, v)
173183 str = ccall ((:g_value_get_string ,libgobject),Ptr{Uint8},(Ptr{GValue},), v)
174184 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
177186 print (io," =\" " ,value,' "' )
178187 else
179188 print (io,' =' ,value)
194203# instance_size::Cuint
195204# GTypeQuery() = new(0,0,0,0)
196205# end
197- # function gsizeof(g_type )
206+ # function gsizeof(gtyp )
198207# 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)
200209# q[].instance_size
201210# end
0 commit comments