/[cffi-objects]/object.lisp
ViewVC logotype

Contents of /object.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 10 - (show annotations)
Fri Aug 24 20:44:53 2012 UTC (19 months, 3 weeks ago) by rklochkov
File size: 5368 byte(s)
Free pointed object on FREE method
1 ;;;; -*- Mode: lisp; indent-tabs-mode: nil -*-
2 ;;;
3 ;;; object.lisp --- CFFI type OBJECT
4 ;;;
5 ;;; Copyright (C) 2007, Roman Klochkov <monk@slavsoft.surgut.ru>
6 ;;;
7
8 (in-package :cffi-objects)
9
10 (defvar *objects* (make-hash-table)
11 "Hash table: foreign-pointer address as integer -> lisp object")
12
13 (defvar *objects-ids* (make-hash-table)
14 "Hash table: atom -> lisp object")
15
16 (defclass object ()
17 ((pointer :accessor pointer :initarg :pointer
18 :initform (null-pointer) :type foreign-pointer)
19 ;; by default object shouldn't be stored unless it is GtkObject
20 (volatile :type boolean :accessor volatile
21 :initarg :volatile :initform t
22 :documentation "Will not be saved in hash")
23 (free-after :type boolean :initarg :free-after :initform t
24 :documentation "Should be freed by finalizer")
25 (initialized :type list :initform nil
26 :documentation "For SETF-INIT. To avoid double-init")
27 (id :type symbol :accessor id :initarg :id :initform nil))
28 (:documentation "Lisp wrapper for any object. VOLATILE slot set when object
29 shouldn't be stored in *OBJECTS*. Stored pointer GC'ed, if VOLATILE."))
30
31 (defmethod (setf pointer) :after (value (object object))
32 (declare (type foreign-pointer value))
33 (tg:cancel-finalization object)
34 (when (and (slot-value object 'free-after) (not (null-pointer-p value)))
35 (let ((class (class-of object)))
36 (format t "Set finalizer: ~a ~a ~a~%" object class value)
37 (tg:finalize object (lambda ()
38 (format t "Finalize: ~a ~a~%" class value)
39 (free-ptr class value)))))
40 ; specialize EQL CLASS to override
41 (unless (or (volatile object) (null-pointer-p value))
42 (setf (gethash (pointer-address value) *objects*) object)
43 (when (id object)
44 (let ((cur-obj (gethash (id object) *objects-ids*)))
45 (unless (or (null cur-obj) (eq cur-obj object))
46 (warn "ID ~a for object ~a already set for ~a~%"
47 (id object) object (gethash (id object) *objects-ids*)))
48 (setf (gethash (id object) *objects-ids*) object)))))
49
50 (defgeneric gconstructor (object &rest initargs)
51 (:documentation "Called during initialization of OBJECT instance.
52 Should return a pointer to foreign OBJECT instance,
53 for example, by g_object_new."))
54
55 (defmethod gconstructor (something-bad &rest rest)
56 (warn "No constructor for ~a ~a~%" something-bad rest))
57
58 (defmethod shared-initialize :after ((object object) slot-names
59 &rest initargs
60 &key pointer &allow-other-keys)
61 (unless pointer
62 (setf (pointer object) (apply #'gconstructor object initargs))))
63
64 (defmethod pointer (something-bad)
65 (declare (ignore something-bad))
66 "Empty method to return null-pointer for non-objects"
67 (null-pointer))
68
69 (defgeneric free (object)
70 (:documentation "Removes object pointer from lisp hashes."))
71
72 (defmethod free ((object object))
73 (unless (null-pointer-p (pointer object))
74 (remhash (pointer-address (pointer object)) *objects*)
75 (remhash (id object) *objects-ids*)
76 (when (and (slot-value object 'free-after) (not (null-pointer-p value)))
77 (free-ptr (class-of object) value))
78 (setf (pointer object) (null-pointer)
79 (id object) nil)))
80
81 (defun find-object (pointer &optional class)
82 "Returns lisp object for an Object pointer.
83 If not found or found with wrong class, create new one with given CLASS"
84 (declare (type symbol class) (type foreign-pointer pointer))
85 (unless (null-pointer-p pointer)
86 (let ((try-find (gethash (pointer-address pointer) *objects*)))
87 (if class
88 (progn
89 (unless (or (null try-find)
90 (eq (class-of try-find) (find-class class)))
91 (progn
92 (free try-find)
93 (setf try-find nil)))
94 (or try-find (make-instance class
95 :pointer pointer
96 :free-after nil)))
97 (or try-find pointer)))))
98
99 (defun object-by-id (id-key)
100 (gethash id-key *objects-ids*))
101
102 ;; Type OBJECT
103 ;; converts class object to pointer and vice versa
104
105 (define-foreign-type cffi-object ()
106 ((class :initarg :class :accessor object-class))
107 (:actual-type :pointer))
108
109 (define-parse-method object (&optional class)
110 (make-instance 'cffi-object :class class))
111
112 (defmethod translate-to-foreign ((value null) (type cffi-object))
113 (null-pointer))
114
115 (defmethod translate-to-foreign ((value object) (type cffi-object))
116 (pointer value))
117
118 ;; Hack: redefine translator for :pointer to be able to use
119 ;; objects or nulls instead of pointer
120 (defmethod translate-to-foreign ((value object)
121 (type cffi::foreign-pointer-type))
122 (pointer value))
123
124 (defmethod translate-to-foreign ((value null)
125 (type cffi::foreign-pointer-type))
126 (null-pointer))
127
128 ;; nil = null string
129 (defmethod translate-to-foreign ((value null)
130 (type cffi::foreign-string-type))
131 (null-pointer))
132
133
134 (defmethod translate-to-foreign (value (type cffi-object))
135 (check-type value foreign-pointer)
136 value)
137
138 (defmethod translate-from-foreign (ptr (cffi-object cffi-object))
139 (find-object ptr (object-class cffi-object)))

  ViewVC Help
Powered by ViewVC 1.1.5