BsonBinaryReaderContext.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* Copyright 2010-2014 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. using System.IO;
  17. namespace MongoDB.Bson.IO
  18. {
  19. internal class BsonBinaryReaderContext
  20. {
  21. // private fields
  22. private BsonBinaryReaderContext _parentContext;
  23. private ContextType _contextType;
  24. private int _startPosition;
  25. private int _size;
  26. // constructors
  27. internal BsonBinaryReaderContext(
  28. BsonBinaryReaderContext parentContext,
  29. ContextType contextType,
  30. int startPosition,
  31. int size)
  32. {
  33. _parentContext = parentContext;
  34. _contextType = contextType;
  35. _startPosition = startPosition;
  36. _size = size;
  37. }
  38. // internal properties
  39. internal ContextType ContextType
  40. {
  41. get { return _contextType; }
  42. }
  43. // public methods
  44. /// <summary>
  45. /// Creates a clone of the context.
  46. /// </summary>
  47. /// <returns>A clone of the context.</returns>
  48. public BsonBinaryReaderContext Clone()
  49. {
  50. return new BsonBinaryReaderContext(_parentContext, _contextType, _startPosition, _size);
  51. }
  52. public BsonBinaryReaderContext PopContext(int position)
  53. {
  54. int actualSize = position - _startPosition;
  55. if (actualSize != _size)
  56. {
  57. var message = string.Format("Expected size to be {0}, not {1}.", _size, actualSize);
  58. throw new Exception(message);
  59. }
  60. return _parentContext;
  61. }
  62. }
  63. }