Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** @file stl/fast_pointer_cast.hpp
- ***
- *** @brief
- *** Smart pointer casts with move semantics based optimization. @n
- *** (If you are casting temporary smart pointer it will be moved-and-casted into
- *** new pointer using lock free mechanism).
- **/
- #pragma once
- #include <memory>
- namespace stl
- {
- //! Dummy struct used to specialize std::shared_ptr base class.
- struct fast_static_pointer_cast__access_hack_tag
- {
- };
- } // namespace stl
- namespace std
- {
- namespace tr1
- {
- //! This specialization used to invoke some internal shared_ptr stuff.
- /** std::_Ptr_base treats this specialization as friend. */
- template<>
- class _Ptr_base< ::stl::fast_static_pointer_cast__access_hack_tag >
- {
- public:
- template< class T, class U >
- static void DoConstCastTransfer( _Ptr_base< T > &rTarget, _Ptr_base< U > &&rrSource )
- {
- // Cast and transfer pointer
- rTarget._Ptr = const_cast< T * >( rrSource._Ptr );
- rTarget._Rep = rrSource._Rep;
- // Invalidate source
- rrSource._Ptr = nullptr;
- rrSource._Rep = nullptr;
- }
- template< class T, class U >
- static void DoStaticCastTransfer( _Ptr_base< T > &rTarget, _Ptr_base< U > &&rrSource )
- {
- // Cast and transfer pointer
- rTarget._Ptr = static_cast< T * >( rrSource._Ptr );
- rTarget._Rep = rrSource._Rep;
- // Invalidate source
- rrSource._Ptr = nullptr;
- rrSource._Rep = nullptr;
- }
- template< class T, class U >
- static void DoDynamicCastTransfer( _Ptr_base< T > &rTarget, _Ptr_base< U > &&rrSource )
- {
- // Cast and transfer pointer
- rTarget._Ptr = dynamic_cast< T * >( rrSource._Ptr );
- rTarget._Rep = rrSource._Rep;
- // Invalidate source
- rrSource._Ptr = nullptr;
- rrSource._Rep = nullptr;
- }
- };
- } // namespace tr1
- } // namespace std
- namespace stl
- {
- //! Implements fast static_pointer_cast for rvalue references (without atomic instructions).
- /** This variant is used for rvalue references, this variant applies lock free optimization. */
- template< class T, class U >
- inline std::shared_ptr< T > fast_static_pointer_cast( std::shared_ptr< U > &&rrp )
- {
- std::shared_ptr< T > pRes;
- std::tr1::_Ptr_base< fast_static_pointer_cast__access_hack_tag >::DoStaticCastTransfer( pRes, std::move( rrp ) );
- return std::move( pRes );
- }
- //! Implements fast static_pointer_cast for rvalue references (without atomic instructions).
- /** This variant is used for lvalue references and just calls slow std implementation. */
- template< class T, class U >
- inline std::shared_ptr< T > fast_static_pointer_cast( const std::shared_ptr< U > &rrp )
- {
- return std::static_pointer_cast< T >( rrp );
- }
- //! Implements fast dynamic_pointer_cast for rvalue references (without atomic instructions).
- /** This variant is used for rvalue references, this variant applies lock free optimization. */
- template< class T, class U >
- inline std::shared_ptr< T > fast_dynamic_pointer_cast( std::shared_ptr< U > &&rrp )
- {
- std::shared_ptr< T > pRes;
- std::tr1::_Ptr_base< fast_static_pointer_cast__access_hack_tag >::DoDynamicCastTransfer( pRes, std::move( rrp ) );
- return std::move( pRes );
- }
- //! Implements fast dynamic_pointer_cast for rvalue references (without atomic instructions).
- /** This variant is used for lvalue references and just calls slow std implementation. */
- template< class T, class U >
- inline std::shared_ptr< T > fast_dynamic_pointer_cast( const std::shared_ptr< U > &rrp )
- {
- return std::static_pointer_cast< T >( rrp );
- }
- //! Implements fast const_pointer_cast for rvalue references (without atomic instructions).
- /** This variant is used for rvalue references, this variant applies lock free optimization. */
- template< class T, class U >
- inline std::shared_ptr< T > fast_const_pointer_cast( std::shared_ptr< U > &&rrp )
- {
- std::shared_ptr< T > pRes;
- std::tr1::_Ptr_base< fast_static_pointer_cast__access_hack_tag >::DoConstCastTransfer( pRes, std::move( rrp ) );
- return std::move( pRes );
- }
- //! Implements fast const_pointer_cast for rvalue references (without atomic instructions).
- /** This variant is used for lvalue references and just calls slow std implementation. */
- template< class T, class U >
- inline std::shared_ptr< T > fast_const_pointer_cast( const std::shared_ptr< U > &rrp )
- {
- return std::static_pointer_cast< T >( rrp );
- }
- } // namespace stl
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement