Nullable.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. //
  2. // Nullable.h
  3. //
  4. // Library: Foundation
  5. // Package: Core
  6. // Module: Nullable
  7. //
  8. // Definition of the Nullable template class.
  9. //
  10. // Copyright (c) 2010, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #ifndef Foundation_Nullable_INCLUDED
  16. #define Foundation_Nullable_INCLUDED
  17. #include "Poco/Foundation.h"
  18. #include "Poco/Exception.h"
  19. #include <algorithm>
  20. #include <iostream>
  21. namespace Poco {
  22. enum NullType
  23. {
  24. NULL_GENERIC = 0
  25. };
  26. template <typename C>
  27. class Nullable
  28. /// Nullable is a simple wrapper class for value types
  29. /// that allows objects or native type variables
  30. /// to have "null" value.
  31. ///
  32. /// The class is useful for passing parameters to functions
  33. /// when parameters are optional and no default values
  34. /// should be used or when a non-assigned state is needed,
  35. /// such as in e.g. fetching null values from database.
  36. ///
  37. /// A Nullable can be default constructed. In this case,
  38. /// the Nullable will have a Null value and isNull() will
  39. /// return true. Calling value() (without default value) on
  40. /// a Null object will throw a NullValueException.
  41. ///
  42. /// A Nullable can also be constructed from a value.
  43. /// It is possible to assign a value to a Nullable, and
  44. /// to reset a Nullable to contain a Null value by calling
  45. /// clear().
  46. ///
  47. /// For use with Nullable, the value type should support
  48. /// default construction.
  49. {
  50. public:
  51. Nullable():
  52. /// Creates an empty Nullable.
  53. _value(),
  54. _isNull(true),
  55. _null()
  56. {
  57. }
  58. Nullable(const NullType&):
  59. /// Creates an empty Nullable.
  60. _value(),
  61. _isNull(true),
  62. _null()
  63. {
  64. }
  65. Nullable(const C& value):
  66. /// Creates a Nullable with the given value.
  67. _value(value),
  68. _isNull(false),
  69. _null()
  70. {
  71. }
  72. Nullable(const Nullable& other):
  73. /// Creates a Nullable by copying another one.
  74. _value(other._value),
  75. _isNull(other._isNull),
  76. _null()
  77. {
  78. }
  79. ~Nullable()
  80. /// Destroys the Nullable.
  81. {
  82. }
  83. Nullable& assign(const C& value)
  84. /// Assigns a value to the Nullable.
  85. {
  86. _value = value;
  87. _isNull = false;
  88. return *this;
  89. }
  90. Nullable& assign(const Nullable& other)
  91. /// Assigns another Nullable.
  92. {
  93. Nullable tmp(other);
  94. swap(tmp);
  95. return *this;
  96. }
  97. Nullable& assign(NullType)
  98. /// Sets value to null.
  99. {
  100. _isNull = true;
  101. return *this;
  102. }
  103. Nullable& operator = (const C& value)
  104. /// Assigns a value to the Nullable.
  105. {
  106. return assign(value);
  107. }
  108. Nullable& operator = (const Nullable& other)
  109. /// Assigns another Nullable.
  110. {
  111. return assign(other);
  112. }
  113. Nullable& operator = (NullType)
  114. /// Assigns another Nullable.
  115. {
  116. _isNull = true;
  117. return *this;
  118. }
  119. void swap(Nullable& other)
  120. /// Swaps this Nullable with other.
  121. {
  122. std::swap(_value, other._value);
  123. std::swap(_isNull, other._isNull);
  124. }
  125. bool operator == (const Nullable<C>& other) const
  126. /// Compares two Nullables for equality
  127. {
  128. return (_isNull && other._isNull) || (_isNull == other._isNull && _value == other._value);
  129. }
  130. bool operator == (const C& value) const
  131. /// Compares Nullable with value for equality
  132. {
  133. return (!_isNull && _value == value);
  134. }
  135. bool operator == (const NullType&) const
  136. /// Compares Nullable with NullData for equality
  137. {
  138. return _isNull;
  139. }
  140. bool operator != (const C& value) const
  141. /// Compares Nullable with value for non equality
  142. {
  143. return !(*this == value);
  144. }
  145. bool operator != (const Nullable<C>& other) const
  146. /// Compares two Nullables for non equality
  147. {
  148. return !(*this == other);
  149. }
  150. bool operator != (const NullType&) const
  151. /// Compares with NullData for non equality
  152. {
  153. return !_isNull;
  154. }
  155. bool operator < (const Nullable<C>& other) const
  156. /// Compares two Nullable objects. Return true if this object's
  157. /// value is smaler than the other object's value.
  158. /// Null value is smaller than a non-null value.
  159. {
  160. if (_isNull && other._isNull) return false;
  161. if (!_isNull && !other._isNull)
  162. return (_value < other._value);
  163. if (_isNull && !other._isNull) return true;
  164. return false;
  165. }
  166. bool operator > (const Nullable<C>& other) const
  167. /// Compares two Nullable objects. Return true if this object's
  168. /// value is greater than the other object's value.
  169. /// A non-null value is greater than a null value.
  170. {
  171. return !(*this == other) && !(*this < other);
  172. }
  173. C& value()
  174. /// Returns the Nullable's value.
  175. ///
  176. /// Throws a NullValueException if the Nullable is empty.
  177. {
  178. if (!_isNull)
  179. return _value;
  180. else
  181. throw NullValueException();
  182. }
  183. const C& value() const
  184. /// Returns the Nullable's value.
  185. ///
  186. /// Throws a NullValueException if the Nullable is empty.
  187. {
  188. if (!_isNull)
  189. return _value;
  190. else
  191. throw NullValueException();
  192. }
  193. const C& value(const C& deflt) const
  194. /// Returns the Nullable's value, or the
  195. /// given default value if the Nullable is empty.
  196. {
  197. return _isNull ? deflt : _value;
  198. }
  199. operator C& ()
  200. /// Get reference to the value
  201. {
  202. return value();
  203. }
  204. operator const C& () const
  205. /// Get const reference to the value
  206. {
  207. return value();
  208. }
  209. operator NullType& ()
  210. /// Get reference to the value
  211. {
  212. return _null;
  213. }
  214. bool isNull() const
  215. /// Returns true if the Nullable is empty.
  216. {
  217. return _isNull;
  218. }
  219. void clear()
  220. /// Clears the Nullable.
  221. {
  222. _isNull = true;
  223. }
  224. private:
  225. C _value;
  226. bool _isNull;
  227. NullType _null;
  228. };
  229. template <typename C>
  230. inline void swap(Nullable<C>& n1, Nullable<C>& n2)
  231. {
  232. n1.swap(n2);
  233. }
  234. template <typename C>
  235. std::ostream& operator<<(std::ostream& out, const Nullable<C>& obj)
  236. {
  237. if (!obj.isNull()) out << obj.value();
  238. return out;
  239. }
  240. template <typename C>
  241. bool operator == (const NullType&, const Nullable<C>& n)
  242. /// Returns true if this Nullable is null.
  243. {
  244. return n.isNull();
  245. }
  246. template <typename C>
  247. bool operator != (const C& c, const Nullable<C>& n)
  248. /// Compares Nullable with value for non equality
  249. {
  250. return !(n == c);
  251. }
  252. template <typename C>
  253. bool operator == (const C& c, const Nullable<C>& n)
  254. /// Compares Nullable with NullData for equality
  255. {
  256. return (n == c);
  257. }
  258. template <typename C>
  259. bool operator != (const NullType&, const Nullable<C>& n)
  260. /// Returns true if this Nullable is not null.
  261. {
  262. return !n.isNull();
  263. }
  264. } // namespace Poco
  265. #endif // Foundation_Nullable_INCLUDED