optional_reference_spec.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 { return ptr_ ? *ptr_ : (throw_exception(bad_optional_access()), *ptr_); }
  123. bool operator!() const BOOST_NOEXCEPT { return ptr_ == 0; }
  124. BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()
  125. void reset() BOOST_NOEXCEPT { ptr_ = 0; }
  126. bool is_initialized() const BOOST_NOEXCEPT { return ptr_ != 0; }
  127. bool has_value() const BOOST_NOEXCEPT { return ptr_ != 0; }
  128. template <typename F>
  129. optional<typename boost::result_of<F(T&)>::type> map(F f) const
  130. {
  131. if (this->has_value())
  132. return f(this->get());
  133. else
  134. return none;
  135. }
  136. template <typename F>
  137. optional<typename optional_detail::optional_value_type<typename boost::result_of<F(T&)>::type>::type> flat_map(F f) const
  138. {
  139. if (this->has_value())
  140. return f(get());
  141. else
  142. return none;
  143. }
  144. #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  145. optional(T&& /* rhs */) BOOST_NOEXCEPT { detail::prevent_binding_rvalue<T&&>(); }
  146. template <class R>
  147. optional(R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::no_unboxing_cond<T, R>, bool>::type = true) BOOST_NOEXCEPT
  148. : ptr_(boost::addressof(r)) { detail::prevent_binding_rvalue<R>(); }
  149. template <class R>
  150. optional(bool cond, R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<R>, bool>::type = true) BOOST_NOEXCEPT
  151. : ptr_(cond ? boost::addressof(r) : 0) { detail::prevent_binding_rvalue<R>(); }
  152. template <class R>
  153. BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<R>, optional<T&>&>::type
  154. operator=(R&& r) BOOST_NOEXCEPT { detail::prevent_binding_rvalue<R>(); ptr_ = boost::addressof(r); return *this; }
  155. template <class R>
  156. void emplace(R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<R>, bool>::type = true) BOOST_NOEXCEPT
  157. { detail::prevent_binding_rvalue<R>(); ptr_ = boost::addressof(r); }
  158. template <class R>
  159. T& get_value_or(R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<R>, bool>::type = true) const BOOST_NOEXCEPT
  160. { detail::prevent_binding_rvalue<R>(); return ptr_ ? *ptr_ : r; }
  161. template <class R>
  162. T& value_or(R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<R>, bool>::type = true) const BOOST_NOEXCEPT
  163. { detail::prevent_binding_rvalue<R>(); return ptr_ ? *ptr_ : r; }
  164. template <class R>
  165. void reset(R&& r, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<R>, bool>::type = true) BOOST_NOEXCEPT
  166. { detail::prevent_binding_rvalue<R>(); ptr_ = boost::addressof(r); }
  167. template <class F>
  168. T& value_or_eval(F f) const { return ptr_ ? *ptr_ : detail::forward_reference(f()); }
  169. #else // BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  170. // the following two implement a 'conditionally explicit' constructor
  171. template <class U>
  172. 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
  173. : ptr_(boost::addressof(v)) { }
  174. template <class U>
  175. 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
  176. : ptr_(boost::addressof(v)) { }
  177. template <class U>
  178. 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) {}
  179. template <class U>
  180. BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<U>, optional<T&>&>::type
  181. operator=(U& v) BOOST_NOEXCEPT
  182. {
  183. detail::prevent_assignment_from_false_const_integral<U>();
  184. ptr_ = boost::addressof(v); return *this;
  185. }
  186. template <class U>
  187. void emplace(U& v, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<U>, bool>::type = true) BOOST_NOEXCEPT
  188. { ptr_ = boost::addressof(v); }
  189. template <class U>
  190. T& get_value_or(U& v, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<U>, bool>::type = true) const BOOST_NOEXCEPT
  191. { return ptr_ ? *ptr_ : v; }
  192. template <class U>
  193. T& value_or(U& v, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<U>, bool>::type = true) const BOOST_NOEXCEPT
  194. { return ptr_ ? *ptr_ : v; }
  195. template <class U>
  196. void reset(U& v, BOOST_DEDUCED_TYPENAME boost::enable_if<detail::is_no_optional<U>, bool>::type = true) BOOST_NOEXCEPT
  197. { ptr_ = boost::addressof(v); }
  198. template <class F>
  199. T& value_or_eval(F f) const { return ptr_ ? *ptr_ : f(); }
  200. #endif // BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES
  201. };
  202. template <class T>
  203. void swap ( optional<T&>& x, optional<T&>& y) BOOST_NOEXCEPT
  204. {
  205. x.swap(y);
  206. }
  207. } // namespace boost
  208. #endif // 1/0
  209. #endif // header guard