QtGStreamer  0.10.2
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
wrap.cpp
1 /*
2  Copyright (C) 2010 Collabora Ltd. <info@collabora.co.uk>
3  @author George Kiagiadakis <george.kiagiadakis@collabora.co.uk>
4 
5  This library is free software; you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published
7  by the Free Software Foundation; either version 2.1 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include "refpointer.h"
19 #include "quark.h"
20 #include <glib-object.h>
21 
22 namespace QGlib {
23 
24 RefCountedObject *constructWrapper(Type instanceType, void *instance)
25 {
26  Quark q = g_quark_from_static_string("QGlib__wrapper_constructor");
27  RefCountedObject *cppClass = NULL;
28 
29  for(Type t = instanceType; t.isValid(); t = t.parent()) {
30  void *funcPtr = t.quarkData(q);
31  if (funcPtr) {
32  cppClass = (reinterpret_cast<RefCountedObject *(*)(void*)>(funcPtr))(instance);
33  Q_ASSERT_X(cppClass, "QGlib::constructWrapper",
34  "Failed to wrap instance. This is a bug in the bindings library.");
35  return cppClass;
36  }
37  }
38 
39  Q_ASSERT_X(false, "QGlib::constructWrapper",
40  "No wrapper constructor found for this type. Did you forget to call init()?.");
41  return cppClass;
42 }
43 
44 namespace Private {
45 
46 static void qdataDestroyNotify(void *cppInstance)
47 {
48  delete static_cast<RefCountedObject*>(cppInstance);
49 }
50 
51 RefCountedObject *wrapObject(void *gobject)
52 {
53  Q_ASSERT(gobject);
54 
55  Quark q = g_quark_from_static_string("QGlib__object_wrapper");
56  RefCountedObject *obj = static_cast<RefCountedObject*>(g_object_get_qdata(G_OBJECT(gobject), q));
57 
58  if (!obj) {
59  obj = constructWrapper(Type::fromInstance(gobject), gobject);
60  g_object_set_qdata_full(G_OBJECT(gobject), q, obj, &qdataDestroyNotify);
61  }
62 
63  return obj;
64 }
65 
66 RefCountedObject *wrapParamSpec(void *param)
67 {
68  Q_ASSERT(param);
69 
70  Quark q = g_quark_from_static_string("QGlib__paramspec_wrapper");
71  RefCountedObject *obj = static_cast<RefCountedObject*>(g_param_spec_get_qdata(G_PARAM_SPEC(param), q));
72 
73  if (!obj) {
74  obj = constructWrapper(Type::fromInstance(param), param);
75  g_param_spec_set_qdata_full(G_PARAM_SPEC(param), q, obj, &qdataDestroyNotify);
76  }
77 
78  return obj;
79 }
80 
81 RefCountedObject *wrapInterface(Type interfaceType, void *gobject)
82 {
83  Q_ASSERT(gobject);
84 
85  Quark q = Quark::fromString(QLatin1String("QGlib__interface_wrapper__") + interfaceType.name());
86  RefCountedObject *obj = static_cast<RefCountedObject*>(g_object_get_qdata(G_OBJECT(gobject), q));
87 
88  if (!obj) {
89  obj = constructWrapper(interfaceType, gobject);
90  g_object_set_qdata_full(G_OBJECT(gobject), q, obj, &qdataDestroyNotify);
91  }
92 
93  return obj;
94 }
95 
96 } //namespace Private
97 } //namespace QGlib