summaryrefslogtreecommitdiff
path: root/glib/glibmm/objectbase.cc
blob: f23c497751ae96b3567fac13aec511da2044a340 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/* Copyright 2002 The gtkmm Development Team
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <glib-object.h>

#include <glibmm/quark.h>
#include <glibmm/objectbase.h>
#include <glibmm/propertyproxy_base.h> //For PropertyProxyConnectionNode
#include <glibmm/interface.h>
#include <glibmm/private/interface_p.h>
#include <utility> // For std::move()

namespace
{

// Used by the Glib::ObjectBase default ctor.  Using an explicitly defined
// char array rather than a string literal allows for fast pointer comparison,
// which is otherwise not guaranteed to work.

static const char anonymous_custom_type_name[] = "gtkmm__anonymous_custom_type";

} // anonymous namespace

namespace Glib
{

/**** Glib::ObjectBase *****************************************************/

// Used only during the construction of named custom types.
struct ObjectBase::PrivImpl
{
  // Pointers to the interfaces of custom types.
  Class::interface_classes_type custom_interface_classes;
  // Pointers to extra class init functions.
  Class::class_init_funcs_type custom_class_init_functions;
  // Pointer to the instance init function.
  GInstanceInitFunc custom_instance_init_function = nullptr;
};

ObjectBase::ObjectBase()
: gobject_(nullptr),
  custom_type_name_(anonymous_custom_type_name),
  cpp_destruction_in_progress_(false)
{
}

ObjectBase::ObjectBase(const char* custom_type_name)
: gobject_(nullptr), custom_type_name_(custom_type_name),
  cpp_destruction_in_progress_(false)
{
}

ObjectBase::ObjectBase(const std::type_info& custom_type_info)
: gobject_(nullptr), custom_type_name_(custom_type_info.name()),
  cpp_destruction_in_progress_(false)
{
}

// initialize() actually initializes the wrapper.  Glib::ObjectBase is used
// as virtual base class, which means the most-derived class' ctor invokes
// the Glib::ObjectBase ctor -- thus it's useless for Glib::Object.
//
void
ObjectBase::initialize(GObject* castitem)
{
  if (gobject_)
  {
    // initialize() might be called twice when used with MI, e.g. by the ctors
    // of Glib::Object and Glib::Interface.  However, they must both refer to
    // the same underlying GObject instance.
    //
    g_assert(gobject_ == castitem);

    // TODO: Think about it.  Will this really be called twice?
    g_printerr("ObjectBase::initialize() called twice for the same GObject\n");

    return; // Don't initialize the wrapper twice.
  }

  gobject_ = castitem;
  _set_current_wrapper(castitem);
}

void
ObjectBase::initialize_move(GObject* castitem, Glib::ObjectBase* previous_wrapper)
{
  if (gobject_)
  {
    g_assert(gobject_ == castitem);

    // TODO: Think about it.  Will this really be called twice?
    g_printerr("ObjectBase::initialize_move() called twice for the same GObject\n");

    return; // Don't initialize the wrapper twice.
  }

  gobject_ = castitem;
  _move_current_wrapper(castitem, previous_wrapper);
  custom_type_name_ = previous_wrapper->custom_type_name_;
  cpp_destruction_in_progress_ = previous_wrapper->cpp_destruction_in_progress_;

  // Clear the previous wrapper:
  previous_wrapper->custom_type_name_ = nullptr;
  previous_wrapper->cpp_destruction_in_progress_ = false;
}

ObjectBase::ObjectBase(ObjectBase&& src) noexcept
  : sigc::trackable(std::move(src)), // not actually called because it's a virtual base
    gobject_(nullptr),
    custom_type_name_(src.custom_type_name_),
    cpp_destruction_in_progress_(src.cpp_destruction_in_progress_)
{
}

ObjectBase&
ObjectBase::operator=(ObjectBase&& src) noexcept
{
  if (this == &src)
    return *this;

  sigc::trackable::operator=(std::move(src));

  if (gobject_)
  {
    // Remove the wrapper, without invoking destroy_notify_callback_():
    g_object_steal_qdata(gobject_, Glib::quark_);
    // Remove a reference, without deleting *this.
    unreference();
    gobject_ = nullptr;
  }
  initialize_move(src.gobject_, &src);

  return *this;
}

ObjectBase::~ObjectBase() noexcept
{
  // Normally, gobject_ should always be 0 at this point, because:
  //
  // a) Gtk::Object handles memory management on its own and always resets
  //    the gobject_ pointer in its destructor.
  //
  // b) Glib::Object instances that aren't Gtk::Objects will always be
  //    deleted by the destroy_notify_() virtual method.  Calling delete
  //    on a Glib::Object is a programming error.
  //
  // The *only* situation where gobject_ is validly not 0 at this point
  // happens if a derived class's ctor throws an exception.  In that case
  // we have to call g_object_unref() on our own.
  //

  if (GObject* const gobject = gobject_)
  {
#ifdef GLIBMM_DEBUG_REFCOUNTING
    g_warning("(Glib::ObjectBase::~ObjectBase): gobject_ = %p", (void*)gobject_);
#endif

    gobject_ = nullptr;

#ifdef GLIBMM_DEBUG_REFCOUNTING
    g_warning("(Glib::ObjectBase::~ObjectBase): before g_object_steal_qdata()");
#endif

    // Remove the pointer to the wrapper from the underlying instance.
    // This does _not_ cause invocation of the destroy_notify callback.
    g_object_steal_qdata(gobject, quark_);

#ifdef GLIBMM_DEBUG_REFCOUNTING
    g_warning("(Glib::ObjectBase::~ObjectBase): calling g_object_unref()");
#endif

    g_object_unref(gobject);
  }
}

void
ObjectBase::reference() const
{
  GLIBMM_DEBUG_REFERENCE(this, gobject_);
  g_object_ref(gobject_);
}

void
ObjectBase::unreference() const
{
  GLIBMM_DEBUG_UNREFERENCE(this, gobject_);
  g_object_unref(gobject_);
}

GObject*
ObjectBase::gobj_copy() const
{
  reference();
  return gobject_;
}

void
ObjectBase::_set_current_wrapper(GObject* object)
{
  // Store a pointer to this wrapper in the underlying instance, so that we
  // never create a second wrapper for the same underlying instance.  Also,
  // specify a callback that will tell us when it's time to delete this C++
  // wrapper instance:

  if (object)
  {
    if (!g_object_get_qdata(object, Glib::quark_))
    {
      g_object_set_qdata_full(object, Glib::quark_, this, &destroy_notify_callback_);
    }
    else
    {
      g_warning("This object, of type %s, already has a wrapper.\n"
                "You should use wrap() instead of a constructor.",
        G_OBJECT_TYPE_NAME(object));
    }
  }
}

void
ObjectBase::_move_current_wrapper(GObject* object, Glib::ObjectBase* previous_wrapper) noexcept
{
  // See _set_current_wrapper().
  ObjectBase* current_wrapper = _get_current_wrapper(object);
  if (current_wrapper != previous_wrapper)
  {
    g_warning("%s: Unexpected previous wrapper, for object of type %s.\n"
              "previous_wrapper=%p, current_wrapper=%p",
      G_STRFUNC, G_OBJECT_TYPE_NAME(object), static_cast<void*>(previous_wrapper),
      static_cast<void*>(current_wrapper));
  }

  // Remove the previous wrapper, without invoking destroy_notify_callback_():
  g_object_steal_qdata(object, Glib::quark_);

  // Set the new wrapper:
  g_object_set_qdata_full(object, Glib::quark_, this, &destroy_notify_callback_);

  // Clear the previous wrapper:
  previous_wrapper->gobject_ = nullptr;
}

// static
ObjectBase*
ObjectBase::_get_current_wrapper(GObject* object)
{
  if (object)
    return static_cast<ObjectBase*>(g_object_get_qdata(object, Glib::quark_));
  else
    return nullptr;
}

// static
void
ObjectBase::destroy_notify_callback_(void* data)
{
  // GLIBMM_LIFECYCLE

  // This method is called (indirectly) from g_object_run_dispose().
  // Get the C++ instance associated with the C instance:
  ObjectBase* cppObject =
    static_cast<ObjectBase*>(data); // Previously set with g_object_set_qdata_full().

#ifdef GLIBMM_DEBUG_REFCOUNTING
  g_warning("ObjectBase::destroy_notify_callback_: cppObject = %p, gobject_ = %p, gtypename = %s\n",
    (void*)cppObject, (void*)cppObject->gobject_, G_OBJECT_TYPE_NAME(cppObject->gobject_));
#endif

  if (cppObject) // This will be nullptr if the C++ destructor has already run.
  {
    cppObject->destroy_notify_(); // Virtual - it does different things for Glib::ObjectBase and Gtk::Object.
  }
}

void
ObjectBase::destroy_notify_()
{
// The C instance is about to be disposed, making it unusable.  Now is a
// good time to delete the C++ wrapper of the C instance.  There is no way
// to force the disposal of the GObject (though Gtk::Object::destroy_notify_()
// can call g_object_run_dispose()), so this is the *only* place where we delete
// the C++ wrapper.
//
// This will only happen after the last unreference(), which will be done by
// the RefPtr<> destructor.  There should be no way to access the wrapper or
// the underlying object instance after that, so it's OK to delete this.

#ifdef GLIBMM_DEBUG_REFCOUNTING
  g_warning("Glib::ObjectBase::destroy_notify_: gobject_ = %p", (void*)gobject_);
#endif

  gobject_ = nullptr; // Make sure we don't unref it again in the dtor.

  delete this;
}

bool
ObjectBase::is_anonymous_custom_() const
{
  // Doing high-speed pointer comparison is OK here.
  return (custom_type_name_ == anonymous_custom_type_name);
}

bool
ObjectBase::is_derived_() const
{
  // gtkmmproc-generated classes initialize this to 0 by default.
  return (custom_type_name_ != nullptr);
}

void
ObjectBase::set_manage()
{
  // This is a private method and Gtk::manage() is a template function.
  // Thus this will probably never run, unless you do something like:
  //
  // manage(static_cast<Gtk::Object*>(refptr.operator->()));

  g_error("Glib::ObjectBase::set_manage(): "
          "only Gtk::Object instances can be managed");
}

bool
ObjectBase::_cpp_destruction_is_in_progress() const
{
  return cpp_destruction_in_progress_;
}

void
ObjectBase::set_property_value(const Glib::ustring& property_name, const Glib::ValueBase& value)
{
  g_object_set_property(gobj(), property_name.c_str(), value.gobj());
}

void
ObjectBase::get_property_value(const Glib::ustring& property_name, Glib::ValueBase& value) const
{
  g_object_get_property(const_cast<GObject*>(gobj()), property_name.c_str(), value.gobj());
}

sigc::connection
ObjectBase::connect_property_changed(
  const Glib::ustring& property_name, const sigc::slot<void()>& slot)
{
  // Create a proxy to hold our connection info
  // This will be deleted by destroy_notify_handler.
  auto pConnectionNode = new PropertyProxyConnectionNode(slot, gobj());

  // connect it to glib
  // pConnectionNode will be passed as the data argument to the callback.
  return pConnectionNode->connect_changed(property_name);
}

sigc::connection
ObjectBase::connect_property_changed(
  const Glib::ustring& property_name, sigc::slot<void()>&& slot)
{
  // Create a proxy to hold our connection info
  // This will be deleted by destroy_notify_handler.
  auto pConnectionNode = new PropertyProxyConnectionNode(std::move(slot), gobj());

  // connect it to glib
  // pConnectionNode will be passed as the data argument to the callback.
  return pConnectionNode->connect_changed(property_name);
}

void
ObjectBase::freeze_notify()
{
  g_object_freeze_notify(gobj());
}

void
ObjectBase::thaw_notify()
{
  g_object_thaw_notify(gobj());
}

GType ObjectBase::get_base_type()
{
  return G_TYPE_OBJECT;
}

void ObjectBase::add_custom_interface_class(const Interface_Class* iface_class)
{
  if (!priv_pimpl_)
    priv_pimpl_ = std::make_unique<PrivImpl>();
  priv_pimpl_->custom_interface_classes.emplace_back(iface_class);
}

void ObjectBase::add_custom_class_init_function(GClassInitFunc class_init_func, void* class_data)
{
  if (!priv_pimpl_)
    priv_pimpl_ = std::make_unique<PrivImpl>();
  priv_pimpl_->custom_class_init_functions.emplace_back(
    std::make_tuple(class_init_func, class_data));
}

void ObjectBase::set_custom_instance_init_function(GInstanceInitFunc instance_init_func)
{
  if (!priv_pimpl_)
    priv_pimpl_ = std::make_unique<PrivImpl>();
  priv_pimpl_->custom_instance_init_function = instance_init_func;
}

const Class::interface_classes_type* ObjectBase::get_custom_interface_classes() const
{
  return priv_pimpl_ ? &priv_pimpl_->custom_interface_classes : nullptr;
}

const Class::class_init_funcs_type* ObjectBase::get_custom_class_init_functions() const
{
  return priv_pimpl_ ? &priv_pimpl_->custom_class_init_functions : nullptr;
}

GInstanceInitFunc ObjectBase::get_custom_instance_init_function() const
{
  return priv_pimpl_ ? priv_pimpl_->custom_instance_init_function : nullptr;
}

void ObjectBase::custom_class_init_finished()
{
  priv_pimpl_.reset();
}

/**** Global function *****************************************************/
bool
_gobject_cppinstance_already_deleted(GObject* gobject)
{
  // This function is used to prevent calling wrap() on a GTK+ instance whose gtkmm instance has
  // been deleted.

  if (gobject)
    return (bool)g_object_get_qdata(
      gobject, Glib::quark_cpp_wrapper_deleted_); // true means that something is odd.
  else
    return false; // Nothing is particularly wrong.
}

} // namespace Glib