optional_reference_spec.hpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. // Copyright (C) 2015-2024 Andrzej Krzemienski.
  2. //
  3. // Use, modification, and distribution is subject to the Boost Software
  4. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/optional for documentation.
  8. //
  9. // You are welcome to contact the author at:
  10. // akrzemi1@gmail.com
  11. #ifndef BOOST_OPTIONAL_DETAIL_OPTIONAL_REFERENCE_SPEC_AJK_03OCT2015_HPP
  12. #define BOOST_OPTIONAL_DETAIL_OPTIONAL_REFERENCE_SPEC_AJK_03OCT2015_HPP
  13. #ifdef BOOST_OPTIONAL_CONFIG_NO_PROPER_ASSIGN_FROM_CONST_INT
  14. #include <boost/type_traits/is_integral.hpp>
  15. #include <boost/type_traits/is_const.hpp>
  16. #endif
  17. # if 1
  18. namespace boost {
  19. namespace detail {
  20. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  21. template <class From>
  22. void prevent_binding_rvalue()
  23. {
  24. #ifndef BOOST_OPTIONAL_CONFIG_ALLOW_BINDING_TO_RVALUES
  25. static_assert(boost::is_lvalue_reference<From>::value,
  26. "binding rvalue references to optional lvalue references is disallowed");
  27. #endif
  28. }
  29. template <class T>
  30. BOOST_DEDUCED_TYPENAME boost::remove_reference<T>::type& forward_reference(T&& r)
  31. {
  32. static_assert(boost::is_lvalue_reference<T>::value,
  33. "binding rvalue references to optional lvalue references is disallowed");
  34. return optional_detail::forward<T>(r);
  35. }
  36. #endif // BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  37. template <class T>
  38. struct is_const_integral
  39. {
  40. static const bool value = boost::is_const<T>::value && boost::is_integral<T>::value;
  41. };
  42. template <class T>
  43. struct is_const_integral_bad_for_conversion
  44. {
  45. #if (!defined BOOST_OPTIONAL_CONFIG_ALLOW_BINDING_TO_RVALUES) && (defined BOOST_OPTIONAL_CONFIG_NO_PROPER_CONVERT_FROM_CONST_INT)
  46. static const bool value = boost::is_const<T>::value && boost::is_integral<T>::value;
  47. #else
  48. static const bool value = false;
  49. #endif
  50. };
  51. template <class From>
  52. void prevent_assignment_from_false_const_integral()
  53. {
  54. #ifndef BOOST_OPTIONAL_CONFIG_ALLOW_BINDING_TO_RVALUES
  55. #ifdef BOOST_OPTIONAL_CONFIG_NO_PROPER_ASSIGN_FROM_CONST_INT
  56. // MSVC compiler without rvalue references: we need to disable the assignment from
  57. // const integral lvalue reference, as it may be an invalid temporary
  58. static_assert(!is_const_integral<From>::value,
  59. "binding const lvalue references to integral types is disabled in this compiler");
  60. #endif
  61. #endif
  62. }
  63. template <class T>
  64. struct is_optional_
  65. {
  66. static const bool value = false;
  67. };
  68. template <class U>
  69. struct is_optional_< ::boost::optional<U> >
  70. {
  71. static const bool value = true;
  72. };
  73. template <class T>
  74. struct is_no_optional
  75. {
  76. static const bool value = !is_optional_<BOOST_DEDUCED_TYPENAME boost::decay<T>::type>::value;
  77. };
  78. template <class T, class U>
  79. struct is_same_decayed
  80. {
  81. static const bool value = ::boost::is_same<T, BOOST_DEDUCED_TYPENAME ::boost::remove_reference<U>::type>::value
  82. || ::boost::is_same<T, const BOOST_DEDUCED_TYPENAME ::boost::remove_reference<U>::type>::value;
  83. };
  84. template <class T, class U>
  85. struct no_unboxing_cond
  86. {
  87. static const bool value = is_no_optional<U>::value && !is_same_decayed<T, U>::value;
  88. };
  89. } // namespace detail
  90. template <class T>
  91. class optional<T&> : public optional_detail::optional_tag
  92. {
  93. T* ptr_;
  94. public:
  95. typedef T& value_type;
  96. typedef T& reference_type;
  97. typedef T& reference_const_type;
  98. typedef T& rval_reference_type;
  99. typedef T* pointer_type;
  100. typedef T* pointer_const_type;
  101. optional() BOOST_NOEXCEPT : ptr_() {}
  102. optional(none_t) BOOST_NOEXCEPT : ptr_() {}
  103. template <class U>
  104. explicit optional(const optional<U&>& rhs) BOOST_NOEXCEPT : ptr_(rhs.get_ptr()) {}
  105. optional(const optional& rhs) BOOST_NOEXCEPT : ptr_(rhs.get_ptr()) {}
  106. // the following two implement a 'conditionally explicit' constructor: condition is a hack for buggy compilers with screwed conversion construction from const int
  107. template <class U>
  108. explicit optional(U& rhs, BOOST_DEDUCED_TYPENAME boost::enable_if_c<detail::is_same_decayed<T, U>::value && detail::is_const_integral_bad_for_conversion<U>::value, bool>::type = true) BOOST_NOEXCEPT
  109. : ptr_(boost::addressof(rhs)) {}
  110. template <class U>
  111. optional(U& rhs, BOOST_DEDUCED_TYPENAME boost::enable_if_c<detail::is_same_decayed<T, U>::value && !detail::is_const_integral_bad_for_conversion<U>::value, bool>::type = true) BOOST_NOEXCEPT
  112. : ptr_(boost::addressof(rhs)) {}
  113. optional& operator=(const optional& rhs) BOOST_NOEXCEPT { ptr_ = rhs.get_ptr(); return *this; }
  114. template <class U>
  115. optional& operator=(const optional<U&>& rhs) BOOST_NOEXCEPT { ptr_ = rhs.get_ptr(); return *this; }
  116. optional& operator=(none_t) BOOST_NOEXCEPT { ptr_ = 0; return *this; }
  117. void swap(optional& rhs) BOOST_NOEXCEPT { std::swap(ptr_, rhs.ptr_); }
  118. T& get() const { BOOST_ASSERT(ptr_); return *ptr_; }
  119. T* get_ptr() const BOOST_NOEXCEPT { return ptr_; }
  120. T* operator->() const { BOOST_ASSERT(ptr_); return ptr_; }
  121. T& operator*() const { BOOST_ASSERT(ptr_); return *ptr_; }
  122. T& value() const
  123. {
  124. if (this->is_initialized())
  125. return this->get();
  126. else
  127. throw_exception(bad_optional_access());
  128. }
  129. explicit operator bool() const BOOST_NOEXCEPT { return ptr_ != 0; }
  130. void reset() BOOST_NOEXCEPT { ptr_ = 0; }
  131. bool is_initialized() const BOOST_NOEXCEPT { return ptr_ != 0; }
  132. bool has_value() const BOOST_NOEXCEPT { return ptr_ != 0; }
  133. template <typename F>
  134. optional<typename optional_detail::result_of<F, reference_const_type>::type>
  135. map(F f) const
  136. {
  137. if (this->has_value())
  138. return f(this->get());
  139. else
  140. return none;
  141. }
  142. template <typename F>
  143. optional<typename optional_detail::result_value_type<F, reference_const_type>::type>
  144. flat_map(F f) const
  145. {
  146. if (this->has_value())
  147. return f(get());
  148. else
  149. return none;
  150. }
  151. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  152. optional(T&& /* rhs */) BOOST_NOEXCEPT { detail::prevent_binding_rvalue<T&&>(); }
  153. template <class R>
  154. optional(R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::no_unboxing_cond<T, R>, bool>::type = true) BOOST_NOEXCEPT
  155. : ptr_(boost::addressof(r)) { detail::prevent_binding_rvalue<R>(); }
  156. template <class R>
  157. optional(bool cond, R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<R>, bool>::type = true) BOOST_NOEXCEPT
  158. : ptr_(cond ? boost::addressof(r) : 0) { detail::prevent_binding_rvalue<R>(); }
  159. template <class R>
  160. BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<R>, optional<T&>&>::type
  161. operator=(R&& r) BOOST_NOEXCEPT { detail::prevent_binding_rvalue<R>(); ptr_ = boost::addressof(r); return *this; }
  162. template <class R>
  163. void emplace(R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<R>, bool>::type = true) BOOST_NOEXCEPT
  164. { detail::prevent_binding_rvalue<R>(); ptr_ = boost::addressof(r); }
  165. template <class R>
  166. T& get_value_or(R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<R>, bool>::type = true) const BOOST_NOEXCEPT
  167. { detail::prevent_binding_rvalue<R>(); return ptr_ ? *ptr_ : r; }
  168. template <class R>
  169. T& value_or(R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<R>, bool>::type = true) const BOOST_NOEXCEPT
  170. { detail::prevent_binding_rvalue<R>(); return ptr_ ? *ptr_ : r; }
  171. template <class R>
  172. void reset(R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<R>, bool>::type = true) BOOST_NOEXCEPT
  173. { detail::prevent_binding_rvalue<R>(); ptr_ = boost::addressof(r); }
  174. template <class F>
  175. T& value_or_eval(F f) const { return ptr_ ? *ptr_ : detail::forward_reference(f()); }
  176. #else // BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  177. // the following two implement a 'conditionally explicit' constructor
  178. template <class U>
  179. explicit optional(U& v, BOOST_DEDUCED_TYPENAME boost::enable_if_c<detail::no_unboxing_cond<T, U>::value && detail::is_const_integral_bad_for_conversion<U>::value, bool>::type = true) BOOST_NOEXCEPT
  180. : ptr_(boost::addressof(v)) { }
  181. template <class U>
  182. optional(U& v, BOOST_DEDUCED_TYPENAME boost::enable_if_c<detail::no_unboxing_cond<T, U>::value && !detail::is_const_integral_bad_for_conversion<U>::value, bool>::type = true) BOOST_NOEXCEPT
  183. : ptr_(boost::addressof(v)) { }
  184. template <class U>
  185. optional(bool cond, U& v, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<U>, bool>::type = true) BOOST_NOEXCEPT : ptr_(cond ? boost::addressof(v) : 0) {}
  186. template <class U>
  187. BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<U>, optional<T&>&>::type
  188. operator=(U& v) BOOST_NOEXCEPT
  189. {
  190. detail::prevent_assignment_from_false_const_integral<U>();
  191. ptr_ = boost::addressof(v); return *this;
  192. }
  193. template <class U>
  194. void emplace(U& v, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<U>, bool>::type = true) BOOST_NOEXCEPT
  195. { ptr_ = boost::addressof(v); }
  196. template <class U>
  197. T& get_value_or(U& v, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<U>, bool>::type = true) const BOOST_NOEXCEPT
  198. { return ptr_ ? *ptr_ : v; }
  199. template <class U>
  200. T& value_or(U& v, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<U>, bool>::type = true) const BOOST_NOEXCEPT
  201. { return ptr_ ? *ptr_ : v; }
  202. template <class U>
  203. void reset(U& v, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<U>, bool>::type = true) BOOST_NOEXCEPT
  204. { ptr_ = boost::addressof(v); }
  205. template <class F>
  206. T& value_or_eval(F f) const { return ptr_ ? *ptr_ : f(); }
  207. #endif // BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  208. };
  209. template <class T>
  210. void swap ( optional<T&>& x, optional<T&>& y) BOOST_NOEXCEPT
  211. {
  212. x.swap(y);
  213. }
  214. } // namespace boost
  215. #endif // 1/0
  216. #endif // header guard