-
Notifications
You must be signed in to change notification settings - Fork 592
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Map struct to record classes (and structs), use default Equals/GetHas… #2235
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2111,18 +2111,12 @@ Slice::Gen::TypesVisitor::visitStructStart(const StructPtr& p) | |
|
||
emitAttributes(p); | ||
emitPartialTypeAttributes(); | ||
_out << nl << "public " << (classMapping ? "sealed partial class" : "partial record struct") << ' ' << name; | ||
|
||
StringList baseNames; | ||
|
||
if (classMapping) | ||
{ | ||
baseNames.push_back("System.IEquatable<" + name + ">"); | ||
} | ||
_out << nl << "public " << (classMapping ? "sealed partial record class" : "partial record struct") << ' ' << name; | ||
|
||
// | ||
// Check for cs:implements metadata. | ||
// | ||
StringList baseNames; | ||
const StringList metaData = p->getMetaData(); | ||
static const string prefix = "cs:implements:"; | ||
for (StringList::const_iterator q = metaData.begin(); q != metaData.end(); ++q) | ||
|
@@ -2235,40 +2229,6 @@ Slice::Gen::TypesVisitor::visitStructEnd(const StructPtr& p) | |
_out << nl << "ice_initialize();"; | ||
_out << eb; | ||
|
||
if (classMapping) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We get all this (and more) for free from the record class. |
||
{ | ||
_out << sp; | ||
emitGeneratedCodeAttribute(); | ||
_out << nl << "public " << name << " Clone() => (" << name << ")MemberwiseClone();"; | ||
|
||
_out << sp; | ||
emitGeneratedCodeAttribute(); | ||
_out << nl << "public override int GetHashCode()"; | ||
_out << sb; | ||
writeMemberHashCode(dataMembers); | ||
_out << eb; | ||
|
||
_out << sp; | ||
emitGeneratedCodeAttribute(); | ||
_out << nl << "public override bool Equals(object? other) => Equals(other as " << name << ");"; | ||
|
||
_out << sp; | ||
emitGeneratedCodeAttribute(); | ||
_out << nl << "public bool Equals(" << name << "? other)"; | ||
_out << sb; | ||
writeMemberEquals(dataMembers); | ||
_out << eb; | ||
|
||
_out << sp; | ||
emitGeneratedCodeAttribute(); | ||
_out << nl << "public static bool operator ==(" << name << "? lhs, " << name << "? rhs) => "; | ||
_out << "lhs is not null ? lhs.Equals(rhs) : rhs is null;"; | ||
|
||
_out << sp; | ||
emitGeneratedCodeAttribute(); | ||
_out << nl << "public static bool operator !=(" << name << "? lhs, " << name << "? rhs) => !(lhs == rhs);"; | ||
} | ||
|
||
_out << sp; | ||
emitGeneratedCodeAttribute(); | ||
_out << nl << "public void ice_writeMembers(Ice.OutputStream ostr)"; | ||
|
@@ -2453,77 +2413,6 @@ Slice::Gen::TypesVisitor::visitDataMember(const DataMemberPtr& p) | |
} | ||
} | ||
|
||
void | ||
Slice::Gen::TypesVisitor::writeMemberHashCode(const DataMemberList& dataMembers) | ||
{ | ||
_out << nl << "var hash = new global::System.HashCode();"; | ||
assert(dataMembers.size() > 0); // a Slice struct must have at least one field. | ||
|
||
for (const auto& q : dataMembers) | ||
{ | ||
TypePtr memberType = q->type(); | ||
string memberName = fixId(q->name(), DotNet::ICloneable); | ||
|
||
if (dynamic_pointer_cast<Sequence>(memberType) || dynamic_pointer_cast<Dictionary>(memberType)) | ||
{ | ||
_out << nl << "Ice.UtilInternal.Collections.HashCodeAdd(ref hash, this." << memberName << ");"; | ||
} | ||
else | ||
{ | ||
_out << nl << "hash.Add(this." << memberName << ");"; | ||
} | ||
} | ||
_out << nl << "return hash.ToHashCode();"; | ||
} | ||
|
||
void | ||
Slice::Gen::TypesVisitor::writeMemberEquals(const DataMemberList& dataMembers) | ||
{ | ||
_out << nl << "if (ReferenceEquals(this, other))"; | ||
_out << sb; | ||
_out << nl << "return true;"; | ||
_out << eb; | ||
_out << nl << "return other is not null"; | ||
|
||
assert(!dataMembers.empty()); // a Slice struct must have at least one field. | ||
|
||
_out.inc(); | ||
|
||
for (DataMemberList::const_iterator q = dataMembers.begin(); q != dataMembers.end(); ++q) | ||
{ | ||
assert(!(*q)->optional()); // a Slice struct does not have optional fields. | ||
|
||
_out << " && " << nl; | ||
|
||
string memberName = fixId((*q)->name(), DotNet::ICloneable); | ||
TypePtr memberType = (*q)->type(); | ||
|
||
if (SequencePtr seq = dynamic_pointer_cast<Sequence>(memberType)) | ||
{ | ||
_out << "Ice.UtilInternal.Collections.NullableSequenceEqual(this." << memberName << ", other." << memberName | ||
<< ")"; | ||
} | ||
else if (DictionaryPtr dict = dynamic_pointer_cast<Dictionary>(memberType)) | ||
{ | ||
// Equals() for generic types does not have value semantics. | ||
_out << "Ice.UtilInternal.Collections.DictionaryEquals(this." << memberName << ", other." << memberName | ||
<< ")"; | ||
} | ||
else if (isProxyType(memberType)) | ||
{ | ||
// We need to cast it to the base concrete type to get == | ||
_out << "(Ice.ObjectPrxHelperBase?)this." << memberName << " == (Ice.ObjectPrxHelperBase?)other." | ||
<< memberName; | ||
} | ||
else | ||
{ | ||
_out << "this." << memberName << " == other." << memberName; | ||
} | ||
} | ||
_out.dec(); | ||
_out << ";"; | ||
} | ||
|
||
Slice::Gen::ResultVisitor::ResultVisitor(::IceUtilInternal::Output& out) : CsVisitor(out) {} | ||
|
||
namespace | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,26 +5,10 @@ | |
|
||
namespace Ice.UtilInternal; | ||
|
||
public sealed class Collections | ||
public static class Collections | ||
{ | ||
// Just like Enumerable.SequenceEqual except it also handles null sequences. | ||
// TODO: Note that this code would be removed by proposal #2115. | ||
public static bool NullableSequenceEqual<TSource>(IEnumerable<TSource> lhs, IEnumerable<TSource> rhs) | ||
{ | ||
if (lhs is null) | ||
{ | ||
return rhs is null || !rhs.Any(); | ||
} | ||
if (rhs is null) | ||
{ | ||
return !lhs.Any(); | ||
} | ||
|
||
return lhs.SequenceEqual(rhs); | ||
} | ||
|
||
// Seq is a nullable seq. | ||
public static void HashCodeAdd<TSource>(ref HashCode hash, IEnumerable<TSource> seq) | ||
internal static void HashCodeAdd<TSource>(ref HashCode hash, IEnumerable<TSource> seq) | ||
{ | ||
if (seq is not null) | ||
{ | ||
|
@@ -35,7 +19,7 @@ public static void HashCodeAdd<TSource>(ref HashCode hash, IEnumerable<TSource> | |
} | ||
} | ||
|
||
public static bool DictionaryEquals(IDictionary d1, IDictionary d2) | ||
internal static bool DictionaryEquals(IDictionary d1, IDictionary d2) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code is still bogus and used internally. Will cleanup in a follow-up PR. |
||
{ | ||
if (ReferenceEquals(d1, d2)) | ||
{ | ||
|
@@ -77,7 +61,7 @@ public static bool DictionaryEquals(IDictionary d1, IDictionary d2) | |
return false; | ||
} | ||
|
||
public static void HashCodeAdd<TKey, TValue>(ref HashCode hash, IDictionary<TKey, TValue> d) where TKey : notnull | ||
internal static void HashCodeAdd<TKey, TValue>(ref HashCode hash, IDictionary<TKey, TValue> d) where TKey : notnull | ||
{ | ||
if (d is not null) | ||
{ | ||
|
@@ -107,7 +91,7 @@ public static void Shuffle<T>(ref List<T> l) | |
} | ||
} | ||
|
||
public static void Sort<T>(ref List<T> array, IComparer<T> comparator) | ||
internal static void Sort<T>(ref List<T> array, IComparer<T> comparator) | ||
{ | ||
// | ||
// This Sort method implements the merge sort algorithm | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is common for record classes and structs to be read-only. However, here they are read-write for backwards source compatibility.