Cleanup C++ to Lisp translation
Annotate for file src/objects/class.lisp
2009-04-05 tobias 1 (in-package #:smoke)
15:36:29 ' 2
2009-06-22 tobias 3 (defclass smoke-class ()
2009-08-02 tobias 4 ((id :initform 0 :type smoke-index
10:12:41 ' 5 :reader id :initarg :id)
' 6 (smoke :type smoke-module
' 7 :reader smoke :initarg :smoke)))
2009-04-05 tobias 8
2009-06-22 tobias 9 (defun make-smoke-class-from-id (smoke id)
2009-07-08 tobias 10 (make-instance 'smoke-class :id id :smoke smoke))
2009-04-05 tobias 11
2009-07-08 tobias 12 (declaim (inline smoke-class-pointer))
20:41:19 ' 13 (defun smoke-class-pointer (class)
2009-08-02 tobias 14 (mem-aref (the foreign-pointer
10:12:41 ' 15 (smoke-array-pointer (smoke-module-classes (smoke class))))
2009-07-08 tobias 16 'smoke-class
20:41:19 ' 17 (the smoke-index (id class))))
2009-04-05 tobias 18
2009-05-26 tobias 19 (declaim (inline class-slot-value))
09:54:47 ' 20 (defun class-slot-value (class slot-name)
2009-07-08 tobias 21 (foreign-slot-value (smoke-class-pointer class)
2009-04-05 tobias 22 'smoke-class slot-name))
15:36:29 ' 23
2009-06-22 tobias 24 (define-compiler-macro class-slot-value (&whole form class slot-name)
12:18:08 ' 25 (if (constantp slot-name)
2009-07-08 tobias 26 `(foreign-slot-value (smoke-class-pointer ,class)
2009-06-22 tobias 27 'smoke-class ,slot-name)
12:18:08 ' 28 form))
' 29
2009-04-05 tobias 30 (defmethod name ((class smoke-class))
2009-05-26 tobias 31 (class-slot-value class 'name))
2009-04-05 tobias 32
15:36:29 ' 33 (defun map-classes (function smoke)
2009-07-01 tobias 34 "Applies FUNCTION to the classes of SMOKE."
2009-05-14 tobias 35 (declare (function function)
12:07:00 ' 36 (optimize (speed 3)))
2009-04-05 tobias 37 (let ((class (make-instance 'smoke-class
2009-05-14 tobias 38 :smoke smoke)))
2009-06-22 tobias 39 (loop for id from 1 to (smoke-array-length (smoke-module-classes smoke)) do
2009-07-08 tobias 40 (setf (slot-value class 'id)
20:41:19 ' 41 id)
2009-04-05 tobias 42 (funcall function class))))
15:36:29 ' 43
' 44 (defun external-p (class)
' 45 "Returns T when CLASS is external in its module; NIL otherwise."
2009-08-02 tobias 46 (declare (type smoke-class class)
10:12:41 ' 47 (optimize (speed 3)))
2009-05-26 tobias 48 (class-slot-value class 'external))
2009-04-05 tobias 49
2009-06-22 tobias 50 (defun get-class-flag (class flag)
12:18:08 ' 51 (declare (optimize (speed 3)))
' 52 (logand (class-slot-value class 'flags)
' 53 (the fixnum (foreign-enum-value 'smoke-class-flags flag))))
2009-04-05 tobias 54
15:36:29 ' 55 (defmethod constructor-p ((class smoke-class))
' 56 "Returns T when CLASS has a constructor; NIL otherwise."
2009-06-22 tobias 57 (/= 0 (get-class-flag class :constructor)))
2009-04-05 tobias 58
2009-05-24 tobias 59 (defun virtual-destructor-p (class)
11:30:05 ' 60 "Returns T when CLASS has a virtual destructor and NIL otherwise."
2009-06-22 tobias 61 (/= 0 (get-class-flag class :virtual-destructor)))
2009-05-24 tobias 62
2009-04-05 tobias 63 (define-condition undefined-class (cell-error)
2010-01-17 tobias 64 ((smoke-name :initarg :smoke-name
21:04:08 ' 65 :initform nil
' 66 :documentation "The name of the Smoke module"))
2009-04-05 tobias 67 (:report (lambda (condition stream)
2010-01-17 tobias 68 (format stream "No Smoke class named ~S in the Smoke module ~S."
21:04:08 ' 69 (cell-error-name condition)
' 70 (slot-value condition 'smoke-name))))
2009-04-05 tobias 71 (:documentation "A undefined Smoke class"))
15:36:29 ' 72
2010-01-17 tobias 73 (defun make-smoke-class (smoke name)
21:04:08 ' 74 "Returns the class named NAME of the smoke module SMOKE.
2009-04-05 tobias 75 Signals an undefined-class condition when there is no class for NAME."
15:36:29 ' 76 (with-foreign-object (c 'smoke-module-index)
' 77 (do () (nil)
2010-01-17 tobias 78 (smoke-find-class c (smoke-module-pointer smoke) name)
2009-04-05 tobias 79 (restart-case
15:36:29 ' 80 (if (null-pointer-p (foreign-slot-value c 'smoke-module-index 'smoke))
2010-01-17 tobias 81 (error (make-condition 'undefined-class :name name :smoke-name (smoke-get-module-name (smoke-module-pointer smoke))))
2009-04-05 tobias 82 (return))
15:36:29 ' 83 (supply (new-name)
' 84 :report "Supply a new class name"
' 85 :interactive read-new-value
' 86 (setf name new-name))))
2010-01-10 tobias 87 (make-instance 'smoke-class
17:31:42 ' 88 :id (foreign-slot-value c 'smoke-module-index 'index)
' 89 :smoke (gethash (pointer-address (foreign-slot-value c 'smoke-module-index 'smoke)) *smoke-modules*))))
2009-04-05 tobias 90
15:36:29 ' 91 (defun real-class (class)
' 92 "Returns the same class like CLASS, but such that EXTERNAL-P returns NIL."
' 93 (if (external-p class)
2009-08-27 tobias 94 (make-smoke-class (smoke class) (name class))
2009-04-05 tobias 95 class))
15:36:29 ' 96
' 97 (defun class-id (module class)
' 98 "Returns the class id of CLASS for the Smoke module MODULE."
' 99 (if (eq (smoke class) module)
' 100 (id class)
' 101 (smoke-class-id module (name class))))
' 102
2010-02-16 tobias 103 ;(defun smoke-subclassp (class base-class) TODO
2009-04-05 tobias 104 (defun derived-p (class base-class)
15:36:29 ' 105 "Returns T when CLASS is derived from BASE-CLASS and NIL when not."
2010-02-16 tobias 106 (values
21:52:02 ' 107 (derived-real-p (real-class class) (real-class base-class))
' 108 T))
2009-04-05 tobias 109
15:36:29 ' 110 (defun derived-real-p (class base-class)
2009-08-02 tobias 111 (smoke-is-derived-from (smoke-module-pointer (smoke class))
10:12:41 ' 112 (id class)
' 113 (smoke-module-pointer (smoke base-class))
' 114 (id base-class)))
2009-04-05 tobias 115
15:36:29 ' 116
' 117 (defun smoke-class-direct-superclasses (class)
2009-05-26 tobias 118 (smoke-add-superclass class nil (class-slot-value class 'parents)))
2009-04-05 tobias 119
15:36:29 ' 120 (defun smoke-add-superclass (class classes index)
2009-06-22 tobias 121 (let ((class-index (mem-aref (smoke-module-inheritance-list
12:18:08 ' 122 (smoke class))
' 123 'smoke-index
' 124 index)))
2009-07-22 tobias 125 (assert (<= class-index (smoke-array-length
22:26:05 ' 126 (smoke-module-classes (smoke class)))))
2009-04-05 tobias 127 (if (= 0 class-index)
15:36:29 ' 128 classes
2009-07-22 tobias 129 (smoke-add-superclass
2009-08-02 tobias 130 class
10:12:41 ' 131 (append classes
' 132 (list (make-smoke-class-from-id (smoke class) class-index)))
2009-07-22 tobias 133 (1+ index)))))