[libgee] Improve the read-only view property by using weak pointers
- From: Didier 'Ptitjes' Villevalois <dvillevalois src gnome org>
- To: svn-commits-list gnome org
- Cc:
- Subject: [libgee] Improve the read-only view property by using weak pointers
- Date: Fri, 11 Sep 2009 19:13:36 +0000 (UTC)
commit 75b5e11db8559694869bb97e0042f5342282d692
Author: Julien Peeters <contact julienpeeters fr>
Date: Thu Sep 10 18:15:13 2009 +0200
Improve the read-only view property by using weak pointers
Fixes bug 594758.
By using weak pointers the read-only view is destroyed when there is no
references to it anymore.
gee/abstractcollection.vala | 11 +++++++----
gee/abstractlist.vala | 11 ++++++++---
gee/abstractmap.vala | 11 +++++++----
gee/abstractset.vala | 11 ++++++++---
gee/collection.vala | 2 +-
gee/list.vala | 2 +-
gee/map.vala | 2 +-
gee/readonlycollection.vala | 2 +-
gee/readonlylist.vala | 2 +-
gee/readonlymap.vala | 2 +-
gee/readonlyset.vala | 2 +-
gee/set.vala | 2 +-
tests/testreadonlycollection.vala | 10 ++++++++++
13 files changed, 48 insertions(+), 22 deletions(-)
---
diff --git a/gee/abstractcollection.vala b/gee/abstractcollection.vala
index 2acf92c..2ff78f1 100644
--- a/gee/abstractcollection.vala
+++ b/gee/abstractcollection.vala
@@ -143,17 +143,20 @@ public abstract class Gee.AbstractCollection<G> : Object, Iterable<G>, Collectio
*/
public abstract Iterator<G> iterator ();
- protected Collection<G> _read_only_view;
+ private weak Collection<G> _read_only_view;
/**
* @inheritDoc
*/
public virtual Collection<G> read_only_view {
- get {
+ owned get {
+ Collection<G> instance = _read_only_view;
if (_read_only_view == null) {
- _read_only_view = new ReadOnlyCollection<G> (this);
+ instance = new ReadOnlyCollection<G> (this);
+ _read_only_view = instance;
+ instance.add_weak_pointer (&_read_only_view);
}
- return _read_only_view;
+ return instance;
}
}
}
diff --git a/gee/abstractlist.vala b/gee/abstractlist.vala
index dc57958..da70499 100644
--- a/gee/abstractlist.vala
+++ b/gee/abstractlist.vala
@@ -95,15 +95,20 @@ public abstract class Gee.AbstractList<G> : Gee.AbstractCollection<G>, List<G> {
TimSort<G>.sort (this, compare);
}
+ private weak List<G> _read_only_view;
+
/**
* @inheritDoc
*/
public virtual new List<G> read_only_view {
- get {
+ owned get {
+ List<G> instance = _read_only_view;
if (_read_only_view == null) {
- _read_only_view = new ReadOnlyList<G> (this);
+ instance = new ReadOnlyList<G> (this);
+ _read_only_view = instance;
+ instance.add_weak_pointer (&_read_only_view);
}
- return _read_only_view as List<G>;
+ return instance;
}
}
}
diff --git a/gee/abstractmap.vala b/gee/abstractmap.vala
index 0666fbe..25f702c 100644
--- a/gee/abstractmap.vala
+++ b/gee/abstractmap.vala
@@ -111,17 +111,20 @@ public abstract class Gee.AbstractMap<K,V> : Object, Map<K,V> {
return true;
}
- protected Map<K,V> _read_only_view;
+ private weak Map<K,V> _read_only_view;
/**
* @inheritDoc
*/
public virtual Map<K,V> read_only_view {
- get {
+ owned get {
+ Map<K,V> instance = _read_only_view;
if (_read_only_view == null) {
- _read_only_view = new ReadOnlyMap<K,V> (this);
+ instance = new ReadOnlyMap<K,V> (this);
+ _read_only_view = instance;
+ instance.add_weak_pointer (&_read_only_view);
}
- return _read_only_view;
+ return instance;
}
}
diff --git a/gee/abstractset.vala b/gee/abstractset.vala
index a9177ec..850257e 100644
--- a/gee/abstractset.vala
+++ b/gee/abstractset.vala
@@ -31,15 +31,20 @@
*/
public abstract class Gee.AbstractSet<G> : Gee.AbstractCollection<G>, Set<G> {
+ private weak Set<G> _read_only_view;
+
/**
* @inheritDoc
*/
public virtual new Set<G> read_only_view {
- get {
+ owned get {
+ Set<G> instance = _read_only_view;
if (_read_only_view == null) {
- _read_only_view = new ReadOnlySet<G> (this);
+ instance = new ReadOnlySet<G> (this);
+ _read_only_view = instance;
+ instance.add_weak_pointer (&_read_only_view);
}
- return _read_only_view as Set<G>;
+ return instance;
}
}
}
diff --git a/gee/collection.vala b/gee/collection.vala
index b506e6a..d70da7a 100644
--- a/gee/collection.vala
+++ b/gee/collection.vala
@@ -125,6 +125,6 @@ public interface Gee.Collection<G> : Iterable<G> {
/**
* Property giving access to the read-only view of this collection.
*/
- public abstract Collection<G> read_only_view { get; }
+ public abstract Collection<G> read_only_view { owned get; }
}
diff --git a/gee/list.vala b/gee/list.vala
index 4c00223..29f1734 100644
--- a/gee/list.vala
+++ b/gee/list.vala
@@ -109,6 +109,6 @@ public interface Gee.List<G> : Collection<G> {
/**
* Property giving access to the read-only view of this list.
*/
- public abstract new List<G> read_only_view { get; }
+ public abstract new List<G> read_only_view { owned get; }
}
diff --git a/gee/map.vala b/gee/map.vala
index 1e53198..40d25f3 100644
--- a/gee/map.vala
+++ b/gee/map.vala
@@ -116,6 +116,6 @@ public interface Gee.Map<K,V> : GLib.Object {
/**
* Property giving access to the read-only view this map.
*/
- public abstract Map<K,V> read_only_view { get; }
+ public abstract Map<K,V> read_only_view { owned get; }
}
diff --git a/gee/readonlycollection.vala b/gee/readonlycollection.vala
index 11c3bb1..b29f914 100644
--- a/gee/readonlycollection.vala
+++ b/gee/readonlycollection.vala
@@ -147,7 +147,7 @@ internal class Gee.ReadOnlyCollection<G> : Object, Iterable<G>, Collection<G> {
}
public virtual Collection<G> read_only_view {
- get { return this; }
+ owned get { return this; }
}
}
diff --git a/gee/readonlylist.vala b/gee/readonlylist.vala
index 46ac354..f29a790 100644
--- a/gee/readonlylist.vala
+++ b/gee/readonlylist.vala
@@ -117,7 +117,7 @@ internal class Gee.ReadOnlyList<G> : Gee.ReadOnlyCollection<G>, List<G> {
* @inheritDoc
*/
public virtual new List<G> read_only_view {
- get { return this; }
+ owned get { return this; }
}
}
diff --git a/gee/readonlymap.vala b/gee/readonlymap.vala
index 1034cd5..180b941 100644
--- a/gee/readonlymap.vala
+++ b/gee/readonlymap.vala
@@ -129,7 +129,7 @@ internal class Gee.ReadOnlyMap<K,V> : Object, Map<K,V> {
}
public virtual Map<K,V> read_only_view {
- get { return this; }
+ owned get { return this; }
}
}
diff --git a/gee/readonlyset.vala b/gee/readonlyset.vala
index 0430d79..8b51503 100644
--- a/gee/readonlyset.vala
+++ b/gee/readonlyset.vala
@@ -43,7 +43,7 @@ internal class Gee.ReadOnlySet<G> : Gee.ReadOnlyCollection<G>, Set<G> {
}
public virtual new Set<G> read_only_view {
- get { return this; }
+ owned get { return this; }
}
}
diff --git a/gee/set.vala b/gee/set.vala
index 35a83d9..15e8ff6 100644
--- a/gee/set.vala
+++ b/gee/set.vala
@@ -28,7 +28,7 @@ public interface Gee.Set<G> : Collection<G> {
/**
* Property giving access to the read-only view of this set.
*/
- public abstract new Set<G> read_only_view { get; }
+ public abstract new Set<G> read_only_view { owned get; }
}
diff --git a/tests/testreadonlycollection.vala b/tests/testreadonlycollection.vala
index eaa4af7..f18ee31 100644
--- a/tests/testreadonlycollection.vala
+++ b/tests/testreadonlycollection.vala
@@ -43,6 +43,16 @@ public abstract class ReadOnlyCollectionTests : Gee.TestCase {
public void test_unique_read_only_view_instance () {
var another_ro_collection = test_collection.read_only_view;
assert (ro_collection == another_ro_collection);
+
+ weak Collection<string> saved_instance = ro_collection;
+ long instance_address = (long) saved_instance;
+ assert (instance_address == (long) another_ro_collection);
+
+ another_ro_collection = null;
+ ro_collection = null;
+
+ another_ro_collection = test_collection.read_only_view;
+ assert (instance_address != (long) another_ro_collection);
}
public void test_add () {
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]