/ src / lib /
/src/lib/lisp_object.cpp
1 #include "lisp_object.h"
2
3 #include "cl_smoke_qt.h"
4
5 #include <QtGlobal>
6 #include <QtDebug>
7 #include <QVariant>
8
9 namespace cl_smoke {
10 namespace qt {
11
12 /** @struct lisp_object::data
13 * @internal
14 * Holds a reference ID for a lisp object and calls
15 * the destructor callback when it is deleted.
16 */
17
18 /** @typedef lisp_object::destructor
19 * Destructor.
20 * @param id The ID
21 */
22
23 lisp_object::destructor lisp_object::destruct = NULL;
24
25
26 /** Constructor. */
27 lisp_object::data::data()
28 : id(id),
29 is_set(false)
30 { }
31
32 /** Constructor.
33 * @param id The ID.
34 */
35 lisp_object::data::data(unsigned int id)
36 : id(id),
37 is_set(true)
38 { }
39
40 /** Destructor. */
41 lisp_object::data::~data()
42 {
43 Q_ASSERT_X(lisp_object::destruct, __func__,
44 "call setup_lisp_object() first.");
45
46 if (this->is_set)
47 (*lisp_object::destruct)(this->id);
48 }
49
50 /** @class lisp_object
51 * @brief Holds a reference ID to a lisp object.
52 *
53 * The registered destructor callback is called when
54 * the last instance for a specific lisp object is deleted.
55 *
56 * Used for lisp objects in QVariants and signal/slots.
57 */
58
59 /** Constructor. */
60 lisp_object::lisp_object()
61 : d(new data())
62 { }
63
64 /** Constructor.
65 * @param id the ID
66 */
67 lisp_object::lisp_object(unsigned int id)
68 : d(new data(id))
69 { }
70
71 /** Constructor.
72 * @param other the lisp_object to copy
73 */
74 lisp_object::lisp_object(const lisp_object& other)
75 : d(other.d)
76 { }
77
78 /** @fn lisp_object::id() const
79 * Gets the ID.
80 *
81 * @return the ID
82 */
83
84 /** @fn lisp_object::set() const
85 * Determines werter the ID is set.
86 *
87 * @return @c true when the id is set and @c false otherwise.
88 */
89
90 /** Sets a new ID.
91 * @param id the ID
92 */
93 void
94 lisp_object::set_id(unsigned int id)
95 {
96 Q_ASSERT(this->set() ? id != this->id() : true);
97
98 d = new data(id);
99 }
100
101 } // namespace qt
102 } // namespace cl_smoke
103
104 using namespace cl_smoke::qt;
105
106 /** Initialize the lisp_object.
107 * @relates cl_smoke::qt::lisp_object
108 * @param destruct destructor callback
109 *
110 * @return the QMetaType ID of lisp_object
111 */
112 CL_SMOKE_QT_EXPORT int
113 cl_smoke_setup_lisp_object(void* destruct)
114 {
115 Q_ASSERT(destruct != NULL);
116 lisp_object::destruct = reinterpret_cast<lisp_object::destructor>(destruct);
117
118 return qRegisterMetaType<lisp_object>();
119 }
120
121 /** Gets the ID of @a object.
122 * @relates cl_smoke::qt::lisp_object
123 * @param object the lisp_object.
124 *
125 * @return the ID
126 */
127 CL_SMOKE_QT_EXPORT unsigned int
128 cl_smoke_lisp_object_id(const void* object)
129 {
130 return static_cast<const lisp_object*>(object)->id();
131 }
132
133
134 /** Determines werter the ID of @a object is set.
135 * @relates cl_smoke::qt::lisp_object
136 * @param object the object
137 *
138 * @return @c true when the ID is set and @c false otherwise.
139 */
140 CL_SMOKE_QT_EXPORT int
141 cl_smoke_lisp_object_is_set(const void* object)
142 {
143 return static_cast<const lisp_object*>(object)->set();
144 }
145
146 /** Makes a new lisp_object.
147 * @relates cl_smoke::qt::lisp_object
148 * @param id the ID
149 *
150 * @return A new lisp_object instance.
151 */
152 CL_SMOKE_QT_EXPORT void*
153 cl_smoke_make_lisp_object(unsigned int id)
154 {
155 return new lisp_object(id);
156 }
157
158 /** Deletes a lisp_object.
159 * @relates cl_smoke::qt::lisp_object
160 * @param object the lisp_object
161 */
162 CL_SMOKE_QT_EXPORT void*
163 cl_smoke_free_lisp_object(void* object)
164 {
165 delete static_cast<lisp_object*>(object);
166 }
167
168 /** Gets the lisp_object of a QVariant.
169 * @relates cl_smoke::qt::lisp_object
170 * @param variant the QVariant
171 *
172 * @return a new lisp_object.
173 */
174 CL_SMOKE_QT_EXPORT void*
175 cl_smoke_lisp_object_value(const void* variant)
176 {
177 const QVariant* qvariant = static_cast<const QVariant*>(variant);
178 Q_ASSERT(QVariant::UserType == qvariant->type());
179
180 new lisp_object(qvariant->value<lisp_object>());
181 }