diff options
author | Michi Henning <michi@zeroc.com> | 2003-12-04 07:46:00 +0000 |
---|---|---|
committer | Michi Henning <michi@zeroc.com> | 2003-12-04 07:46:00 +0000 |
commit | 1aea2c0c62bb995cab4d19dc7ed401ce21c5dd49 (patch) | |
tree | cff91babd13ed563a5124523337b4bee8f61c3ba /cpp/src | |
parent | removed project project (diff) | |
download | ice-1aea2c0c62bb995cab4d19dc7ed401ce21c5dd49.tar.bz2 ice-1aea2c0c62bb995cab4d19dc7ed401ce21c5dd49.tar.xz ice-1aea2c0c62bb995cab4d19dc7ed401ce21c5dd49.zip |
*** empty log message ***
Diffstat (limited to 'cpp/src')
-rwxr-xr-x | cpp/src/slice2cs/Gen.cpp | 692 |
1 files changed, 511 insertions, 181 deletions
diff --git a/cpp/src/slice2cs/Gen.cpp b/cpp/src/slice2cs/Gen.cpp index e04680cc3f5..905bffb2866 100755 --- a/cpp/src/slice2cs/Gen.cpp +++ b/cpp/src/slice2cs/Gen.cpp @@ -134,7 +134,9 @@ bool Slice::Gen::TypesVisitor::visitModuleStart(const ModulePtr& p) { string name = fixKwd(fixGlobal(p)); - _out << sp << nl << "namespace " << name << nl << '{'; + _out << sp << nl << "namespace " << name; + _out << sb; + _out << nl << "#region " << name << " members"; return true; } @@ -142,7 +144,215 @@ Slice::Gen::TypesVisitor::visitModuleStart(const ModulePtr& p) void Slice::Gen::TypesVisitor::visitModuleEnd(const ModulePtr& p) { - _out << sp << nl << '}'; + _out << sp << nl << "#endregion"; // module + _out << eb; +} + +bool +Slice::Gen::TypesVisitor::visitClassDefStart(const ClassDefPtr& p) +{ + string name = fixKwd(fixGlobal(p)); + string scoped = fixKwd(p->scoped()); + ClassList bases = p->bases(); + + _out << sp << nl << "public "; + if(p->isAbstract()) + { + _out << "abstract "; + } + _out << "class " << name << " : "; + if(bases.empty()) + { + if(p->isLocal()) + { + _out << "Ice.LocalObject"; + } + else + { + _out << "Ice.Object"; + } + } + else + { + ClassList::const_iterator q = bases.begin(); + while(q != bases.end()) + { + _out << fixKwd((*q)->scoped()); + if(++q != bases.end()) + { + _out << ',' << nl; + } + } + } + _out << sb; + + _out << sp << nl << "#region " << name << " members"; + + _out << sp << nl << "#region Slice data members and operations"; + + return true; +} + +void +Slice::Gen::TypesVisitor::visitClassDefEnd(const ClassDefPtr& p) +{ + string name = fixKwd(fixGlobal(p)); + string scoped = fixKwd(p->scoped()); + ClassList bases = p->bases(); + DataMemberList dataMembers = p->dataMembers(); + DataMemberList::const_iterator q; + + _out << sp << nl << "#endregion"; // Slice data members and operations + + _out << sp << nl << "#region IComparable members"; + + _out << sp << nl << "public override int CompareTo(object __other)"; + _out << sb; + _out << nl << "if(__other == null)"; + _out << sb; + _out << nl << "return 1;"; + _out << eb; + _out << nl << "if(object.ReferenceEquals(this, __other))"; + _out << sb; + _out << nl << "return 0;"; + _out << eb; + _out << nl << "if(!(__other is " << name << "))"; + _out << sb; + _out << nl << "throw new System.ArgumentException(\"expected argument of type `" << name << "'\", \"__other\");"; + _out << eb; + _out << nl << "int __ret = base.CompareTo(__other);"; + _out << nl << "if(__ret != 0)"; + _out << sb; + _out << nl << "return __ret;"; + _out << eb; + for(q = dataMembers.begin(); q != dataMembers.end(); ++q) + { + string memberName = fixKwd((*q)->name()); + _out << nl << "if((object)" << memberName << " == null && __other != null)"; + _out << sb; + _out << nl << "return -1;"; + _out << eb; + _out << nl << "if((__ret = " << memberName << ".CompareTo(((" << name << ")__other)." + << memberName << ")) != 0)"; + _out << sb; + _out << nl << "return __ret;"; + _out << eb; + } + _out << nl << "return 0;"; + + _out << eb; + + _out << sp << nl << "#endregion"; // IComparable members + + _out << sp << nl << "#region Comparison operators"; + + _out << sp << nl << "public static bool operator==(" << name << " __lhs, " << name << " __rhs)"; + _out << sb; + _out << nl << "return Equals(__lhs, __rhs);"; + _out << eb; + + _out << sp << nl << "public static bool operator!=(" << name << " __lhs, " << name << " __rhs)"; + _out << sb; + _out << nl << "return !Equals(__lhs, __rhs);"; + _out << eb; + + _out << sp << nl << "public static bool operator<(" << name << " __lhs, " << name << " __rhs)"; + _out << sb; + _out << nl << "return __lhs == null ? __rhs != null : __lhs.CompareTo(__rhs) < 0;"; + _out << eb; + + _out << sp << nl << "public static bool operator>(" << name << " __lhs, " << name << " __rhs)"; + _out << sb; + _out << nl << "return __lhs == null ? false : __lhs.CompareTo(__rhs) > 0;"; + _out << eb; + + _out << sp << nl << "public static bool operator<=(" << name << " __lhs, " << name << " __rhs)"; + _out << sb; + _out << nl << "return __lhs == null ? true : __lhs.CompareTo(__rhs) <= 0;"; + _out << eb; + + _out << sp << nl << "public static bool operator>=(" << name << " __lhs, " << name << " __rhs)"; + _out << sb; + _out << nl << "return __lhs == null ? __rhs == null : __lhs.CompareTo(__rhs) >= 0;"; + _out << eb; + + _out << sp << nl << "#endregion"; // Comparison operators + + _out << sp << nl << "#region ICloneable members"; + + _out << sp << nl << "protected void __copyMembers(" << scoped << " __to)"; + _out << sb; + _out << nl << "base.__copyMembers(__to);"; + for(q = dataMembers.begin(); q != dataMembers.end(); ++q) + { + string memberName = fixKwd((*q)->name()); + _out << nl << "__to." << memberName << " = " << memberName << ";"; + } + _out << eb; + + if(!p->isAbstract()) + { + _out << sp << nl << "public override object Clone()"; + _out << sb; + _out << nl << scoped << " __ret = new " << scoped << "();"; + _out << nl << "__copyMembers(__ret);"; + _out << nl << "return __ret;"; + _out << eb; + } + + _out << sp << nl << "#endregion"; // ICloneable members + + _out << sp << nl << "#region Object members"; + + _out << sp << nl << "public override int GetHashCode()"; + _out << sb; + _out << nl << "int __h = 0;"; + for(q = dataMembers.begin(); q != dataMembers.end(); ++q) + { + string memberName = fixKwd((*q)->name()); + _out << nl << "__h = (__h << 5) ^ " << memberName << ".GetHashCode();"; + } + _out << nl << "return __h;"; + _out << eb; + + _out << sp << nl << "public override bool Equals(object other)"; + _out << sb; + _out << nl << "return CompareTo(other) == 0;"; + _out << eb; + + _out << sp << nl << "public static bool Equals(" << name << " __lhs, " << name << " __rhs)"; + _out << sb; + _out << nl << "return __lhs == null ? __rhs == null : __lhs.CompareTo(__rhs) == 0;"; + _out << eb; + + _out << sp << nl << "#endregion"; // Object members + + _out << sp << nl << "#endregion"; // class + + _out << eb; +} + +void +Slice::Gen::TypesVisitor::visitOperation(const OperationPtr& p) +{ + string name = fixKwd(p->name()); + string scoped = fixKwd(p->scoped()); + + _out << sp << nl << "public abstract " << typeToString(p->returnType()) << " " << name << "("; + ParamDeclList paramList = p->parameters(); + for(ParamDeclList::const_iterator q = paramList.begin(); q != paramList.end(); ++q) + { + if(q != paramList.begin()) + { + _out << ", "; + } + if((*q)->isOutParam()) + { + _out << "ref "; + } + _out << typeToString((*q)->type()) << " " << fixKwd((*q)->name()); + } + _out << ");"; } void @@ -152,9 +362,13 @@ Slice::Gen::TypesVisitor::visitSequence(const SequencePtr& p) string s = typeToString(p->type()); _out << sp << nl << "public class " << name - << " : System.Collections.CollectionBase, System.ICloneable, System.IComparable"; + << " : System.Collections.CollectionBase, System.IComparable, System.ICloneable"; _out << sb; + _out << sp << nl << "#region " << name << " members"; + + _out << sp << nl << "#region Indexer"; + _out << nl << "public " << s << " this[int index]"; _out << sb; _out << nl << "get"; @@ -168,46 +382,38 @@ Slice::Gen::TypesVisitor::visitSequence(const SequencePtr& p) _out << eb; _out << eb; - _out << sp << nl << "public int"; - _out << nl << "Add(" << s << " value)"; + _out << sp << nl << "#endregion"; // Indexer + + _out << sp << nl << "#region ICollectionBase members"; + + _out << sp << nl << "public int Add(" << s << " value)"; _out << sb; _out << nl << "return List.Add(value);"; _out << eb; - _out << sp << nl << "public int"; - _out << nl << "IndexOf(" << s << " value)"; + _out << sp << nl << "public int IndexOf(" << s << " value)"; _out << sb; _out << nl << "return List.IndexOf(value);"; _out << eb; - _out << sp << nl << "public void"; - _out << nl << "Insert(int index, " << s << " value)"; + _out << sp << nl << "public void Insert(int index, " << s << " value)"; _out << sb; _out << nl << "List.Insert(index, value);"; _out << eb; - _out << sp << nl << "public void"; - _out << nl << "Remove(" << s << " value)"; + _out << sp << nl << "public void Remove(" << s << " value)"; _out << sb; _out << nl << "List.Remove(value);"; _out << eb; - _out << sp << nl << "public bool"; - _out << nl << "Contains(" << s << " value)"; + _out << sp << nl << "public bool Contains(" << s << " value)"; _out << sb; _out << nl << "return List.Contains(value);"; _out << eb; - _out << sp << nl << "public object"; - _out << nl << "Clone()"; - _out << sb; - _out << nl << name << " __ret = new "<< name << "();"; - _out << nl << "foreach(" << s << " __i in __ret)"; - _out << sb; - _out << nl << "__ret.Add(__i);"; - _out << eb; - _out << nl << "return __ret;"; - _out << eb; + _out << sp << nl << "#endregion"; // ICollectionBase members + + _out << sp << nl << "#region IComparable members"; _out << sp << nl << "public int CompareTo(object other)"; _out << sb; @@ -224,72 +430,99 @@ Slice::Gen::TypesVisitor::visitSequence(const SequencePtr& p) _out << nl << "throw new System.ArgumentException(\"CompareTo: expected argument of type `" << name << "'\", \"other\");"; _out << eb; - _out << nl << name << " __other = (" << name << ")other;"; - _out << nl << "int __limit = System.Math.Min(this.Count, __other.Count);"; - _out << nl << "for(int __i = 0; __i < __limit; ++__i)"; + _out << nl << "int limit = System.Math.Min(this.Count, ((" << name << ")other).Count);"; + _out << nl << "for(int i = 0; i < limit; ++i)"; _out << sb; - _out << nl << "if(this[__i] < __other[__i])"; + _out << nl << "if((object)this[i] == null && (object)((" << name << ")other)[i] != null)"; _out << sb; _out << nl << "return -1;"; _out << eb; - _out << nl << "if(this[__i] > __other[__i])"; + _out << nl << "int __ret = this[i].CompareTo(((" << name << ")other)[i]);"; + _out << nl << "if(__ret != 0)"; _out << sb; - _out << nl << "return 1;"; + _out << nl << "return __ret;"; _out << eb; _out << eb; - _out << nl << "return this.Count < __other.Count ? -1 : ((this.Count > __other.Count) ? 1 : 0);"; + _out << nl << "return this.Count < ((" << name << ")other).Count ? -1 : ((this.Count > ((" + << name << ")other).Count) ? 1 : 0);"; _out << eb; - _out << sp << nl << "public override int GetHashCode()"; - _out << sb; - _out << nl << "int hash = 0;"; - _out << nl << "for(int i = 0; i < Count; ++i)"; + _out << sp << nl << "#endregion"; // IComparable members + + _out << sp << nl << "#region Comparison operators"; + + _out << sp << nl << "public static bool operator==(" << name << " __lhs, " << name << " __rhs)"; _out << sb; - _out << nl << "hash = (hash << 5) ^ this[i].GetHashCode();"; + _out << nl << "return Equals(__lhs, __rhs);"; _out << eb; - _out << nl << "return hash;"; + + _out << sp << nl << "public static bool operator!=(" << name << " __lhs, " << name << " __rhs)"; + _out << sb; + _out << nl << "return !Equals(__lhs, __rhs);"; _out << eb; - _out << sp << nl << "public override bool Equals(object other)"; + _out << sp << nl << "public static bool operator<(" << name << " __lhs, " << name << " __rhs)"; _out << sb; - _out << nl << "return CompareTo(other) == 0;"; + _out << nl << "return __lhs == null ? __rhs != null : __lhs.CompareTo(__rhs) < 0;"; _out << eb; - _out << sp << nl << "public static bool Equals(" << name << " lhs, " << name << " rhs)"; + _out << sp << nl << "public static bool operator>(" << name << " __lhs, " << name << " __rhs)"; _out << sb; - _out << nl << "return lhs == null ? rhs == null : lhs.CompareTo(rhs) == 0;"; + _out << nl << "return __lhs == null ? false : __lhs.CompareTo(__rhs) > 0;"; _out << eb; - _out << sp << nl << "public static bool operator==(" << name << " lhs, " << name << " rhs)"; + _out << sp << nl << "public static bool operator<=(" << name << " __lhs, " << name << " __rhs)"; _out << sb; - _out << nl << "return Equals(lhs, rhs);"; + _out << nl << "return __lhs == null ? true : __lhs.CompareTo(__rhs) <= 0;"; _out << eb; - _out << sp << nl << "public static bool operator!=(" << name << " lhs, " << name << " rhs)"; + _out << sp << nl << "public static bool operator>=(" << name << " __lhs, " << name << " __rhs)"; _out << sb; - _out << nl << "return !Equals(lhs, rhs);"; + _out << nl << "return __lhs == null ? __rhs == null : __lhs.CompareTo(__rhs) >= 0;"; _out << eb; - _out << sp << nl << "public static bool operator<(" << name << " lhs, " << name << " rhs)"; + _out << sp << nl << "#endregion"; // Comparison operators + + _out << sp << nl << "#region ICloneable members"; + + _out << sp << nl << "public object Clone()"; + _out << sb; + _out << nl << name << " __ret = new "<< name << "();"; + _out << nl << "foreach(" << s << " i in __ret)"; _out << sb; - _out << nl << "return lhs == null ? rhs != null : lhs.CompareTo(rhs) < 0;"; + _out << nl << "__ret.Add(i);"; + _out << eb; + _out << nl << "return __ret;"; _out << eb; - _out << sp << nl << "public static bool operator>(" << name << " lhs, " << name << " rhs)"; + _out << sp << nl << "#endregion"; // ICloneable members + + _out << sp << nl << "#region Object members"; + + _out << sp << nl << "public override int GetHashCode()"; _out << sb; - _out << nl << "return lhs == null ? false : lhs.CompareTo(rhs) > 0;"; + _out << nl << "int hash = 0;"; + _out << nl << "for(int i = 0; i < Count; ++i)"; + _out << sb; + _out << nl << "hash = (hash << 5) ^ this[i].GetHashCode();"; + _out << eb; + _out << nl << "return hash;"; _out << eb; - _out << sp << nl << "public static bool operator<=(" << name << " lhs, " << name << " rhs)"; + _out << sp << nl << "public override bool Equals(object other)"; _out << sb; - _out << nl << "return lhs == null ? true : lhs.CompareTo(rhs) <= 0;"; + _out << nl << "return CompareTo(other) == 0;"; _out << eb; - _out << sp << nl << "public static bool operator>=(" << name << " lhs, " << name << " rhs)"; + _out << sp << nl << "public static bool Equals(" << name << " __lhs, " << name << " __rhs)"; _out << sb; - _out << nl << "return lhs == null ? rhs == null : lhs.CompareTo(rhs) >= 0;"; + _out << nl << "return __lhs == null ? __rhs == null : __lhs.CompareTo(__rhs) == 0;"; _out << eb; + _out << sp << nl << "#endregion"; // Object members + + _out << sp << nl << "#endregion"; // sequence + _out << eb; } @@ -310,6 +543,10 @@ Slice::Gen::TypesVisitor::visitExceptionStart(const ExceptionPtr& p) } _out << sb; + _out << sp << nl << "#region " << name << " members"; + + _out << sp << nl << "#region Slice data members"; + return true; } @@ -317,51 +554,98 @@ void Slice::Gen::TypesVisitor::visitExceptionEnd(const ExceptionPtr& p) { string name = fixKwd(fixGlobal(p)); + string scoped = fixKwd(p->scoped()); DataMemberList dataMembers = p->dataMembers(); DataMemberList::const_iterator q; + _out << sp << nl << "#endregion"; // Slice data members + + _out << sp << nl << "#region Constructors"; + _out << sp << nl << "public " << name << "() : base(\"" << name << "\")"; _out << sb; _out << eb; - _out << sp << nl << "public " << name << "(string __m) : base(__m)"; + _out << sp << nl << "public " << name << "(string m) : base(m)"; _out << sb; _out << eb; - _out << sp << nl << "public " << name << "(string __m, System.Exception __e) : base(__m, __e)"; + _out << sp << nl << "public " << name << "(string m, System.Exception e) : base(m, e)"; + _out << sb; + _out << eb; + + _out << sp << nl << "#endregion"; // Constructors + + _out << sp << nl << "#region Comparison operators"; + + _out << sp << nl << "public static bool operator==(" << name << " __lhs, " << name << " __rhs)"; + _out << sb; + _out << nl << "return Equals(__lhs, __rhs);"; + _out << eb; + + _out << sp << nl << "public static bool operator!=(" << name << " __lhs, " << name << " __rhs)"; + _out << sb; + _out << nl << "return !Equals(__lhs, __rhs);"; + _out << eb; + + _out << sp << nl << "#endregion"; // Comparison operators + + _out << sp << nl << "#region ICloneable members"; + + _out << sp << nl << "protected void __copyMembers(" << scoped << " __to)"; + _out << sb; + if(p->base()) + { + _out << nl << "base.__copyMembers(__to);"; + } + for(q = dataMembers.begin(); q != dataMembers.end(); ++q) + { + string memberName = fixKwd((*q)->name()); + _out << nl << "__to." << memberName << " = " << memberName << ";"; + } + _out << eb; + + _out << sp << nl << "public override object Clone()"; _out << sb; + _out << nl << scoped << " __ret = new " << scoped << "(Message, InnerException);"; + _out << nl << "__copyMembers(__ret);"; + _out << nl << "return __ret;"; _out << eb; + _out << sp << nl << "#endregion"; // ICloneable members + + _out << sp << nl << "#region Object members"; + _out << sp << nl << "public override int GetHashCode()"; _out << sb; - _out << nl << "int hash = 0;"; + _out << nl << "int __h = 0;"; for(q = dataMembers.begin(); q != dataMembers.end(); ++q) { string memberName = fixKwd((*q)->name()); - _out << nl << "hash = (hash << 5) ^ " << memberName << ".GetHashCode();"; + _out << nl << "__h = (__h << 5) ^ " << memberName << ".GetHashCode();"; } - _out << nl << "return hash;"; + _out << nl << "return __h;"; _out << eb; - _out << sp << nl << "public override bool Equals(object other)"; + _out << sp << nl << "public override bool Equals(object __other)"; _out << sb; - _out << nl << "if(other == null)"; + _out << nl << "if(__other == null)"; _out << sb; _out << nl << "return false;"; _out << eb; - _out << nl << "if(object.ReferenceEquals(this, other))"; + _out << nl << "if(object.ReferenceEquals(this, __other))"; _out << sb; _out << nl << "return true;"; _out << eb; - _out << nl << "if(!(other is " << name << "))"; + _out << nl << "if(!(__other is " << name << "))"; _out << sb; - _out << nl << "throw new System.ArgumentException(\"expected argument of type `" << name << "'\", \"other\");"; + _out << nl << "throw new System.ArgumentException(\"expected argument of type `" << name << "'\", \"__other\");"; _out << eb; for(q = dataMembers.begin(); q != dataMembers.end(); ++q) { string memberName = fixKwd((*q)->name()); - _out << nl << "if(this." << memberName << " != ((" << name << ")other)." << memberName << ")"; + _out << nl << "if(this." << memberName << " != ((" << name << ")__other)." << memberName << ")"; _out << sb; _out << nl << "return false;"; _out << eb; @@ -369,20 +653,14 @@ Slice::Gen::TypesVisitor::visitExceptionEnd(const ExceptionPtr& p) _out << nl << "return true;"; _out << eb; - _out << sp << nl << "public static bool Equals(" << name << " lhs, " << name << " rhs)"; + _out << sp << nl << "public static bool Equals(" << name << " __lhs, " << name << " __rhs)"; _out << sb; - _out << nl << "return lhs == null ? rhs == null : lhs.Equals(rhs);"; + _out << nl << "return __lhs == null ? __rhs == null : __lhs.Equals(__rhs);"; _out << eb; - _out << sp << nl << "public static bool operator==(" << name << " lhs, " << name << " rhs)"; - _out << sb; - _out << nl << "return Equals(lhs, rhs);"; - _out << eb; + _out << sp << nl << "#endregion"; // Object members - _out << sp << nl << "public static bool operator!=(" << name << " lhs, " << name << " rhs)"; - _out << sb; - _out << nl << "return !Equals(lhs, rhs);"; - _out << eb; + _out << sp << nl << "#endregion"; // exception _out << eb; } @@ -392,9 +670,13 @@ Slice::Gen::TypesVisitor::visitStructStart(const StructPtr& p) { string name = fixKwd(fixGlobal(p)); - _out << sp << nl << "public class " << name << " : System.ICloneable, System.IComparable"; + _out << sp << nl << "public class " << name << " : System.IComparable, System.ICloneable"; _out << sb; + _out << sp << nl << "#region " << name << " members"; + + _out << sp << nl << "#region Slice data members"; + return true; } @@ -406,97 +688,119 @@ Slice::Gen::TypesVisitor::visitStructEnd(const StructPtr& p) DataMemberList dataMembers = p->dataMembers(); DataMemberList::const_iterator q; - _out << sp << nl << "public object Clone()"; - _out << sb; - _out << nl << name << " __ret = new " << name << "();"; - for(q = dataMembers.begin(); q != dataMembers.end(); ++q) - { - string memberName = fixKwd((*q)->name()); - _out << nl << "__ret." << memberName << " = this." << memberName << ";"; - } - _out << nl << "return __ret;"; - _out << eb; + _out << sp << nl << "#endregion"; // Slice data members - _out << sp << nl << "public int CompareTo(object other)"; + _out << sp << nl << "#region IComparable members"; + + _out << sp << nl << "public int CompareTo(object __other)"; _out << sb; - _out << nl << "if(other == null)"; + _out << nl << "if(__other == null)"; _out << sb; _out << nl << "return 1;"; _out << eb; - _out << nl << "if(object.ReferenceEquals(this, other))"; + _out << nl << "if(object.ReferenceEquals(this, __other))"; _out << sb; _out << nl << "return 0;"; _out << eb; - _out << nl << "if(!(other is " << name << "))"; + _out << nl << "if(!(__other is " << name << "))"; _out << sb; - _out << nl << "throw new System.ArgumentException(\"expected argument of type `" << name << "'\", \"other\");"; + _out << nl << "throw new System.ArgumentException(\"expected argument of type `" << name << "'\", \"__other\");"; _out << eb; + _out << nl << "int __ret;"; for(q = dataMembers.begin(); q != dataMembers.end(); ++q) { string memberName = fixKwd((*q)->name()); - _out << nl << "if(this." << memberName << " < ((" << name << ")other)." << memberName << ")"; + _out << nl << "if((object)" << memberName << " == null && __other != null)"; _out << sb; _out << nl << "return -1;"; _out << eb; - _out << nl << "if(this." << memberName << " > ((" << name << ")other)." << memberName << ")"; + _out << nl << "if((__ret = " << memberName << ".CompareTo(((" << name << ")__other)." + << memberName << ")) != 0)"; _out << sb; - _out << nl << "return 1;"; + _out << nl << "return __ret;"; _out << eb; } _out << nl << "return 0;"; _out << eb; - _out << sp << nl << "public override int GetHashCode()"; + _out << sp << nl << "#endregion"; // IComparable members + + _out << sp << nl << "#region Comparison operators"; + + _out << sp << nl << "public static bool operator==(" << name << " __lhs, " << name << " __rhs)"; _out << sb; - _out << nl << "int hash = 0;"; - for(q = dataMembers.begin(); q != dataMembers.end(); ++q) - { - string memberName = fixKwd((*q)->name()); - _out << nl << "hash = (hash << 5) ^ " << memberName << ".GetHashCode();"; - } - _out << nl << "return hash;"; + _out << nl << "return Equals(__lhs, __rhs);"; _out << eb; - _out << sp << nl << "public override bool Equals(object other)"; + _out << sp << nl << "public static bool operator!=(" << name << " __lhs, " << name << " __rhs)"; _out << sb; - _out << nl << "return CompareTo(other) == 0;"; + _out << nl << "return !Equals(__lhs, __rhs);"; + _out << eb; + + _out << sp << nl << "public static bool operator<(" << name << " __lhs, " << name << " __rhs)"; + _out << sb; + _out << nl << "return __lhs == null ? __rhs != null : __lhs.CompareTo(__rhs) < 0;"; _out << eb; - _out << sp << nl << "public static bool Equals(" << name << " lhs, " << name << " rhs)"; + _out << sp << nl << "public static bool operator>(" << name << " __lhs, " << name << " __rhs)"; _out << sb; - _out << nl << "return lhs == null ? rhs == null : lhs.CompareTo(rhs) == 0;"; + _out << nl << "return __lhs == null ? false : __lhs.CompareTo(__rhs) > 0;"; _out << eb; - _out << sp << nl << "public static bool operator==(" << name << " lhs, " << name << " rhs)"; + _out << sp << nl << "public static bool operator<=(" << name << " __lhs, " << name << " __rhs)"; _out << sb; - _out << nl << "return Equals(lhs, rhs);"; + _out << nl << "return __lhs == null ? true : __lhs.CompareTo(__rhs) <= 0;"; _out << eb; - _out << sp << nl << "public static bool operator!=(" << name << " lhs, " << name << " rhs)"; + _out << sp << nl << "public static bool operator>=(" << name << " __lhs, " << name << " __rhs)"; _out << sb; - _out << nl << "return !Equals(lhs, rhs);"; + _out << nl << "return __lhs == null ? __rhs == null : __lhs.CompareTo(__rhs) >= 0;"; _out << eb; - _out << sp << nl << "public static bool operator<(" << name << " lhs, " << name << " rhs)"; + _out << sp << nl << "#endregion"; // Comparison operators + + _out << sp << nl << "#region ICloneable members"; + + _out << sp << nl << "public object Clone()"; _out << sb; - _out << nl << "return lhs == null ? rhs != null : lhs.CompareTo(rhs) < 0;"; + _out << nl << name << " __ret = new " << name << "();"; + for(q = dataMembers.begin(); q != dataMembers.end(); ++q) + { + string memberName = fixKwd((*q)->name()); + _out << nl << "__ret." << memberName << " = this." << memberName << ";"; + } + _out << nl << "return __ret;"; _out << eb; - _out << sp << nl << "public static bool operator>(" << name << " lhs, " << name << " rhs)"; + _out << sp << nl << "#endregion"; // ICloneable members + + _out << sp << nl << "#region Object members"; + + _out << sp << nl << "public override int GetHashCode()"; _out << sb; - _out << nl << "return lhs == null ? false : lhs.CompareTo(rhs) > 0;"; + _out << nl << "int __h = 0;"; + for(q = dataMembers.begin(); q != dataMembers.end(); ++q) + { + string memberName = fixKwd((*q)->name()); + _out << nl << "__h = (__h << 5) ^ " << memberName << ".GetHashCode();"; + } + _out << nl << "return __h;"; _out << eb; - _out << sp << nl << "public static bool operator<=(" << name << " lhs, " << name << " rhs)"; + _out << sp << nl << "public override bool Equals(object other)"; _out << sb; - _out << nl << "return lhs == null ? true : lhs.CompareTo(rhs) <= 0;"; + _out << nl << "return CompareTo(other) == 0;"; _out << eb; - _out << sp << nl << "public static bool operator>=(" << name << " lhs, " << name << " rhs)"; + _out << sp << nl << "public static bool Equals(" << name << " __lhs, " << name << " __rhs)"; _out << sb; - _out << nl << "return lhs == null ? rhs == null : lhs.CompareTo(rhs) >= 0;"; + _out << nl << "return __lhs == null ? __rhs == null : __lhs.CompareTo(__rhs) == 0;"; _out << eb; + _out << sp << nl << "#endregion"; // Object members + + _out << sp << nl << "#endregion"; // struct + _out << eb; } @@ -508,9 +812,15 @@ Slice::Gen::TypesVisitor::visitDictionary(const DictionaryPtr& p) string vs = typeToString(p->valueType()); _out << sp << nl << "public class " << name - << " : System.Collections.DictionaryBase, System.ICloneable, System.IComparable"; + << " : System.Collections.DictionaryBase, System.IComparable, System.ICloneable"; _out << sb; + _out << sp << nl << "#region " << name << " members"; + + _out << sp << nl << "#region " << name << " properties"; + + _out << sp << nl << "#region Indexer"; + _out << nl << "public " << vs << " this[" << ks << " key]"; _out << sb; _out << nl << "get"; @@ -524,6 +834,8 @@ Slice::Gen::TypesVisitor::visitDictionary(const DictionaryPtr& p) _out << eb; _out << eb; + _out << sp << nl << "#endregion"; // Indexer + _out << sp << nl << "public System.Collections.ICollection Keys"; _out << sb; _out << nl << "get"; @@ -540,34 +852,28 @@ Slice::Gen::TypesVisitor::visitDictionary(const DictionaryPtr& p) _out << eb; _out << eb; - _out << sp << nl << "public void"; - _out << nl << "Add(" << ks << " key, " << vs << " value)"; + _out << sp << nl << "#endregion"; // properties + + _out << sp << nl << "#region IDictionary members"; + + _out << sp << nl << "public void Add(" << ks << " key, " << vs << " value)"; _out << sb; _out << nl << "Dictionary[key] = value;"; _out << eb; - _out << sp << nl << "public void"; - _out << nl << "Remove(" << ks << " key)"; + _out << sp << nl << "public void Remove(" << ks << " key)"; _out << sb; _out << nl << "Dictionary.Remove(key);"; _out << eb; - _out << sp << nl << "public bool"; - _out << nl << "Contains(" << ks << " key)"; + _out << sp << nl << "public bool Contains(" << ks << " key)"; _out << sb; _out << nl << "return Dictionary.Contains(key);"; _out << eb; - _out << sp << nl << "public object"; - _out << nl << "Clone()"; - _out << sb; - _out << nl << name << " __ret = new " << name << "();"; - _out << nl << "foreach(System.Collections.DictionaryEntry __i in Dictionary)"; - _out << sb; - _out << nl << "__ret[(" << ks << ")__i.Key] = (" << vs << ")__i.Value;"; - _out << eb; - _out << nl << "return __ret;"; - _out << eb; + _out << sp << nl << "#endregion"; // IDictionary members + + _out << sp << nl << "#region IComparable members"; _out << sp << nl << "public int CompareTo(object other)"; _out << sb; @@ -584,95 +890,119 @@ Slice::Gen::TypesVisitor::visitDictionary(const DictionaryPtr& p) _out << nl << "throw new System.ArgumentException(\"CompareTo: expected argument of type `" << name << "'\", \"other\");"; _out << eb; - _out << nl << ks << "[] __klhs = new " << ks << "[Count];"; - _out << nl << "Keys.CopyTo(__klhs, 0);"; - _out << nl << "System.Array.Sort(__klhs);"; - _out << nl << ks << "[] __krhs = new " << ks << "[((" << name << ")other).Count];"; - _out << nl << "((" << name << ")other).Keys.CopyTo(__krhs, 0);"; - _out << nl << "System.Array.Sort(__krhs);"; - _out << nl << "int __limit = System.Math.Min(__klhs.Length, __krhs.Length);"; - _out << nl << "for(int __i = 0; __i < __limit; ++__i)"; + _out << nl << ks << "[] k__lhs = new " << ks << "[Count];"; + _out << nl << "Keys.CopyTo(k__lhs, 0);"; + _out << nl << "System.Array.Sort(k__lhs);"; + _out << nl << ks << "[] k__rhs = new " << ks << "[((" << name << ")other).Count];"; + _out << nl << "((" << name << ")other).Keys.CopyTo(k__rhs, 0);"; + _out << nl << "System.Array.Sort(k__rhs);"; + _out << nl << "int limit = System.Math.Min(k__lhs.Length, k__rhs.Length);"; + _out << nl << "for(int i = 0; i < limit; ++i)"; _out << sb; - _out << nl << "if(__klhs[__i] < __krhs[__i])"; + _out << nl << "int ret = k__lhs[i].CompareTo(k__rhs[i]);"; + _out << nl << "if(ret != 0)"; _out << sb; - _out << nl << "return -1;"; + _out << nl << "return ret;"; _out << eb; - _out << nl << "if(__klhs[__i] > __krhs[__i])"; - _out << sb; - _out << nl << "return 1;"; _out << eb; - _out << eb; - _out << nl << vs << "[] __vlhs = new " << vs << "[Count];"; - _out << nl << "Values.CopyTo(__vlhs, 0);"; - _out << nl << "System.Array.Sort(__vlhs);"; - _out << nl << vs << "[] __vrhs = new " << vs << "[((" << name << ")other).Count];"; - _out << nl << "((" << name << ")other).Values.CopyTo(__vrhs, 0);"; - _out << nl << "System.Array.Sort(__vrhs);"; - _out << nl << "for(int __i = 0; __i < __limit; ++__i)"; + _out << nl << vs << "[] v__lhs = new " << vs << "[Count];"; + _out << nl << "Values.CopyTo(v__lhs, 0);"; + _out << nl << "System.Array.Sort(v__lhs);"; + _out << nl << vs << "[] v__rhs = new " << vs << "[((" << name << ")other).Count];"; + _out << nl << "((" << name << ")other).Values.CopyTo(v__rhs, 0);"; + _out << nl << "System.Array.Sort(v__rhs);"; + _out << nl << "for(int i = 0; i < limit; ++i)"; _out << sb; - _out << nl << "if(__vlhs[__i] < __vrhs[__i])"; + _out << nl << "if((object)v__lhs[i] == null && v__rhs[i] != null)"; _out << sb; _out << nl << "return -1;"; _out << eb; - _out << nl << "if(__vlhs[__i] > __vrhs[__i])"; + _out << nl << "int ret = v__lhs[i].CompareTo(v__rhs[i]);"; + _out << nl << "if(ret != 0)"; _out << sb; - _out << nl << "return 1;"; + _out << nl << "return ret;"; _out << eb; _out << eb; - _out << nl << "return __klhs.Length < __krhs.Length ? -1 : (__klhs.Length > __krhs.Length ? 1 : 0);"; + _out << nl << "return k__lhs.Length < k__rhs.Length ? -1 : (k__lhs.Length > k__rhs.Length ? 1 : 0);"; _out << eb; - _out << sp << nl << "public override int GetHashCode()"; - _out << sb; - _out << nl << "int hash = 0;"; - _out << nl << "foreach(System.Collections.DictionaryEntry e in Dictionary)"; + _out << sp << nl << "#endregion"; // IComparable members + + _out << sp << nl << "#region Comparison operators"; + + _out << sp << nl << "public static bool operator==(" << name << " __lhs, " << name << " __rhs)"; _out << sb; - _out << nl << "hash = (hash << 5) ^ e.Key.GetHashCode();"; - _out << nl << "hash = (hash << 5) ^ e.Value.GetHashCode();"; + _out << nl << "return Equals(__lhs, __rhs);"; _out << eb; - _out << nl << "return hash;"; + + _out << sp << nl << "public static bool operator!=(" << name << " __lhs, " << name << " __rhs)"; + _out << sb; + _out << nl << "return !Equals(__lhs, __rhs);"; _out << eb; - _out << sp << nl << "public override bool Equals(object other)"; + _out << sp << nl << "public static bool operator<(" << name << " __lhs, " << name << " __rhs)"; _out << sb; - _out << nl << "return CompareTo(other) == 0;"; + _out << nl << "return __lhs == null ? __rhs != null : __lhs.CompareTo(__rhs) < 0;"; _out << eb; - _out << sp << nl << "public static bool Equals(" << name << " lhs, " << name << " rhs)"; + _out << sp << nl << "public static bool operator>(" << name << " __lhs, " << name << " __rhs)"; _out << sb; - _out << nl << "return lhs == null ? rhs == null : lhs.CompareTo(rhs) == 0;"; + _out << nl << "return __lhs == null ? false : __lhs.CompareTo(__rhs) > 0;"; _out << eb; - _out << sp << nl << "public static bool operator==(" << name << " lhs, " << name << " rhs)"; + _out << sp << nl << "public static bool operator<=(" << name << " __lhs, " << name << " __rhs)"; _out << sb; - _out << nl << "return Equals(lhs, rhs);"; + _out << nl << "return __lhs == null ? true : __lhs.CompareTo(__rhs) <= 0;"; _out << eb; - _out << sp << nl << "public static bool operator!=(" << name << " lhs, " << name << " rhs)"; + _out << sp << nl << "public static bool operator>=(" << name << " __lhs, " << name << " __rhs)"; _out << sb; - _out << nl << "return !Equals(lhs, rhs);"; + _out << nl << "return __lhs == null ? __rhs == null : __lhs.CompareTo(__rhs) >= 0;"; _out << eb; - _out << sp << nl << "public static bool operator<(" << name << " lhs, " << name << " rhs)"; + _out << sp << nl << "#endregion"; // Comparison operators + + _out << sp << nl << "#region ICloneable members"; + + _out << sp << nl << "public object Clone()"; + _out << sb; + _out << nl << name << " __ret = new " << name << "();"; + _out << nl << "foreach(System.Collections.DictionaryEntry i in Dictionary)"; _out << sb; - _out << nl << "return lhs == null ? rhs != null : lhs.CompareTo(rhs) < 0;"; + _out << nl << "__ret[(" << ks << ")i.Key] = (" << vs << ")i.Value;"; + _out << eb; + _out << nl << "return __ret;"; _out << eb; - _out << sp << nl << "public static bool operator>(" << name << " lhs, " << name << " rhs)"; + _out << sp << nl << "#endregion"; // ICloneable members + + _out << sp << nl << "#region Object members"; + + _out << sp << nl << "public override int GetHashCode()"; _out << sb; - _out << nl << "return lhs == null ? false : lhs.CompareTo(rhs) > 0;"; + _out << nl << "int hash = 0;"; + _out << nl << "foreach(System.Collections.DictionaryEntry e in Dictionary)"; + _out << sb; + _out << nl << "hash = (hash << 5) ^ e.Key.GetHashCode();"; + _out << nl << "hash = (hash << 5) ^ e.Value.GetHashCode();"; + _out << eb; + _out << nl << "return hash;"; _out << eb; - _out << sp << nl << "public static bool operator<=(" << name << " lhs, " << name << " rhs)"; + _out << sp << nl << "public override bool Equals(object other)"; _out << sb; - _out << nl << "return lhs == null ? true : lhs.CompareTo(rhs) <= 0;"; + _out << nl << "return CompareTo(other) == 0;"; _out << eb; - _out << sp << nl << "public static bool operator>=(" << name << " lhs, " << name << " rhs)"; + _out << sp << nl << "public static bool Equals(" << name << " __lhs, " << name << " __rhs)"; _out << sb; - _out << nl << "return lhs == null ? rhs == null : lhs.CompareTo(rhs) >= 0;"; + _out << nl << "return __lhs == null ? __rhs == null : __lhs.CompareTo(__rhs) == 0;"; _out << eb; + _out << sp << nl << "#endregion"; // Object members + + _out << sp << nl << "#endregion"; // dictionary + _out << eb; } |