2 #ifndef _GLIBMM_OBJECTBASE_H
3 #define _GLIBMM_OBJECTBASE_H
5 /* $Id: objectbase.h 385 2007-03-23 17:23:42Z murrayc $ */
7 /* Copyright 2002 The gtkmm Development Team
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the Free
21 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <glibmm/signalproxy.h>
25 #include <glibmm/propertyproxy.h>
26 #include <glibmm/ustring.h>
27 #include <glibmm/value.h>
28 #include <glibmm/quark.h>
29 #include <sigc++/trackable.h>
31 #include <glibmmconfig.h>
32 #include <glibmm/debug.h>
34 #include <glibmmconfig.h>
36 GLIBMM_USING_STD(type_info)
38 #ifndef DOXYGEN_SHOULD_SKIP_THIS
39 extern "C" { typedef struct _GObject GObject; }
46 #ifndef DOXYGEN_SHOULD_SKIP_THIS
47 class GSigConnectionNode;
50 //This inherits virtually from sigc::trackable so that people can multiply inherit glibmm classes from other sigc::trackable-derived classes.
51 //See bugzilla.gnome.org bug # 116280
52 /** Glib::ObjectBase is a common base class for Objects and Interfaces.
54 * This is used as virtual base class. This means the ObjectBase
55 * constructor runs before all others, either implicitly or explicitly. Each of
56 * the available constructors initializes custom_type_name_ in a different way.
58 class GLIBMM_API ObjectBase : virtual public sigc::trackable
61 /** This default constructor is called implicitly from the constructor of user-derived
62 * classes, even if, for instance, Gtk::Button calls a different ObjectBase constructor.
63 * This is normal behaviour for C++ virtual inheritance.
65 * The GType name will be gtkmm__anonymous_custom_type.
69 /** A derived constructor always overrides this choice.
70 * The C++ language itself ensures that the constructor
71 * is only invoked once.
73 * All classes generated by gtkmmproc use this constructor, with custom_type_name = 0,
74 * which essentially means it's not a custom type. This is used to optimize
75 * vfunc and signal handler callbacks -- since the C++ virtual methods are
76 * not overridden, invocation can be skipped.
78 * The GType name will be @a custom_type_name.
80 explicit ObjectBase(const char* custom_type_name);
82 /** This constructor is a special feature to allow creation of derived types on the
83 * fly, without having to use g_object_new() manually. This feature is
84 * sometimes necessary, e.g. to implement a custom Gtk::CellRenderer. The
85 * neat trick with the virtual base class ctor makes it possible to reuse
86 * the same direct base class' constructor as with non-custom types.
88 * The GType name will be @a custom_type_info.name().
90 explicit ObjectBase(const std::type_info& custom_type_info);
92 virtual ~ObjectBase() = 0;
94 // Called by Glib::Object and Glib::Interface constructors. See comments there.
95 void initialize(GObject* castitem);
99 /// You probably want to use a specific property_*() accessor method instead.
100 void set_property_value(const Glib::ustring& property_name, const Glib::ValueBase& value);
102 /// You probably want to use a specific property_*() accessor method instead.
103 void get_property_value(const Glib::ustring& property_name, Glib::ValueBase& value) const;
105 /// You probably want to use a specific property_*() accessor method instead.
106 template <class PropertyType>
107 void set_property(const Glib::ustring& property_name, const PropertyType& value);
109 /// You probably want to use a specific property_*() accessor method instead.
110 template <class PropertyType>
111 void get_property(const Glib::ustring& property_name, PropertyType& value) const;
113 /** You can use the signal_changed() signal of the property proxy instead,
114 * but this is necessary when using the reduced API.
116 void connect_property_changed(const Glib::ustring& property_name, const sigc::slot<void>& slot);
118 /** Increment the reference count for this object.
119 * You should never need to do this manually - use the object via a RefPtr instead.
121 virtual void reference() const;
123 /** Decrement the reference count for this object.
124 * You should never need to do this manually - use the object via a RefPtr instead.
126 virtual void unreference() const;
128 ///Provides access to the underlying C GtkObject.
129 inline GObject* gobj() { return gobject_; }
131 ///Provides access to the underlying C GtkObject.
132 inline const GObject* gobj() const { return gobject_; }
134 /// Give a ref-ed copy to someone. Use for direct struct access.
135 GObject* gobj_copy() const;
137 #ifndef DOXYGEN_SHOULD_SKIP_THIS
139 /// This is for use by gtkmm wrappers only, and not by applications.
140 static ObjectBase* _get_current_wrapper(GObject* object); //We keep this non-inline version, to preserve ABI.
142 // This is commented-out because it's not clear yet whether it's a worthwhile optimization.
143 /// This is for use by gtkmm wrappers only, and not by applications.
145 //inline static ObjectBase* _get_current_wrapper_inline(GObject* object)
147 // // This is what g_object_get_qdata does internally. However,
148 // // g_object_get_qdata does an addition G_IS_OBJECT(object) check that
149 // // needs three times as much time as the actual lookup.
151 // return static_cast<ObjectBase*>(g_datalist_id_get_data(&object->qdata, Glib::quark_));
156 bool _cpp_destruction_is_in_progress() const;
157 #endif //DOXYGEN_SHOULD_SKIP_THIS
161 #ifndef DOXYGEN_SHOULD_SKIP_THIS
162 GObject* gobject_; // the GLib/GDK/GTK+ object instance
163 const char* custom_type_name_;
164 bool cpp_destruction_in_progress_;
166 bool is_anonymous_custom_() const;
168 public: // is_derived_() must be public, so that overridden vfuncs and signal handlers can call it via ObjectBase.
170 /// This is for use by gtkmm wrappers only, and not by applications.
171 bool is_derived_() const; //We keep this non-inline version, to preserve ABI.
173 //This is commented-out because it's not clear yet whether it's a worthwhile optimization.
175 /// This is for use by gtkmm wrappers only, and not by applications.
176 //inline bool is_derived_inline_() const
178 // return (custom_type_name_ != 0);
182 static void destroy_notify_callback_(void* data);
183 virtual void destroy_notify_();
185 void _set_current_wrapper(GObject* object);
186 #endif //DOXYGEN_SHOULD_SKIP_THIS
190 ObjectBase(const ObjectBase&);
191 ObjectBase& operator=(const ObjectBase&);
193 #ifndef DOXYGEN_SHOULD_SKIP_THIS
194 virtual void set_manage(); // calls g_error()
195 #endif //DOXYGEN_SHOULD_SKIP_THIS
197 #ifndef DOXYGEN_SHOULD_SKIP_THIS
198 friend class Glib::GSigConnectionNode; // for GSigConnectionNode::notify()
202 #ifndef DOXYGEN_SHOULD_SKIP_THIS
204 template <class PropertyType> inline
205 void ObjectBase::set_property(const Glib::ustring& property_name, const PropertyType& value)
207 Glib::Value<PropertyType> property_value;
208 property_value.init(Glib::Value<PropertyType>::value_type());
210 property_value.set(value);
211 this->set_property_value(property_name, property_value);
214 template <class PropertyType> inline
215 void ObjectBase::get_property(const Glib::ustring& property_name, PropertyType& value) const
217 Glib::Value<PropertyType> property_value;
218 property_value.init(Glib::Value<PropertyType>::value_type());
220 this->get_property_value(property_name, property_value);
222 value = property_value.get();
225 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
228 bool _gobject_cppinstance_already_deleted(GObject* gobject);
233 #endif /* _GLIBMM_OBJECTBASE_H */