BsonBinaryReaderContext.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* Copyright 2010-2016 MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. namespace MongoDB.Bson.IO
  17. {
  18. internal class BsonBinaryReaderContext
  19. {
  20. // private fields
  21. private readonly BsonBinaryReaderContext _parentContext;
  22. private readonly ContextType _contextType;
  23. private readonly long _startPosition;
  24. private readonly long _size;
  25. private string _currentElementName;
  26. private int _currentArrayIndex = -1;
  27. // constructors
  28. internal BsonBinaryReaderContext(
  29. BsonBinaryReaderContext parentContext,
  30. ContextType contextType,
  31. long startPosition,
  32. long size)
  33. {
  34. _parentContext = parentContext;
  35. _contextType = contextType;
  36. _startPosition = startPosition;
  37. _size = size;
  38. }
  39. // public properties
  40. public ContextType ContextType
  41. {
  42. get { return _contextType; }
  43. }
  44. public int CurrentArrayIndex
  45. {
  46. get { return _currentArrayIndex; }
  47. set { _currentArrayIndex = value; }
  48. }
  49. public string CurrentElementName
  50. {
  51. get { return _currentElementName; }
  52. set { _currentElementName = value; }
  53. }
  54. public BsonBinaryReaderContext ParentContext
  55. {
  56. get { return _parentContext; }
  57. }
  58. // public methods
  59. /// <summary>
  60. /// Creates a clone of the context.
  61. /// </summary>
  62. /// <returns>A clone of the context.</returns>
  63. public BsonBinaryReaderContext Clone()
  64. {
  65. return new BsonBinaryReaderContext(_parentContext, _contextType, _startPosition, _size);
  66. }
  67. public BsonBinaryReaderContext PopContext(long position)
  68. {
  69. var actualSize = position - _startPosition;
  70. if (actualSize != _size)
  71. {
  72. var message = string.Format("Expected size to be {0}, not {1}.", _size, actualSize);
  73. throw new FormatException(message);
  74. }
  75. return _parentContext;
  76. }
  77. }
  78. }