BsonDocumentReaderContext.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. namespace MongoDB.Bson.IO
  16. {
  17. internal class BsonDocumentReaderContext
  18. {
  19. // private fields
  20. private BsonDocumentReaderContext _parentContext;
  21. private ContextType _contextType;
  22. private BsonDocument _document;
  23. private BsonArray _array;
  24. private int _index;
  25. // constructors
  26. internal BsonDocumentReaderContext(
  27. BsonDocumentReaderContext parentContext,
  28. ContextType contextType,
  29. BsonArray array)
  30. {
  31. _parentContext = parentContext;
  32. _contextType = contextType;
  33. _array = array;
  34. }
  35. internal BsonDocumentReaderContext(
  36. BsonDocumentReaderContext parentContext,
  37. ContextType contextType,
  38. BsonDocument document)
  39. {
  40. _parentContext = parentContext;
  41. _contextType = contextType;
  42. _document = document;
  43. }
  44. // used by Clone
  45. private BsonDocumentReaderContext(
  46. BsonDocumentReaderContext parentContext,
  47. ContextType contextType,
  48. BsonDocument document,
  49. BsonArray array,
  50. int index)
  51. {
  52. _parentContext = parentContext;
  53. _contextType = contextType;
  54. _document = document;
  55. _array = array;
  56. _index = index;
  57. }
  58. // internal properties
  59. internal BsonArray Array
  60. {
  61. get { return _array; }
  62. }
  63. internal ContextType ContextType
  64. {
  65. get { return _contextType; }
  66. }
  67. internal BsonDocument Document
  68. {
  69. get { return _document; }
  70. }
  71. internal int Index
  72. {
  73. get { return _index; }
  74. set { _index = value; }
  75. }
  76. // public methods
  77. /// <summary>
  78. /// Creates a clone of the context.
  79. /// </summary>
  80. /// <returns>A clone of the context.</returns>
  81. public BsonDocumentReaderContext Clone()
  82. {
  83. return new BsonDocumentReaderContext(_parentContext, _contextType, _document, _array, _index);
  84. }
  85. public bool TryGetNextElement(out BsonElement element)
  86. {
  87. if (_index < _document.ElementCount)
  88. {
  89. element = _document.GetElement(_index++);
  90. return true;
  91. }
  92. else
  93. {
  94. element = default(BsonElement);
  95. return false;
  96. }
  97. }
  98. public bool TryGetNextValue(out BsonValue value)
  99. {
  100. if (_index < _array.Count)
  101. {
  102. value = _array[_index++];
  103. return true;
  104. }
  105. else
  106. {
  107. value = null;
  108. return false;
  109. }
  110. }
  111. public BsonDocumentReaderContext PopContext()
  112. {
  113. return _parentContext;
  114. }
  115. }
  116. }