More gracefully handle type mismatch errors when doing playlist things (just ignore...
[ardour.git] / libs / glibmm2 / glib / glibmm / object.cc
1 // -*- c++ -*-
2 /* $Id: object.cc 369 2007-01-20 10:19:33Z daniel $ */
3
4 /* Copyright 1998-2002 The gtkmm Development Team
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free
18  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20
21 #include <glibmm/object.h>
22 #include <glibmm/private/object_p.h>
23 #include <glibmm/property.h>
24
25 #include <glib.h>
26 #include <glib-object.h>
27 #include <gobject/gvaluecollector.h>
28
29 #include <cstdarg>
30 #include <cstring>
31
32 //Weak references:
33 //I'm not sure what the point of these are apart from being a hacky way out of circular references,
34 //but maybe we could make it easier to use them by making a Java Reference Object -style class like so:
35 // Glib::WeakRef<SomeDerivedObject> weakrefSomeObject(object1);
36 // ...
37 // if(weakrefSomeObject->isStillAlive())
38 // {
39 //   weakrefSomeObject->some_method();
40 // }
41 // else
42 // {
43 //   //Deal with it, maybe recreating the object.
44 // }
45 //
46 // Without this, the coder has to define his own signal handler which sets his own isStillAlive boolean.
47 // weakrefSomeObject<> could still have its own signal_destroyed signal so that coders can choose to deal
48 // with the destruction as soon as it happens instead of just checking later before they try to use it.
49
50
51 namespace Glib
52 {
53
54 ConstructParams::ConstructParams(const Glib::Class& glibmm_class_)
55 :
56   glibmm_class (glibmm_class_),
57   n_parameters (0),
58   parameters   (0)
59 {}
60
61 /*
62  * The implementation is mostly copied from gobject.c, with some minor tweaks.
63  * Basically, it looks up each property name to get its GType, and then uses
64  * G_VALUE_COLLECT() to store the varargs argument in a GValue of the correct
65  * type.
66  *
67  * Note that the property name arguments are assumed to be static string
68  * literals.  No attempt is made to copy the string content.  This is no
69  * different from g_object_new().
70  */
71 ConstructParams::ConstructParams(const Glib::Class& glibmm_class_,
72                                  const char* first_property_name, ...)
73 :
74   glibmm_class (glibmm_class_),
75   n_parameters (0),
76   parameters   (0)
77 {
78   va_list var_args;
79   va_start(var_args, first_property_name);
80
81   GObjectClass *const g_class =
82       static_cast<GObjectClass*>(g_type_class_ref(glibmm_class.get_type()));
83
84   unsigned int n_alloced_params = 0;
85   char*        collect_error    = 0; // output argument of G_VALUE_COLLECT()
86
87   for(const char* name = first_property_name;
88       name != 0;
89       name = va_arg(var_args, char*))
90   {
91     GParamSpec *const pspec = g_object_class_find_property(g_class, name);
92
93     if(!pspec)
94     {
95       g_warning("Glib::ConstructParams::ConstructParams(): "
96                 "object class \"%s\" has no property named \"%s\"",
97                 g_type_name(glibmm_class.get_type()), name);
98       break;
99     }
100
101     if(n_parameters >= n_alloced_params)
102       parameters = g_renew(GParameter, parameters, n_alloced_params += 8);
103
104     GParameter& param = parameters[n_parameters];
105
106     param.name = name;
107     param.value.g_type = 0;
108
109     // Fill the GValue with the current vararg, and move on to the next one.
110     g_value_init(&param.value, G_PARAM_SPEC_VALUE_TYPE(pspec));
111     G_VALUE_COLLECT(&param.value, var_args, 0, &collect_error);
112
113     if(collect_error)
114     {
115       g_warning("Glib::ConstructParams::ConstructParams(): %s", collect_error);
116       g_free(collect_error);
117       g_value_unset(&param.value);
118       break;
119     }
120
121     ++n_parameters;
122   }
123
124   g_type_class_unref(g_class);
125
126   va_end(var_args);
127 }
128
129 ConstructParams::~ConstructParams()
130 {
131   while(n_parameters > 0)
132     g_value_unset(&parameters[--n_parameters].value);
133
134   g_free(parameters);
135 }
136
137 /*
138  * Some compilers require the existence of a copy constructor in certain
139  * usage contexts.  This implementation is fully functional, but unlikely
140  * to be ever actually called due to optimization.
141  */
142 ConstructParams::ConstructParams(const ConstructParams& other)
143 :
144   glibmm_class  (other.glibmm_class),
145   n_parameters  (other.n_parameters),
146   parameters    (g_new(GParameter, n_parameters))
147 {
148   for(unsigned int i = 0; i < n_parameters; ++i)
149   {
150     parameters[i].name = other.parameters[i].name;
151     parameters[i].value.g_type = 0;
152
153     g_value_init(&parameters[i].value, G_VALUE_TYPE(&other.parameters[i].value));
154     g_value_copy(&other.parameters[i].value, &parameters[i].value);
155   }
156 }
157
158
159 /**** Glib::Object_Class ***************************************************/
160
161 const Glib::Class& Object_Class::init()
162 {
163   if(!gtype_)
164   {
165     class_init_func_ = &Object_Class::class_init_function;
166     register_derived_type(G_TYPE_OBJECT);
167   }
168
169   return *this;
170 }
171
172 void Object_Class::class_init_function(void*, void*)
173 {}
174
175 Object* Object_Class::wrap_new(GObject* object)
176 {
177   return new Object(object);
178 }
179
180
181 /**** Glib::Object *********************************************************/
182
183 // static data
184 Object::CppClassType Object::object_class_;
185
186 Object::Object()
187 {
188   // This constructor is ONLY for derived classes that are NOT wrappers of
189   // derived C objects.  For instance, Gtk::Object should NOT use this
190   // constructor.
191
192   //g_warning("Object::Object(): Did you really mean to call this?");
193
194   // If Glib::ObjectBase has been constructed with a custom typeid, we derive
195   // a new GType on the fly.  This works because ObjectBase is a virtual base
196   // class, therefore its constructor is always executed first.
197
198   GType object_type = G_TYPE_OBJECT; // the default -- not very useful
199
200   if(custom_type_name_ && !is_anonymous_custom_())
201   {
202     object_class_.init();
203     // This creates a type that is derived (indirectly) from GObject.
204     object_type = object_class_.clone_custom_type(custom_type_name_);
205   }
206
207   void *const new_object = g_object_newv(object_type, 0, 0);
208
209   // Connect the GObject and Glib::Object instances.
210   ObjectBase::initialize(static_cast<GObject*>(new_object));
211
212 }
213
214 Object::Object(const Glib::ConstructParams& construct_params)
215 {
216   GType object_type = construct_params.glibmm_class.get_type();
217
218   // If Glib::ObjectBase has been constructed with a custom typeid, we derive
219   // a new GType on the fly.  This works because ObjectBase is a virtual base
220   // class, therefore its constructor is always executed first.
221
222   if(custom_type_name_ && !is_anonymous_custom_())
223     object_type = construct_params.glibmm_class.clone_custom_type(custom_type_name_);
224
225   // Create a new GObject with the specified array of construct properties.
226   // This works with custom types too, since those inherit the properties of
227   // their base class.
228
229   void *const new_object = g_object_newv(
230       object_type, construct_params.n_parameters, construct_params.parameters);
231
232   // Connect the GObject and Glib::Object instances.
233   ObjectBase::initialize(static_cast<GObject*>(new_object));
234 }
235
236 Object::Object(GObject* castitem)
237 {
238   //I disabled this check because libglademm really does need to do this.
239   //(actually it tells libglade to instantiate "gtkmm_" types.
240   //The 2nd instance bug will be caught elsewhere anyway.
241 /*
242   static const char gtkmm_prefix[] = "gtkmm__";
243   const char *const type_name = G_OBJECT_TYPE_NAME(castitem);
244
245   if(strncmp(type_name, gtkmm_prefix, sizeof(gtkmm_prefix) - 1) == 0)
246   {
247     g_warning("Glib::Object::Object(GObject*): "
248               "An object of type '%s' was created directly via g_object_new(). "
249               "The Object::Object(const Glib::ConstructParams&) constructor "
250               "should be used instead.\n"
251               "This could happen if the C instance lived longer than the C++ instance, so that "
252               "a second C++ instance was created automatically to wrap it. That would be a gtkmm bug that you should report.",
253                type_name);
254   }
255 */
256
257   // Connect the GObject and Glib::Object instances.
258   ObjectBase::initialize(castitem);
259 }
260
261 Object::~Object()
262 {
263   cpp_destruction_in_progress_ = true;
264 }
265
266 /*
267 RefPtr<Object> Object::create()
268 {
269   // Derived classes will actually return RefPtr<>s that contain useful instances.
270   return RefPtr<Object>();
271 }
272 */
273
274 GType Object::get_type()
275 {
276   return object_class_.init().get_type();
277 }
278
279 GType Object::get_base_type()
280 {
281   return G_TYPE_OBJECT;
282 }
283
284 // Data services
285 void* Object::get_data(const QueryQuark& id)
286 {
287   return g_object_get_qdata(gobj(),id);
288 }
289
290 void Object::set_data(const Quark& id, void* data)
291 {
292   g_object_set_qdata(gobj(),id,data);
293 }
294
295 void Object::set_data(const Quark& id, void* data, DestroyNotify destroy)
296 {
297   g_object_set_qdata_full(gobj(), id, data, destroy);
298 }
299
300 void Object::remove_data(const QueryQuark& id)
301 {
302   // missing in glib??
303   g_return_if_fail(id.id() > 0);
304   g_datalist_id_remove_data(&gobj()->qdata, id);
305 }
306
307 void* Object::steal_data(const QueryQuark& id)
308 {
309   return g_object_steal_qdata(gobj(), id);
310 }
311
312 } // namespace Glib
313