root/trunk/thirdparty/cl-store_0.8.4/utils.lisp

Revision 2554, 5.5 kB (checked in by ksprotte, 11 months ago)

added cl-store

  • Property svn:executable set to *
Line 
1 ;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;; See the file LICENCE for licence information.
3
4 ;; Miscellaneous utilities used throughout the package.
5 (in-package :cl-store)
6
7 (defmacro aif (test then &optional else)
8   `(let ((it ,test))
9     (if it ,then ,else)))
10
11 (defmacro with-gensyms (names &body body)
12   `(let ,(mapcar #'(lambda (x) `(,x (gensym))) names)
13     ,@body))
14
15 (defgeneric serializable-slots (object)
16   (declare (optimize speed))
17   (:documentation
18    "Return a list of slot-definitions to serialize. The default
19     is to call serializable-slots-using-class with the object
20     and the objects class")
21   (:method ((object standard-object))
22    (serializable-slots-using-class object (class-of object)))
23 #+(or sbcl cmu openmcl)
24   (:method ((object structure-object))
25    (serializable-slots-using-class object (class-of object)))
26   (:method ((object condition))
27    (serializable-slots-using-class object (class-of object))))
28
29 ; unfortunately the metaclass of conditions in sbcl and cmu
30 ; are not standard-class
31
32 (defgeneric serializable-slots-using-class (object class)
33   (declare (optimize speed))
34   (:documentation "Return a list of slot-definitions to serialize.
35    The default calls compute slots with class")
36   (:method ((object t) (class standard-class))
37    (class-slots class))
38 #+(or sbcl cmu openmcl)
39   (:method ((object t) (class structure-class))
40    (class-slots class))
41 #+sbcl
42   (:method ((object t) (class sb-pcl::condition-class))
43    (class-slots class))
44 #+cmu
45   (:method ((object t) (class pcl::condition-class))
46    (class-slots class)))
47
48
49 ; Generify get-slot-details for customization (from Thomas Stenhaug)
50 (defgeneric get-slot-details (slot-definition)
51   (declare (optimize speed))
52   (:documentation
53    "Return a list of slot details which can be used
54     as an argument to ensure-class")
55   (:method ((slot-definition #+(or ecl abcl (and clisp (not mop))) t
56                              #-(or ecl abcl (and clisp (not mop))) slot-definition))
57    (list :name (slot-definition-name slot-definition)
58          :allocation (slot-definition-allocation slot-definition)
59          :initargs (slot-definition-initargs slot-definition)
60          ;; :initform. dont use initform until we can
61          ;; serialize functions
62          :readers (slot-definition-readers slot-definition)
63          :type (slot-definition-type slot-definition)
64          :writers (slot-definition-writers slot-definition)))
65   #+openmcl
66   (:method ((slot-definition ccl::structure-slot-definition))
67    (list :name (slot-definition-name slot-definition)
68          :allocation (slot-definition-allocation slot-definition)
69          :initargs (slot-definition-initargs slot-definition)
70          ;; :initform. dont use initform until we can
71          ;; serialize functions
72          ;; :readers (slot-definition-readers slot-definition)
73          :type (slot-definition-type slot-definition)
74          ;; :writers (slot-definition-writers slot-definition)
75          )))
76
77 (defmacro when-let ((var test) &body body)
78   `(let ((,var ,test))
79      (when ,var
80        ,@body)))
81
82
83 ;; because clisp doesn't have the class single-float or double-float.
84 (defun float-type (float)
85   (etypecase float
86     (single-float 0)
87     (double-float 1)
88     (short-float 2)
89     (long-float 3)))
90
91 (defun get-float-type (num)
92   (ecase num
93     (0 1.0)
94     (1 1.0d0)
95     (2 1.0s0)
96     (3 1.0l0)))
97
98 (deftype ub32 ()
99   `(unsigned-byte 32))
100
101 (deftype sb32 ()
102   `(signed-byte 32))
103
104 (deftype array-size ()
105   "The maximum size of a vector"
106   `(integer 0 , array-dimension-limit))
107
108 (deftype array-tot-size ()
109   "The maximum total size of an array"
110   `(integer 0 , array-total-size-limit))
111
112 (defun store-32-bit (obj stream)
113   "Write OBJ down STREAM as a 32 bit integer."
114   (declare (optimize speed (debug 0) (safety 0))
115            (type ub32 obj))
116     (write-byte (ldb (byte 8 0) obj) stream)
117     (write-byte (ldb (byte 8 8) obj) stream)
118     (write-byte (ldb (byte 8 16) obj) stream)
119     (write-byte (+ 0 (ldb (byte 8 24) obj)) stream))
120
121 (defmacro make-ub32 (a b c d)
122   `(the ub32 (logior (ash ,a 24) (ash ,b 16) (ash ,c 8) ,d)))
123
124 (defun read-32-bit (buf &optional (signed t))
125   "Read a signed or unsigned byte off STREAM."
126   (declare (optimize speed (debug 0) (safety 0)))
127   (let ((byte1 (read-byte buf))
128         (byte2 (read-byte buf))
129         (byte3 (read-byte buf))
130         (byte4 (read-byte buf)))
131     (declare (type (mod 256) byte1 byte2 byte3 byte4))
132     (let ((ret (make-ub32 byte4 byte3 byte2 byte1)))
133       (if (and signed (> byte1 127))
134           (logior (ash -1 32) ret)
135           ret))))
136
137 (defun kwd (name)
138   (values (intern (string-upcase name) :keyword)))
139
140 (defun mkstr (&rest args)
141   (with-output-to-string (s)
142     (dolist (x args)
143       (format s "~@:(~A~)" x))))
144
145 (defun symbolicate (&rest syms)
146   "Concatenate all symbol names into one big symbol"
147   (values (intern (apply #'mkstr syms))))
148
149 ;; Taken straight from swank.lisp --- public domain
150 ;; and then slightly modified
151 (defun safe-length (list)
152   "Similar to `list-length', but avoid errors on improper lists.
153 Return two values: the length of the list and the last cdr.
154 Modified to work on non proper lists."
155   (do ((n 0 (+ n 2))                    ;Counter.
156        (fast list (cddr fast))          ;Fast pointer: leaps by 2.
157        (slow list (cdr slow)))          ;Slow pointer: leaps by 1.
158       (nil)
159     (cond ((null fast) (return (values n nil)))
160           ((not (consp fast)) (return (values n fast)))
161           ((null (cdr fast)) (return (values (1+ n) (cdr fast))))
162           ((and (eq fast slow) (> n 0)) (return (values (/ n 2) list)))
163           ((not (consp (cdr fast))) (return (values (1+ n) (cdr fast)))))))
164
165 ;; EOF
Note: See TracBrowser for help on using the browser.