From 3b3150881a677e3c8e1ceda5be25bb1ac9a725ea Mon Sep 17 00:00:00 2001 From: Jon Skeet Date: Thu, 14 Aug 2008 20:37:11 +0100 Subject: Optimisations of IsInitialized and removal of unnecessary references. --- .../ProtocolBuffers.Test.csproj | 2 -- csharp/ProtocolBuffers/ExtendableMessage.cs | 8 ++++- csharp/ProtocolBuffers/GeneratedMessage.cs | 34 ++++++++++++++++++++++ csharp/ProtocolBuffers/ProtocolBuffers.csproj | 2 -- 4 files changed, 41 insertions(+), 5 deletions(-) (limited to 'csharp') diff --git a/csharp/ProtocolBuffers.Test/ProtocolBuffers.Test.csproj b/csharp/ProtocolBuffers.Test/ProtocolBuffers.Test.csproj index 015451fc..5b3a6f14 100644 --- a/csharp/ProtocolBuffers.Test/ProtocolBuffers.Test.csproj +++ b/csharp/ProtocolBuffers.Test/ProtocolBuffers.Test.csproj @@ -42,8 +42,6 @@ lib\Rhino.Mocks.dll - - diff --git a/csharp/ProtocolBuffers/ExtendableMessage.cs b/csharp/ProtocolBuffers/ExtendableMessage.cs index c3b37dd5..a4a6e17e 100644 --- a/csharp/ProtocolBuffers/ExtendableMessage.cs +++ b/csharp/ProtocolBuffers/ExtendableMessage.cs @@ -67,12 +67,18 @@ namespace Google.ProtocolBuffers { } /// - /// Called by subclasses to check if all extensions are initialized. + /// Called to check if all extensions are initialized. /// protected bool ExtensionsAreInitialized { get { return extensions.IsInitialized; } } + public override bool IsInitialized { + get { + return base.IsInitialized && ExtensionsAreInitialized; + } + } + #region Reflection public override IDictionary AllFields { get { diff --git a/csharp/ProtocolBuffers/GeneratedMessage.cs b/csharp/ProtocolBuffers/GeneratedMessage.cs index e1de84e1..479986f6 100644 --- a/csharp/ProtocolBuffers/GeneratedMessage.cs +++ b/csharp/ProtocolBuffers/GeneratedMessage.cs @@ -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, 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 AllFields { get { return Dictionaries.AsReadOnly(GetMutableFieldMap()); } } diff --git a/csharp/ProtocolBuffers/ProtocolBuffers.csproj b/csharp/ProtocolBuffers/ProtocolBuffers.csproj index adae31b4..e74d94db 100644 --- a/csharp/ProtocolBuffers/ProtocolBuffers.csproj +++ b/csharp/ProtocolBuffers/ProtocolBuffers.csproj @@ -34,8 +34,6 @@ - - -- cgit v1.2.3