No need to construct a SmokeBinding per Smoke module.
Annotate for file src/smoke.lisp
2010-01-10 tobias 1 ;;; Copyright (C) 2009, 2010 Tobias Rautenkranz <tobias@rautenkranz.ch>
2009-04-14 tobias 2 ;;;
14:23:24 ' 3 ;;; This program is free software: you can redistribute it and/or modify
' 4 ;;; it under the terms of the GNU General Public License as published by
' 5 ;;; the Free Software Foundation, either version 3 of the License, or
' 6 ;;; (at your option) any later version.
' 7 ;;;
' 8 ;;; This program is distributed in the hope that it will be useful,
' 9 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
' 10 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' 11 ;;; GNU General Public License for more details.
' 12 ;;;
' 13 ;;; You should have received a copy of the GNU General Public License
' 14 ;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
' 15 ;;;
' 16 ;;; As a special exception, the copyright holders of this library give you
' 17 ;;; permission to link this library with independent modules to produce an
' 18 ;;; executable, regardless of the license terms of these independent
' 19 ;;; modules, and to copy and distribute the resulting executable under
' 20 ;;; terms of your choice, provided that you also meet, for each linked
' 21 ;;; independent module, the terms and conditions of the license of that
' 22 ;;; module. An independent module is a module which is not derived from or
' 23 ;;; based on this library. If you modify this library, you may extend this
' 24 ;;; exception to your version of the library, but you are not obligated to
' 25 ;;; do so. If you do not wish to do so, delete this exception statement
' 26 ;;; from your version.
2009-04-05 tobias 27
2009-04-14 tobias 28 (in-package #:smoke)
2009-04-05 tobias 29
2009-06-30 tobias 30 (declaim (inline call-s-method))
2009-06-22 tobias 31 (defun call-s-method (method object-pointer stack-pointer)
12:18:08 ' 32 (foreign-funcall-pointer
2009-07-08 tobias 33 (foreign-slot-value (smoke-class-pointer (get-class method))
2009-08-02 tobias 34 'smoke-class 'class-function)
2009-06-22 tobias 35 ()
12:18:08 ' 36 smoke-index (foreign-slot-value (smoke-method-pointer method)
2009-08-02 tobias 37 'smoke-method 'method)
2009-06-22 tobias 38 :pointer object-pointer
12:18:08 ' 39 smoke-stack stack-pointer
' 40 :void))
' 41
' 42 (defun s-call (method object-pointer &optional (args nil))
2009-04-05 tobias 43 (with-stack (stack args (arguments method) )
2009-07-08 tobias 44 (call-s-method method object-pointer (call-stack-pointer stack))
20:41:19 ' 45 (type-to-lisp (call-stack-pointer stack) (return-type method))))
2009-04-05 tobias 46
2009-06-22 tobias 47 (defun pointer-call (method object-pointer &optional (args nil))
2009-04-05 tobias 48 (with-stack (stack args (arguments method) )
2009-07-08 tobias 49 (call-s-method method object-pointer (call-stack-pointer stack))
20:41:19 ' 50 (foreign-slot-value (call-stack-pointer stack) 'smoke-stack-item 'class)))
2009-04-05 tobias 51
15:36:29 ' 52 (defun smoke-call (class pointer method-name &optional (args nil))
2009-08-02 tobias 53 (s-call (make-smoke-method-from-name class method-name) pointer args))
2009-04-05 tobias 54
15:36:29 ' 55 (defun enum-call (method)
' 56 "Return the enum value for METHOD."
' 57 ;; FIXME:
2009-08-02 tobias 58 ;;
2009-04-05 tobias 59 ;; we could use static call, but QGraphicsEllipseItem::Type has
15:36:29 ' 60 ;; wrongly QGraphicsGridLayout as return type. Smoke ignores case
2009-08-02 tobias 61 ;; and confuses it with the member function type() ?? (27.2.09)
2009-04-05 tobias 62 ;;
15:36:29 ' 63 (assert (enum-p method))
' 64 (with-stack (stack nil nil)
2009-07-08 tobias 65 (call-s-method method (null-pointer) (call-stack-pointer stack))
20:41:19 ' 66 (foreign-slot-value (call-stack-pointer stack) 'smoke-stack-item 'long)))
2009-04-05 tobias 67
15:36:29 ' 68 (defun delete-pointer (pointer class)
' 69 "Destructs the object at POINTER of type CLASS.
2009-07-01 tobias 70 Calls the destructor and frees the memory."
2009-06-30 tobias 71 (declare (optimize (speed 3)))
2009-07-22 tobias 72 (let ((method-name (concatenate 'string "~" (constructor-name class))))
2009-08-02 tobias 73 (s-call (make-smoke-method-from-name class method-name) pointer)))
2009-04-05 tobias 74
15:36:29 ' 75 (defun delete-object (object)
2010-02-19 tobias 76 (let ((method-name (concatenate 'string "~" (name (class-of object)))))
21:22:50 ' 77 (s-call
' 78 (make-smoke-method-from-name (class-of object) method-name)
' 79 (pointer object)))
2009-04-05 tobias 80 (setf (slot-value object 'pointer) (null-pointer)))
15:36:29 ' 81
2010-02-19 tobias 82 (eval-startup (:load-toplevel :execute)
21:10:24 ' 83 (defparameter *binding* (smoke-construct-binding
' 84 (callback destructed)
' 85 (callback dispatch-method)))
' 86 (defparameter *no-dispatch-binding* (smoke-construct-binding
' 87 (callback destructed)
' 88 (null-pointer))))
' 89
2009-06-22 tobias 90 (defun set-binding (object)
12:18:08 ' 91 "Sets the Smoke binding for OBJECT, that receives its callbacks."
' 92 (declare (optimize (speed 3)))
2010-02-18 tobias 93 (let ((class (class-of object)))
19:57:00 ' 94 (with-foreign-object (stack 'smoke-stack-item 2)
' 95 (setf (foreign-slot-value (mem-aref stack 'smoke-stack-item 1)
' 96 'smoke-stack-item 'voidp)
' 97 (if (typep class 'cxx:class)
2010-02-19 tobias 98 *binding*
21:10:24 ' 99 *no-dispatch-binding*))
2010-02-18 tobias 100 (foreign-funcall-pointer
19:57:00 ' 101 (foreign-slot-value (smoke-class-pointer class)
' 102 'smoke-class 'class-function)
' 103 ()
' 104 smoke-index 0 ;; set binding method index
' 105 :pointer (pointer object)
' 106 smoke-stack stack
' 107 :void))))
2009-04-05 tobias 108
2009-06-22 tobias 109 (defun init (smoke module)
2009-04-05 tobias 110 "Returns the a new Smoke binding for the Smoke module SMOKE."
2010-01-10 tobias 111 (use-foreign-library libclsmoke)
2010-02-19 tobias 112 (setf (smoke-module-pointer module) smoke)
21:10:24 ' 113 (init-smoke-module module)
' 114 (setf (gethash (pointer-address smoke) *smoke-modules*) module)
' 115 module)
2009-04-05 tobias 116
2009-05-12 tobias 117 (let ((pointer-symbol-map (make-hash-table)))
2010-01-10 tobias 118 ;; Used by make-load-form for enums to reference the smoke module.
2009-05-12 tobias 119 (defun register-smoke-module-var (symbol)
13:54:46 ' 120 "Registers SYMBOL of a variable containing a pointer to a Smoke module."
2009-08-02 tobias 121 (setf (gethash (pointer-address (smoke-module-pointer (eval symbol)))
10:12:41 ' 122 pointer-symbol-map)
2009-05-12 tobias 123 symbol))
13:54:46 ' 124 (defun get-smoke-variable-for-pointer (pointer)
' 125 "Returns the SYMBOL of the variable whose value is POINTER."
' 126 (gethash (pointer-address pointer) pointer-symbol-map)))
' 127
2009-04-05 tobias 128 (defun call (object method-name &rest args)
2009-08-02 tobias 129 (smoke-call (class-of object) (pointer object)
10:12:41 ' 130 method-name args))
2009-04-05 tobias 131
2009-04-12 tobias 132 (defmethod documentation ((class smoke-standard-class) (doc-type (eql 't)))
20:25:47 ' 133 (declare (optimize (speed 3)))
2009-04-05 tobias 134 (format nil "~@[~A~%~]C++ name: ~A" (call-next-method) (name class)))
15:36:29 ' 135
2009-07-22 tobias 136 (defmethod documentation ((gf smoke-gf) (doc-type (eql 't)))
2009-04-12 tobias 137 (declare (optimize (speed 3)))
2009-04-05 tobias 138 (let ((methods (all-methods (name gf))))
15:36:29 ' 139 (format nil "~@[~A~%~]~{~T~A~%~}"
' 140 (call-next-method)
' 141 (sort (mapcar #'method-declaration methods) #'string<=))))
' 142
2009-07-22 tobias 143 (declaim (inline cstring=))
22:26:05 ' 144 (defun cstring= (string1 string2)
' 145 "Returns T when the C strings STRING1 and STRING2 are equal
' 146 and NIL otherwise."
' 147 (zerop (strcmp string1 string2)))
' 148
2009-04-05 tobias 149 (defun all-methods (name)
15:36:29 ' 150 "Returns a list of all methods named NAME."
2009-07-22 tobias 151 (declare (optimize (speed 3)))
22:26:05 ' 152 (with-foreign-string (name name)
2009-04-05 tobias 153 (let ((methods))
2009-06-22 tobias 154 (maphash
2009-07-22 tobias 155 #'(lambda (address module)
22:26:05 ' 156 (declare (ignore address))
' 157 (map-methods #'(lambda (method)
' 158 (when (and (cstring= name (smoke-method-name method))
' 159 (not (enum-p method)))
' 160 (push (make-smoke-method
' 161 :id (smoke-method-id method)
' 162 :smoke (smoke-method-smoke method))
' 163 methods)))
' 164 module))
' 165 *smoke-modules*)
' 166 methods)))
2009-04-05 tobias 167
15:36:29 ' 168 (defun fgrep-methods (smoke str)
' 169 (map-methods #'(lambda (method)
2009-06-22 tobias 170 (when (search str (name method))
12:18:08 ' 171 (princ (method-declaration method))
' 172 (terpri)))
2009-04-05 tobias 173 smoke))
15:36:29 ' 174
2009-06-11 tobias 175 (defmacro define-smoke-module (package library
14:35:40 ' 176 (variable variable-name)
2009-05-14 tobias 177 (init-function function-name))
12:07:00 ' 178 "Define a Smoke module."
2009-06-22 tobias 179 (let ((smoke-module (intern "*SMOKE-MODULE*")))
12:18:08 ' 180 `(progn
2009-07-22 tobias 181 (eval-when (:compile-toplevel :load-toplevel :execute)
22:26:05 ' 182 (define-foreign-library ,library
2010-02-17 tobias 183 (:darwin ,(format nil "~(~A~).3.dylib" library))
2009-12-13 tobias 184 (:unix ,(format nil "~(~A~).so.3" library))
2009-07-22 tobias 185 (t (:default ,(format nil "~(~A~)" library)))))
2009-07-02 tobias 186 (eval-startup (:compile-toplevel :execute)
2009-07-22 tobias 187 (load-foreign-library ',library))
2009-08-02 tobias 188
2009-07-02 tobias 189 (eval-startup (:compile-toplevel :execute)
2009-08-02 tobias 190 (defcvar (,variable ,variable-name :read-only t :library ,library)
10:12:41 ' 191 :pointer)
2009-12-13 tobias 192 (defcfun (,init-function ,function-name :library ,library)
2009-06-30 tobias 193 :void))
2009-06-12 tobias 194 (eval-when (:compile-toplevel :load-toplevel :execute)
2009-06-22 tobias 195 (defparameter ,smoke-module (make-smoke-module)))
12:18:08 ' 196 (eval-startup (:compile-toplevel :execute)
2009-06-30 tobias 197 (,init-function)
22:47:39 ' 198 (init ,variable ,smoke-module)
' 199 (register-smoke-module-var ',smoke-module))
2009-06-22 tobias 200 (define-classes-and-gfs ,package ,smoke-module))))
2009-05-14 tobias 201
2009-04-05 tobias 202 (defun fgrep-classes (smoke str)
15:36:29 ' 203 (map-classes #'(lambda (class)
' 204 (when (search str (name class))
' 205 (format t "~A~%" (name class))))
' 206 smoke))
2009-05-28 tobias 207 (defmacro define-takes-ownership (method lambda-list object)
2009-06-08 tobias 208 "Declares METHOD transfers the ownership of OBJECT to the
09:20:54 ' 209 first argument of LAMBDA-LIST."
2009-05-31 tobias 210 `(defmethod ,method :before ,lambda-list
2009-06-30 tobias 211 (declare (ignorable ,@(loop for arg in (rest lambda-list) collect
22:47:39 ' 212 (if (consp arg)
' 213 (first arg)
' 214 arg))))
2009-06-08 tobias 215 (transfer-ownership-to ,object ,(if (consp (first lambda-list))
09:20:54 ' 216 (first (first lambda-list))
' 217 (first lambda-list)))))
2009-05-28 tobias 218