Setting.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /* Copyright 2010-present 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.Driver
  16. {
  17. /// <summary>
  18. /// Represents a setting that may or may not have been set.
  19. /// </summary>
  20. /// <typeparam name="T">The type of the value.</typeparam>
  21. public struct Setting<T>
  22. {
  23. // private fields
  24. private T _value;
  25. private bool _hasBeenSet;
  26. // public properties
  27. /// <summary>
  28. /// Gets the value of the setting.
  29. /// </summary>
  30. public T Value
  31. {
  32. get { return _value; }
  33. set {
  34. _value = value;
  35. _hasBeenSet = true;
  36. }
  37. }
  38. /// <summary>
  39. /// Gets a value indicating whether the setting has been set.
  40. /// </summary>
  41. public bool HasBeenSet
  42. {
  43. get { return _hasBeenSet; }
  44. }
  45. // public methods
  46. /// <summary>
  47. /// Resets the setting to the unset state.
  48. /// </summary>
  49. public void Reset()
  50. {
  51. _value = default(T);
  52. _hasBeenSet = false;
  53. }
  54. /// <summary>
  55. /// Gets a canonical string representation for this setting.
  56. /// </summary>
  57. /// <returns>A canonical string representation for this setting.</returns>
  58. public override string ToString()
  59. {
  60. return _hasBeenSet ? ((_value == null) ? "null" : _value.ToString()) : "default";
  61. }
  62. // internal methods
  63. internal Setting<T> Clone()
  64. {
  65. var clone = new Setting<T>();
  66. clone._value = _value;
  67. clone._hasBeenSet = _hasBeenSet;
  68. return clone;
  69. }
  70. }
  71. }