1 #include "lisp_object.h"
3 #include "cl_smoke_qt.h"
12 /** @struct lisp_object::data
14 * Holds a reference ID for a lisp object and calls
15 * the destructor callback when it is deleted.
18 /** @typedef lisp_object::destructor
23 lisp_object::destructor lisp_object::destruct = NULL;
27 lisp_object::data::data()
35 lisp_object::data::data(unsigned int id)
41 lisp_object::data::~data()
43 Q_ASSERT_X(lisp_object::destruct, __func__,
44 "call setup_lisp_object() first.");
47 (*lisp_object::destruct)(this->id);
50 /** @class lisp_object
51 * @brief Holds a reference ID to a lisp object.
53 * The registered destructor callback is called when
54 * the last instance for a specific lisp object is deleted.
56 * Used for lisp objects in QVariants and signal/slots.
60 lisp_object::lisp_object()
67 lisp_object::lisp_object(unsigned int id)
72 * @param other the lisp_object to copy
74 lisp_object::lisp_object(const lisp_object& other)
78 /** @fn lisp_object::id() const
84 /** @fn lisp_object::set() const
85 * Determines werter the ID is set.
87 * @return @c true when the id is set and @c false otherwise.
94 lisp_object::set_id(unsigned int id)
96 Q_ASSERT(this->set() ? id != this->id() : true);
102 } // namespace cl_smoke
104 using namespace cl_smoke::qt;
106 /** Initialize the lisp_object.
107 * @relates cl_smoke::qt::lisp_object
108 * @param destruct destructor callback
110 * @return the QMetaType ID of lisp_object
112 CL_SMOKE_QT_EXPORT int
113 cl_smoke_setup_lisp_object(void* destruct)
115 Q_ASSERT(destruct != NULL);
116 lisp_object::destruct = reinterpret_cast<lisp_object::destructor>(destruct);
118 return qRegisterMetaType<lisp_object>();
121 /** Gets the ID of @a object.
122 * @relates cl_smoke::qt::lisp_object
123 * @param object the lisp_object.
127 CL_SMOKE_QT_EXPORT unsigned int
128 cl_smoke_lisp_object_id(const void* object)
130 return static_cast<const lisp_object*>(object)->id();
134 /** Determines werter the ID of @a object is set.
135 * @relates cl_smoke::qt::lisp_object
136 * @param object the object
138 * @return @c true when the ID is set and @c false otherwise.
140 CL_SMOKE_QT_EXPORT int
141 cl_smoke_lisp_object_is_set(const void* object)
143 return static_cast<const lisp_object*>(object)->set();
146 /** Makes a new lisp_object.
147 * @relates cl_smoke::qt::lisp_object
150 * @return A new lisp_object instance.
152 CL_SMOKE_QT_EXPORT void*
153 cl_smoke_make_lisp_object(unsigned int id)
155 return new lisp_object(id);
158 /** Deletes a lisp_object.
159 * @relates cl_smoke::qt::lisp_object
160 * @param object the lisp_object
162 CL_SMOKE_QT_EXPORT void*
163 cl_smoke_free_lisp_object(void* object)
165 delete static_cast<lisp_object*>(object);
168 /** Gets the lisp_object of a QVariant.
169 * @relates cl_smoke::qt::lisp_object
170 * @param variant the QVariant
172 * @return a new lisp_object.
174 CL_SMOKE_QT_EXPORT void*
175 cl_smoke_lisp_object_value(const void* variant)
177 const QVariant* qvariant = static_cast<const QVariant*>(variant);
178 Q_ASSERT(QVariant::UserType == qvariant->type());
180 new lisp_object(qvariant->value<lisp_object>());