Removed LoadedFunction struct

main
noah metz 2024-04-23 15:05:01 -06:00
parent 10449a124f
commit 281f038f5f
1 changed files with 19 additions and 43 deletions

@ -1,7 +1,6 @@
package ncurses package ncurses
import ( import (
"fmt"
"sync/atomic" "sync/atomic"
"github.com/ebitengine/purego" "github.com/ebitengine/purego"
@ -9,41 +8,18 @@ import (
var ncurses atomic.Value var ncurses atomic.Value
func Init() error { func ncursesFunction[T any](name string) T {
if ncurses.Load() == nil { if ncurses.Load() == nil {
lib, err := purego.Dlopen(LIBRARY_PATH, purego.RTLD_GLOBAL | purego.RTLD_LAZY) lib, err := purego.Dlopen(LIBRARY_PATH, purego.RTLD_GLOBAL | purego.RTLD_LAZY)
if err != nil { if err != nil {
return err panic(err)
} }
ncurses.CompareAndSwap(nil, lib) ncurses.CompareAndSwap(nil, lib)
} }
return nil
}
type loaded_function[T any] struct {
name string
value atomic.Value
library *atomic.Value
}
func NewLoadedFunction[T any](library *atomic.Value, name string) loaded_function[T] { var tmp = new(T)
return loaded_function[T]{ purego.RegisterLibFunc(tmp, ncurses.Load().(uintptr), name)
name: name, return *tmp
value: atomic.Value{},
library: library,
}
}
func(f *loaded_function[T]) Load() T {
if f.library.Load() == nil {
panic(fmt.Sprintf("Attempted to call library function %s before loading library", f.name))
} else if f.value.Load() == nil {
var tmp T
purego.RegisterLibFunc(&tmp, f.library.Load().(uintptr), f.name)
f.value.CompareAndSwap(nil, tmp)
}
return f.value.Load().(T)
} }
type Window uintptr type Window uintptr
@ -52,28 +28,28 @@ type voidFP func() int
type winFP func(window Window) int type winFP func(window Window) int
type initscrFP func() Window type initscrFP func() Window
var InitScr = NewLoadedFunction[initscrFP](&ncurses, "initscr") var InitScr = ncursesFunction[initscrFP]("initscr")
var EndWin = NewLoadedFunction[voidFP](&ncurses, "endwin") var EndWin = ncursesFunction[voidFP]("endwin")
type newwinFP func(nlines, ncols, begin_x, begin_y int) Window type newwinFP func(nlines, ncols, begin_x, begin_y int) Window
var NewWin = NewLoadedFunction[newwinFP](&ncurses, "newwin") var NewWin = ncursesFunction[newwinFP]("newwin")
var DelWin = NewLoadedFunction[winFP](&ncurses, "delwin") var DelWin = ncursesFunction[winFP]("delwin")
type dupwinFP func(window Window) Window type dupwinFP func(window Window) Window
var DupWin = NewLoadedFunction[dupwinFP](&ncurses, "dupwin") var DupWin = ncursesFunction[dupwinFP]("dupwin")
var Refresh = NewLoadedFunction[voidFP](&ncurses, "refresh") var Refresh = ncursesFunction[voidFP]("refresh")
var WRefresh = NewLoadedFunction[winFP](&ncurses, "wrefresh") var WRefresh = ncursesFunction[winFP]("wrefresh")
var WNOutRefresh = NewLoadedFunction[winFP](&ncurses, "wnoutrefresh") var WNOutRefresh = ncursesFunction[winFP]("wnoutrefresh")
var DoUpdate = NewLoadedFunction[voidFP](&ncurses, "doupdate") var DoUpdate = ncursesFunction[voidFP]("doupdate")
type waddstrFP func(window Window, str string) int type waddstrFP func(window Window, str string) int
var WAddStr = NewLoadedFunction[waddstrFP](&ncurses, "waddstr") var WAddStr = ncursesFunction[waddstrFP]("waddstr")
type waddnstrFP func(window Window, str string, n int) int type waddnstrFP func(window Window, str string, n int) int
var WAddNStr = NewLoadedFunction[waddstrFP](&ncurses, "waddnstr") var WAddNStr = ncursesFunction[waddstrFP]("waddnstr")
var GetMaxY = NewLoadedFunction[winFP](&ncurses, "getmaxy") var GetMaxY = ncursesFunction[winFP]("getmaxy")
var GetMaxX = NewLoadedFunction[winFP](&ncurses, "getmaxx") var GetMaxX = ncursesFunction[winFP]("getmaxx")
type mvwaddstrFP func(window Window, x, y int, str string) int type mvwaddstrFP func(window Window, x, y int, str string) int
var MvWAddStr = NewLoadedFunction[mvwaddstrFP](&ncurses, "mvwaddstr") var MvWAddStr = ncursesFunction[mvwaddstrFP]("mvwaddstr")