BsonReaderSettings.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. /// <summary>
  19. /// Represents settings for a BsonReader.
  20. /// </summary>
  21. #if NET45
  22. [Serializable]
  23. #endif
  24. public abstract class BsonReaderSettings
  25. {
  26. // private fields
  27. private GuidRepresentation _guidRepresentation = BsonDefaults.GuidRepresentation;
  28. private bool _isFrozen;
  29. // constructors
  30. /// <summary>
  31. /// Initializes a new instance of the BsonReaderSettings class.
  32. /// </summary>
  33. protected BsonReaderSettings()
  34. {
  35. }
  36. /// <summary>
  37. /// Initializes a new instance of the BsonReaderSettings class.
  38. /// </summary>
  39. /// <param name="guidRepresentation">The representation for Guids.</param>
  40. protected BsonReaderSettings(GuidRepresentation guidRepresentation)
  41. {
  42. _guidRepresentation = guidRepresentation;
  43. }
  44. // public properties
  45. /// <summary>
  46. /// Gets or sets the representation for Guids.
  47. /// </summary>
  48. public GuidRepresentation GuidRepresentation
  49. {
  50. get { return _guidRepresentation; }
  51. set
  52. {
  53. if (_isFrozen) { ThrowFrozenException(); }
  54. _guidRepresentation = value;
  55. }
  56. }
  57. /// <summary>
  58. /// Gets whether the settings are frozen.
  59. /// </summary>
  60. public bool IsFrozen
  61. {
  62. get { return _isFrozen; }
  63. }
  64. // public methods
  65. /// <summary>
  66. /// Creates a clone of the settings.
  67. /// </summary>
  68. /// <returns>A clone of the settings.</returns>
  69. public BsonReaderSettings Clone()
  70. {
  71. return CloneImplementation();
  72. }
  73. /// <summary>
  74. /// Freezes the settings.
  75. /// </summary>
  76. /// <returns>The frozen settings.</returns>
  77. public BsonReaderSettings Freeze()
  78. {
  79. _isFrozen = true;
  80. return this;
  81. }
  82. /// <summary>
  83. /// Returns a frozen copy of the settings.
  84. /// </summary>
  85. /// <returns>A frozen copy of the settings.</returns>
  86. public BsonReaderSettings FrozenCopy()
  87. {
  88. if (_isFrozen)
  89. {
  90. return this;
  91. }
  92. else
  93. {
  94. return Clone().Freeze();
  95. }
  96. }
  97. // protected methods
  98. /// <summary>
  99. /// Creates a clone of the settings.
  100. /// </summary>
  101. /// <returns>A clone of the settings.</returns>
  102. protected abstract BsonReaderSettings CloneImplementation();
  103. /// <summary>
  104. /// Throws an InvalidOperationException when an attempt is made to change a setting after the settings are frozen.
  105. /// </summary>
  106. protected void ThrowFrozenException()
  107. {
  108. var message = string.Format("{0} is frozen.", this.GetType().Name);
  109. throw new InvalidOperationException(message);
  110. }
  111. }
  112. }