2 /* $Id: wrap.cc 749 2008-12-10 14:23:33Z jjongsma $ */
6 * Copyright (C) 1998-2002 The gtkmm Development Team
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the Free
20 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <glib-object.h>
27 #include <glibmm/object.h>
28 #include <glibmm/quark.h>
29 #include <glibmm/wrap.h>
31 #include <glibmmconfig.h>
32 GLIBMM_USING_STD(vector)
38 // Although the new g_type_set_qdata() interface is used now, we still need
39 // a table because we cannot assume that a function pointer fits into void*
40 // on any platform. Nevertheless, indexing a vector costs almost nothing
41 // compared to a map lookup.
43 typedef std::vector<Glib::WrapNewFunction> WrapFuncTable;
45 static WrapFuncTable* wrap_func_table = 0;
47 } // anonymous namespace
53 void wrap_register_init()
59 Glib::quark_ = g_quark_from_static_string("glibmm__Glib::quark_");
60 Glib::quark_cpp_wrapper_deleted_ = g_quark_from_static_string("glibmm__Glib::quark_cpp_wrapper_deleted_");
65 // Make the first element a dummy so we can detect unregistered types.
66 // g_type_get_qdata() returns NULL if no data has been set up.
67 wrap_func_table = new WrapFuncTable(1);
71 void wrap_register_cleanup()
75 delete wrap_func_table;
80 // Register the unique wrap_new() function of a new C++ wrapper type.
81 // The GType argument specifies the parent C type to wrap from.
83 void wrap_register(GType type, WrapNewFunction func)
85 const guint idx = wrap_func_table->size();
86 wrap_func_table->push_back(func);
88 // Store the table index in the type's static data.
89 g_type_set_qdata(type, Glib::quark_, GUINT_TO_POINTER(idx));
93 static Glib::ObjectBase* wrap_create_new_wrapper(GObject* object)
95 g_return_val_if_fail(wrap_func_table != 0, 0);
97 const bool gtkmm_wrapper_already_deleted = (bool)g_object_get_qdata((GObject*)object, Glib::quark_cpp_wrapper_deleted_);
98 if(gtkmm_wrapper_already_deleted)
100 g_warning("Glib::wrap_create_new_wrapper: Attempted to create a 2nd C++ wrapper for a C instance whose C++ wrapper has been deleted.");
104 // Traverse upwards through the inheritance hierarchy
105 // to find the most-specialized wrap_new() for this GType.
107 for(GType type = G_OBJECT_TYPE(object); type != 0; type = g_type_parent(type))
109 // Look up the wrap table index stored in the type's static data.
110 // If a wrap_new() has been registered for the type then call it.
112 if(const gpointer idx = g_type_get_qdata(type, Glib::quark_))
114 const Glib::WrapNewFunction func = (*wrap_func_table)[GPOINTER_TO_UINT(idx)];
115 return (*func)(object);
122 static gboolean gtype_wraps_interface(GType implementer_type, GType interface_type)
125 GType *ifaces = g_type_interfaces (implementer_type, &n_ifaces);
127 gboolean found = FALSE;
128 while (n_ifaces-- && !found)
130 found = (ifaces[n_ifaces] == interface_type);
138 Glib::ObjectBase* wrap_create_new_wrapper_for_interface(GObject* object, GType interface_gtype)
140 g_return_val_if_fail(wrap_func_table != 0, 0);
142 const bool gtkmm_wrapper_already_deleted = (bool)g_object_get_qdata((GObject*)object, Glib::quark_cpp_wrapper_deleted_);
143 if(gtkmm_wrapper_already_deleted)
145 g_warning("Glib::wrap_create_new_wrapper: Attempted to create a 2nd C++ wrapper for a C instance whose C++ wrapper has been deleted.");
149 // Traverse upwards through the inheritance hierarchy
150 // to find the most-specialized wrap_new() for this GType.
152 for(GType type = G_OBJECT_TYPE(object); type != 0; type = g_type_parent(type))
154 // Look up the wrap table index stored in the type's static data.
155 // If a wrap_new() has been registered for the type then call it.
156 // But only if the type implements the interface,
157 // so that the C++ instance is likely to inherit from the appropriate class too.
159 const gpointer idx = g_type_get_qdata(type, Glib::quark_);
160 if(idx && gtype_wraps_interface(type, interface_gtype))
162 const Glib::WrapNewFunction func = (*wrap_func_table)[GPOINTER_TO_UINT(idx)];
163 return (*func)(object);
171 // This is a factory function that converts any type to
172 // its C++ wrapper instance by looking up a wrap_new() function in a map.
174 ObjectBase* wrap_auto(GObject* object, bool take_copy)
179 // Look up current C++ wrapper instance:
180 ObjectBase* pCppObject = ObjectBase::_get_current_wrapper(object);
184 // There's not already a wrapper: generate a new C++ instance.
185 pCppObject = wrap_create_new_wrapper(object);
189 g_warning("Failed to wrap object of type '%s'. Hint: this error is commonly caused by failing to call a library init() function.", G_OBJECT_TYPE_NAME(object));
194 // take_copy=true is used where the GTK+ function doesn't do
195 // an extra ref for us, and always for plain struct members.
197 pCppObject->reference();
202 Glib::RefPtr<Object> wrap(GObject* object, bool take_copy /* = false */)
204 return Glib::RefPtr<Object>(dynamic_cast<Object*>(wrap_auto(object, take_copy)));
207 } /* namespace Glib */