Optional.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //
  2. // Optional.h
  3. //
  4. // $Id: //poco/1.4/Foundation/include/Poco/Optional.h#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Core
  8. // Module: Optional
  9. //
  10. // Definition of the Optional class template.
  11. //
  12. // Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
  13. // and Contributors.
  14. //
  15. // SPDX-License-Identifier: BSL-1.0
  16. //
  17. #ifndef Foundation_Optional_INCLUDED
  18. #define Foundation_Optional_INCLUDED
  19. #include "Poco/Foundation.h"
  20. #include "Poco/Exception.h"
  21. #include <algorithm>
  22. namespace Poco {
  23. template <typename C>
  24. class Optional
  25. /// Optional is a simple wrapper class for value types
  26. /// that allows to introduce a specified/unspecified state
  27. /// to value objects.
  28. ///
  29. /// An Optional can be default constructed. In this case,
  30. /// the Optional will have a Null value and isSpecified() will
  31. /// return false. Calling value()(without default value) on
  32. /// a Null object will throw a NullValueException.
  33. ///
  34. /// An Optional can also be constructed from a value.
  35. /// It is possible to assign a value to an Optional, and
  36. /// to reset an Optional to contain a Null value by calling
  37. /// clear().
  38. ///
  39. /// For use with Optional, the value type should support
  40. /// default construction.
  41. ///
  42. /// Note that the Optional class is basically the same as
  43. /// Nullable. However, serializers may treat Nullable
  44. /// and Optional differently. An example is XML serialization based
  45. /// on XML Schema, where Optional would be used for an element with
  46. /// minOccurs == 0, whereas Nullable would be used on an element with
  47. /// nillable == true.
  48. {
  49. public:
  50. Optional():
  51. /// Creates an empty Optional.
  52. _value(),
  53. _isSpecified(false)
  54. {
  55. }
  56. Optional(const C& value):
  57. /// Creates a Optional with the given value.
  58. _value(value),
  59. _isSpecified(true)
  60. {
  61. }
  62. Optional(const Optional& other):
  63. /// Creates a Optional by copying another one.
  64. _value(other._value),
  65. _isSpecified(other._isSpecified)
  66. {
  67. }
  68. ~Optional()
  69. /// Destroys the Optional.
  70. {
  71. }
  72. Optional& assign(const C& value)
  73. /// Assigns a value to the Optional.
  74. {
  75. _value = value;
  76. _isSpecified = true;
  77. return *this;
  78. }
  79. Optional& assign(const Optional& other)
  80. /// Assigns another Optional.
  81. {
  82. Optional tmp(other);
  83. swap(tmp);
  84. return *this;
  85. }
  86. Optional& operator = (const C& value)
  87. {
  88. return assign(value);
  89. }
  90. Optional& operator = (const Optional& other)
  91. {
  92. return assign(other);
  93. }
  94. void swap(Optional& other)
  95. {
  96. std::swap(_value, other._value);
  97. std::swap(_isSpecified, other._isSpecified);
  98. }
  99. const C& value() const
  100. /// Returns the Optional's value.
  101. ///
  102. /// Throws a Poco::NullValueException if the value has not been specified.
  103. {
  104. if (_isSpecified)
  105. return _value;
  106. else
  107. throw Poco::NullValueException();
  108. }
  109. const C& value(const C& deflt) const
  110. /// Returns the Optional's value, or the
  111. /// given default value if the Optional's
  112. /// value has not been specified.
  113. {
  114. return _isSpecified ? _value : deflt;
  115. }
  116. bool isSpecified() const
  117. /// Returns true iff the Optional's value has been specified.
  118. {
  119. return _isSpecified;
  120. }
  121. void clear()
  122. /// Clears the Optional.
  123. {
  124. _isSpecified = false;
  125. }
  126. private:
  127. C _value;
  128. bool _isSpecified;
  129. };
  130. template <typename C>
  131. inline void swap(Optional<C>& n1, Optional<C>& n2)
  132. {
  133. n1.swap(n2);
  134. }
  135. } // namespace Poco
  136. #endif // Foundation_Optional_INCLUDED