00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "refpointer.h"
00019 #include "quark.h"
00020 #include <glib-object.h>
00021
00022 namespace QGlib {
00023
00024 RefCountedObject *constructWrapper(Type instanceType, void *instance)
00025 {
00026 Quark q = g_quark_from_static_string("QGlib__wrapper_constructor");
00027 RefCountedObject *cppClass = NULL;
00028
00029 for(Type t = instanceType; t.isValid(); t = t.parent()) {
00030 void *funcPtr = t.quarkData(q);
00031 if (funcPtr) {
00032 cppClass = (reinterpret_cast<RefCountedObject *(*)(void*)>(funcPtr))(instance);
00033 Q_ASSERT_X(cppClass, "QGlib::constructWrapper",
00034 "Failed to wrap instance. This is a bug in the bindings library.");
00035 return cppClass;
00036 }
00037 }
00038
00039 Q_ASSERT_X(false, "QGlib::constructWrapper",
00040 "No wrapper constructor found for this type. Did you forget to call init()?.");
00041 return cppClass;
00042 }
00043
00044 namespace Private {
00045
00046 static void qdataDestroyNotify(void *cppInstance)
00047 {
00048 delete static_cast<RefCountedObject*>(cppInstance);
00049 }
00050
00051 RefCountedObject *wrapObject(void *gobject)
00052 {
00053 Q_ASSERT(gobject);
00054
00055 Quark q = g_quark_from_static_string("QGlib__object_wrapper");
00056 RefCountedObject *obj = static_cast<RefCountedObject*>(g_object_get_qdata(G_OBJECT(gobject), q));
00057
00058 if (!obj) {
00059 obj = constructWrapper(Type::fromInstance(gobject), gobject);
00060 g_object_set_qdata_full(G_OBJECT(gobject), q, obj, &qdataDestroyNotify);
00061 }
00062
00063 return obj;
00064 }
00065
00066 RefCountedObject *wrapParamSpec(void *param)
00067 {
00068 Q_ASSERT(param);
00069
00070 Quark q = g_quark_from_static_string("QGlib__paramspec_wrapper");
00071 RefCountedObject *obj = static_cast<RefCountedObject*>(g_param_spec_get_qdata(G_PARAM_SPEC(param), q));
00072
00073 if (!obj) {
00074 obj = constructWrapper(Type::fromInstance(param), param);
00075 g_param_spec_set_qdata_full(G_PARAM_SPEC(param), q, obj, &qdataDestroyNotify);
00076 }
00077
00078 return obj;
00079 }
00080
00081 RefCountedObject *wrapInterface(Type interfaceType, void *gobject)
00082 {
00083 Q_ASSERT(gobject);
00084
00085 Quark q = Quark::fromString(QLatin1String("QGlib__interface_wrapper__") + interfaceType.name());
00086 RefCountedObject *obj = static_cast<RefCountedObject*>(g_object_get_qdata(G_OBJECT(gobject), q));
00087
00088 if (!obj) {
00089 obj = constructWrapper(interfaceType, gobject);
00090 g_object_set_qdata_full(G_OBJECT(gobject), q, obj, &qdataDestroyNotify);
00091 }
00092
00093 return obj;
00094 }
00095
00096 }
00097 }