QtGStreamer  0.10.2
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
wrap.cpp
00001 /*
00002     Copyright (C) 2010 Collabora Ltd. <info@collabora.co.uk>
00003       @author George Kiagiadakis <george.kiagiadakis@collabora.co.uk>
00004 
00005     This library is free software; you can redistribute it and/or modify
00006     it under the terms of the GNU Lesser General Public License as published
00007     by the Free Software Foundation; either version 2.1 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU Lesser General Public License for more details.
00014 
00015     You should have received a copy of the GNU Lesser General Public License
00016     along with this program.  If not, see <http://www.gnu.org/licenses/>.
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 } //namespace Private
00097 } //namespace QGlib