2
#ifndef _GLIBMM_OBJECT_H
3
#define _GLIBMM_OBJECT_H
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
#include <glibmm/objectbase.h>
24
#include <glibmm/wrap.h>
25
#include <glibmm/quark.h>
26
#include <glibmm/refptr.h>
27
#include <glibmm/utility.h> /* Could be private, but that would be tedious. */
28
#include <glibmm/containerhandle_shared.h> //Because its specializations may be here.
29
#include <glibmm/value.h>
31
#ifndef DOXYGEN_SHOULD_SKIP_THIS
34
typedef struct _GObject GObject;
35
typedef struct _GObjectClass GObjectClass;
37
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
43
#ifndef DOXYGEN_SHOULD_SKIP_THIS
47
class GSigConnectionNode;
49
/* ConstructParams::ConstructParams() takes a varargs list of properties
50
* and values, like g_object_new() does. This list will then be converted
51
* to a GParameter array, for use with g_object_newv(). No overhead is
52
* involved, since g_object_new() is just a wrapper around g_object_newv()
55
* The advantage of an auxilary ConstructParams object over g_object_new()
56
* is that the actual construction is always done in the Glib::Object ctor.
57
* This allows for neat tricks like easy creation of derived custom types,
58
* without adding special support to each ctor of every class.
60
* The comments in object.cc and objectbase.cc should explain in detail
66
const Glib::Class& glibmm_class;
67
unsigned int n_parameters;
68
GParameter* parameters;
70
explicit ConstructParams(const Glib::Class& glibmm_class_);
71
ConstructParams(const Glib::Class& glibmm_class_, const char* first_property_name, ...);
74
// This is only used by the C++ compiler (since g++ 3.4) to create temporary instances.
75
// Apparently the compiler will actually optimize away the use of this.
77
ConstructParams(const ConstructParams& other);
81
ConstructParams& operator=(const ConstructParams&);
84
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
87
class Object : virtual public ObjectBase
90
#ifndef DOXYGEN_SHOULD_SKIP_THIS
91
typedef Object CppObjectType;
92
typedef Object_Class CppClassType;
93
typedef GObject BaseObjectType;
94
typedef GObjectClass BaseClassType;
95
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
98
Object(); //For use by C++-only sub-types.
99
explicit Object(const Glib::ConstructParams& construct_params);
100
explicit Object(GObject* castitem);
101
virtual ~Object(); //It should only be deleted by the callback.
104
//static RefPtr<Object> create(); //You must reimplement this in each derived class.
106
#ifndef DOXYGEN_SHOULD_SKIP_THIS
107
static GType get_type() G_GNUC_CONST;
108
static GType get_base_type() G_GNUC_CONST;
111
//GObject* gobj_copy(); //Give a ref-ed copy to someone. Use for direct struct access.
113
// Glib::Objects contain a list<Quark, pair<void*, DestroyNotify> >
114
// to store run time data added to the object at run time.
115
//TODO: Use slots instead:
116
void* get_data(const QueryQuark &key);
117
void set_data(const Quark &key, void* data);
118
typedef void (*DestroyNotify) (gpointer data);
119
void set_data(const Quark &key, void* data, DestroyNotify notify);
120
void remove_data(const QueryQuark& quark);
121
// same as remove without notifying
122
void* steal_data(const QueryQuark& quark);
124
// convenience functions
126
//void set_data_typed(const Quark& quark, const T& data)
127
// { set_data(quark, new T(data), delete_typed<T>); }
130
//T& get_data_typed(const QueryQuark& quark)
131
// { return *static_cast<T*>(get_data(quark)); }
133
#ifndef DOXYGEN_SHOULD_SKIP_THIS
136
friend class Glib::Object_Class;
137
static CppClassType object_class_;
140
Object(const Object&);
141
Object& operator=(const Object&);
143
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
145
// Glib::Object can not be dynamic because it lacks a float state.
146
//virtual void set_manage();
150
//For some (proably, more spec-compliant) compilers, these specializations must
151
//be next to the objects that they use.
152
#ifndef GLIBMM_CAN_USE_DYNAMIC_CAST_IN_UNUSED_TEMPLATE_WITHOUT_DEFINITION
153
#ifndef DOXYGEN_SHOULD_SKIP_THIS /* hide the specializations */
155
namespace Container_Helpers
158
/** Partial specialization for pointers to GObject instances.
159
* @ingroup ContHelpers
160
* The C++ type is always a Glib::RefPtr<>.
163
struct TypeTraits< Glib::RefPtr<T> >
165
typedef Glib::RefPtr<T> CppType;
166
typedef typename T::BaseObjectType * CType;
167
typedef typename T::BaseObjectType * CTypeNonConst;
169
static CType to_c_type (const CppType& ptr) { return Glib::unwrap(ptr); }
170
static CType to_c_type (CType ptr) { return ptr; }
171
static CppType to_cpp_type (CType ptr)
173
//return Glib::wrap(ptr, true);
175
//We copy/paste the wrap() implementation here,
176
//because we can not use a specific Glib::wrap(CType) overload here,
177
//because that would be "dependent", and g++ 3.4 does not allow that.
178
//The specific Glib::wrap() overloads don't do anything special anyway.
179
GObject* cobj = (GObject*)const_cast<CTypeNonConst>(ptr);
180
return Glib::RefPtr<T>( dynamic_cast<T*>(Glib::wrap_auto(cobj, true /* take_copy */)) );
181
//We use dynamic_cast<> in case of multiple inheritance.
184
static void release_c_type (CType ptr)
186
GLIBMM_DEBUG_UNREFERENCE(0, ptr);
191
//This confuse the SUN Forte compiler, so we ifdef it out:
192
#ifdef GLIBMM_HAVE_DISAMBIGUOUS_CONST_TEMPLATE_SPECIALIZATIONS
194
/** Partial specialization for pointers to const GObject instances.
195
* @ingroup ContHelpers
196
* The C++ type is always a Glib::RefPtr<>.
199
struct TypeTraits< Glib::RefPtr<const T> >
201
typedef Glib::RefPtr<const T> CppType;
202
typedef const typename T::BaseObjectType * CType;
203
typedef typename T::BaseObjectType * CTypeNonConst;
205
static CType to_c_type (const CppType& ptr) { return Glib::unwrap(ptr); }
206
static CType to_c_type (CType ptr) { return ptr; }
207
static CppType to_cpp_type (CType ptr)
209
//return Glib::wrap(ptr, true);
211
//We copy/paste the wrap() implementation here,
212
//because we can not use a specific Glib::wrap(CType) overload here,
213
//because that would be "dependent", and g++ 3.4 does not allow that.
214
//The specific Glib::wrap() overloads don't do anything special anyway.
215
GObject* cobj = (GObject*)(ptr);
216
return Glib::RefPtr<const T>( dynamic_cast<const T*>(Glib::wrap_auto(cobj, true /* take_copy */)) );
217
//We use dynamic_cast<> in case of multiple inheritance.
220
static void release_c_type (CType ptr)
222
GLIBMM_DEBUG_UNREFERENCE(0, ptr);
223
g_object_unref(const_cast<CTypeNonConst>(ptr));
227
#endif //GLIBMM_HAVE_DISAMBIGUOUS_CONST_TEMPLATE_SPECIALIZATIONS
229
} //namespace Container_Helpers
232
template <class T, class PtrT> inline
233
PtrT Value_Pointer<T,PtrT>::get_(Glib::Object*) const
235
return dynamic_cast<T*>(get_object());
239
/** Partial specialization for RefPtr<> to Glib::Object.
240
* @ingroup glibmmValue
243
class Value< Glib::RefPtr<T> > : public ValueBase_Object
246
typedef Glib::RefPtr<T> CppType;
247
typedef typename T::BaseObjectType* CType;
249
static GType value_type() { return T::get_base_type(); }
251
void set(const CppType& data) { set_object(data.operator->()); }
252
CppType get() const { return Glib::RefPtr<T>::cast_dynamic(get_object_copy()); }
255
//The SUN Forte Compiler has a problem with this:
256
#ifdef GLIBMM_HAVE_DISAMBIGUOUS_CONST_TEMPLATE_SPECIALIZATIONS
258
/** Partial specialization for RefPtr<> to const Glib::Object.
259
* @ingroup glibmmValue
262
class Value< Glib::RefPtr<const T> > : public ValueBase_Object
265
typedef Glib::RefPtr<const T> CppType;
266
typedef typename T::BaseObjectType* CType;
268
static GType value_type() { return T::get_base_type(); }
270
void set(const CppType& data) { set_object(const_cast<T*>(data.operator->())); }
271
CppType get() const { return Glib::RefPtr<T>::cast_dynamic(get_object_copy()); }
273
#endif //GLIBMM_HAVE_DISAMBIGUOUS_CONST_TEMPLATE_SPECIALIZATIONS
276
#endif //DOXYGEN_SHOULD_SKIP_THIS
277
#endif //GLIBMM_CAN_USE_DYNAMIC_CAST_IN_UNUSED_TEMPLATE_WITHOUT_DEFINITION
281
#endif /* _GLIBMM_OBJECT_H */