Are there a bz entry for libgee? I wasn't able to find it.

When the collections in Gee are holding strong references, it is
difficult to obtain a weak reference to the elements in the container.

The situation is:

class Node {
        Gee.List<Node> childNodes;
        public Node? firstChild {
                get {
                        return childNodes.get(0);
////// panic! the signature of properties are weak!.
                }
        }

}
I was able to workaround by
        get {
                Node rt = childNodes.get(0);
                return rt;
        }
But this doesn't look intuitive and causes further troubles[difficult to
explain]. a better looking solution is
        get {
                return childNode.get_weak_ref(0);
        }

The attached patch adds get_weak_ref to libgee.

Regards,

Yu
Index: hashset.vala
===================================================================
--- hashset.vala	(revision 39)
+++ hashset.vala	(working copy)
@@ -199,6 +199,12 @@
 			assert (_node != null);
 			return _node.key;
 		}
+
+		public weak G? get_weak_ref () {
+			assert (_stamp == _set._stamp);
+			assert (_node != null);
+			return _node.key;
+		}
 	}
 }
 
Index: readonlylist.vala
===================================================================
--- readonlylist.vala	(revision 39)
+++ readonlylist.vala	(working copy)
@@ -92,6 +92,14 @@
 		return _list.get (index);
 	}
 
+	public weak G? get_weak_ref (int index) {
+		if (_list == null) {
+			return null;
+		}
+
+		return _list.get_weak_ref (index);
+	}
+
 	public void set (int index, G o) {
 		assert_not_reached ();
 	}
@@ -108,6 +116,10 @@
 		public G? get () {
 			return null;
 		}
+
+		public weak G? get_weak_ref () {
+			return null;
+		}
 	}
 }
 
Index: arraylist.vala
===================================================================
--- arraylist.vala	(revision 39)
+++ arraylist.vala	(working copy)
@@ -74,6 +74,12 @@
 		return _items[index];
 	}
 
+	public weak G? get_weak_ref (int index) {
+		assert (index >= 0 && index < _size);
+
+		return _items[index];
+	}
+
 	public void set (int index, G item) {
 		assert (index >= 0 && index < _size);
 
@@ -187,6 +193,16 @@
 
 			return _list.get (_index);
 		}
+
+		public weak G? get_weak_ref () {
+			assert (_stamp == _list._stamp);
+
+			if (_index < 0 || _index >= _list._size) {
+				return null;
+			}
+
+			return _list.get_weak_ref (_index);
+		}
 	}
 }
 
Index: list.vala
===================================================================
--- list.vala	(revision 39)
+++ list.vala	(working copy)
@@ -34,6 +34,15 @@
 	public abstract G? get (int index);
 
 	/**
+	 * Returns a weak reference to the item at the specified index in this list.
+	 *
+	 * @param index zero-based index of the item to be returned
+	 *
+	 * @return      a weak reference to the item at the specified index in the list
+	 */
+	public abstract weak G? get_weak_ref (int index);
+
+	/**
 	 * Sets the item at the specified index in this list.
 	 *
 	 * @param index zero-based index of the item to be set
Index: readonlymap.vala
===================================================================
--- readonlymap.vala	(revision 39)
+++ readonlymap.vala	(working copy)
@@ -72,6 +72,14 @@
 		return _map.get (key);
 	}
 
+	public weak V? get_weak_ref (K key) {
+		if (_map == null) {
+			return null;
+		}
+
+		return _map.get_weak_ref (key);
+	}
+
 	public void set (K key, V value) {
 		assert_not_reached ();
 	}
Index: map.vala
===================================================================
--- map.vala	(revision 39)
+++ map.vala	(working copy)
@@ -63,6 +63,17 @@
 	public abstract V? get (K key);
 
 	/**
+	 * Returns a weak reference to the value of the specified key in this map.
+	 *
+	 * @param key the key whose value is to be retrieved
+	 *
+	 * @return    a weak reference to the value associated with the key, or null if the key
+	 *            couldn't be found
+	 */
+
+	public abstract weak V? get_weak_ref (K key);
+
+	/**
 	 * Inserts a new key and value into this map.
 	 *
 	 * @param key   the key to insert
Index: hashmap.vala
===================================================================
--- hashmap.vala	(revision 39)
+++ hashmap.vala	(working copy)
@@ -100,6 +100,15 @@
 		}
 	}
 
+	public weak V? get_weak_ref (K key) {
+		Node<K,V>* node = (*lookup_node (key));
+		if (node != null) {
+			return node->value;
+		} else {
+			return null;
+		}
+	}
+
 	public void set (K key, V value) {
 		Node<K,V>** node = lookup_node (key);
 		if (*node != null) {
@@ -257,6 +266,12 @@
 			assert (_node != null);
 			return _node.key;
 		}
+
+		public weak K? get_weak_ref () {
+			assert (_stamp == _map._stamp);
+			assert (_node != null);
+			return _node.key;
+		}
 	}
 
 	private class ValueCollection<K,V> : Object, Iterable<V>, Collection<V> {
@@ -340,6 +355,12 @@
 			assert (_node != null);
 			return _node.value;
 		}
+
+		public weak V? get_weak_ref () {
+			assert (_stamp == _map._stamp);
+			assert (_node != null);
+			return _node.value;
+		}
 	}
 }
 
Index: iterator.vala
===================================================================
--- iterator.vala	(revision 39)
+++ iterator.vala	(working copy)
@@ -38,5 +38,13 @@
 	 * @return the current element in the iteration
 	 */
 	public abstract G? get ();
+
+	/**
+	 * Returns a weak reference to the current element in the iteration.
+	 *
+	 * @return a weak reference to the current element in the iteration
+	 */
+
+	public abstract weak G? get_weak_ref ();
 }
 
Index: readonlycollection.vala
===================================================================
--- readonlycollection.vala	(revision 39)
+++ readonlycollection.vala	(working copy)
@@ -80,6 +80,10 @@
 		public G? get () {
 			return null;
 		}
+
+		public weak G? get_weak_ref () {
+			return null;
+		}
 	}
 }
 
Index: readonlyset.vala
===================================================================
--- readonlyset.vala	(revision 39)
+++ readonlyset.vala	(working copy)
@@ -80,6 +80,10 @@
 		public G? get () {
 			return null;
 		}
+
+		public weak G? get_weak_ref () {
+			return null;
+		}
 	}
 }
 
_______________________________________________
Vala-list mailing list
[email protected]
http://mail.gnome.org/mailman/listinfo/vala-list

Reply via email to