2 #ifndef _GLIBMM_OBJECT_H
3 #define _GLIBMM_OBJECT_H
4 /* $Id: object.h 369 2007-01-20 10:19:33Z daniel $ */
6 /* Copyright 2002 The gtkmm Development Team
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.
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.
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.
23 //X11 defines DestroyNotify and some other non-prefixed stuff, and it's too late to change that now,
24 //so let's give people a clue about the compilation errors that they will see:
26 # error "X11/Xlib.h seems to have been included before this header. Due to some commonly-named macros in X11/Xlib.h, it may only be included after any glibmm, gdkmm, or gtkmm headers."
29 #include <glib/gmacros.h> /* for G_GNUC_NULL_TERMINATED */
30 #include <glibmm/objectbase.h>
31 #include <glibmm/wrap.h>
32 #include <glibmm/quark.h>
33 #include <glibmm/refptr.h>
34 #include <glibmm/utility.h> /* Could be private, but that would be tedious. */
35 #include <glibmm/containerhandle_shared.h> /* Because its specializations may be here. */
36 #include <glibmm/value.h>
38 #include <glibmmconfig.h>
40 #ifndef DOXYGEN_SHOULD_SKIP_THIS
43 typedef struct _GObject GObject;
44 typedef struct _GObjectClass GObjectClass;
46 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
52 #ifndef DOXYGEN_SHOULD_SKIP_THIS
56 class GSigConnectionNode;
58 /* ConstructParams::ConstructParams() takes a varargs list of properties
59 * and values, like g_object_new() does. This list will then be converted
60 * to a GParameter array, for use with g_object_newv(). No overhead is
61 * involved, since g_object_new() is just a wrapper around g_object_newv()
64 * The advantage of an auxiliary ConstructParams object over g_object_new()
65 * is that the actual construction is always done in the Glib::Object ctor.
66 * This allows for neat tricks like easy creation of derived custom types,
67 * without adding special support to each ctor of every class.
69 * The comments in object.cc and objectbase.cc should explain in detail
75 const Glib::Class& glibmm_class;
76 unsigned int n_parameters;
77 GParameter* parameters;
79 explicit ConstructParams(const Glib::Class& glibmm_class_);
80 ConstructParams(const Glib::Class& glibmm_class_, const char* first_property_name, ...)
81 G_GNUC_NULL_TERMINATED; // warn if called without a trailing NULL pointer
84 // The copy constructor is semantically required by the C++ compiler
85 // (since g++ 3.4) to be able to create temporary instances, depending
86 // on the usage context. Apparently the compiler will actually optimize
87 // away the copy, though. See bug #132300.
88 ConstructParams(const ConstructParams& other);
92 ConstructParams& operator=(const ConstructParams&);
95 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
98 class GLIBMM_API Object : virtual public ObjectBase
101 #ifndef DOXYGEN_SHOULD_SKIP_THIS
102 typedef Object CppObjectType;
103 typedef Object_Class CppClassType;
104 typedef GObject BaseObjectType;
105 typedef GObjectClass BaseClassType;
106 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
109 Object(); //For use by C++-only sub-types.
110 explicit Object(const Glib::ConstructParams& construct_params);
111 explicit Object(GObject* castitem);
112 virtual ~Object(); //It should only be deleted by the callback.
115 //static RefPtr<Object> create(); //You must reimplement this in each derived class.
117 #ifndef DOXYGEN_SHOULD_SKIP_THIS
118 static GType get_type() G_GNUC_CONST;
119 static GType get_base_type() G_GNUC_CONST;
122 //GObject* gobj_copy(); //Give a ref-ed copy to someone. Use for direct struct access.
124 // Glib::Objects contain a list<Quark, pair<void*, DestroyNotify> >
125 // to store run time data added to the object at run time.
126 //TODO: Use slots instead:
127 void* get_data(const QueryQuark &key);
128 void set_data(const Quark &key, void* data);
129 typedef void (*DestroyNotify) (gpointer data);
130 void set_data(const Quark &key, void* data, DestroyNotify notify);
131 void remove_data(const QueryQuark& quark);
132 // same as remove without notifying
133 void* steal_data(const QueryQuark& quark);
135 // convenience functions
137 //void set_data_typed(const Quark& quark, const T& data)
138 // { set_data(quark, new T(data), delete_typed<T>); }
141 //T& get_data_typed(const QueryQuark& quark)
142 // { return *static_cast<T*>(get_data(quark)); }
144 #ifndef DOXYGEN_SHOULD_SKIP_THIS
147 friend class Glib::Object_Class;
148 static CppClassType object_class_;
151 Object(const Object&);
152 Object& operator=(const Object&);
154 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
156 // Glib::Object can not be dynamic because it lacks a float state.
157 //virtual void set_manage();
161 //For some (proably, more spec-compliant) compilers, these specializations must
162 //be next to the objects that they use.
163 #ifndef GLIBMM_CAN_USE_DYNAMIC_CAST_IN_UNUSED_TEMPLATE_WITHOUT_DEFINITION
164 # ifndef DOXYGEN_SHOULD_SKIP_THIS /* hide the specializations */
166 namespace Container_Helpers
169 /** Partial specialization for pointers to GObject instances.
170 * @ingroup ContHelpers
171 * The C++ type is always a Glib::RefPtr<>.
174 struct TypeTraits< Glib::RefPtr<T> >
176 typedef Glib::RefPtr<T> CppType;
177 typedef typename T::BaseObjectType * CType;
178 typedef typename T::BaseObjectType * CTypeNonConst;
180 static CType to_c_type (const CppType& ptr) { return Glib::unwrap(ptr); }
181 static CType to_c_type (CType ptr) { return ptr; }
182 static CppType to_cpp_type(CType ptr)
184 //return Glib::wrap(ptr, true);
186 //We copy/paste the wrap() implementation here,
187 //because we can not use a specific Glib::wrap(CType) overload here,
188 //because that would be "dependent", and g++ 3.4 does not allow that.
189 //The specific Glib::wrap() overloads don't do anything special anyway.
190 GObject* cobj = (GObject*)const_cast<CTypeNonConst>(ptr);
191 return Glib::RefPtr<T>( dynamic_cast<T*>(Glib::wrap_auto(cobj, true /* take_copy */)) );
192 //We use dynamic_cast<> in case of multiple inheritance.
195 static void release_c_type(CType ptr)
197 GLIBMM_DEBUG_UNREFERENCE(0, ptr);
202 //This confuses the SUN Forte compiler, so we ifdef it out:
203 # ifdef GLIBMM_HAVE_DISAMBIGUOUS_CONST_TEMPLATE_SPECIALIZATIONS
205 /** Partial specialization for pointers to const GObject instances.
206 * @ingroup ContHelpers
207 * The C++ type is always a Glib::RefPtr<>.
210 struct TypeTraits< Glib::RefPtr<const T> >
212 typedef Glib::RefPtr<const T> CppType;
213 typedef const typename T::BaseObjectType * CType;
214 typedef typename T::BaseObjectType * CTypeNonConst;
216 static CType to_c_type (const CppType& ptr) { return Glib::unwrap(ptr); }
217 static CType to_c_type (CType ptr) { return ptr; }
218 static CppType to_cpp_type(CType ptr)
220 //return Glib::wrap(ptr, true);
222 //We copy/paste the wrap() implementation here,
223 //because we can not use a specific Glib::wrap(CType) overload here,
224 //because that would be "dependent", and g++ 3.4 does not allow that.
225 //The specific Glib::wrap() overloads don't do anything special anyway.
226 GObject* cobj = (GObject*)(ptr);
227 return Glib::RefPtr<const T>( dynamic_cast<const T*>(Glib::wrap_auto(cobj, true /* take_copy */)) );
228 //We use dynamic_cast<> in case of multiple inheritance.
231 static void release_c_type (CType ptr)
233 GLIBMM_DEBUG_UNREFERENCE(0, ptr);
234 g_object_unref(const_cast<CTypeNonConst>(ptr));
238 # endif /* GLIBMM_HAVE_DISAMBIGUOUS_CONST_TEMPLATE_SPECIALIZATIONS */
240 } //namespace Container_Helpers
243 template <class T, class PtrT> inline
244 PtrT Value_Pointer<T,PtrT>::get_(Glib::Object*) const
246 return dynamic_cast<T*>(get_object());
250 /** Partial specialization for RefPtr<> to Glib::Object.
251 * @ingroup glibmmValue
254 class Value< Glib::RefPtr<T> > : public ValueBase_Object
257 typedef Glib::RefPtr<T> CppType;
258 typedef typename T::BaseObjectType* CType;
260 static GType value_type() { return T::get_base_type(); }
262 void set(const CppType& data) { set_object(data.operator->()); }
263 CppType get() const { return Glib::RefPtr<T>::cast_dynamic(get_object_copy()); }
266 //The SUN Forte Compiler has a problem with this:
267 # ifdef GLIBMM_HAVE_DISAMBIGUOUS_CONST_TEMPLATE_SPECIALIZATIONS
269 /** Partial specialization for RefPtr<> to const Glib::Object.
270 * @ingroup glibmmValue
273 class Value< Glib::RefPtr<const T> > : public ValueBase_Object
276 typedef Glib::RefPtr<const T> CppType;
277 typedef typename T::BaseObjectType* CType;
279 static GType value_type() { return T::get_base_type(); }
281 void set(const CppType& data) { set_object(const_cast<T*>(data.operator->())); }
282 CppType get() const { return Glib::RefPtr<T>::cast_dynamic(get_object_copy()); }
284 # endif /* GLIBMM_HAVE_DISAMBIGUOUS_CONST_TEMPLATE_SPECIALIZATIONS */
286 # endif /* DOXYGEN_SHOULD_SKIP_THIS */
287 #endif /* GLIBMM_CAN_USE_DYNAMIC_CAST_IN_UNUSED_TEMPLATE_WITHOUT_DEFINITION */
291 #endif /* _GLIBMM_OBJECT_H */