Skip to content

Commit

Permalink
allow transfer=none for GLists and GSLists
Browse files Browse the repository at this point in the history
  • Loading branch information
jwahlstrand committed Feb 17, 2024
1 parent 7332028 commit d94555e
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 25 deletions.
12 changes: 8 additions & 4 deletions GI/src/giimport.jl
Original file line number Diff line number Diff line change
Expand Up @@ -762,11 +762,15 @@ function extract_type(typeinfo::GITypeInfo,listtype::Type{T}) where {T<:GLib._LL
TypeDesc{Type{GList}}(GList, :(GLib.LList{$lt{$elmtype}}),:(GLib.LList{$lt{$elmtype}}), :(Ptr{$lt{$elmtype}}))
end
function convert_from_c(name::Symbol, arginfo::ArgInfo, typeinfo::TypeDesc{Type{GList}})
owns = (get_ownership_transfer(arginfo) == GITransfer.EVERYTHING)
if get_ownership_transfer(arginfo) == GITransfer.NOTHING
nothing # just return the pointer
ot = get_ownership_transfer(arginfo)
if ot == GITransfer.NOTHING
:( GLib.GList($name, false, false) )
elseif ot == GITransfer.CONTAINER
:( GLib.GList($name, false) )
elseif ot == GITransfer.EVERYTHING
:( GLib.GList($name, true) )
else
:( GLib.GList($name, $owns) )
error("Unknown transfer type for GList")
end
end

Expand Down
20 changes: 13 additions & 7 deletions src/GLib/glist.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Gtk linked list
# GLib linked list

## Type hierarchy information

Expand All @@ -21,19 +21,21 @@ eltype(::Type{L}) where {L <: _LList} = eltype(supertype(L))
mutable struct GList{L <: _LList, T} <: AbstractVector{T}
handle::Ptr{L}
transfer_full::Bool
function GList{L,T}(handle, transfer_full::Bool) where {L<:_LList,T}
# if transfer_full == true, then also free the elements when finalizing the list
transfer_container::Bool
function GList{L,T}(handle, transfer_elements::Bool, transfer_container::Bool=true) where {L<:_LList,T}
# if transfer_elements == true, then also free the elements when finalizing the list
# if transfer_container == false, then do not call `g_list_free` when emptying
# this function assumes the caller will take care of holding a pointer to the returned object
# until it wants to be garbage collected
@assert T == eltype(L)
l = new{L,T}(handle, transfer_full)
l = new{L,T}(handle, transfer_elements, transfer_container)
finalizer(empty!, l)
return l
end
end
GList(list::Type{T}) where {T} = GList(convert(Ptr{_GList{T}}, C_NULL), true)
GSList(list::Type{T}) where {T} = GList(convert(Ptr{_GSList{T}}, C_NULL), true)
GList(list::Ptr{L}, transfer_full::Bool = false) where {L <: _LList} = GList{L, eltype(L)}(list, transfer_full)
GList(list::Ptr{L}, transfer_full::Bool = false, transfer_container::Bool = true) where {L <: _LList} = GList{L, eltype(L)}(list, transfer_full, transfer_container)

const LList{L <: _LList} = Union{Ptr{L}, GList{L}}
eltype(::LList{L}) where {L <: _LList} = eltype(L)
Expand Down Expand Up @@ -132,7 +134,9 @@ function empty!(list::GList{L}) where L <: _GSList
s = next_(list, s)[2]
end
end
#ccall((:g_slist_free, libglib), Nothing, (Ptr{L},), list)
if list.transfer_container
ccall((:g_slist_free, libglib), Nothing, (Ptr{L},), list)
end
list.handle = C_NULL
end
return list
Expand All @@ -146,7 +150,9 @@ function empty!(list::GList{L}) where L <: _GList
s = next_(list, s)[2]
end
end
ccall((:g_list_free, libglib), Nothing, (Ptr{L},), list)
if list.transfer_container
ccall((:g_list_free, libglib), Nothing, (Ptr{L},), list)
end
list.handle = C_NULL
end
return list
Expand Down
3 changes: 2 additions & 1 deletion src/gen/gio_methods
Original file line number Diff line number Diff line change
Expand Up @@ -2020,7 +2020,8 @@ $(Expr(:toplevel, quote
end
function get_emblems(instance::GEmblemedIcon)
ret = ccall(("g_emblemed_icon_get_emblems", libgio), Ptr{GLib._GList{Ptr{GObject}}}, (Ptr{GObject},), instance)
ret
ret2 = GLib.GList(ret, false, false)
ret2
end
function get_icon(instance::GEmblemedIcon)
ret = ccall(("g_emblemed_icon_get_icon", libgio), Ptr{GObject}, (Ptr{GObject},), instance)
Expand Down
15 changes: 12 additions & 3 deletions src/gen/gtk4_methods
Original file line number Diff line number Diff line change
Expand Up @@ -1311,7 +1311,8 @@ $(Expr(:toplevel, quote
end
function get_windows(instance::GtkApplication)
ret = ccall(("gtk_application_get_windows", libgtk4), Ptr{GLib._GList{Ptr{GObject}}}, (Ptr{GObject},), instance)
ret
ret2 = GLib.GList(ret, false, false)
ret2
end
function inhibit(instance::GtkApplication, _window::Maybe(GtkWindow), _flags, _reason::Maybe(Union{AbstractString, Symbol}))
_window_maybe = nothing_to_null(_window)
Expand Down Expand Up @@ -1400,6 +1401,9 @@ $(Expr(:toplevel, quote
function remove_action(instance::GtkApplication, _action_name::Union{AbstractString, Symbol})
remove_action(GActionMap(instance), _action_name)
end
function remove_action_entries(instance::GtkApplication, _entries)
remove_action_entries(GActionMap(instance), _entries)
end
function ApplicationWindow_new(_application::GtkApplication)
ret = ccall(("gtk_application_window_new", libgtk4), Ptr{GObject}, (Ptr{GObject},), _application)
ret2 = GtkApplicationWindowLeaf(ret, false)
Expand Down Expand Up @@ -1482,6 +1486,9 @@ $(Expr(:toplevel, quote
function remove_action(instance::GtkApplicationWindow, _action_name::Union{AbstractString, Symbol})
remove_action(GActionMap(instance), _action_name)
end
function remove_action_entries(instance::GtkApplicationWindow, _entries)
remove_action_entries(GActionMap(instance), _entries)
end
function get_accessible_parent(instance::GtkApplicationWindow)
get_accessible_parent(GtkAccessible(instance))
end
Expand Down Expand Up @@ -2569,7 +2576,8 @@ $(Expr(:toplevel, quote
end
function get_focus_siblings(instance::GtkCellArea, _renderer::GtkCellRenderer)
ret = ccall(("gtk_cell_area_get_focus_siblings", libgtk4), Ptr{GLib._GList{Ptr{GObject}}}, (Ptr{GObject}, Ptr{GObject}), instance, _renderer)
ret
ret2 = GLib.GList(ret, false, false)
ret2
end
function get_preferred_height(instance::GtkCellArea, _context::GtkCellAreaContext, _widget::GtkWidget)
m_minimum_height = Ref{Int32}()
Expand Down Expand Up @@ -15856,7 +15864,8 @@ $(Expr(:toplevel, quote
end
function get_widgets(instance::GtkSizeGroup)
ret = ccall(("gtk_size_group_get_widgets", libgtk4), Ptr{GLib._GSList{Ptr{GObject}}}, (Ptr{GObject},), instance)
ret
ret2 = GLib.GList(ret, false, false)
ret2
end
function remove_widget(instance::GtkSizeGroup, _widget::GtkWidget)
ret = ccall(("gtk_size_group_remove_widget", libgtk4), Nothing, (Ptr{GObject}, Ptr{GObject}), instance, _widget)
Expand Down
6 changes: 3 additions & 3 deletions src/gen/pango_consts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ $(Expr(:toplevel, quote
const GLYPH_UNKNOWN_FLAG = 0x10000000
const SCALE = 1024
const VERSION_MAJOR = 1
const VERSION_MICRO = 0
const VERSION_MINOR = 51
const VERSION_STRING = "1.51.0"
const VERSION_MICRO = 14
const VERSION_MINOR = 50
const VERSION_STRING = "1.50.14"
begin
@cenum Alignment::Int32 Alignment_LEFT = 0 Alignment_CENTER = 1 Alignment_RIGHT = 2
(GLib.g_type(::Type{T}) where T <: Alignment) = begin
Expand Down
6 changes: 4 additions & 2 deletions src/gen/pango_methods
Original file line number Diff line number Diff line change
Expand Up @@ -1445,11 +1445,13 @@ $(Expr(:toplevel, quote
end
function get_lines(instance::PangoLayout)
ret = ccall(("pango_layout_get_lines", libpango), Ptr{GLib._GSList{_PangoLayoutLine}}, (Ptr{GObject},), instance)
ret
ret2 = GLib.GList(ret, false, false)
ret2
end
function get_lines_readonly(instance::PangoLayout)
ret = ccall(("pango_layout_get_lines_readonly", libpango), Ptr{GLib._GSList{_PangoLayoutLine}}, (Ptr{GObject},), instance)
ret
ret2 = GLib.GList(ret, false, false)
ret2
end
function get_log_attrs(instance::PangoLayout)
m_attrs = Ref{Ptr{_PangoLogAttr}}()
Expand Down
10 changes: 6 additions & 4 deletions test/gui/misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,13 @@ push!(b4; filename="test.ui")
win = b4["a_window"]
destroy(win)

b5 = GtkBuilder()
Sys.WORD_SIZE == 64 && push!(b5; buffer=s)
if Sys.WORD_SIZE == 64
b5 = GtkBuilder()
push!(b5; buffer=s)

win = b5["a_window"]
destroy(win)
win = b5["a_window"]
destroy(win)
end

end

Expand Down
2 changes: 1 addition & 1 deletion test/list.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Gtk4.GLib
using Gtk4.GLib, Test

@testset "glist" begin

Expand Down

0 comments on commit d94555e

Please sign in to comment.