package provide Freetype::ftlist 0.1

if {![info exists _source([file tail [info script]])]} {
package require Freetype::ft2build
package require $FT_FREETYPE

::ffidl::callout _FT_List_Find {pointer pointer} pointer \
    [::ffidl::symbol $::ft::lib FT_List_Find]
proc FT_List_Find {list data} {
    if {[info object isa object $data]} {
        set p [$data getPointer]
    } else {
        set p $data
    }
    set result [_FT_List_Find [$list getPointer] $p]
    if {$result == 0} {return {}}

    foreach obj [info class instances FT_ListNode] {
        if {[$obj getPointer] == $result} {
            return $obj
        }
    }
    return [FT_ListNode new $result]
}

::ffidl::callout _FT_List_Add {pointer pointer} void \
    [::ffidl::symbol $::ft::lib FT_List_Add]
proc FT_List_Add {list node} {
    _FT_List_Add [$list getPointer] [$node getPointer]
}

::ffidl::callout _FT_List_Insert {pointer pointer} void \
    [::ffidl::symbol $::ft::lib FT_List_Insert]
proc FT_List_Insert {list node} {
    _FT_List_Insert [$list getPointer] [$node getPointer]
}

::ffidl::callout _FT_List_Remove {pointer pointer} void \
    [::ffidl::symbol $::ft::lib FT_List_Remove]
proc FT_List_Remove {list node} {
    _FT_List_Remove [$list getPointer] [$node getPointer]
}

::ffidl::callout _FT_List_Up {pointer pointer} void \
    [::ffidl::symbol $::ft::lib FT_List_Up]
proc FT_List_Up {list node} {
    _FT_List_Up [$list getPointer] [$node getPointer]
}

#TODO
#  typedef FT_Error
#  (*FT_List_Iterator)( FT_ListNode  node,
#                       void*        user );

::ffidl::callout _FT_List_Iterate {pointer pointer-proc pointer} int \
    [::ffidl::symbol $::ft::lib FT_List_Iterate]
proc FT_List_Iterate {list iterator user} {
    set result [_FT_List_Iterate [$list getPointer] $iterator $user]
    return $result
}

# TODO
#  typedef void
#  (*FT_List_Destructor)( FT_Memory  memory,
#                         void*      data,
#                         void*      user );

::ffidl::callout _FT_List_Finalize {pointer pointer pointer pointer} void \
    [::ffidl::symbol $::ft::lib FT_List_Finalize]
proc FT_List_Finalize {list destroy memory user} {
    _FT_List_Finalize [$list getPointer] [$destroy getPointer] \
        [$memory $getPointer] $user
}

set _source([file tail [info script]]) 1
}
