Merged revisions 6293,6296-6306,6308 via svnmerge from
[ardour.git] / libs / glibmm2 / glib / glibmm / wrap.cc
1 // -*- c++ -*-
2 /* $Id: wrap.cc 749 2008-12-10 14:23:33Z jjongsma $ */
3
4 /* wrap.cc
5  *
6  * Copyright (C) 1998-2002 The gtkmm Development Team
7  *
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.
12  *
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.
17  *
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.
21  */
22
23 #include <glib-object.h>
24 #include <glib.h>
25
26 #include <vector>
27 #include <glibmm/object.h>
28 #include <glibmm/quark.h>
29 #include <glibmm/wrap.h>
30
31 #include <glibmmconfig.h>
32 GLIBMM_USING_STD(vector)
33
34
35 namespace
36 {
37
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.
42
43 typedef std::vector<Glib::WrapNewFunction> WrapFuncTable;
44
45 static WrapFuncTable* wrap_func_table = 0;
46
47 } // anonymous namespace
48
49
50 namespace Glib
51 {
52
53 void wrap_register_init()
54 {
55   g_type_init();
56
57   if(!Glib::quark_)
58   {
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_");
61   }
62
63   if(!wrap_func_table)
64   {
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);
68   }
69 }
70
71 void wrap_register_cleanup()
72 {
73   if(wrap_func_table)
74   {
75     delete wrap_func_table;
76     wrap_func_table = 0;
77   }
78 }
79
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.
82 //
83 void wrap_register(GType type, WrapNewFunction func)
84 {
85   const guint idx = wrap_func_table->size();
86   wrap_func_table->push_back(func);
87
88   // Store the table index in the type's static data.
89   g_type_set_qdata(type, Glib::quark_, GUINT_TO_POINTER(idx));
90 }
91
92
93 static Glib::ObjectBase* wrap_create_new_wrapper(GObject* object)
94 {
95   g_return_val_if_fail(wrap_func_table != 0, 0);
96
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)
99   {
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.");
101     return 0;
102   }
103
104   // Traverse upwards through the inheritance hierarchy
105   // to find the most-specialized wrap_new() for this GType.
106   //
107   for(GType type = G_OBJECT_TYPE(object); type != 0; type = g_type_parent(type))
108   {
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.
111     //
112     if(const gpointer idx = g_type_get_qdata(type, Glib::quark_))
113     {
114       const Glib::WrapNewFunction func = (*wrap_func_table)[GPOINTER_TO_UINT(idx)];
115       return (*func)(object);
116     }
117   }
118
119   return 0;
120 }
121
122 static gboolean gtype_wraps_interface(GType implementer_type, GType interface_type)
123 {
124   guint n_ifaces = 0;
125   GType *ifaces = g_type_interfaces (implementer_type, &n_ifaces);
126
127   gboolean found = FALSE;
128   while (n_ifaces-- && !found)
129   {
130     found = (ifaces[n_ifaces] == interface_type);
131   }
132       
133   g_free (ifaces);
134
135   return found;
136 }
137
138 Glib::ObjectBase* wrap_create_new_wrapper_for_interface(GObject* object, GType interface_gtype)
139 {
140   g_return_val_if_fail(wrap_func_table != 0, 0);
141
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)
144   {
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.");
146     return 0;
147   }
148
149   // Traverse upwards through the inheritance hierarchy
150   // to find the most-specialized wrap_new() for this GType.
151   //
152   for(GType type = G_OBJECT_TYPE(object); type != 0; type = g_type_parent(type))
153   {
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.
158     //
159     const gpointer idx = g_type_get_qdata(type, Glib::quark_);
160     if(idx && gtype_wraps_interface(type, interface_gtype))
161     {
162       const Glib::WrapNewFunction func = (*wrap_func_table)[GPOINTER_TO_UINT(idx)];
163       return (*func)(object);
164     }
165   }
166
167   return 0;
168 }
169
170
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.
173 //
174 ObjectBase* wrap_auto(GObject* object, bool take_copy)
175 {
176   if(!object)
177     return 0;
178
179   // Look up current C++ wrapper instance:
180   ObjectBase* pCppObject = ObjectBase::_get_current_wrapper(object);
181
182   if(!pCppObject)
183   {
184     // There's not already a wrapper: generate a new C++ instance.
185     pCppObject = wrap_create_new_wrapper(object);
186
187     if(!pCppObject)
188     {
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));
190       return 0;
191     }
192   }
193
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.
196   if(take_copy)
197     pCppObject->reference();
198
199   return pCppObject;
200 }
201
202 Glib::RefPtr<Object> wrap(GObject* object, bool take_copy /* = false */)
203 {
204   return Glib::RefPtr<Object>(dynamic_cast<Object*>(wrap_auto(object, take_copy)));
205 }
206
207 } /* namespace Glib */
208