optional_reference_spec.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. // Copyright (C) 2015-2018 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. BOOST_STATIC_ASSERT_MSG(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. BOOST_STATIC_ASSERT_MSG(boost::is_lvalue_reference<T>::value,
  33. "binding rvalue references to optional lvalue references is disallowed");
  34. return boost::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 refernces: we need to disable the asignment from
  57. // const integral lvalue reference, as it may be an invalid temporary
  58. BOOST_STATIC_ASSERT_MSG(!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 srewed 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. bool operator!() const BOOST_NOEXCEPT { return ptr_ == 0; }
  130. BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()
  131. void reset() BOOST_NOEXCEPT { ptr_ = 0; }
  132. bool is_initialized() const BOOST_NOEXCEPT { return ptr_ != 0; }
  133. bool has_value() const BOOST_NOEXCEPT { return ptr_ != 0; }
  134. template <typename F>
  135. optional<typename boost::result_of<F(T&)>::type> 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::optional_value_type<typename boost::result_of<F(T&)>::type>::type> flat_map(F f) const
  144. {
  145. if (this->has_value())
  146. return f(get());
  147. else
  148. return none;
  149. }
  150. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  151. optional(T&& /* rhs */) BOOST_NOEXCEPT { detail::prevent_binding_rvalue<T&&>(); }
  152. template <class R>
  153. optional(R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::no_unboxing_cond<T, R>, bool>::type = true) BOOST_NOEXCEPT
  154. : ptr_(boost::addressof(r)) { detail::prevent_binding_rvalue<R>(); }
  155. template <class R>
  156. optional(bool cond, R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<R>, bool>::type = true) BOOST_NOEXCEPT
  157. : ptr_(cond ? boost::addressof(r) : 0) { detail::prevent_binding_rvalue<R>(); }
  158. template <class R>
  159. BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<R>, optional<T&>&>::type
  160. operator=(R&& r) BOOST_NOEXCEPT { detail::prevent_binding_rvalue<R>(); ptr_ = boost::addressof(r); return *this; }
  161. template <class R>
  162. void emplace(R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<R>, bool>::type = true) BOOST_NOEXCEPT
  163. { detail::prevent_binding_rvalue<R>(); ptr_ = boost::addressof(r); }
  164. template <class R>
  165. T& get_value_or(R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<R>, bool>::type = true) const BOOST_NOEXCEPT
  166. { detail::prevent_binding_rvalue<R>(); return ptr_ ? *ptr_ : r; }
  167. template <class R>
  168. T& value_or(R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<R>, bool>::type = true) const BOOST_NOEXCEPT
  169. { detail::prevent_binding_rvalue<R>(); return ptr_ ? *ptr_ : r; }
  170. template <class R>
  171. void reset(R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<R>, bool>::type = true) BOOST_NOEXCEPT
  172. { detail::prevent_binding_rvalue<R>(); ptr_ = boost::addressof(r); }
  173. template <class F>
  174. T& value_or_eval(F f) const { return ptr_ ? *ptr_ : detail::forward_reference(f()); }
  175. #else // BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  176. // the following two implement a 'conditionally explicit' constructor
  177. template <class U>
  178. 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
  179. : ptr_(boost::addressof(v)) { }
  180. template <class U>
  181. 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
  182. : ptr_(boost::addressof(v)) { }
  183. template <class U>
  184. 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) {}
  185. template <class U>
  186. BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<U>, optional<T&>&>::type
  187. operator=(U& v) BOOST_NOEXCEPT
  188. {
  189. detail::prevent_assignment_from_false_const_integral<U>();
  190. ptr_ = boost::addressof(v); return *this;
  191. }
  192. template <class U>
  193. void emplace(U& v, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<U>, bool>::type = true) BOOST_NOEXCEPT
  194. { ptr_ = boost::addressof(v); }
  195. template <class U>
  196. T& get_value_or(U& v, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<U>, bool>::type = true) const BOOST_NOEXCEPT
  197. { return ptr_ ? *ptr_ : v; }
  198. template <class U>
  199. T& value_or(U& v, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<U>, bool>::type = true) const BOOST_NOEXCEPT
  200. { return ptr_ ? *ptr_ : v; }
  201. template <class U>
  202. void reset(U& v, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<U>, bool>::type = true) BOOST_NOEXCEPT
  203. { ptr_ = boost::addressof(v); }
  204. template <class F>
  205. T& value_or_eval(F f) const { return ptr_ ? *ptr_ : f(); }
  206. #endif // BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  207. };
  208. template <class T>
  209. void swap ( optional<T&>& x, optional<T&>& y) BOOST_NOEXCEPT
  210. {
  211. x.swap(y);
  212. }
  213. } // namespace boost
  214. #endif // 1/0
  215. #endif // header guard