Chris@102: #ifndef BOOST_CORE_REF_HPP Chris@102: #define BOOST_CORE_REF_HPP Chris@102: Chris@102: // MS compatible compilers support #pragma once Chris@102: Chris@102: #if defined(_MSC_VER) && (_MSC_VER >= 1020) Chris@102: # pragma once Chris@102: #endif Chris@102: Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: Chris@102: // Chris@102: // ref.hpp - ref/cref, useful helper functions Chris@102: // Chris@102: // Copyright (C) 1999, 2000 Jaakko Jarvi (jaakko.jarvi@cs.utu.fi) Chris@102: // Copyright (C) 2001, 2002 Peter Dimov Chris@102: // Copyright (C) 2002 David Abrahams Chris@102: // Chris@102: // Copyright (C) 2014 Glen Joseph Fernandes Chris@102: // glenfe at live dot com Chris@102: // Copyright (C) 2014 Agustin Berge Chris@102: // Chris@102: // Distributed under the Boost Software License, Version 1.0. (See Chris@102: // accompanying file LICENSE_1_0.txt or copy at Chris@102: // http://www.boost.org/LICENSE_1_0.txt) Chris@102: // Chris@102: // See http://www.boost.org/libs/core/doc/html/core/ref.html for documentation. Chris@102: // Chris@102: Chris@102: /** Chris@102: @file Chris@102: */ Chris@102: Chris@102: /** Chris@102: Boost namespace. Chris@102: */ Chris@102: namespace boost Chris@102: { Chris@102: Chris@102: #if defined( BOOST_MSVC ) && BOOST_WORKAROUND( BOOST_MSVC, == 1600 ) Chris@102: Chris@102: struct ref_workaround_tag {}; Chris@102: Chris@102: #endif Chris@102: Chris@102: // reference_wrapper Chris@102: Chris@102: /** Chris@102: @brief Contains a reference to an object of type `T`. Chris@102: Chris@102: `reference_wrapper` is primarily used to "feed" references to Chris@102: function templates (algorithms) that take their parameter by Chris@102: value. It provides an implicit conversion to `T&`, which Chris@102: usually allows the function templates to work on references Chris@102: unmodified. Chris@102: */ Chris@102: template class reference_wrapper Chris@102: { Chris@102: public: Chris@102: /** Chris@102: Type `T`. Chris@102: */ Chris@102: typedef T type; Chris@102: Chris@102: /** Chris@102: Constructs a `reference_wrapper` object that stores a Chris@102: reference to `t`. Chris@102: Chris@102: @remark Does not throw. Chris@102: */ Chris@102: BOOST_FORCEINLINE explicit reference_wrapper(T& t): t_(boost::addressof(t)) {} Chris@102: Chris@102: #if defined( BOOST_MSVC ) && BOOST_WORKAROUND( BOOST_MSVC, == 1600 ) Chris@102: Chris@102: BOOST_FORCEINLINE explicit reference_wrapper( T & t, ref_workaround_tag ): t_( boost::addressof( t ) ) {} Chris@102: Chris@102: #endif Chris@102: Chris@102: #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) Chris@102: /** Chris@102: @remark Construction from a temporary object is disabled. Chris@102: */ Chris@102: BOOST_DELETED_FUNCTION(reference_wrapper(T&& t)) Chris@102: public: Chris@102: #endif Chris@102: Chris@102: /** Chris@102: @return The stored reference. Chris@102: @remark Does not throw. Chris@102: */ Chris@102: BOOST_FORCEINLINE operator T& () const { return *t_; } Chris@102: Chris@102: /** Chris@102: @return The stored reference. Chris@102: @remark Does not throw. Chris@102: */ Chris@102: BOOST_FORCEINLINE T& get() const { return *t_; } Chris@102: Chris@102: /** Chris@102: @return A pointer to the object referenced by the stored Chris@102: reference. Chris@102: @remark Does not throw. Chris@102: */ Chris@102: BOOST_FORCEINLINE T* get_pointer() const { return t_; } Chris@102: Chris@102: private: Chris@102: Chris@102: T* t_; Chris@102: }; Chris@102: Chris@102: // ref Chris@102: Chris@102: /** Chris@102: @cond Chris@102: */ Chris@102: #if defined( __BORLANDC__ ) && BOOST_WORKAROUND( __BORLANDC__, BOOST_TESTED_AT(0x581) ) Chris@102: # define BOOST_REF_CONST Chris@102: #else Chris@102: # define BOOST_REF_CONST const Chris@102: #endif Chris@102: /** Chris@102: @endcond Chris@102: */ Chris@102: Chris@102: /** Chris@102: @return `reference_wrapper(t)` Chris@102: @remark Does not throw. Chris@102: */ Chris@102: template BOOST_FORCEINLINE reference_wrapper BOOST_REF_CONST ref( T & t ) Chris@102: { Chris@102: #if defined( BOOST_MSVC ) && BOOST_WORKAROUND( BOOST_MSVC, == 1600 ) Chris@102: Chris@102: return reference_wrapper( t, ref_workaround_tag() ); Chris@102: Chris@102: #else Chris@102: Chris@102: return reference_wrapper( t ); Chris@102: Chris@102: #endif Chris@102: } Chris@102: Chris@102: // cref Chris@102: Chris@102: /** Chris@102: @return `reference_wrapper(t)` Chris@102: @remark Does not throw. Chris@102: */ Chris@102: template BOOST_FORCEINLINE reference_wrapper BOOST_REF_CONST cref( T const & t ) Chris@102: { Chris@102: return reference_wrapper(t); Chris@102: } Chris@102: Chris@102: #undef BOOST_REF_CONST Chris@102: Chris@102: #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) Chris@102: Chris@102: /** Chris@102: @cond Chris@102: */ Chris@102: #if defined(BOOST_NO_CXX11_DELETED_FUNCTIONS) Chris@102: # define BOOST_REF_DELETE Chris@102: #else Chris@102: # define BOOST_REF_DELETE = delete Chris@102: #endif Chris@102: /** Chris@102: @endcond Chris@102: */ Chris@102: Chris@102: /** Chris@102: @remark Construction from a temporary object is disabled. Chris@102: */ Chris@102: template void ref(T const&&) BOOST_REF_DELETE; Chris@102: Chris@102: /** Chris@102: @remark Construction from a temporary object is disabled. Chris@102: */ Chris@102: template void cref(T const&&) BOOST_REF_DELETE; Chris@102: Chris@102: #undef BOOST_REF_DELETE Chris@102: Chris@102: #endif Chris@102: Chris@102: // is_reference_wrapper Chris@102: Chris@102: /** Chris@102: @brief Determine if a type `T` is an instantiation of Chris@102: `reference_wrapper`. Chris@102: Chris@102: The value static constant will be true if the type `T` is a Chris@102: specialization of `reference_wrapper`. Chris@102: */ Chris@102: template struct is_reference_wrapper Chris@102: { Chris@102: BOOST_STATIC_CONSTANT( bool, value = false ); Chris@102: }; Chris@102: Chris@102: /** Chris@102: @cond Chris@102: */ Chris@102: template struct is_reference_wrapper< reference_wrapper > Chris@102: { Chris@102: BOOST_STATIC_CONSTANT( bool, value = true ); Chris@102: }; Chris@102: Chris@102: #if !defined(BOOST_NO_CV_SPECIALIZATIONS) Chris@102: Chris@102: template struct is_reference_wrapper< reference_wrapper const > Chris@102: { Chris@102: BOOST_STATIC_CONSTANT( bool, value = true ); Chris@102: }; Chris@102: Chris@102: template struct is_reference_wrapper< reference_wrapper volatile > Chris@102: { Chris@102: BOOST_STATIC_CONSTANT( bool, value = true ); Chris@102: }; Chris@102: Chris@102: template struct is_reference_wrapper< reference_wrapper const volatile > Chris@102: { Chris@102: BOOST_STATIC_CONSTANT( bool, value = true ); Chris@102: }; Chris@102: Chris@102: #endif // !defined(BOOST_NO_CV_SPECIALIZATIONS) Chris@102: Chris@102: /** Chris@102: @endcond Chris@102: */ Chris@102: Chris@102: Chris@102: // unwrap_reference Chris@102: Chris@102: /** Chris@102: @brief Find the type in a `reference_wrapper`. Chris@102: Chris@102: The `typedef` type is `T::type` if `T` is a Chris@102: `reference_wrapper`, `T` otherwise. Chris@102: */ Chris@102: template struct unwrap_reference Chris@102: { Chris@102: typedef T type; Chris@102: }; Chris@102: Chris@102: /** Chris@102: @cond Chris@102: */ Chris@102: template struct unwrap_reference< reference_wrapper > Chris@102: { Chris@102: typedef T type; Chris@102: }; Chris@102: Chris@102: #if !defined(BOOST_NO_CV_SPECIALIZATIONS) Chris@102: Chris@102: template struct unwrap_reference< reference_wrapper const > Chris@102: { Chris@102: typedef T type; Chris@102: }; Chris@102: Chris@102: template struct unwrap_reference< reference_wrapper volatile > Chris@102: { Chris@102: typedef T type; Chris@102: }; Chris@102: Chris@102: template struct unwrap_reference< reference_wrapper const volatile > Chris@102: { Chris@102: typedef T type; Chris@102: }; Chris@102: Chris@102: #endif // !defined(BOOST_NO_CV_SPECIALIZATIONS) Chris@102: Chris@102: /** Chris@102: @endcond Chris@102: */ Chris@102: Chris@102: // unwrap_ref Chris@102: Chris@102: /** Chris@102: @return `unwrap_reference::type&(t)` Chris@102: @remark Does not throw. Chris@102: */ Chris@102: template BOOST_FORCEINLINE typename unwrap_reference::type& unwrap_ref( T & t ) Chris@102: { Chris@102: return t; Chris@102: } Chris@102: Chris@102: // get_pointer Chris@102: Chris@102: /** Chris@102: @cond Chris@102: */ Chris@102: template BOOST_FORCEINLINE T* get_pointer( reference_wrapper const & r ) Chris@102: { Chris@102: return r.get_pointer(); Chris@102: } Chris@102: /** Chris@102: @endcond Chris@102: */ Chris@102: Chris@102: } // namespace boost Chris@102: Chris@102: #endif // #ifndef BOOST_CORE_REF_HPP