[libgee] Fix standard functions for nullable types
- From: Maciej Marcin Piechotka <mpiechotka src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [libgee] Fix standard functions for nullable types
- Date: Wed, 17 Aug 2011 22:49:34 +0000 (UTC)
commit d120c5703097a86cd69cf895476dade671b9213b
Author: Maciej Piechotka <uzytkownik2 gmail com>
Date: Wed Aug 17 22:07:24 2011 +0100
Fix standard functions for nullable types
gee/functions.vala | 62 ++++++++++++++++++++++++++++++++++++++++-----
tests/testfunctions.vala | 3 +-
2 files changed, 57 insertions(+), 8 deletions(-)
---
diff --git a/gee/functions.vala b/gee/functions.vala
index 16f7713..0a72832 100644
--- a/gee/functions.vala
+++ b/gee/functions.vala
@@ -48,11 +48,31 @@ namespace Gee {
*/
public static EqualDataFunc get_equal_func_for (Type t) {
if (t == typeof (string)) {
- return (a, b) => {return str_equal ((string) a, (string) b);};
+ return (a, b) => {
+ if (a == b)
+ return true;
+ else if (a == null || b == null)
+ return false;
+ else
+ return str_equal ((string) a, (string) b);
+ };
} else if (t.is_a (typeof (Hashable))) {
- return (a, b) => {return ((Hashable<Hashable>) a).equal_to ((Hashable) b);};
+ return (a, b) => {
+ if (a == b)
+ return true;
+ else if (a == null || b == null)
+ return false;
+ else
+ return ((Hashable<Hashable>) a).equal_to ((Hashable) b);
+ };
} else if (t.is_a (typeof (Comparable))) {
- return (a, b) => {return ((Comparable<Comparable>) a).compare_to ((Comparable) b) == 0;};
+ return (a, b) => {
+ if (a == b)
+ return true;
+ else if (a == null || b == null)
+ return false;
+ else
+ return ((Comparable<Comparable>) a).compare_to ((Comparable) b) == 0;};
} else {
return (a, b) => {return direct_equal (a, b);};
}
@@ -67,9 +87,19 @@ namespace Gee {
*/
public static HashDataFunc get_hash_func_for (Type t) {
if (t == typeof (string)) {
- return (a) => {return str_hash ((string) a);};
+ return (a) => {
+ if (a == null)
+ return (uint)0xdeadbeef;
+ else
+ return str_hash ((string) a);
+ };
} else if (t.is_a (typeof (Hashable))) {
- return (a) => {return ((Hashable) a).hash();};
+ return (a) => {
+ if (a == null)
+ return (uint)0xdeadbeef;
+ else
+ return ((Hashable) a).hash();
+ };
} else {
return (a) => {return direct_hash (a);};
}
@@ -84,9 +114,27 @@ namespace Gee {
*/
public static CompareDataFunc get_compare_func_for (Type t) {
if (t == typeof (string)) {
- return (a, b) => {return strcmp((string) a, (string) b);};
+ return (a, b) => {
+ if (a == b)
+ return 0;
+ else if (a == null)
+ return -1;
+ else if (b == null)
+ return 1;
+ else
+ return strcmp((string) a, (string) b);
+ };
} else if (t.is_a (typeof (Comparable))) {
- return (a, b) => {return ((Comparable<Comparable>) a).compare_to ((Comparable) b);};
+ return (a, b) => {
+ if (a == b)
+ return 0;
+ else if (a == null)
+ return -1;
+ else if (b == null)
+ return 1;
+ else
+ return ((Comparable<Comparable>) a).compare_to ((Comparable) b);
+ };
} else {
return (_val1, _val2) => {
long val1 = (long)_val1, val2 = (long)_val2;
diff --git a/tests/testfunctions.vala b/tests/testfunctions.vala
index c46f7b8..1fff514 100644
--- a/tests/testfunctions.vala
+++ b/tests/testfunctions.vala
@@ -114,6 +114,7 @@ public class FunctionsTests : Gee.TestCase {
MyHashable one = new MyHashable(1);
MyHashable two_copy = new MyHashable(2);
MyHashable minus_one = new MyHashable(-1);
+ MyHashable minus_one2 = new MyHashable(-1);
Gee.EqualDataFunc eq = Gee.Functions.get_equal_func_for (typeof (MyHashable));
CompareDataFunc cmp = Gee.Functions.get_compare_func_for (typeof (MyHashable));
@@ -139,7 +140,7 @@ public class FunctionsTests : Gee.TestCase {
// Check if correct functions taken
assert (hash (one) == 1);
- assert (!eq (minus_one, minus_one));
+ assert (!eq (minus_one, minus_one2));
}
public void test_unfold () {
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]