QtGStreamer  1.2.0
 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  QString(QLatin1String("No wrapper constructor found for this type (") +
41  instanceType.name() + QLatin1String("). Did you forget to call init()?.")).toUtf8());
42  return cppClass;
43 }
44 
45 namespace Private {
46 
47 static void qdataDestroyNotify(void *cppInstance)
48 {
49  delete static_cast<RefCountedObject*>(cppInstance);
50 }
51 
52 RefCountedObject *wrapObject(void *gobject)
53 {
54  Q_ASSERT(gobject);
55 
56  Quark q = g_quark_from_static_string("QGlib__object_wrapper");
57  RefCountedObject *obj = static_cast<RefCountedObject*>(g_object_get_qdata(G_OBJECT(gobject), q));
58 
59  if (!obj) {
60  obj = constructWrapper(Type::fromInstance(gobject), gobject);
61  g_object_set_qdata_full(G_OBJECT(gobject), q, obj, &qdataDestroyNotify);
62  }
63 
64  return obj;
65 }
66 
67 RefCountedObject *wrapParamSpec(void *param)
68 {
69  Q_ASSERT(param);
70 
71  Quark q = g_quark_from_static_string("QGlib__paramspec_wrapper");
72  RefCountedObject *obj = static_cast<RefCountedObject*>(g_param_spec_get_qdata(G_PARAM_SPEC(param), q));
73 
74  if (!obj) {
75  obj = constructWrapper(Type::fromInstance(param), param);
76  g_param_spec_set_qdata_full(G_PARAM_SPEC(param), q, obj, &qdataDestroyNotify);
77  }
78 
79  return obj;
80 }
81 
82 RefCountedObject *wrapInterface(Type interfaceType, void *gobject)
83 {
84  Q_ASSERT(gobject);
85 
86  Quark q = Quark::fromString(QLatin1String("QGlib__interface_wrapper__") + interfaceType.name());
87  RefCountedObject *obj = static_cast<RefCountedObject*>(g_object_get_qdata(G_OBJECT(gobject), q));
88 
89  if (!obj) {
90  obj = constructWrapper(interfaceType, gobject);
91  g_object_set_qdata_full(G_OBJECT(gobject), q, obj, &qdataDestroyNotify);
92  }
93 
94  return obj;
95 }
96 
97 } //namespace Private
98 } //namespace QGlib
static Quark fromString(const char *str)
Definition: quark.cpp:25
RefCountedObject * constructWrapper(Type instanceType, void *instance)
Definition: wrap.cpp:24
Base class for all the reference-counted object wrappers.
Definition: refpointer.h:179
Wrapper class for GQuark.
Definition: quark.h:42
Wrapper class for GType.
Definition: type.h:63