Permalink
Browse files

Implement IEquatable<T> on structs.

* gdk/Gdk.metadata: suppress Color.GetHashCode().
* gdk/Point.custom: remove generated methods.
* generator/StructBase.cs: Generate Equals(T), Equals(object), and GetHashCode.
* generator/StructField.cs: helper property for equality testing.
* gtk/TreeIter.custom: remove generated methods.
  • Loading branch information...
mkestner committed Oct 8, 2011
1 parent 9d448f4 commit aae2b053005753c5b1b0121cf620f6d22226c2d8
Showing with 72 additions and 34 deletions.
  1. +1 −0 gdk/Gdk.metadata
  2. +0 −13 gdk/Point.custom
  3. +50 −1 generator/StructBase.cs
  4. +21 −1 generator/StructField.cs
  5. +0 −19 gtk/TreeIter.custom
View
@@ -1,5 +1,6 @@
<?xml version="1.0"?>
<metadata>
+ <attr path="/api/namespace/boxed[@cname='GdkColor']" name="nohash">true</attr>
<attr path="/api/namespace/boxed[@cname='GdkColor']/method[@name='Copy']" name="hidden">1</attr>
<attr path="/api/namespace/boxed[@cname='GdkColor']/method[@name='Free']" name="hidden">1</attr>
<attr path="/api/namespace/boxed[@cname='GdkColor']/method[@name='Hash']" name="hidden">1</attr>
View
@@ -43,19 +43,6 @@ public Point (Size sz)
this.Y = sz.Height;
}
-public override bool Equals (object o)
-{
- if (!(o is Point))
- return false;
-
- return (this == (Point) o);
-}
-
-public override int GetHashCode ()
-{
- return X ^ Y;
-}
-
public void Offset (int dx, int dy)
{
X += dx;
View
@@ -24,6 +24,7 @@ namespace GtkSharp.Generation {
using System;
using System.Collections;
using System.IO;
+ using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
@@ -106,6 +107,53 @@ public string ReleaseNative (string var)
}
}
+ protected new void GenEqualsAndHash (StreamWriter sw)
+ {
+ int bitfields = 0;
+ bool need_field = true;
+ StringBuilder sb = new StringBuilder ();
+
+ sw.WriteLine ("\t\tpublic bool Equals ({0} other)", Name);
+ sw.WriteLine ("\t\t{");
+
+ foreach (StructField field in fields) {
+ if (field.IsBitfield) {
+ if (need_field) {
+ sw.WriteLine ("\t\tif (!_bitfield{0}.Equals (other._bitfield{0})) return false;", bitfields);
+ if (sb.Length > 0)
+ sb.Append (" ^ ");
+ sb.Append ("_bitfield");
+ sb.Append (bitfields++);
+ sb.Append (".GetHashCode ()");
+ need_field = false;
+ }
+ } else {
+ need_field = true;
+ sw.WriteLine ("\t\t\tif (!{0}.Equals (other.{0})) return false;", field.EqualityName);
+ if (sb.Length > 0)
+ sb.Append (" ^ ");
+ sb.Append (field.EqualityName);
+ sb.Append (".GetHashCode ()");
+ }
+ }
+ sw.WriteLine ("\t\t\treturn true;");
+ sw.WriteLine ("\t\t}");
+ sw.WriteLine ();
+ sw.WriteLine ("\t\tpublic override bool Equals (object other)");
+ sw.WriteLine ("\t\t{");
+ sw.WriteLine ("\t\t\treturn other is {0} && Equals (({0}) other);", Name);
+ sw.WriteLine ("\t\t}");
+ sw.WriteLine ();
+ if (Elem.GetAttribute ("nohash") == "true")
+ return;
+ sw.WriteLine ("\t\tpublic override int GetHashCode ()");
+ sw.WriteLine ("\t\t{");
+ sw.WriteLine ("\t\t\treturn {0};", sb.ToString ());
+ sw.WriteLine ("\t\t}");
+ sw.WriteLine ();
+
+ }
+
protected new void GenFields (GenerationInfo gen_info)
{
int bitfields = 0;
@@ -160,7 +208,7 @@ public override void Generate (GenerationInfo gen_info)
sw.WriteLine ("\t[Obsolete]");
sw.WriteLine ("\t[StructLayout(LayoutKind.Sequential)]");
string access = IsInternal ? "internal" : "public";
- sw.WriteLine ("\t" + access + " partial struct " + Name + " {");
+ sw.WriteLine ("\t" + access + " partial struct {0} : IEquatable<{0}> {{", Name);
sw.WriteLine ();
need_read_native = false;
@@ -170,6 +218,7 @@ public override void Generate (GenerationInfo gen_info)
GenMethods (gen_info, null, this);
if (need_read_native)
GenReadNative (sw);
+ GenEqualsAndHash (sw);
if (!need_close)
return;
View
@@ -70,6 +70,26 @@ public class StructField : FieldBase {
}
}
+ public string EqualityName {
+ get {
+ SymbolTable table = SymbolTable.Table;
+ string wrapped = table.GetCSType (CType);
+ string wrapped_name = SymbolTable.Table.MangleName (CName);
+ IGeneratable gen = table [CType];
+
+ if (IsArray || gen is IAccessor)
+ return StudlyName;
+ else if (IsBitfield)
+ return Name;
+ else if (IsPointer && (gen is StructGen || gen is BoxedGen))
+ return Access != "private" ? wrapped_name : Name;
+ else if (IsPointer && CSType != "string")
+ return Name;
+ else
+ return Access == "public" ? StudlyName : Name;
+ }
+ }
+
bool IsPadding {
get {
return (CName.StartsWith ("dummy") || CName.StartsWith ("padding"));
@@ -93,7 +113,7 @@ public class StructField : FieldBase {
}
}
- string StudlyName {
+ public string StudlyName {
get {
string studly = base.Name;
if (studly == "")
View
@@ -16,25 +16,6 @@
// Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.
- public override int GetHashCode ()
- {
- return Stamp ^ (int) _user_data ^ (int) _user_data2 ^ (int) _user_data3;
- }
-
- public override bool Equals (object o)
- {
- if (o == null)
- return false;
-
- if (!(o is TreeIter))
- return false;
- TreeIter ti = (TreeIter) o;
- return ti.Stamp == Stamp &&
- ti._user_data == _user_data &&
- ti._user_data2 == _user_data2 &&
- ti._user_data3 == _user_data3;
- }
-
public IntPtr UserData {
get {
return _user_data;

0 comments on commit aae2b05

Please sign in to comment.