optional.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. // Copyright 2019 Google LLC
  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. // https://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. #ifndef dap_optional_h
  15. #define dap_optional_h
  16. #include <assert.h>
  17. #include <type_traits>
  18. #include <utility> // std::move, std::forward
  19. namespace dap {
  20. // optional holds an 'optional' contained value.
  21. // This is similar to C++17's std::optional.
  22. template <typename T>
  23. class optional {
  24. template <typename U>
  25. using IsConvertibleToT =
  26. typename std::enable_if<std::is_convertible<U, T>::value>::type;
  27. public:
  28. using value_type = T;
  29. // constructors
  30. inline optional() = default;
  31. inline optional(const optional& other);
  32. inline optional(optional&& other);
  33. template <typename U>
  34. inline optional(const optional<U>& other);
  35. template <typename U>
  36. inline optional(optional<U>&& other);
  37. template <typename U = value_type, typename = IsConvertibleToT<U>>
  38. inline optional(U&& value);
  39. // value() returns the contained value.
  40. // If the optional does not contain a value, then value() will assert.
  41. inline T& value();
  42. inline const T& value() const;
  43. // value() returns the contained value, or defaultValue if the optional does
  44. // not contain a value.
  45. inline const T& value(const T& defaultValue) const;
  46. // operator bool() returns true if the optional contains a value.
  47. inline explicit operator bool() const noexcept;
  48. // has_value() returns true if the optional contains a value.
  49. inline bool has_value() const;
  50. // assignment
  51. inline optional& operator=(const optional& other);
  52. inline optional& operator=(optional&& other) noexcept;
  53. template <typename U = T, typename = IsConvertibleToT<U>>
  54. inline optional& operator=(U&& value);
  55. template <typename U>
  56. inline optional& operator=(const optional<U>& other);
  57. template <typename U>
  58. inline optional& operator=(optional<U>&& other);
  59. // value access
  60. inline const T* operator->() const;
  61. inline T* operator->();
  62. inline const T& operator*() const;
  63. inline T& operator*();
  64. private:
  65. T val{};
  66. bool set = false;
  67. };
  68. template <typename T>
  69. optional<T>::optional(const optional& other) : val(other.val), set(other.set) {}
  70. template <typename T>
  71. optional<T>::optional(optional&& other)
  72. : val(std::move(other.val)), set(other.set) {}
  73. template <typename T>
  74. template <typename U>
  75. optional<T>::optional(const optional<U>& other) : set(other.has_value()) {
  76. if (set) {
  77. val = static_cast<T>(other.value());
  78. }
  79. }
  80. template <typename T>
  81. template <typename U>
  82. optional<T>::optional(optional<U>&& other) : set(other.has_value()) {
  83. if (set) {
  84. val = static_cast<T>(std::move(other.value()));
  85. }
  86. }
  87. template <typename T>
  88. template <typename U /*= T*/, typename>
  89. optional<T>::optional(U&& value) : val(std::forward<U>(value)), set(true) {}
  90. template <typename T>
  91. T& optional<T>::value() {
  92. assert(set);
  93. return val;
  94. }
  95. template <typename T>
  96. const T& optional<T>::value() const {
  97. assert(set);
  98. return val;
  99. }
  100. template <typename T>
  101. const T& optional<T>::value(const T& defaultValue) const {
  102. if (!has_value()) {
  103. return defaultValue;
  104. }
  105. return val;
  106. }
  107. template <typename T>
  108. optional<T>::operator bool() const noexcept {
  109. return set;
  110. }
  111. template <typename T>
  112. bool optional<T>::has_value() const {
  113. return set;
  114. }
  115. template <typename T>
  116. optional<T>& optional<T>::operator=(const optional& other) {
  117. val = other.val;
  118. set = other.set;
  119. return *this;
  120. }
  121. template <typename T>
  122. optional<T>& optional<T>::operator=(optional&& other) noexcept {
  123. val = std::move(other.val);
  124. set = other.set;
  125. return *this;
  126. }
  127. template <typename T>
  128. template <typename U /* = T */, typename>
  129. optional<T>& optional<T>::operator=(U&& value) {
  130. val = std::forward<U>(value);
  131. set = true;
  132. return *this;
  133. }
  134. template <typename T>
  135. template <typename U>
  136. optional<T>& optional<T>::operator=(const optional<U>& other) {
  137. val = other.val;
  138. set = other.set;
  139. return *this;
  140. }
  141. template <typename T>
  142. template <typename U>
  143. optional<T>& optional<T>::operator=(optional<U>&& other) {
  144. val = std::move(other.val);
  145. set = other.set;
  146. return *this;
  147. }
  148. template <typename T>
  149. const T* optional<T>::operator->() const {
  150. assert(set);
  151. return &val;
  152. }
  153. template <typename T>
  154. T* optional<T>::operator->() {
  155. assert(set);
  156. return &val;
  157. }
  158. template <typename T>
  159. const T& optional<T>::operator*() const {
  160. assert(set);
  161. return val;
  162. }
  163. template <typename T>
  164. T& optional<T>::operator*() {
  165. assert(set);
  166. return val;
  167. }
  168. template <class T, class U>
  169. inline bool operator==(const optional<T>& lhs, const optional<U>& rhs) {
  170. if (!lhs.has_value() && !rhs.has_value()) {
  171. return true;
  172. }
  173. if (!lhs.has_value() || !rhs.has_value()) {
  174. return false;
  175. }
  176. return lhs.value() == rhs.value();
  177. }
  178. template <class T, class U>
  179. inline bool operator!=(const optional<T>& lhs, const optional<U>& rhs) {
  180. return !(lhs == rhs);
  181. }
  182. template <class T, class U>
  183. inline bool operator<(const optional<T>& lhs, const optional<U>& rhs) {
  184. if (!rhs.has_value()) {
  185. return false;
  186. }
  187. if (!lhs.has_value()) {
  188. return true;
  189. }
  190. return lhs.value() < rhs.value();
  191. }
  192. template <class T, class U>
  193. inline bool operator<=(const optional<T>& lhs, const optional<U>& rhs) {
  194. if (!lhs.has_value()) {
  195. return true;
  196. }
  197. if (!rhs.has_value()) {
  198. return false;
  199. }
  200. return lhs.value() <= rhs.value();
  201. }
  202. template <class T, class U>
  203. inline bool operator>(const optional<T>& lhs, const optional<U>& rhs) {
  204. if (!lhs.has_value()) {
  205. return false;
  206. }
  207. if (!rhs.has_value()) {
  208. return true;
  209. }
  210. return lhs.value() > rhs.value();
  211. }
  212. template <class T, class U>
  213. inline bool operator>=(const optional<T>& lhs, const optional<U>& rhs) {
  214. if (!rhs.has_value()) {
  215. return true;
  216. }
  217. if (!lhs.has_value()) {
  218. return false;
  219. }
  220. return lhs.value() >= rhs.value();
  221. }
  222. } // namespace dap
  223. #endif // dap_optional_h