summaryrefslogtreecommitdiff
path: root/glib/glibmm/weakref.h
blob: b03c3273259bd74f058bfc4f3709d66f3d93ab02 (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
454
455
456
457
458
459
460
461
462
463
#ifndef _GLIBMM_WEAKREF_H
#define _GLIBMM_WEAKREF_H

/* Copyright (C) 2015 The glibmm 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/refptr.h>
#include <glibmm/objectbase.h>
#include <type_traits> // std::is_base_of<>
#include <utility> // std::swap<>, std::forward<>

namespace Glib
{

/** WeakRef<> is a weak reference smartpointer.
 *
 * WeakRef can store a pointer to any class that is derived from Glib::ObjectBase,
 * and whose reference() method is noexcept.
 * In glibmm and gtkmm, that is anything derived from Glib::ObjectBase.
 *
 * Unlike a RefPtr, a WeakRef does not contribute to the reference counting of
 * the underlying object.
 *
 * @newin{2,46}
 */
template <typename T_CppObject>
class WeakRef
{
  static_assert(std::is_base_of<Glib::ObjectBase, T_CppObject>::value,
    "Glib::WeakRef can be used only for classes derived from Glib::ObjectBase.");

public:
  /** Default constructor.
   *
   * Create an empty weak reference.
   */
  inline WeakRef() noexcept;

  /// Copy constructor.
  inline WeakRef(const WeakRef& src) noexcept;

  /// Move constructor.
  inline WeakRef(WeakRef&& src) noexcept;

  /// Copy constructor from different, but castable type.
  template <typename T_CastFrom>
  inline WeakRef(const WeakRef<T_CastFrom>& src) noexcept;

  /// Move constructor from different, but castable type.
  template <typename T_CastFrom>
  inline WeakRef(WeakRef<T_CastFrom>&& src) noexcept;

  /** Constructor from a RefPtr of the same or a castable type.
   *
   * Create a weak reference from a RefPtr of the same or a castable type.
   * If the RefPtr references nothing, an empty weak reference will be constructed.
   */
  template <typename T_CastFrom>
  inline WeakRef(const RefPtr<T_CastFrom>& src) noexcept;

  /// Destructor.
  inline ~WeakRef() noexcept;

  /// Swap the contents of two WeakRef<>.
  inline void swap(WeakRef& other) noexcept;

  /// Copy assignment operator.
  inline WeakRef& operator=(const WeakRef& src) noexcept;

  /// Move assignment operator.
  inline WeakRef& operator=(WeakRef&& src) noexcept;

  /// Copy assignment from different, but castable type.
  template <typename T_CastFrom>
  inline WeakRef& operator=(const WeakRef<T_CastFrom>& src) noexcept;

  /// Move assignment from different, but castable type.
  template <typename T_CastFrom>
  inline WeakRef& operator=(WeakRef<T_CastFrom>&& src) noexcept;

  /// Assignment from a RefPtr of the same or a castable type.
  template <typename T_CastFrom>
  inline WeakRef& operator=(const RefPtr<T_CastFrom>& src) noexcept;

  /** Test whether the WeakRef<> points to any underlying instance.
   *
   * Mimics usage of ordinary pointers:
   * @code
   * if (ptr)
   *   do_something();
   * @endcode
   *
   * In a multi-threaded program a <tt>true</tt> return value can become
   * obsolete at any time, even before the caller has a chance to test it,
   * because the underlying instance may lose its last reference in another
   * thread. Use get() if this is not acceptable.
   */
  inline explicit operator bool() const noexcept;

  /** Create a strong reference to the underlying object.
   *
   * This is a thread-safe way to acquire a strong reference to the underlying
   * object. If the WeakRef is empty, the returned RefPtr will reference nothing.
   */
  inline RefPtr<T_CppObject> get() const noexcept;

  /// Make this WeakRef empty.
  inline void reset() noexcept;

  /** Dynamic cast to derived class.
   *
   * The WeakRef can't be cast with the usual notation so instead you can use
   * @code
   * ptr_derived = Glib::WeakRef<Derived>::cast_dynamic(ptr_base);
   * @endcode
   */
  template <typename T_CastFrom>
  static inline WeakRef cast_dynamic(const WeakRef<T_CastFrom>& src) noexcept;

  /** Static cast to derived class.
   *
   * The WeakRef can't be cast with the usual notation so instead you can use
   * @code
   * ptr_derived = Glib::WeakRef<Derived>::cast_static(ptr_base);
   * @endcode
   */
  template <typename T_CastFrom>
  static inline WeakRef cast_static(const WeakRef<T_CastFrom>& src) noexcept;

  /** Cast to non-const.
   *
   * The WeakRef can't be cast with the usual notation so instead you can use
   * @code
   * ptr_nonconst = Glib::WeakRef<NonConstType>::cast_const(ptr_const);
   * @endcode
   */
  template <typename T_CastFrom>
  static inline WeakRef cast_const(const WeakRef<T_CastFrom>& src) noexcept;

private:
  // Let all instantiations of WeakRef access private data.
  template <typename T_CastFrom>
  friend class WeakRef;

  // If pCppObject != nullptr && gobject == nullptr,
  // then the caller holds a strong reference.
  void set(T_CppObject* pCppObject, GWeakRef* gobject) noexcept;

  // WeakRef owns *gobject_, but it does not own *pCppObject_.
  // Invariant: (!pCppObject_ || gobject_),
  // i.e. if pCppObject_ != nullptr then also gobject_ != nullptr.
  T_CppObject* pCppObject_;
  GWeakRef* gobject_;

  // Some methods would be simpler if gobject_ were a GWeakRef instead of
  // a GWeakRef*, but then the move constructor and the move assignment
  // operation would not be efficient.

}; // end class WeakRef

#ifndef DOXYGEN_SHOULD_SKIP_THIS

template <typename T_CppObject>
WeakRef<T_CppObject>::WeakRef() noexcept : pCppObject_(nullptr), gobject_(nullptr)
{
}

template <typename T_CppObject>
WeakRef<T_CppObject>::WeakRef(const WeakRef& src) noexcept : pCppObject_(src.pCppObject_),
                                                             gobject_(nullptr)
{
  if (pCppObject_)
  {
    // We must own a strong reference to the underlying GObject while
    // calling g_weak_ref_init().
    gpointer ptr = g_weak_ref_get(src.gobject_);
    if (ptr)
    {
      gobject_ = new GWeakRef;
      g_weak_ref_init(gobject_, pCppObject_->gobj());
      g_object_unref(ptr);
    }
    else
      pCppObject_ = nullptr;
  }
}

template <typename T_CppObject>
WeakRef<T_CppObject>::WeakRef(WeakRef&& src) noexcept : pCppObject_(src.pCppObject_),
                                                        gobject_(src.gobject_)
{
  src.pCppObject_ = nullptr;
  src.gobject_ = nullptr;
}

// The templated ctor allows copy construction from any object that's
// castable. Thus, it does downcasts:
//   base_ref = derived_ref
template <typename T_CppObject>
template <typename T_CastFrom>
WeakRef<T_CppObject>::WeakRef(const WeakRef<T_CastFrom>& src) noexcept
  : pCppObject_(src.pCppObject_),
    gobject_(nullptr)
{
  if (pCppObject_)
  {
    // We must own a strong reference to the underlying GObject while
    // calling g_weak_ref_init().
    gpointer ptr = g_weak_ref_get(src.gobject_);
    if (ptr)
    {
      gobject_ = new GWeakRef;
      g_weak_ref_init(gobject_, pCppObject_->gobj());
      g_object_unref(ptr);
    }
    else
      pCppObject_ = nullptr;
  }
}

// The templated ctor allows move construction from any object that's
// castable. Thus, it does downcasts:
//   base_ref = std::move(derived_ref)
template <typename T_CppObject>
template <typename T_CastFrom>
WeakRef<T_CppObject>::WeakRef(WeakRef<T_CastFrom>&& src) noexcept : pCppObject_(src.pCppObject_),
                                                                    gobject_(src.gobject_)
{
  src.pCppObject_ = nullptr;
  src.gobject_ = nullptr;
}

template <typename T_CppObject>
template <typename T_CastFrom>
WeakRef<T_CppObject>::WeakRef(const RefPtr<T_CastFrom>& src) noexcept
  : pCppObject_(src.get()),
    gobject_(nullptr)
{
  if (pCppObject_)
  {
    gobject_ = new GWeakRef;
    g_weak_ref_init(gobject_, pCppObject_->gobj());
  }
}

template <typename T_CppObject>
WeakRef<T_CppObject>::~WeakRef() noexcept
{
  if (gobject_)
  {
    g_weak_ref_clear(gobject_);
    delete gobject_;
  }
}

template <class T_CppObject>
void
WeakRef<T_CppObject>::swap(WeakRef& other) noexcept
{
  std::swap(pCppObject_, other.pCppObject_);
  std::swap(gobject_, other.gobject_);
}

template <typename T_CppObject>
WeakRef<T_CppObject>&
WeakRef<T_CppObject>::operator=(const WeakRef& src) noexcept
{
  set(src.pCppObject_, src.gobject_);
  return *this;
}

template <typename T_CppObject>
WeakRef<T_CppObject>&
WeakRef<T_CppObject>::operator=(WeakRef&& src) noexcept
{
  // This technique is inefficient for copy assignment of WeakRef,
  // because it involves copy construction + destruction, i.e. in a typical
  // case g_weak_ref_init() + g_weak_ref_clear(), when a g_weak_ref_set()
  // would be enough. For move assignment, the swap technique is fine.
  WeakRef<T_CppObject> temp(std::forward<WeakRef<T_CppObject>>(src));
  this->swap(temp);
  return *this;
}

template <typename T_CppObject>
template <typename T_CastFrom>
WeakRef<T_CppObject>&
WeakRef<T_CppObject>::operator=(const WeakRef<T_CastFrom>& src) noexcept
{
  set(src.pCppObject_, src.gobject_);
  return *this;
}

template <typename T_CppObject>
template <typename T_CastFrom>
WeakRef<T_CppObject>&
WeakRef<T_CppObject>::operator=(WeakRef<T_CastFrom>&& src) noexcept
{
  WeakRef<T_CppObject> temp(std::forward<WeakRef<T_CastFrom>>(src));
  this->swap(temp);
  return *this;
}

template <typename T_CppObject>
template <typename T_CastFrom>
WeakRef<T_CppObject>&
WeakRef<T_CppObject>::operator=(const RefPtr<T_CastFrom>& src) noexcept
{
  T_CppObject* pCppObject = src.get();
  set(pCppObject, nullptr);
  return *this;
}

template <class T_CppObject>
WeakRef<T_CppObject>::operator bool() const noexcept
{
  if (!pCppObject_)
    return false;

  gpointer ptr = g_weak_ref_get(gobject_);
  if (!ptr)
    return false;

  g_object_unref(ptr);
  return true;
}

template <typename T_CppObject>
RefPtr<T_CppObject>
WeakRef<T_CppObject>::get() const noexcept
{
  RefPtr<T_CppObject> ret;

  if (!pCppObject_)
    return ret;

  gpointer ptr = g_weak_ref_get(gobject_);
  if (!ptr)
    return ret;

  // A RefPtr constructed from pointer expects reference to be done externally.
  pCppObject_->reference();
  ret = make_refptr_for_instance<T_CppObject>(pCppObject_);

  g_object_unref(ptr);

  return ret;
}

template <typename T_CppObject>
void
WeakRef<T_CppObject>::reset() noexcept
{
  set(nullptr, nullptr);
}

template <typename T_CppObject>
template <typename T_CastFrom>
WeakRef<T_CppObject>
WeakRef<T_CppObject>::cast_dynamic(const WeakRef<T_CastFrom>& src) noexcept
{
  WeakRef<T_CppObject> ret;

  if (!src.pCppObject_)
    return ret;

  gpointer ptr = g_weak_ref_get(src.gobject_);
  if (!ptr)
    return ret;

  // Don't call dynamic_cast<>() unless we know that the referenced object
  // still exists.
  T_CppObject* const pCppObject = dynamic_cast<T_CppObject*>(src.pCppObject_);
  ret.set(pCppObject, nullptr);
  g_object_unref(ptr);

  return ret;
}

template <typename T_CppObject>
template <typename T_CastFrom>
WeakRef<T_CppObject>
WeakRef<T_CppObject>::cast_static(const WeakRef<T_CastFrom>& src) noexcept
{
  T_CppObject* const pCppObject = static_cast<T_CppObject*>(src.pCppObject_);

  WeakRef<T_CppObject> ret;
  ret.set(pCppObject, src.gobject_);
  return ret;
}

template <typename T_CppObject>
template <typename T_CastFrom>
WeakRef<T_CppObject>
WeakRef<T_CppObject>::cast_const(const WeakRef<T_CastFrom>& src) noexcept
{
  T_CppObject* const pCppObject = const_cast<T_CppObject*>(src.pCppObject_);

  WeakRef<T_CppObject> ret;
  ret.set(pCppObject, src.gobject_);
  return ret;
}

template <typename T_CppObject>
void
WeakRef<T_CppObject>::set(T_CppObject* pCppObject, GWeakRef* gobject) noexcept
{
  // We must own a strong reference to the underlying GObject while
  // calling g_weak_ref_init() or g_weak_ref_set().
  // If pCppObject != nullptr && gobject == nullptr,
  // then the caller holds a strong reference.

  // An aim with this moderately complicated method is to keep the same
  // GWeakRef, calling g_weak_ref_set() when possible, instead of using swap(),
  // which implies creating a new WeakRef, swapping with *this, and deleting
  // the new WeakRef.

  gpointer ptr = nullptr;
  if (pCppObject && gobject)
    ptr = g_weak_ref_get(gobject);

  pCppObject_ = (ptr || !gobject) ? pCppObject : nullptr;
  if (pCppObject_ && !gobject_)
  {
    gobject_ = new GWeakRef;
    g_weak_ref_init(gobject_, pCppObject_->gobj());
  }
  else if (gobject_)
    g_weak_ref_set(gobject_, pCppObject_ ? pCppObject_->gobj() : nullptr);

  if (ptr)
    g_object_unref(ptr);
}

#endif // DOXYGEN_SHOULD_SKIP_THIS

/** Swap the contents of two WeakRef<>.
 * @relates Glib::WeakRef
 */
template <class T_CppObject>
inline void
swap(WeakRef<T_CppObject>& lhs, WeakRef<T_CppObject>& rhs) noexcept
{
  lhs.swap(rhs);
}

} // namespace Glib

#endif // _GLIBMM_WEAKREF_H