ref.hpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. #ifndef BOOST_CORE_REF_HPP
  2. #define BOOST_CORE_REF_HPP
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER) && (_MSC_VER >= 1020)
  5. # pragma once
  6. #endif
  7. #include <boost/config.hpp>
  8. #include <boost/config/workaround.hpp>
  9. #include <boost/core/addressof.hpp>
  10. #include <boost/core/enable_if.hpp>
  11. //
  12. // ref.hpp - ref/cref, useful helper functions
  13. //
  14. // Copyright (C) 1999, 2000 Jaakko Jarvi (jaakko.jarvi@cs.utu.fi)
  15. // Copyright (C) 2001, 2002 Peter Dimov
  16. // Copyright (C) 2002 David Abrahams
  17. //
  18. // Copyright (C) 2014 Glen Joseph Fernandes
  19. // (glenjofe@gmail.com)
  20. //
  21. // Copyright (C) 2014 Agustin Berge
  22. //
  23. // Distributed under the Boost Software License, Version 1.0. (See
  24. // accompanying file LICENSE_1_0.txt or copy at
  25. // http://www.boost.org/LICENSE_1_0.txt)
  26. //
  27. // See http://www.boost.org/libs/core/doc/html/core/ref.html for documentation.
  28. //
  29. /**
  30. @file
  31. */
  32. /**
  33. Boost namespace.
  34. */
  35. namespace boost
  36. {
  37. #if defined( BOOST_MSVC ) && BOOST_WORKAROUND( BOOST_MSVC, == 1600 )
  38. struct ref_workaround_tag {};
  39. #endif
  40. namespace detail
  41. {
  42. template< class Y, class T > struct ref_convertible
  43. {
  44. typedef char (&yes) [1];
  45. typedef char (&no) [2];
  46. static yes f( T* );
  47. static no f( ... );
  48. enum _vt { value = sizeof( (f)( static_cast<Y*>(0) ) ) == sizeof(yes) };
  49. };
  50. struct ref_empty
  51. {
  52. };
  53. } // namespace detail
  54. // reference_wrapper
  55. /**
  56. @brief Contains a reference to an object of type `T`.
  57. `reference_wrapper` is primarily used to "feed" references to
  58. function templates (algorithms) that take their parameter by
  59. value. It provides an implicit conversion to `T&`, which
  60. usually allows the function templates to work on references
  61. unmodified.
  62. */
  63. template<class T> class reference_wrapper
  64. {
  65. public:
  66. /**
  67. Type `T`.
  68. */
  69. typedef T type;
  70. /**
  71. Constructs a `reference_wrapper` object that stores a
  72. reference to `t`.
  73. @remark Does not throw.
  74. */
  75. BOOST_FORCEINLINE explicit reference_wrapper(T& t): t_(boost::addressof(t)) {}
  76. #if defined( BOOST_MSVC ) && BOOST_WORKAROUND( BOOST_MSVC, == 1600 )
  77. BOOST_FORCEINLINE explicit reference_wrapper( T & t, ref_workaround_tag ): t_( boost::addressof( t ) ) {}
  78. #endif
  79. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  80. /**
  81. @remark Construction from a temporary object is disabled.
  82. */
  83. BOOST_DELETED_FUNCTION(reference_wrapper(T&& t))
  84. public:
  85. #endif
  86. template<class Y> friend class reference_wrapper;
  87. /**
  88. Constructs a `reference_wrapper` object that stores the
  89. reference stored in the compatible `reference_wrapper` `r`.
  90. @remark Only enabled when `Y*` is convertible to `T*`.
  91. @remark Does not throw.
  92. */
  93. template<class Y> reference_wrapper( reference_wrapper<Y> r,
  94. typename enable_if_c<boost::detail::ref_convertible<Y, T>::value,
  95. boost::detail::ref_empty>::type = boost::detail::ref_empty() ): t_( r.t_ )
  96. {
  97. }
  98. /**
  99. @return The stored reference.
  100. @remark Does not throw.
  101. */
  102. BOOST_FORCEINLINE operator T& () const { return *t_; }
  103. /**
  104. @return The stored reference.
  105. @remark Does not throw.
  106. */
  107. BOOST_FORCEINLINE T& get() const { return *t_; }
  108. /**
  109. @return A pointer to the object referenced by the stored
  110. reference.
  111. @remark Does not throw.
  112. */
  113. BOOST_FORCEINLINE T* get_pointer() const { return t_; }
  114. private:
  115. T* t_;
  116. };
  117. // ref
  118. /**
  119. @cond
  120. */
  121. #if defined( BOOST_BORLANDC ) && BOOST_WORKAROUND( BOOST_BORLANDC, BOOST_TESTED_AT(0x581) )
  122. # define BOOST_REF_CONST
  123. #else
  124. # define BOOST_REF_CONST const
  125. #endif
  126. /**
  127. @endcond
  128. */
  129. /**
  130. @return `reference_wrapper<T>(t)`
  131. @remark Does not throw.
  132. */
  133. template<class T> BOOST_FORCEINLINE reference_wrapper<T> BOOST_REF_CONST ref( T & t )
  134. {
  135. #if defined( BOOST_MSVC ) && BOOST_WORKAROUND( BOOST_MSVC, == 1600 )
  136. return reference_wrapper<T>( t, ref_workaround_tag() );
  137. #else
  138. return reference_wrapper<T>( t );
  139. #endif
  140. }
  141. // cref
  142. /**
  143. @return `reference_wrapper<T const>(t)`
  144. @remark Does not throw.
  145. */
  146. template<class T> BOOST_FORCEINLINE reference_wrapper<T const> BOOST_REF_CONST cref( T const & t )
  147. {
  148. return reference_wrapper<T const>(t);
  149. }
  150. #undef BOOST_REF_CONST
  151. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  152. /**
  153. @cond
  154. */
  155. #if defined(BOOST_NO_CXX11_DELETED_FUNCTIONS)
  156. # define BOOST_REF_DELETE
  157. #else
  158. # define BOOST_REF_DELETE = delete
  159. #endif
  160. /**
  161. @endcond
  162. */
  163. /**
  164. @remark Construction from a temporary object is disabled.
  165. */
  166. template<class T> void ref(T const&&) BOOST_REF_DELETE;
  167. /**
  168. @remark Construction from a temporary object is disabled.
  169. */
  170. template<class T> void cref(T const&&) BOOST_REF_DELETE;
  171. #undef BOOST_REF_DELETE
  172. #endif
  173. // is_reference_wrapper
  174. /**
  175. @brief Determine if a type `T` is an instantiation of
  176. `reference_wrapper`.
  177. The value static constant will be true if the type `T` is a
  178. specialization of `reference_wrapper`.
  179. */
  180. template<typename T> struct is_reference_wrapper
  181. {
  182. BOOST_STATIC_CONSTANT( bool, value = false );
  183. };
  184. /**
  185. @cond
  186. */
  187. template<typename T> struct is_reference_wrapper< reference_wrapper<T> >
  188. {
  189. BOOST_STATIC_CONSTANT( bool, value = true );
  190. };
  191. #if !defined(BOOST_NO_CV_SPECIALIZATIONS)
  192. template<typename T> struct is_reference_wrapper< reference_wrapper<T> const >
  193. {
  194. BOOST_STATIC_CONSTANT( bool, value = true );
  195. };
  196. template<typename T> struct is_reference_wrapper< reference_wrapper<T> volatile >
  197. {
  198. BOOST_STATIC_CONSTANT( bool, value = true );
  199. };
  200. template<typename T> struct is_reference_wrapper< reference_wrapper<T> const volatile >
  201. {
  202. BOOST_STATIC_CONSTANT( bool, value = true );
  203. };
  204. #endif // !defined(BOOST_NO_CV_SPECIALIZATIONS)
  205. /**
  206. @endcond
  207. */
  208. // unwrap_reference
  209. /**
  210. @brief Find the type in a `reference_wrapper`.
  211. The `typedef` type is `T::type` if `T` is a
  212. `reference_wrapper`, `T` otherwise.
  213. */
  214. template<typename T> struct unwrap_reference
  215. {
  216. typedef T type;
  217. };
  218. /**
  219. @cond
  220. */
  221. template<typename T> struct unwrap_reference< reference_wrapper<T> >
  222. {
  223. typedef T type;
  224. };
  225. #if !defined(BOOST_NO_CV_SPECIALIZATIONS)
  226. template<typename T> struct unwrap_reference< reference_wrapper<T> const >
  227. {
  228. typedef T type;
  229. };
  230. template<typename T> struct unwrap_reference< reference_wrapper<T> volatile >
  231. {
  232. typedef T type;
  233. };
  234. template<typename T> struct unwrap_reference< reference_wrapper<T> const volatile >
  235. {
  236. typedef T type;
  237. };
  238. #endif // !defined(BOOST_NO_CV_SPECIALIZATIONS)
  239. /**
  240. @endcond
  241. */
  242. // unwrap_ref
  243. /**
  244. @return `unwrap_reference<T>::type&(t)`
  245. @remark Does not throw.
  246. */
  247. template<class T> BOOST_FORCEINLINE typename unwrap_reference<T>::type& unwrap_ref( T & t )
  248. {
  249. return t;
  250. }
  251. // get_pointer
  252. /**
  253. @cond
  254. */
  255. template<class T> BOOST_FORCEINLINE T* get_pointer( reference_wrapper<T> const & r )
  256. {
  257. return r.get_pointer();
  258. }
  259. /**
  260. @endcond
  261. */
  262. } // namespace boost
  263. #endif // #ifndef BOOST_CORE_REF_HPP