+(defmacro define-flags-type (name &rest args)
+ (let ((flags-int (intern (format nil "~A-TO-INT" name)))
+ (int-flags (intern (format nil "INT-TO-~A" name))))
+ `(progn
+ (deftype ,name () '(flags ,@args))
+ (defun ,flags-int (value)
+ (ecase value
+ ,@(%map-enum args :flags-int)))
+ (defun ,int-flags (value)
+ (ecase value
+ ,@(%map-enum args :int-flags)))
+ (defmethod to-alien-form (form (type (eql ',name)) &rest args)
+ (declare (ignore type args))
+ (list ',flags-int form))
+ (defmethod from-alien-form (form (type (eql ',name)) &rest args)
+ (declare (ignore type args))
+ (list ',int-flags form))
+ (defmethod to-alien-function ((type (eql ',name)) &rest args)
+ (declare (ignore type args))
+ #',flags-int)
+ (defmethod from-alien-function ((type (eql ',name)) &rest args)
+ (declare (ignore type args))
+ #',int-flags)
+ (defmethod writer-function ((type (eql ',name)) &rest args)
+ (declare (ignore type args))
+ (let ((writer (writer-function 'signed)))
+ #'(lambda (flags location &optional (offset 0))
+ (funcall writer (,flags-int flags) location offset))))
+ (defmethod reader-function ((type (eql ',name)) &rest args)
+ (declare (ignore type args))
+ (let ((reader (reader-function 'signed)))
+ #'(lambda (location &optional (offset 0))
+ (,int-flags (funcall reader location offset))))))))
+
+
+
+;;;; Type definition by introspection