add new sigc++2 directory
[ardour.git] / libs / glibmm2 / glib / glibmm / objectbase.h
1 // -*- c++ -*-
2 #ifndef _GLIBMM_OBJECTBASE_H
3 #define _GLIBMM_OBJECTBASE_H
4
5 /* $Id: objectbase.h 385 2007-03-23 17:23:42Z murrayc $ */
6
7 /* Copyright 2002 The gtkmm Development Team
8  *
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.
13  *
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.
18  *
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.
22  */
23
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>
30 #include <typeinfo>
31 #include <glibmmconfig.h>
32 #include <glibmm/debug.h>
33
34 #include <glibmmconfig.h>
35
36 GLIBMM_USING_STD(type_info)
37
38 #ifndef DOXYGEN_SHOULD_SKIP_THIS
39 extern "C" { typedef struct _GObject GObject; }
40 #endif
41
42
43 namespace Glib
44 {
45
46 #ifndef DOXYGEN_SHOULD_SKIP_THIS
47 class GSigConnectionNode;
48 #endif
49
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.
53  *
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.
57  */
58 class GLIBMM_API ObjectBase : virtual public sigc::trackable
59 {
60 protected:
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.
64    * 
65    * The GType name will be gtkmm__anonymous_custom_type.
66    */
67   ObjectBase();
68
69   /** A derived constructor always overrides this choice.
70    * The C++ language itself ensures that the constructor
71    * is only invoked once.
72    * 
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.
77    *
78    * The GType name will be @a custom_type_name.
79    */
80   explicit ObjectBase(const char* custom_type_name);
81
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.
87    *
88    * The GType name will be @a custom_type_info.name().
89    */
90   explicit ObjectBase(const std::type_info& custom_type_info);
91
92   virtual ~ObjectBase() = 0;
93
94   // Called by Glib::Object and Glib::Interface constructors. See comments there.
95   void initialize(GObject* castitem);
96
97 public:
98
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);
101
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;
104
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);
108
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;
112
113   /** You can use the signal_changed() signal of the property proxy instead, 
114    * but this is necessary when using the reduced API.
115    */
116   void connect_property_changed(const Glib::ustring& property_name, const sigc::slot<void>& slot);
117
118   /** Increment the reference count for this object.
119    * You should never need to do this manually - use the object via a RefPtr instead.
120    */
121   virtual void reference() const;
122
123   /** Decrement the reference count for this object.
124    * You should never need to do this manually - use the object via a RefPtr instead.
125    */
126   virtual void unreference() const;
127
128   ///Provides access to the underlying C GtkObject.
129   inline GObject*       gobj()       { return gobject_; }
130
131   ///Provides access to the underlying C GtkObject.
132   inline const GObject* gobj() const { return gobject_; }
133
134   /// Give a ref-ed copy to someone. Use for direct struct access.
135   GObject* gobj_copy() const;
136
137 #ifndef DOXYGEN_SHOULD_SKIP_THIS
138
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.
141
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.
144   //
145   //inline static ObjectBase* _get_current_wrapper_inline(GObject* object)
146   //{
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.
150   //  if(object)
151   //    return static_cast<ObjectBase*>(g_datalist_id_get_data(&object->qdata, Glib::quark_));
152   //  else
153   //    return 0;
154   //}
155
156   bool _cpp_destruction_is_in_progress() const;
157 #endif //DOXYGEN_SHOULD_SKIP_THIS
158
159 protected:
160
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_;
165
166   bool is_anonymous_custom_() const;
167
168 public: //  is_derived_() must be public, so that overridden vfuncs and signal handlers can call it via ObjectBase.
169
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.
172
173   //This is commented-out because it's not clear yet whether it's a worthwhile optimization.
174   //
175   /// This is for use by gtkmm wrappers only, and not by applications.
176   //inline bool is_derived_inline_() const
177   //{
178   //  return (custom_type_name_ != 0);
179   //}
180
181 protected:
182   static  void destroy_notify_callback_(void* data);
183   virtual void destroy_notify_();
184
185   void _set_current_wrapper(GObject* object);
186 #endif //DOXYGEN_SHOULD_SKIP_THIS
187
188 private:
189   // noncopyable
190   ObjectBase(const ObjectBase&);
191   ObjectBase& operator=(const ObjectBase&);
192
193 #ifndef DOXYGEN_SHOULD_SKIP_THIS
194   virtual void set_manage(); // calls g_error()
195 #endif //DOXYGEN_SHOULD_SKIP_THIS
196
197 #ifndef DOXYGEN_SHOULD_SKIP_THIS
198   friend class Glib::GSigConnectionNode; // for GSigConnectionNode::notify()
199 #endif
200 };
201
202 #ifndef DOXYGEN_SHOULD_SKIP_THIS
203
204 template <class PropertyType> inline
205 void ObjectBase::set_property(const Glib::ustring& property_name, const PropertyType& value)
206 {
207   Glib::Value<PropertyType> property_value;
208   property_value.init(Glib::Value<PropertyType>::value_type());
209
210   property_value.set(value);
211   this->set_property_value(property_name, property_value);
212 }
213
214 template <class PropertyType> inline
215 void ObjectBase::get_property(const Glib::ustring& property_name, PropertyType& value) const
216 {
217   Glib::Value<PropertyType> property_value;
218   property_value.init(Glib::Value<PropertyType>::value_type());
219
220   this->get_property_value(property_name, property_value);
221
222   value = property_value.get();
223 }
224
225 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
226
227
228 bool _gobject_cppinstance_already_deleted(GObject* gobject);
229
230 } // namespace Glib
231
232
233 #endif /* _GLIBMM_OBJECTBASE_H */
234