chiark / gitweb /
Adding reader and writer functions to COPY-OF
[clg] / glib / genums.lisp
1 ;; Common Lisp bindings for GTK+ v2.0
2 ;; Copyright (C) 2000-2001 Espen S. Johnsen <esj@stud.cs.uit.no>
3 ;;
4 ;; This library is free software; you can redistribute it and/or
5 ;; modify it under the terms of the GNU Lesser General Public
6 ;; License as published by the Free Software Foundation; either
7 ;; version 2 of the License, or (at your option) any later version.
8 ;;
9 ;; This library is distributed in the hope that it will be useful,
10 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 ;; Lesser General Public License for more details.
13 ;;
14 ;; You should have received a copy of the GNU Lesser General Public
15 ;; License along with this library; if not, write to the Free Software
16 ;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
18 ;; $Id: genums.lisp,v 1.4 2004-11-06 21:39:58 espen Exp $
19
20 (in-package "GLIB")
21
22
23 (defun %map-enum (args op)
24   (let ((current-value 0))
25     (mapcar
26      #'(lambda (mapping)
27          (destructuring-bind (symbol &optional (value current-value))
28              (mklist mapping)
29            (setf current-value (1+ value))
30            (case op
31              (:enum-int (list symbol value))
32              (:flags-int (list symbol value))
33              (:int-enum (list value symbol))
34              (:int-flags (list value symbol))
35              (:symbols symbol))))
36      args)))
37
38 (defun %query-enum-or-flags-values (query-function class type)
39   (multiple-value-bind (sap length)
40       (funcall query-function (type-class-ref type))
41     (let ((values nil)
42           (size (proxy-instance-size (find-class class)))
43           (proxy (make-instance class :location sap)))
44       (dotimes (i length)
45         (with-slots (location nickname value) proxy
46           (setf location sap)
47           (setq sap (sap+ sap size))
48           (push
49            (list
50             (intern (substitute #\- #\_ (string-upcase nickname)) "KEYWORD")
51             value)
52            values)))
53       values)))
54    
55   
56 ;;;; Generic enum type
57
58 (deftype enum (&rest args)
59   `(member ,@(%map-enum args :symbols)))
60
61 (defmethod alien-type ((type (eql 'enum)) &rest args)
62   (declare (ignore type args))
63   (alien-type 'signed))
64
65 (defmethod size-of ((type (eql 'enum)) &rest args)
66   (declare (ignore type args))
67   (size-of 'signed))
68
69 (defmethod to-alien-form (form (type (eql 'enum)) &rest args)
70   (declare (ignore type))
71   `(ecase ,form
72     ,@(%map-enum args :enum-int)))
73
74 (defmethod from-alien-form (form (type (eql 'enum)) &rest args)
75   (declare (ignore type))
76   `(ecase ,form
77     ,@(%map-enum args :int-enum)))
78
79 (defmethod to-alien-function ((type (eql 'enum)) &rest args)
80   (let ((mappings (%map-enum args :enum-int)))
81     #'(lambda (enum)
82         (or
83          (second (assoc enum mappings))
84          (error "~S is not of type ~S" enum (cons type args))))))
85
86 (defmethod from-alien-function ((type (eql 'enum)) &rest args)
87   (declare (ignore type))
88   (let ((mappings (%map-enum args :int-enum)))
89     #'(lambda (int)
90         (second (assoc int mappings)))))
91
92 (defmethod writer-function ((type (eql 'enum)) &rest args)
93   (declare (ignore type))
94   (let ((writer (writer-function 'signed))
95         (function (apply #'to-alien-function 'enum args)))
96     #'(lambda (enum location &optional (offset 0))
97         (funcall writer (funcall function enum) location offset))))
98     
99 (defmethod reader-function ((type (eql 'enum)) &rest args)
100   (declare (ignore type))
101   (let ((reader (reader-function 'signed))
102         (function (apply #'from-alien-function 'enum args)))
103     #'(lambda (location &optional (offset 0))
104         (funcall function (funcall reader location offset)))))
105
106
107
108 (defclass %enum-value (struct)
109   ((value :allocation :alien :type int)
110    (name :allocation :alien :type string)
111    (nickname :allocation :alien :type string))
112   (:metaclass static-struct-class))
113
114 (defbinding %enum-class-values () pointer
115   (class pointer)
116   (n-values unsigned-int :out))
117
118 (defun query-enum-values (type)
119   (%query-enum-or-flags-values #'%enum-class-values '%enum-value type))
120
121
122
123 ;;;;  Generic flags type
124
125 (deftype flags (&rest args)
126   `(or null (cons (member ,@(%map-enum args :symbols)) list)))
127
128 (defmethod alien-type ((type (eql 'flags)) &rest args)
129   (declare (ignore type args))
130   (alien-type 'unsigned))
131
132 (defmethod size-of ((type (eql 'flags)) &rest args)
133   (declare (ignore type args))
134   (size-of 'unsigned))
135
136 (defmethod to-alien-form (flags (type (eql 'flags)) &rest args)
137   `(loop
138     with value = 0
139     with flags = ,flags
140     for flag in (mklist flags)
141     do (let ((flagval
142               (or
143                (second (assoc flag ',(%map-enum args :flags-int)))
144                (error "~S is not of type ~S" flags '(,type ,@args)))))
145          (setq value (logior value flagval)))
146     finally (return value)))
147
148 (defmethod from-alien-form (int (type (eql 'flags)) &rest args)
149   (declare (ignore type))
150   `(loop
151     for mapping in ',(%map-enum args :int-flags)
152     unless (zerop (logand int (first mapping)))
153     collect (second mapping)))
154
155 (defmethod to-alien-function ((type (eql 'flags)) &rest args)
156   (let ((mappings (%map-enum args :flags-int)))
157     #'(lambda (flags)   
158         (loop
159          with value = 0
160          for flag in (mklist flags)
161          do (let ((flagval (or
162                     (second (assoc flag mappings))
163                     (error "~S is not of type ~S" flags (cons type args)))))
164               (setq value (logior value flagval)))
165          finally (return value)))))
166
167 (defmethod from-alien-function ((type (eql 'flags)) &rest args)
168   (declare (ignore type))
169   (let ((mappings (%map-enum args :int-flags)))
170     #'(lambda (int)
171         (loop
172          for mapping in mappings
173          unless (zerop (logand int (first mapping)))
174          collect (second mapping)))))
175
176 (defmethod writer-function ((type (eql 'flags)) &rest args)
177   (declare (ignore type))
178   (let ((writer (writer-function 'unsigned))
179         (function (apply #'to-alien-function 'flags args)))
180     #'(lambda (flags location &optional (offset 0))
181         (funcall writer (funcall function flags) location offset))))
182     
183 (defmethod reader-function ((type (eql 'flags)) &rest args)
184   (declare (ignore type))
185   (let ((reader (reader-function 'unsigned))
186         (function (apply #'from-alien-function 'flags args)))
187     #'(lambda (location &optional (offset 0))
188         (funcall function (funcall reader location offset)))))
189
190
191
192 (defclass %flags-value (struct)
193   ((value :allocation :alien :type unsigned-int)
194    (name :allocation :alien :type string)
195    (nickname :allocation :alien :type string))
196   (:metaclass static-struct-class))
197
198 (defbinding %flags-class-values () pointer
199   (class pointer)
200   (n-values unsigned-int :out))
201
202 (defun query-flags-values (type)
203   (%query-enum-or-flags-values #'%flags-class-values '%flags-value type))
204
205
206
207 ;;;;
208
209 (defun expand-enum-type (type-number &optional options)
210   (let* ((super (supertype type-number))
211          (type (type-from-number type-number))
212          (mappings (getf options :mappings))
213          (expanded-mappings
214           (append
215            (delete-if
216             #'(lambda (mapping)
217                 (or
218                  (assoc (first mapping) mappings)
219                  (rassoc (cdr mapping) mappings :test #'equal)))
220             (if (eq super 'enum)
221                 (query-enum-values type-number)
222               (query-flags-values type-number)))
223            (remove-if
224             #'(lambda (mapping) (eq (second mapping) nil)) mappings))))
225     `(progn
226        (register-type ',type ,(find-type-name type-number))
227        (deftype ,type () '(,super ,@expanded-mappings)))))
228
229
230 (register-derivable-type 'enum "GEnum" 'expand-enum-type)
231 (register-derivable-type 'flags "GFlags" 'expand-enum-type)
232