2 /* $Id: object.cc 369 2007-01-20 10:19:33Z daniel $ */
4 /* Copyright 1998-2002 The gtkmm Development Team
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.
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.
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.
21 #include <glibmm/object.h>
22 #include <glibmm/private/object_p.h>
23 #include <glibmm/property.h>
26 #include <glib-object.h>
27 #include <gobject/gvaluecollector.h>
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);
37 // if(weakrefSomeObject->isStillAlive())
39 // weakrefSomeObject->some_method();
43 // //Deal with it, maybe recreating the object.
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.
54 ConstructParams::ConstructParams(const Glib::Class& glibmm_class_)
56 glibmm_class (glibmm_class_),
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
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().
71 ConstructParams::ConstructParams(const Glib::Class& glibmm_class_,
72 const char* first_property_name, ...)
74 glibmm_class (glibmm_class_),
79 va_start(var_args, first_property_name);
81 GObjectClass *const g_class =
82 static_cast<GObjectClass*>(g_type_class_ref(glibmm_class.get_type()));
84 unsigned int n_alloced_params = 0;
85 char* collect_error = 0; // output argument of G_VALUE_COLLECT()
87 for(const char* name = first_property_name;
89 name = va_arg(var_args, char*))
91 GParamSpec *const pspec = g_object_class_find_property(g_class, name);
95 g_warning("Glib::ConstructParams::ConstructParams(): "
96 "object class \"%s\" has no property named \"%s\"",
97 g_type_name(glibmm_class.get_type()), name);
101 if(n_parameters >= n_alloced_params)
102 parameters = g_renew(GParameter, parameters, n_alloced_params += 8);
104 GParameter& param = parameters[n_parameters];
107 param.value.g_type = 0;
109 // Fill the GValue with the current vararg, and move on to the next one.
110 g_value_init(¶m.value, G_PARAM_SPEC_VALUE_TYPE(pspec));
111 G_VALUE_COLLECT(¶m.value, var_args, 0, &collect_error);
115 g_warning("Glib::ConstructParams::ConstructParams(): %s", collect_error);
116 g_free(collect_error);
117 g_value_unset(¶m.value);
124 g_type_class_unref(g_class);
129 ConstructParams::~ConstructParams()
131 while(n_parameters > 0)
132 g_value_unset(¶meters[--n_parameters].value);
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.
142 ConstructParams::ConstructParams(const ConstructParams& other)
144 glibmm_class (other.glibmm_class),
145 n_parameters (other.n_parameters),
146 parameters (g_new(GParameter, n_parameters))
148 for(unsigned int i = 0; i < n_parameters; ++i)
150 parameters[i].name = other.parameters[i].name;
151 parameters[i].value.g_type = 0;
153 g_value_init(¶meters[i].value, G_VALUE_TYPE(&other.parameters[i].value));
154 g_value_copy(&other.parameters[i].value, ¶meters[i].value);
159 /**** Glib::Object_Class ***************************************************/
161 const Glib::Class& Object_Class::init()
165 class_init_func_ = &Object_Class::class_init_function;
166 register_derived_type(G_TYPE_OBJECT);
172 void Object_Class::class_init_function(void*, void*)
175 Object* Object_Class::wrap_new(GObject* object)
177 return new Object(object);
181 /**** Glib::Object *********************************************************/
184 Object::CppClassType Object::object_class_;
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
192 //g_warning("Object::Object(): Did you really mean to call this?");
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.
198 GType object_type = G_TYPE_OBJECT; // the default -- not very useful
200 if(custom_type_name_ && !is_anonymous_custom_())
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_);
207 void *const new_object = g_object_newv(object_type, 0, 0);
209 // Connect the GObject and Glib::Object instances.
210 ObjectBase::initialize(static_cast<GObject*>(new_object));
214 Object::Object(const Glib::ConstructParams& construct_params)
216 GType object_type = construct_params.glibmm_class.get_type();
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.
222 if(custom_type_name_ && !is_anonymous_custom_())
223 object_type = construct_params.glibmm_class.clone_custom_type(custom_type_name_);
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
229 void *const new_object = g_object_newv(
230 object_type, construct_params.n_parameters, construct_params.parameters);
232 // Connect the GObject and Glib::Object instances.
233 ObjectBase::initialize(static_cast<GObject*>(new_object));
236 Object::Object(GObject* castitem)
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.
242 static const char gtkmm_prefix[] = "gtkmm__";
243 const char *const type_name = G_OBJECT_TYPE_NAME(castitem);
245 if(strncmp(type_name, gtkmm_prefix, sizeof(gtkmm_prefix) - 1) == 0)
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.",
257 // Connect the GObject and Glib::Object instances.
258 ObjectBase::initialize(castitem);
263 cpp_destruction_in_progress_ = true;
267 RefPtr<Object> Object::create()
269 // Derived classes will actually return RefPtr<>s that contain useful instances.
270 return RefPtr<Object>();
274 GType Object::get_type()
276 return object_class_.init().get_type();
279 GType Object::get_base_type()
281 return G_TYPE_OBJECT;
285 void* Object::get_data(const QueryQuark& id)
287 return g_object_get_qdata(gobj(),id);
290 void Object::set_data(const Quark& id, void* data)
292 g_object_set_qdata(gobj(),id,data);
295 void Object::set_data(const Quark& id, void* data, DestroyNotify destroy)
297 g_object_set_qdata_full(gobj(), id, data, destroy);
300 void Object::remove_data(const QueryQuark& id)
303 g_return_if_fail(id.id() > 0);
304 g_datalist_id_remove_data(&gobj()->qdata, id);
307 void* Object::steal_data(const QueryQuark& id)
309 return g_object_steal_qdata(gobj(), id);