GeneratedMessage.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. // Check that all required fields are present.
  62. foreach (FieldDescriptor field in DescriptorForType.Fields) {
  63. if (field.IsRequired && !HasField(field)) {
  64. return false;
  65. }
  66. }
  67. // Check that embedded messages are initialized.
  68. // This code is similar to that in AbstractMessage, but we don't
  69. // fetch all the field values - just the ones we need to.
  70. foreach (FieldDescriptor field in DescriptorForType.Fields) {
  71. if (field.MappedType == MappedType.Message) {
  72. if (field.IsRepeated) {
  73. // We know it's an IList<T>, but not the exact type - so
  74. // IEnumerable is the best we can do. (C# generics aren't covariant yet.)
  75. foreach (IMessage element in (IEnumerable) this[field]) {
  76. if (!element.IsInitialized) {
  77. return false;
  78. }
  79. }
  80. } else {
  81. if (!((IMessage) this[field]).IsInitialized) {
  82. return false;
  83. }
  84. }
  85. }
  86. }
  87. return true;
  88. }
  89. }
  90. public override IDictionary<FieldDescriptor, object> AllFields {
  91. get { return Dictionaries.AsReadOnly(GetMutableFieldMap()); }
  92. }
  93. public override bool HasField(FieldDescriptor field) {
  94. return InternalFieldAccessors[field].Has(ThisMessage);
  95. }
  96. public override int GetRepeatedFieldCount(FieldDescriptor field) {
  97. return InternalFieldAccessors[field].GetRepeatedCount(ThisMessage);
  98. }
  99. public override object this[FieldDescriptor field, int index] {
  100. get { return InternalFieldAccessors[field].GetRepeatedValue(this, index); }
  101. }
  102. public override object this[FieldDescriptor field] {
  103. get { return InternalFieldAccessors[field].GetValue(ThisMessage); }
  104. }
  105. public override UnknownFieldSet UnknownFields {
  106. get { return unknownFields; }
  107. }
  108. /// <summary>
  109. /// Replaces the set of unknown fields for this message. This should
  110. /// only be used before a message is built, by the builder. (In the
  111. /// Java code it is private, but the builder is nested so has access
  112. /// to it.)
  113. /// </summary>
  114. internal void SetUnknownFields(UnknownFieldSet fieldSet) {
  115. unknownFields = fieldSet;
  116. }
  117. }
  118. }