BsonReaderSettings.cs 3.5 KB

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