Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-15 08:38:34

0001 /////////////////////////////////////////////////////////////////////////////
0002 //
0003 // (C) Copyright Ion Gaztanaga  2007-2013
0004 //
0005 // Distributed under the Boost Software License, Version 1.0.
0006 //    (See accompanying file LICENSE_1_0.txt or copy at
0007 //          http://www.boost.org/LICENSE_1_0.txt)
0008 //
0009 // See http://www.boost.org/libs/intrusive for documentation.
0010 //
0011 /////////////////////////////////////////////////////////////////////////////
0012 #ifndef BOOST_INTRUSIVE_DETAIL_PARENT_FROM_MEMBER_HPP
0013 #define BOOST_INTRUSIVE_DETAIL_PARENT_FROM_MEMBER_HPP
0014 
0015 #ifndef BOOST_CONFIG_HPP
0016 #  include <boost/config.hpp>
0017 #endif
0018 
0019 #if defined(BOOST_HAS_PRAGMA_ONCE)
0020 #  pragma once
0021 #endif
0022 
0023 #include <boost/intrusive/detail/config_begin.hpp>
0024 #include <boost/intrusive/detail/workaround.hpp>
0025 #include <boost/move/detail/launder.hpp>
0026 #include <cstddef>
0027 
0028 #if defined(_MSC_VER)
0029    #define BOOST_INTRUSIVE_MSVC_ABI_PTR_TO_MEMBER
0030    #endif
0031 
0032 namespace boost {
0033 namespace intrusive {
0034 namespace detail {
0035 
0036 template<class Parent, class Member>
0037 BOOST_INTRUSIVE_FORCEINLINE std::ptrdiff_t offset_from_pointer_to_member(const Member Parent::* ptr_to_member)
0038 {
0039    //The implementation of a pointer to member is compiler dependent.
0040    #if defined(BOOST_INTRUSIVE_MSVC_ABI_PTR_TO_MEMBER)
0041 
0042    //MSVC compliant compilers use their the first 32 bits as offset (even in 64 bit mode)
0043    union caster_union
0044    {
0045       const Member Parent::* ptr_to_member;
0046       int offset;
0047    } caster;
0048 
0049    //MSVC ABI can use up to 3 int32 to represent pointer to member data
0050    //with virtual base classes, in those cases there is no simple to
0051    //obtain the address of the parent. So static assert to avoid runtime errors
0052    BOOST_INTRUSIVE_STATIC_ASSERT( sizeof(caster) == sizeof(int) );
0053 
0054    caster.ptr_to_member = ptr_to_member;
0055    return std::ptrdiff_t(caster.offset);
0056    //Additional info on MSVC behaviour for the future. For 2/3 int ptr-to-member
0057    //types dereference seems to be:
0058    //
0059    // vboffset = [compile_time_offset if 2-int ptr2memb] /
0060    //            [ptr2memb.i32[2] if 3-int ptr2memb].
0061    // vbtable = *(this + vboffset);
0062    // adj = vbtable[ptr2memb.i32[1]];
0063    // var = adj + (this + vboffset) + ptr2memb.i32[0];
0064    //
0065    //To reverse the operation we need to
0066    // - obtain vboffset (in 2-int ptr2memb implementation only)
0067    // - Go to Parent's vbtable and obtain adjustment at index ptr2memb.i32[1]
0068    // - parent = member - adj - vboffset - ptr2memb.i32[0]
0069    //
0070    //Even accessing to RTTI we might not be able to obtain this information
0071    //so anyone who thinks it's possible, please send a patch.
0072 
0073    //This works with gcc, msvc, ac++, ibmcpp
0074    #elif defined(__GNUC__)   || defined(__HP_aCC) || defined(BOOST_INTEL) || \
0075          defined(__IBMCPP__) || defined(__DECCXX)
0076    const Parent * const parent = 0;
0077    const char *const member = static_cast<const char*>(static_cast<const void*>(&(parent->*ptr_to_member)));
0078    return std::ptrdiff_t(member - static_cast<const char*>(static_cast<const void*>(parent)));
0079    #else
0080    //This is the traditional C-front approach: __MWERKS__, __DMC__, __SUNPRO_CC
0081    union caster_union
0082    {
0083       const Member Parent::* ptr_to_member;
0084       std::ptrdiff_t offset;
0085    } caster;
0086    caster.ptr_to_member = ptr_to_member;
0087    return caster.offset - 1;
0088    #endif
0089 }
0090 
0091 template<class Parent, class Member>
0092 BOOST_INTRUSIVE_FORCEINLINE Parent *parent_from_member(Member *member, const Member Parent::* ptr_to_member)
0093 {
0094    return boost::move_detail::launder(reinterpret_cast<Parent*>
0095       (reinterpret_cast<std::size_t>(member) - static_cast<std::size_t>(offset_from_pointer_to_member(ptr_to_member))));
0096 }
0097 
0098 template<class Parent, class Member>
0099 BOOST_INTRUSIVE_FORCEINLINE const Parent *parent_from_member(const Member *member, const Member Parent::* ptr_to_member)
0100 {
0101    return boost::move_detail::launder(reinterpret_cast<const Parent*>
0102       ( reinterpret_cast<std::size_t>(member) - static_cast<std::size_t>(offset_from_pointer_to_member(ptr_to_member)) ));
0103 }
0104 
0105 }  //namespace detail {
0106 }  //namespace intrusive {
0107 }  //namespace boost {
0108 
0109 #include <boost/intrusive/detail/config_end.hpp>
0110 
0111 #endif   //#ifndef BOOST_INTRUSIVE_DETAIL_PARENT_FROM_MEMBER_HPP