546840301c582807e0c5662cbf630d4c9ba93a3e
[cl-gtk2.git] / glib / gobject.type-designator.lisp
1 (in-package :gobject.ffi)
2
3 (defctype g-type gsize)
4
5 (define-foreign-type g-type-designator ()
6   ((mangled-p :initarg :mangled-p
7               :reader g-type-designator-mangled-p
8               :initform nil
9               :documentation "Whether the type designator is mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag"))
10   (:documentation "Values of this CFFI foreign type identify the GType. GType is designated by a its name (a string) or a numeric identifier. Functions accept GType designators as a string or integer and return them as a string. Functions @fun{g-type-name} and @fun{g-type-from-name} are used to convert between name and numeric identifier.
11
12 Numeric identifier of GType may be different between different program runs. But string identifier of GType does not change.")
13   (:actual-type g-type)
14   (:simple-parser g-type-designator))
15
16 (defun unmangle-g-type (g-type)
17   (logxor g-type (ldb (byte 1 0) g-type)));;subtract the G_SIGNAL_TYPE_STATIC_SCOPE
18
19 (defmethod translate-from-foreign (value (type g-type-designator))
20   (g-type-name (if (g-type-designator-mangled-p type)
21                    (unmangle-g-type value)
22                    value)))
23
24 (defmethod translate-to-foreign (value (type g-type-designator))
25   (etypecase value
26     (string (g-type-from-name value))
27     (integer value)
28     (null 0)))
29
30 (defun g-type-numeric (g-type-designator)
31   (etypecase g-type-designator
32     (string (g-type-from-name g-type-designator))
33     (integer g-type-designator)
34     (null 0)))
35
36 (defun g-type-string (g-type-designator)
37   (etypecase g-type-designator
38     (string (g-type-name g-type-designator))
39     (integer (g-type-name g-type-designator))
40     (null nil)))
41
42 (defcfun (g-type-name "g_type_name") :string
43   "Returns the name of a GType.@see{g-type-from-name}
44
45 Example:
46 @pre{
47 \(g-type-from-name \"GtkLabel\")
48 => 7151952
49 \(g-type-name 7151952)
50 => \"GtkLabel\"
51 }
52 @arg[type]{GType designator (see @class{g-type-designator})}
53 @return{a string}"
54   (type g-type-designator))
55
56 (defcfun (g-type-from-name "g_type_from_name") g-type
57   "Returns the numeric identifier of a GType by its name. @see{g-type-name}
58
59 Example:
60 @pre{
61 \(g-type-from-name \"GtkLabel\")
62 => 7151952
63 \(g-type-name 7151952)
64 => \"GtkLabel\"
65 }
66 @arg[name]{a string - name of GType}
67 @return{an integer}"
68   (name :string))
69
70 (defun g-type= (type-1 type-2)
71   (= (g-type-numeric type-1)
72      (g-type-numeric type-2)))
73
74 (defun g-type/= (type-1 type-2)
75   (/= (g-type-numeric type-1)
76       (g-type-numeric type-2)))