GeneratedMessage.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc.
  3. // http://code.google.com/p/protobuf/
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. using System;
  17. using System.Collections.Generic;
  18. using Google.ProtocolBuffers.Collections;
  19. using Google.ProtocolBuffers.Descriptors;
  20. using Google.ProtocolBuffers.FieldAccess;
  21. using System.Collections;
  22. namespace Google.ProtocolBuffers {
  23. /// <summary>
  24. /// All generated protocol message classes extend this class. It implements
  25. /// most of the IMessage interface using reflection. Users
  26. /// can ignore this class as an implementation detail.
  27. /// </summary>
  28. public abstract class GeneratedMessage<TMessage, TBuilder> : AbstractMessage<TMessage, TBuilder>
  29. where TMessage : GeneratedMessage<TMessage, TBuilder>
  30. where TBuilder : GeneratedBuilder<TMessage, TBuilder> {
  31. private UnknownFieldSet unknownFields = UnknownFieldSet.DefaultInstance;
  32. /// <summary>
  33. /// Returns the message as a TMessage.
  34. /// </summary>
  35. protected abstract TMessage ThisMessage { get; }
  36. internal FieldAccessorTable<TMessage, TBuilder> FieldAccessorsFromBuilder {
  37. get { return InternalFieldAccessors; }
  38. }
  39. protected abstract FieldAccessorTable<TMessage, TBuilder> InternalFieldAccessors { get; }
  40. public override MessageDescriptor DescriptorForType {
  41. get { return InternalFieldAccessors.Descriptor; }
  42. }
  43. internal IDictionary<FieldDescriptor, Object> GetMutableFieldMap() {
  44. // Use a SortedList so we'll end up serializing fields in order
  45. var ret = new SortedList<FieldDescriptor, object>();
  46. MessageDescriptor descriptor = DescriptorForType;
  47. foreach (FieldDescriptor field in descriptor.Fields) {
  48. IFieldAccessor<TMessage, TBuilder> accessor = InternalFieldAccessors[field];
  49. if (field.IsRepeated) {
  50. if (accessor.GetRepeatedCount(ThisMessage) != 0) {
  51. ret[field] = accessor.GetValue(ThisMessage);
  52. }
  53. } else if (HasField(field)) {
  54. ret[field] = accessor.GetValue(ThisMessage);
  55. }
  56. }
  57. return ret;
  58. }
  59. public override bool IsInitialized {
  60. get {
  61. /* if (!DescriptorForType.HasRequiredFields) {
  62. return true;
  63. }*/
  64. // Check that all required fields are present.
  65. foreach (FieldDescriptor field in DescriptorForType.Fields) {
  66. if (field.IsRequired && !HasField(field)) {
  67. return false;
  68. }
  69. }
  70. // Check that embedded messages are initialized.
  71. // This code is similar to that in AbstractMessage, but we don't
  72. // fetch all the field values - just the ones we need to.
  73. foreach (FieldDescriptor field in DescriptorForType.Fields) {
  74. if (field.MappedType == MappedType.Message) {
  75. if (field.IsRepeated) {
  76. // We know it's an IList<T>, but not the exact type - so
  77. // IEnumerable is the best we can do. (C# generics aren't covariant yet.)
  78. foreach (IMessage element in (IEnumerable) this[field]) {
  79. if (!element.IsInitialized) {
  80. return false;
  81. }
  82. }
  83. } else {
  84. if (!((IMessage) this[field]).IsInitialized) {
  85. return false;
  86. }
  87. }
  88. }
  89. }
  90. return true;
  91. }
  92. }
  93. public override IDictionary<FieldDescriptor, object> AllFields {
  94. get { return Dictionaries.AsReadOnly(GetMutableFieldMap()); }
  95. }
  96. public override bool HasField(FieldDescriptor field) {
  97. return InternalFieldAccessors[field].Has(ThisMessage);
  98. }
  99. public override int GetRepeatedFieldCount(FieldDescriptor field) {
  100. return InternalFieldAccessors[field].GetRepeatedCount(ThisMessage);
  101. }
  102. public override object this[FieldDescriptor field, int index] {
  103. get { return InternalFieldAccessors[field].GetRepeatedValue(ThisMessage, index); }
  104. }
  105. public override object this[FieldDescriptor field] {
  106. get { return InternalFieldAccessors[field].GetValue(ThisMessage); }
  107. }
  108. public override UnknownFieldSet UnknownFields {
  109. get { return unknownFields; }
  110. }
  111. /// <summary>
  112. /// Replaces the set of unknown fields for this message. This should
  113. /// only be used before a message is built, by the builder. (In the
  114. /// Java code it is private, but the builder is nested so has access
  115. /// to it.)
  116. /// </summary>
  117. internal void SetUnknownFields(UnknownFieldSet fieldSet) {
  118. unknownFields = fieldSet;
  119. }
  120. }
  121. }