Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-11 08:14:01

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