#include "lisp_object.h" #include "cl_smoke_qt.h" #include #include #include namespace cl_smoke { namespace qt { /** @struct lisp_object::data * @internal * Holds a reference ID for a lisp object and calls * the destructor callback when it is deleted. */ /** @typedef lisp_object::destructor * Destructor. * @param id The ID */ lisp_object::destructor lisp_object::destruct = NULL; /** Constructor. */ lisp_object::data::data() : id(id), is_set(false) { } /** Constructor. * @param id The ID. */ lisp_object::data::data(unsigned int id) : id(id), is_set(true) { } /** Destructor. */ lisp_object::data::~data() { Q_ASSERT_X(lisp_object::destruct, __func__, "call setup_lisp_object() first."); if (this->is_set) (*lisp_object::destruct)(this->id); } /** @class lisp_object * @brief Holds a reference ID to a lisp object. * * The registered destructor callback is called when * the last instance for a specific lisp object is deleted. * * Used for lisp objects in QVariants and signal/slots. */ /** Constructor. */ lisp_object::lisp_object() : d(new data()) { } /** Constructor. * @param id the ID */ lisp_object::lisp_object(unsigned int id) : d(new data(id)) { } /** Constructor. * @param other the lisp_object to copy */ lisp_object::lisp_object(const lisp_object& other) : d(other.d) { } /** @fn lisp_object::id() const * Gets the ID. * * @return the ID */ /** @fn lisp_object::set() const * Determines werter the ID is set. * * @return @c true when the id is set and @c false otherwise. */ /** Sets a new ID. * @param id the ID */ void lisp_object::set_id(unsigned int id) { Q_ASSERT(this->set() ? id != this->id() : true); d = new data(id); } } // namespace qt } // namespace cl_smoke using namespace cl_smoke::qt; /** Initialize the lisp_object. * @relates cl_smoke::qt::lisp_object * @param destruct destructor callback * * @return the QMetaType ID of lisp_object */ CL_SMOKE_QT_EXPORT int cl_smoke_setup_lisp_object(void* destruct) { Q_ASSERT(destruct != NULL); lisp_object::destruct = reinterpret_cast(destruct); return qRegisterMetaType(); } /** Gets the ID of @a object. * @relates cl_smoke::qt::lisp_object * @param object the lisp_object. * * @return the ID */ CL_SMOKE_QT_EXPORT unsigned int cl_smoke_lisp_object_id(const void* object) { return static_cast(object)->id(); } /** Determines werter the ID of @a object is set. * @relates cl_smoke::qt::lisp_object * @param object the object * * @return @c true when the ID is set and @c false otherwise. */ CL_SMOKE_QT_EXPORT int cl_smoke_lisp_object_is_set(const void* object) { return static_cast(object)->set(); } /** Makes a new lisp_object. * @relates cl_smoke::qt::lisp_object * @param id the ID * * @return A new lisp_object instance. */ CL_SMOKE_QT_EXPORT void* cl_smoke_make_lisp_object(unsigned int id) { return new lisp_object(id); } /** Deletes a lisp_object. * @relates cl_smoke::qt::lisp_object * @param object the lisp_object */ CL_SMOKE_QT_EXPORT void* cl_smoke_free_lisp_object(void* object) { delete static_cast(object); } /** Gets the lisp_object of a QVariant. * @relates cl_smoke::qt::lisp_object * @param variant the QVariant * * @return a new lisp_object. */ CL_SMOKE_QT_EXPORT void* cl_smoke_lisp_object_value(const void* variant) { const QVariant* qvariant = static_cast(variant); Q_ASSERT(QVariant::UserType == qvariant->type()); new lisp_object(qvariant->value()); }