more qt:variant conversions
Annotate for file src/variant.lisp
2009-06-11 tobias 1 (in-package :qt)
2010-01-10 tobias 2
2009-06-11 tobias 3 (defmethod print-object ((variant variant) stream)
2010-01-10 tobias 4 "Print the type and value of the variant."
08:52:49 ' 5 (if (or (not (slot-boundp variant 'pointer))
' 6 (null-pointer-p (pointer variant)))
' 7 (call-next-method)
' 8 (print-unreadable-object (variant stream :type t :identity t)
' 9 (format stream "~A~@[ ~S~]"
' 10 (cxx:type-name variant)
2009-06-11 tobias 11 (handler-case (from-variant variant)
2010-01-10 tobias 12 (error () nil))))))
08:52:49 ' 13
2009-06-11 tobias 14 (defun make-variant (&optional (value nil value-p))
2010-01-10 tobias 15 "Returns a new VARIANT containing a C++ version of VALUE
08:52:49 ' 16 or an empty variant when VALUE is not specified."
' 17 (if value-p
2009-06-11 tobias 18 (make-instance 'variant :args (list value))
14:59:48 ' 19 (make-instance 'variant)))
2010-01-10 tobias 20
2009-06-11 tobias 21 (defun make-char (character)
2010-01-10 tobias 22 "Returns a char for a lisp CHARACTER."
08:52:49 ' 23 (let ((octets (babel:string-to-octets (string character))))
' 24 (case (length octets)
2009-06-11 tobias 25 (1 (make-instance 'char :args (list (aref octets 0))))
14:59:48 ' 26 (2 (make-instance 'char :args (list (aref octets 0)
2009-07-22 tobias 27 (aref octets 1))))
2010-01-10 tobias 28 (t (error "qt:char requires the character ~A to be encoded
08:52:49 ' 29 in one or two octets, but it is using ~A."
' 30 character (length octets))))))
' 31
' 32 (defun surrogate-p (char)
' 33 (or (cxx:is-high-surrogate char)
' 34 (cxx:is-low-surrogate char)))
' 35
2009-06-11 tobias 36 (defun from-char (char)
2009-07-22 tobias 37 "Returns the lisp character represented by CHAR."
2010-01-10 tobias 38 (assert (not (surrogate-p char))
08:52:49 ' 39 (char)
' 40 "The char ~A is part of a surrogate.")
' 41 (char
' 42 (babel:octets-to-string (make-array 2 :element-type '(unsigned-byte 8)
' 43 :initial-contents
' 44 (list
' 45 (char-code (cxx:cell char))
' 46 (char-code (cxx:row char)))))
' 47 0))
' 48
2009-06-11 tobias 49 (defmethod print-object ((char char) stream)
2010-01-10 tobias 50 (if (or (null-pointer-p (pointer char))
08:52:49 ' 51 (surrogate-p char))
' 52 (call-next-method)
' 53 (print-unreadable-object (char stream :type t)
2009-06-11 tobias 54 (princ (from-char char) stream))))
2010-01-10 tobias 55
08:52:49 ' 56
2009-07-22 tobias 57 ;; FIXME include in MAKE-VARIANT?
2009-06-11 tobias 58 (defun make-lisp-variant (value)
2010-01-10 tobias 59 "Returns a new VARIANT that wraps VALUE.
08:52:49 ' 60
2009-07-22 tobias 61 The variant contains the actual Lisp object
2010-01-10 tobias 62 and not its C++ value like in MAKE-VARIANT."
08:52:49 ' 63 (let ((object (make-cxx-lisp-object value)))
' 64 (unwind-protect
2009-06-11 tobias 65 (make-instance 'variant :args (list *cxx-lisp-object-metatype*
14:59:48 ' 66 object))
2010-01-10 tobias 67 (free-cxx-lisp-object object))))
08:52:49 ' 68
' 69 (defcfun qt-smoke-lisp-object-value :pointer
' 70 (variant :pointer))
' 71
2009-06-11 tobias 72 (defun variant-boundp (variant)
2010-01-10 tobias 73 "Returns true when VARIANT is valid (has a value) and false otherwise."
08:52:49 ' 74 (cxx:is-valid variant))
' 75
2009-06-10 tobias 76 (defmacro variant-conversions ((variant) &body types)
12:02:01 ' 77 `(ecase (cxx:user-type ,variant)
' 78
' 79 ,@(loop for type in types collect
' 80 (if (symbolp type)
' 81 `(,(value (symbol-value (alexandria:symbolicate 'variant.+ type '+)))
' 82 (,(intern (format nil "TO-~A" type) :cxx) ,variant))
' 83 type))))
' 84
2009-06-11 tobias 85 (defun from-variant (variant)
2010-01-10 tobias 86 "Returns the value of VARIANT."
2009-06-10 tobias 87 (variant-conversions (variant)
2009-06-11 tobias 88 (#.(value variant.+invalid+)
2010-01-10 tobias 89 (cerror "Return (VALUES)" "Type of variant ~A is invalid." variant)
08:52:49 ' 90 (values))
2009-06-10 tobias 91 bit-array bool byte-array
12:02:01 ' 92 char
' 93 date date-time double
' 94 int
' 95 line line-f list locale long-long
' 96 point point-f
' 97 rect rect-f reg-exp
' 98 size size-f string string-list
' 99 time
' 100 uint
' 101 ulong-long
' 102 url
2010-01-10 tobias 103 (#.*cxx-lisp-object-metatype*
08:52:49 ' 104 (let* ((lisp-object (qt-smoke-lisp-object-value (smoke::pointer variant)))
' 105 (value))
' 106 (setf value (translate-cxx-lisp-object lisp-object))
' 107 (free-cxx-lisp-object lisp-object)
' 108 value))))
2009-06-10 tobias 109
2009-06-11 tobias 110 (defmethod value ((variant variant))
2010-01-10 tobias 111 "Returns the value of VARIANT."
2009-06-11 tobias 112 (from-variant variant))
2010-01-10 tobias 113
2009-06-11 tobias 114 (defmethod (setf value) (new-value (variant variant))
14:59:48 ' 115 (cxx:operator= variant (make-variant new-value))
2010-01-10 tobias 116 new-value)