This repository has been archived by the owner on Jan 5, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtoolkit.lisp
51 lines (42 loc) · 1.89 KB
/
toolkit.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
(in-package #:org.shirakumo.fraf.soloud)
(defvar *c-object-table* (tg:make-weak-hash-table :test 'eql :weakness :value))
(defclass c-backed-object ()
((handle :initarg :handle :initform NIL :accessor handle)))
(defmethod initialize-instance :after ((c-backed-object c-backed-object) &key handle)
(unless handle
(let ((handle (create-handle c-backed-object)))
(when (cffi:null-pointer-p handle)
(error "Failed to create ~a handle." c-backed-object))
(setf (handle c-backed-object) handle)
(tg:finalize c-backed-object (destroy-handle c-backed-object handle))
(setf (gethash (cffi:pointer-address handle) *c-object-table*) c-backed-object))))
(defmethod pointer->object ((pointer integer))
(gethash integer *c-object-table*))
(defmethod pointer->object (pointer)
(gethash (cffi:pointer-address pointer) *c-object-table*))
(defmethod pointer->object ((object c-backed-object))
object)
(defmethod free ((object c-backed-object))
(let ((handle (handle object)))
(when handle
(setf (handle object) NIL)
(remhash (cffi:pointer-address handle) *c-object-table*)
(tg:cancel-finalization object)
(funcall (destroy-handle object handle)))))
(defmacro with-callback-handling ((instance &optional default (error default)) &body body)
`(handler-case
(let ((,instance (pointer->object ,instance)))
(if ,instance
,@body
,default))
(error (err)
(format T "~&! Error in callback: ~a~%" err)
,error)))
(defun find-cffi-symbol (temp fill)
(let ((symb (with-output-to-string (o)
(loop for c across (string temp)
do (if (eql c #\_)
(write-sequence (string fill) o)
(write-char c o))))))
(or (find-symbol symb '#:org.shirakumo.fraf.soloud.cffi)
(error "No such symbol ~a found in CL-SOLOUD-CFFI" symb))))