Commit 3b315088 authored by Jon Skeet's avatar Jon Skeet

Optimisations of IsInitialized and removal of unnecessary references.

parent 0677933d
......@@ -42,8 +42,6 @@
<HintPath>lib\Rhino.Mocks.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="AbstractMessageTest.cs" />
......
......@@ -67,12 +67,18 @@ namespace Google.ProtocolBuffers {
}
/// <summary>
/// Called by subclasses to check if all extensions are initialized.
/// Called to check if all extensions are initialized.
/// </summary>
protected bool ExtensionsAreInitialized {
get { return extensions.IsInitialized; }
}
public override bool IsInitialized {
get {
return base.IsInitialized && ExtensionsAreInitialized;
}
}
#region Reflection
public override IDictionary<FieldDescriptor, object> AllFields {
get {
......
......@@ -18,6 +18,7 @@ using System.Collections.Generic;
using Google.ProtocolBuffers.Collections;
using Google.ProtocolBuffers.Descriptors;
using Google.ProtocolBuffers.FieldAccess;
using System.Collections;
namespace Google.ProtocolBuffers {
......@@ -60,6 +61,39 @@ namespace Google.ProtocolBuffers {
return ret;
}
public override bool IsInitialized {
get {
// Check that all required fields are present.
foreach (FieldDescriptor field in DescriptorForType.Fields) {
if (field.IsRequired && !HasField(field)) {
return false;
}
}
// Check that embedded messages are initialized.
// This code is similar to that in AbstractMessage, but we don't
// fetch all the field values - just the ones we need to.
foreach (FieldDescriptor field in DescriptorForType.Fields) {
if (field.MappedType == MappedType.Message) {
if (field.IsRepeated) {
// We know it's an IList<T>, but not the exact type - so
// IEnumerable is the best we can do. (C# generics aren't covariant yet.)
foreach (IMessage element in (IEnumerable) this[field]) {
if (!element.IsInitialized) {
return false;
}
}
} else {
if (!((IMessage) this[field]).IsInitialized) {
return false;
}
}
}
}
return true;
}
}
public override IDictionary<FieldDescriptor, object> AllFields {
get { return Dictionaries.AsReadOnly(GetMutableFieldMap()); }
}
......
......@@ -34,8 +34,6 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="AbstractBuilder.cs" />
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment