GeneratedBuilderLite.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #region Copyright notice and license
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2008 Google Inc. All rights reserved.
  4. // http://github.com/jskeet/dotnet-protobufs/
  5. // Original C++/Java/Python code:
  6. // http://code.google.com/p/protobuf/
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions are
  10. // met:
  11. //
  12. // * Redistributions of source code must retain the above copyright
  13. // notice, this list of conditions and the following disclaimer.
  14. // * Redistributions in binary form must reproduce the above
  15. // copyright notice, this list of conditions and the following disclaimer
  16. // in the documentation and/or other materials provided with the
  17. // distribution.
  18. // * Neither the name of Google Inc. nor the names of its
  19. // contributors may be used to endorse or promote products derived from
  20. // this software without specific prior written permission.
  21. //
  22. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  25. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  26. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  27. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  28. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  30. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. #endregion
  34. using System;
  35. using System.Collections.Generic;
  36. namespace Google.ProtocolBuffers
  37. {
  38. /// <summary>
  39. /// All generated protocol message builder classes extend this class. It implements
  40. /// most of the IBuilder interface using reflection. Users can ignore this class
  41. /// as an implementation detail.
  42. /// </summary>
  43. public abstract partial class GeneratedBuilderLite<TMessage, TBuilder> : AbstractBuilderLite<TMessage, TBuilder>
  44. where TMessage : GeneratedMessageLite<TMessage, TBuilder>
  45. where TBuilder : GeneratedBuilderLite<TMessage, TBuilder>
  46. {
  47. /// <summary>
  48. /// Returns the message being built at the moment.
  49. /// </summary>
  50. protected abstract TMessage MessageBeingBuilt { get; }
  51. public override TBuilder MergeFrom(IMessageLite other)
  52. {
  53. //do nothing, Lite runtime does not support cross-message merges
  54. return ThisBuilder;
  55. }
  56. public abstract TBuilder MergeFrom(TMessage other);
  57. /// <summary>
  58. /// Called by derived classes to parse an unknown field.
  59. /// </summary>
  60. /// <returns>true unless the tag is an end-group tag</returns>
  61. [CLSCompliant(false)]
  62. protected virtual bool ParseUnknownField(ICodedInputStream input,
  63. ExtensionRegistry extensionRegistry, uint tag, string fieldName)
  64. {
  65. return input.SkipField();
  66. }
  67. /// <summary>
  68. /// Like Build(), but will wrap UninitializedMessageException in
  69. /// InvalidProtocolBufferException.
  70. /// </summary>
  71. public TMessage BuildParsed()
  72. {
  73. if (!IsInitialized)
  74. {
  75. throw new UninitializedMessageException(MessageBeingBuilt).AsInvalidProtocolBufferException();
  76. }
  77. return BuildPartial();
  78. }
  79. /// <summary>
  80. /// Implementation of <see cref="IBuilder{TMessage, TBuilder}.Build" />.
  81. /// </summary>
  82. public override TMessage Build()
  83. {
  84. // If the message is null, we'll throw a more appropriate exception in BuildPartial.
  85. if (!IsInitialized)
  86. {
  87. throw new UninitializedMessageException(MessageBeingBuilt);
  88. }
  89. return BuildPartial();
  90. }
  91. }
  92. }