@@ -413,7 +413,7 @@ namespace geode {
413
413
* to it is freed or locked
414
414
* @param obj Object to construct the WeakRef from
415
415
*/
416
- WeakRef (T* obj) : m_controller(WeakRefPool::get()->manage (obj)) {}
416
+ WeakRef (T* obj) : m_controller(obj ? WeakRefPool::get()->manage (obj) : nullptr ) {}
417
417
418
418
WeakRef (WeakRef<T> const & other) : WeakRef(other.m_controller) {}
419
419
@@ -437,7 +437,7 @@ namespace geode {
437
437
* a null Ref if the object has been freed
438
438
*/
439
439
Ref<T> lock () const {
440
- if (m_controller->isManaged ()) {
440
+ if (m_controller && m_controller ->isManaged ()) {
441
441
return Ref (static_cast <T*>(m_controller->get ()));
442
442
}
443
443
return Ref<T>(nullptr );
@@ -447,7 +447,7 @@ namespace geode {
447
447
* Check if the WeakRef points to a valid object
448
448
*/
449
449
bool valid () const {
450
- return m_controller->isManaged ();
450
+ return m_controller && m_controller ->isManaged ();
451
451
}
452
452
453
453
/* *
@@ -456,7 +456,13 @@ namespace geode {
456
456
* @param other The new object to swap to
457
457
*/
458
458
void swap (T* other) {
459
- m_controller->swap (other);
459
+ if (m_controller) {
460
+ m_controller->swap (other);
461
+ } else if (other) {
462
+ m_controller = WeakRefPool::get ()->manage (other);
463
+ } else {
464
+ m_controller = nullptr ;
465
+ }
460
466
}
461
467
462
468
Ref<T> operator =(T* obj) {
@@ -465,7 +471,7 @@ namespace geode {
465
471
}
466
472
467
473
WeakRef<T>& operator =(WeakRef<T> const & other) {
468
- this ->swap (static_cast <T*>(other.m_controller ->get ()));
474
+ this ->swap (static_cast <T*>(other.m_controller ? other. m_controller ->get () : nullptr ));
469
475
return *this ;
470
476
}
471
477
@@ -479,33 +485,40 @@ namespace geode {
479
485
}
480
486
481
487
bool operator ==(T* other) const {
482
- return m_controller->get () == other;
488
+ return ( m_controller && m_controller ->get () == other) || (!m_controller && !other) ;
483
489
}
484
490
485
491
bool operator ==(WeakRef<T> const & other) const {
492
+ if (!m_controller && !other.m_controller ) return true ;
493
+ if (!m_controller || !other.m_controller ) return false ;
494
+
486
495
return m_controller->get () == other.m_controller ->get ();
487
496
}
488
497
489
498
bool operator !=(T* other) const {
490
- return m_controller-> get () != other;
499
+ return !(* this == other) ;
491
500
}
492
501
493
502
bool operator !=(WeakRef<T> const & other) const {
494
- return m_controller-> get () != other. m_controller -> get ( );
503
+ return !(* this == other);
495
504
}
496
505
497
506
// for containers
498
507
bool operator <(WeakRef<T> const & other) const {
508
+ if (!m_controller && !other.m_controller ) return false ;
509
+ if (!m_controller) return true ;
510
+ if (!other.m_controller ) return false ;
511
+
499
512
return m_controller->get () < other.m_controller ->get ();
500
513
}
501
514
bool operator <=(WeakRef<T> const & other) const {
502
- return m_controller-> get () <= other. m_controller -> get ( );
515
+ return !(* this > other);
503
516
}
504
517
bool operator >(WeakRef<T> const & other) const {
505
- return m_controller-> get () > other. m_controller -> get () ;
518
+ return other < * this ;
506
519
}
507
520
bool operator >=(WeakRef<T> const & other) const {
508
- return m_controller-> get () >= other. m_controller -> get ( );
521
+ return !(* this < other);
509
522
}
510
523
};
511
524
0 commit comments