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

Contents of /object.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (show annotations)
Thu Feb 9 15:53:55 2012 UTC (2 years, 2 months ago) by rklochkov
File size: 4735 byte(s)
Initial release
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 (id :type symbol :accessor id :initarg :id :initform nil))
26 (:documentation "Lisp wrapper for any object. VOLATILE slot set when object
27 shouldn't be stored in *OBJECTS*. Stored pointer GC'ed, if VOLATILE."))
28
29 (defmethod (setf pointer) :after (value (object object))
30 (declare (type foreign-pointer value))
31 (tg:cancel-finalization object)
32 (when (and (slot-value object 'free-after) (not (null-pointer-p value)))
33 (let ((class (class-of object)))
34 (tg:finalize object (lambda ()
35 (free-ptr class value)))))
36 ; specialize EQL CLASS to override
37 (unless (or (volatile object) (null-pointer-p value))
38 (setf (gethash (pointer-address value) *objects*) object)
39 (when (id object)
40 (let ((cur-obj (gethash (id object) *objects-ids*)))
41 (unless (or (null cur-obj) (eq cur-obj object))
42 (warn "ID ~a for object ~a already set for ~a~%"
43 (id object) object (gethash (id object) *objects-ids*)))
44 (setf (gethash (id object) *objects-ids*) object)))))
45
46 (defgeneric gconstructor (object &rest initargs)
47 (:documentation "Called during initialization of OBJECT instance.
48 Should return a pointer to foreign OBJECT instance,
49 for example, by g_object_new."))
50
51 (defmethod gconstructor (something-bad &rest rest)
52 (warn "No constructor for ~a ~a~%" something-bad rest))
53
54 (defmethod shared-initialize :after ((object object) slot-names
55 &rest initargs
56 &key pointer &allow-other-keys)
57 (unless pointer
58 (setf (pointer object) (apply #'gconstructor object initargs))))
59
60 (defmethod pointer (something-bad)
61 (declare (ignore something-bad))
62 "Empty method to return null-pointer for non-objects"
63 (null-pointer))
64
65 (defgeneric free (object)
66 (:documentation "Removes object pointer from lisp hashes."))
67
68 (defmethod free ((object object))
69 (unless (null-pointer-p (pointer object))
70 (remhash (pointer-address (pointer object)) *objects*)
71 (remhash (id object) *objects-ids*)
72 (setf (pointer object) (null-pointer)
73 (id object) nil)))
74
75 (defun find-object (pointer &optional class)
76 "Returns lisp object for an Object pointer.
77 If not found or found with wrong class, create new one with given CLASS"
78 (declare (type symbol class) (type foreign-pointer pointer))
79 (unless (null-pointer-p pointer)
80 (let ((try-find (gethash (pointer-address pointer) *objects*)))
81 (if class
82 (progn
83 (unless (or (null try-find)
84 (eq (class-of try-find) (find-class class)))
85 (progn
86 (free try-find)
87 (setf try-find nil)))
88 (or try-find (make-instance class :pointer pointer)))
89 try-find))))
90
91 (defun object-by-id (id-key)
92 (gethash id-key *objects-ids*))
93
94 ;; Type OBJECT
95 ;; converts class object to pointer and vice versa
96
97 (define-foreign-type cffi-object ()
98 ((class :initarg :class :accessor object-class))
99 (:actual-type :pointer))
100
101 (define-parse-method object (&optional class)
102 (make-instance 'cffi-object :class class))
103
104 (defmethod translate-to-foreign ((value null) (type cffi-object))
105 (null-pointer))
106
107 (defmethod translate-to-foreign ((value object) (type cffi-object))
108 (pointer value))
109
110 ;; Hack: redefine translator for :pointer to be able to use
111 ;; objects or nulls instead of pointer
112 (defmethod translate-to-foreign ((value object)
113 (type cffi::foreign-pointer-type))
114 (pointer value))
115
116 (defmethod translate-to-foreign ((value null)
117 (type cffi::foreign-pointer-type))
118 (null-pointer))
119
120 (defmethod translate-to-foreign (value (type cffi-object))
121 (check-type value foreign-pointer)
122 value)
123
124 (defmethod translate-from-foreign (ptr (cffi-object cffi-object))
125 (find-object ptr (object-class cffi-object)))
126
127
128

  ViewVC Help
Powered by ViewVC 1.1.5