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

Contents of /object.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 7 - (show annotations)
Thu Aug 9 16:55:23 2012 UTC (20 months, 1 week ago) by rklochkov
File size: 5241 byte(s)
Added function INITIALIZE
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 (setf (pointer object) (null-pointer)
77 (id object) nil)))
78
79 (defun find-object (pointer &optional class)
80 "Returns lisp object for an Object pointer.
81 If not found or found with wrong class, create new one with given CLASS"
82 (declare (type symbol class) (type foreign-pointer pointer))
83 (unless (null-pointer-p pointer)
84 (let ((try-find (gethash (pointer-address pointer) *objects*)))
85 (if class
86 (progn
87 (unless (or (null try-find)
88 (eq (class-of try-find) (find-class class)))
89 (progn
90 (free try-find)
91 (setf try-find nil)))
92 (or try-find (make-instance class
93 :pointer pointer
94 :free-after nil)))
95 (or try-find pointer)))))
96
97 (defun object-by-id (id-key)
98 (gethash id-key *objects-ids*))
99
100 ;; Type OBJECT
101 ;; converts class object to pointer and vice versa
102
103 (define-foreign-type cffi-object ()
104 ((class :initarg :class :accessor object-class))
105 (:actual-type :pointer))
106
107 (define-parse-method object (&optional class)
108 (make-instance 'cffi-object :class class))
109
110 (defmethod translate-to-foreign ((value null) (type cffi-object))
111 (null-pointer))
112
113 (defmethod translate-to-foreign ((value object) (type cffi-object))
114 (pointer value))
115
116 ;; Hack: redefine translator for :pointer to be able to use
117 ;; objects or nulls instead of pointer
118 (defmethod translate-to-foreign ((value object)
119 (type cffi::foreign-pointer-type))
120 (pointer value))
121
122 (defmethod translate-to-foreign ((value null)
123 (type cffi::foreign-pointer-type))
124 (null-pointer))
125
126 ;; nil = null string
127 (defmethod translate-to-foreign ((value null)
128 (type cffi::foreign-string-type))
129 (null-pointer))
130
131
132 (defmethod translate-to-foreign (value (type cffi-object))
133 (check-type value foreign-pointer)
134 value)
135
136 (defmethod translate-from-foreign (ptr (cffi-object cffi-object))
137 (find-object ptr (object-class cffi-object)))

  ViewVC Help
Powered by ViewVC 1.1.5