Chris@101: /* Copyright 2003-2013 Joaquin M Lopez Munoz. Chris@16: * Distributed under the Boost Software License, Version 1.0. Chris@16: * (See accompanying file LICENSE_1_0.txt or copy at Chris@16: * http://www.boost.org/LICENSE_1_0.txt) Chris@16: * Chris@16: * See http://www.boost.org/libs/multi_index for library home page. Chris@16: */ Chris@16: Chris@16: #ifndef BOOST_MULTI_INDEX_MEMBER_HPP Chris@16: #define BOOST_MULTI_INDEX_MEMBER_HPP Chris@16: Chris@101: #if defined(_MSC_VER) Chris@16: #pragma once Chris@16: #endif Chris@16: Chris@16: #include /* keep it first to prevent nasty warns in MSVC */ Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #if !defined(BOOST_NO_SFINAE) Chris@16: #include Chris@16: #endif Chris@16: Chris@16: namespace boost{ Chris@16: Chris@16: template class reference_wrapper; /* fwd decl. */ Chris@16: Chris@16: namespace multi_index{ Chris@16: Chris@16: namespace detail{ Chris@16: Chris@16: /* member is a read/write key extractor for accessing a given Chris@16: * member of a class. Chris@16: * Additionally, member is overloaded to support referece_wrappers Chris@16: * of T and "chained pointers" to T's. By chained pointer to T we mean Chris@16: * a type P such that, given a p of Type P Chris@16: * *...n...*x is convertible to T&, for some n>=1. Chris@16: * Examples of chained pointers are raw and smart pointers, iterators and Chris@16: * arbitrary combinations of these (vg. T** or auto_ptr.) Chris@16: */ Chris@16: Chris@16: template Chris@16: struct const_member_base Chris@16: { Chris@16: typedef Type result_type; Chris@16: Chris@16: template Chris@16: Chris@16: #if !defined(BOOST_NO_SFINAE) Chris@16: typename disable_if< Chris@16: is_convertible,Type&>::type Chris@16: #else Chris@16: Type& Chris@16: #endif Chris@16: Chris@16: operator()(const ChainedPtr& x)const Chris@16: { Chris@16: return operator()(*x); Chris@16: } Chris@16: Chris@16: Type& operator()(const Class& x)const Chris@16: { Chris@16: return x.*PtrToMember; Chris@16: } Chris@16: Chris@16: Type& operator()(const reference_wrapper& x)const Chris@16: { Chris@16: return operator()(x.get()); Chris@16: } Chris@16: Chris@101: Type& operator()(const reference_wrapper& x)const Chris@16: { Chris@16: return operator()(x.get()); Chris@16: } Chris@16: }; Chris@16: Chris@16: template Chris@16: struct non_const_member_base Chris@16: { Chris@16: typedef Type result_type; Chris@16: Chris@16: template Chris@16: Chris@16: #if !defined(BOOST_NO_SFINAE) Chris@16: typename disable_if< Chris@16: is_convertible,Type&>::type Chris@16: #else Chris@16: Type& Chris@16: #endif Chris@16: Chris@16: operator()(const ChainedPtr& x)const Chris@16: { Chris@16: return operator()(*x); Chris@16: } Chris@16: Chris@101: const Type& operator()(const Class& x)const Chris@16: { Chris@16: return x.*PtrToMember; Chris@16: } Chris@16: Chris@16: Type& operator()(Class& x)const Chris@16: { Chris@16: return x.*PtrToMember; Chris@16: } Chris@16: Chris@101: const Type& operator()(const reference_wrapper& x)const Chris@16: { Chris@16: return operator()(x.get()); Chris@16: } Chris@16: Chris@16: Type& operator()(const reference_wrapper& x)const Chris@16: { Chris@16: return operator()(x.get()); Chris@16: } Chris@16: }; Chris@16: Chris@16: } /* namespace multi_index::detail */ Chris@16: Chris@16: template Chris@16: struct member: Chris@16: mpl::if_c< Chris@16: is_const::value, Chris@16: detail::const_member_base, Chris@16: detail::non_const_member_base Chris@16: >::type Chris@16: { Chris@16: }; Chris@16: Chris@16: namespace detail{ Chris@16: Chris@16: /* MSVC++ 6.0 does not support properly pointers to members as Chris@16: * non-type template arguments, as reported in Chris@16: * http://support.microsoft.com/default.aspx?scid=kb;EN-US;249045 Chris@16: * A similar problem (though not identical) is shown by MSVC++ 7.0. Chris@16: * We provide an alternative to member<> accepting offsets instead Chris@16: * of pointers to members. This happens to work even for non-POD Chris@16: * types (although the standard forbids use of offsetof on these), Chris@16: * so it serves as a workaround in this compiler for all practical Chris@16: * purposes. Chris@16: * Surprisingly enough, other compilers, like Intel C++ 7.0/7.1 and Chris@16: * Visual Age 6.0, have similar bugs. This replacement of member<> Chris@16: * can be used for them too. Chris@101: * Chris@101: * Support for such old compilers is dropped and Chris@101: * [non_]const_member_offset_base is deprecated. Chris@16: */ Chris@16: Chris@16: template Chris@16: struct const_member_offset_base Chris@16: { Chris@16: typedef Type result_type; Chris@16: Chris@16: template Chris@16: Chris@16: #if !defined(BOOST_NO_SFINAE) Chris@16: typename disable_if< Chris@16: is_convertible,Type&>::type Chris@16: #else Chris@16: Type& Chris@16: #endif Chris@16: Chris@16: operator()(const ChainedPtr& x)const Chris@16: { Chris@16: return operator()(*x); Chris@16: } Chris@16: Chris@16: Type& operator()(const Class& x)const Chris@16: { Chris@16: return *static_cast( Chris@16: static_cast( Chris@16: static_cast( Chris@16: static_cast(&x))+OffsetOfMember)); Chris@16: } Chris@16: Chris@16: Type& operator()(const reference_wrapper& x)const Chris@16: { Chris@16: return operator()(x.get()); Chris@16: } Chris@16: Chris@101: Type& operator()(const reference_wrapper& x)const Chris@16: { Chris@16: return operator()(x.get()); Chris@16: } Chris@16: }; Chris@16: Chris@16: template Chris@16: struct non_const_member_offset_base Chris@16: { Chris@16: typedef Type result_type; Chris@16: Chris@16: template Chris@16: Chris@16: #if !defined(BOOST_NO_SFINAE) Chris@16: typename disable_if< Chris@16: is_convertible,Type&>::type Chris@16: #else Chris@16: Type& Chris@16: #endif Chris@16: Chris@16: operator()(const ChainedPtr& x)const Chris@16: { Chris@16: return operator()(*x); Chris@16: } Chris@16: Chris@101: const Type& operator()(const Class& x)const Chris@16: { Chris@16: return *static_cast( Chris@16: static_cast( Chris@16: static_cast( Chris@16: static_cast(&x))+OffsetOfMember)); Chris@16: } Chris@16: Chris@16: Type& operator()(Class& x)const Chris@16: { Chris@16: return *static_cast( Chris@16: static_cast( Chris@16: static_cast(static_cast(&x))+OffsetOfMember)); Chris@16: } Chris@16: Chris@101: const Type& operator()(const reference_wrapper& x)const Chris@16: { Chris@16: return operator()(x.get()); Chris@16: } Chris@16: Chris@16: Type& operator()(const reference_wrapper& x)const Chris@16: { Chris@16: return operator()(x.get()); Chris@16: } Chris@16: }; Chris@16: Chris@16: } /* namespace multi_index::detail */ Chris@16: Chris@16: template Chris@16: struct member_offset: Chris@16: mpl::if_c< Chris@16: is_const::value, Chris@16: detail::const_member_offset_base, Chris@16: detail::non_const_member_offset_base Chris@16: >::type Chris@16: { Chris@16: }; Chris@16: Chris@16: /* BOOST_MULTI_INDEX_MEMBER resolves to member in the normal cases, Chris@16: * and to member_offset as a workaround in those defective compilers for Chris@16: * which BOOST_NO_POINTER_TO_MEMBER_TEMPLATE_PARAMETERS is defined. Chris@16: */ Chris@16: Chris@16: #if defined(BOOST_NO_POINTER_TO_MEMBER_TEMPLATE_PARAMETERS) Chris@16: #define BOOST_MULTI_INDEX_MEMBER(Class,Type,MemberName) \ Chris@16: ::boost::multi_index::member_offset< Class,Type,offsetof(Class,MemberName) > Chris@16: #else Chris@16: #define BOOST_MULTI_INDEX_MEMBER(Class,Type,MemberName) \ Chris@16: ::boost::multi_index::member< Class,Type,&Class::MemberName > Chris@16: #endif Chris@16: Chris@16: } /* namespace multi_index */ Chris@16: Chris@16: } /* namespace boost */ Chris@16: Chris@16: #endif