diff options
author | Jon Skeet <skeet@pobox.com> | 2008-08-14 20:37:11 +0100 |
---|---|---|
committer | Jon Skeet <skeet@pobox.com> | 2008-08-14 20:37:11 +0100 |
commit | 3b3150881a677e3c8e1ceda5be25bb1ac9a725ea (patch) | |
tree | 396bea118f605ef8cc30d67533a4231e8b4d558d /csharp/ProtocolBuffers/GeneratedMessage.cs | |
parent | 0677933d9efbd322c0b310068b03d871eea3a49d (diff) | |
download | protobuf-3b3150881a677e3c8e1ceda5be25bb1ac9a725ea.tar.gz protobuf-3b3150881a677e3c8e1ceda5be25bb1ac9a725ea.tar.bz2 protobuf-3b3150881a677e3c8e1ceda5be25bb1ac9a725ea.zip |
Optimisations of IsInitialized and removal of unnecessary references.
Diffstat (limited to 'csharp/ProtocolBuffers/GeneratedMessage.cs')
-rw-r--r-- | csharp/ProtocolBuffers/GeneratedMessage.cs | 34 |
1 files changed, 34 insertions, 0 deletions
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<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()); } } |