Brian Silverman | 7e17102 | 2018-08-05 00:17:49 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Distributed under the Boost Software License, Version 1.0. |
| 3 | * (See accompanying file LICENSE_1_0.txt or copy at |
| 4 | * http://www.boost.org/LICENSE_1_0.txt) |
| 5 | * |
| 6 | * Copyright (c) 2018 Andrey Semashev |
| 7 | */ |
| 8 | /*! |
| 9 | * \file atomic/detail/addressof.hpp |
| 10 | * |
| 11 | * This header defines \c addressof helper function. It is similar to \c boost::addressof but it is more |
| 12 | * lightweight and also contains a workaround for some compiler warnings. |
| 13 | */ |
| 14 | |
| 15 | #ifndef BOOST_ATOMIC_DETAIL_ADDRESSOF_HPP_INCLUDED_ |
| 16 | #define BOOST_ATOMIC_DETAIL_ADDRESSOF_HPP_INCLUDED_ |
| 17 | |
| 18 | #include <boost/atomic/detail/config.hpp> |
| 19 | |
| 20 | #ifdef BOOST_HAS_PRAGMA_ONCE |
| 21 | #pragma once |
| 22 | #endif |
| 23 | |
| 24 | // Detection logic is based on boost/core/addressof.hpp |
| 25 | #if defined(BOOST_MSVC_FULL_VER) && BOOST_MSVC_FULL_VER >= 190024215 |
| 26 | #define BOOST_ATOMIC_DETAIL_HAS_BUILTIN_ADDRESSOF |
| 27 | #elif defined(BOOST_GCC) && BOOST_GCC >= 70000 |
| 28 | #define BOOST_ATOMIC_DETAIL_HAS_BUILTIN_ADDRESSOF |
| 29 | #elif defined(__has_builtin) |
| 30 | #if __has_builtin(__builtin_addressof) |
| 31 | #define BOOST_ATOMIC_DETAIL_HAS_BUILTIN_ADDRESSOF |
| 32 | #endif |
| 33 | #endif |
| 34 | |
| 35 | namespace boost { |
| 36 | namespace atomics { |
| 37 | namespace detail { |
| 38 | |
| 39 | template< typename T > |
| 40 | BOOST_FORCEINLINE T* addressof(T& value) BOOST_NOEXCEPT |
| 41 | { |
| 42 | #if defined(BOOST_ATOMIC_DETAIL_HAS_BUILTIN_ADDRESSOF) |
| 43 | return __builtin_addressof(value); |
| 44 | #else |
| 45 | // Note: The point of using a local struct as the intermediate type instead of char is to avoid gcc warnings |
| 46 | // if T is a const volatile char*: |
| 47 | // warning: casting 'const volatile char* const' to 'const volatile char&' does not dereference pointer |
| 48 | // The local struct makes sure T is not related to the cast target type. |
| 49 | struct opaque_type; |
| 50 | return reinterpret_cast< T* >(&const_cast< opaque_type& >(reinterpret_cast< const volatile opaque_type& >(value))); |
| 51 | #endif |
| 52 | } |
| 53 | |
| 54 | } // namespace detail |
| 55 | } // namespace atomics |
| 56 | } // namespace boost |
| 57 | |
| 58 | #endif // BOOST_ATOMIC_DETAIL_ADDRESSOF_HPP_INCLUDED_ |