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) 2009 Helge Bahmann |
7 | * Copyright (c) 2012 Tim Blechmann |
8 | * Copyright (c) 2013 - 2014 Andrey Semashev |
9 | */ |
10 | /*! |
11 | * \file atomic/detail/bitwise_cast.hpp |
12 | * |
13 | * This header defines \c bitwise_cast used to convert between storage and value types |
14 | */ |
15 | |
16 | #ifndef BOOST_ATOMIC_DETAIL_BITWISE_CAST_HPP_INCLUDED_ |
17 | #define BOOST_ATOMIC_DETAIL_BITWISE_CAST_HPP_INCLUDED_ |
18 | |
19 | #include <boost/atomic/detail/config.hpp> |
20 | #if !defined(BOOST_ATOMIC_DETAIL_HAS_BUILTIN_MEMCPY) |
21 | #include <cstring> |
22 | #endif |
23 | |
24 | #ifdef BOOST_HAS_PRAGMA_ONCE |
25 | #pragma once |
26 | #endif |
27 | |
28 | #if defined(BOOST_GCC) && (BOOST_GCC+0) >= 40600 |
29 | #pragma GCC diagnostic push |
30 | // missing initializer for member var |
31 | #pragma GCC diagnostic ignored "-Wmissing-field-initializers" |
32 | #endif |
33 | |
34 | namespace boost { |
35 | namespace atomics { |
36 | namespace detail { |
37 | |
38 | template< typename T > |
39 | BOOST_FORCEINLINE T* addressof(T& value) BOOST_NOEXCEPT |
40 | { |
41 | // Note: The point of using a local struct as the intermediate type instead of char is to avoid gcc warnings |
42 | // if T is a const volatile char*: |
43 | // warning: casting 'const volatile char* const' to 'const volatile char&' does not dereference pointer |
44 | // The local struct makes sure T is not related to the cast target type. |
45 | struct opaque_type; |
46 | return reinterpret_cast< T* >(&const_cast< opaque_type& >(reinterpret_cast< const volatile opaque_type& >(value))); |
47 | } |
48 | |
49 | template< typename To, typename From > |
50 | BOOST_FORCEINLINE To bitwise_cast(From const& from) BOOST_NOEXCEPT |
51 | { |
52 | struct |
53 | { |
54 | To to; |
55 | } |
56 | value = {}; |
57 | BOOST_ATOMIC_DETAIL_MEMCPY |
58 | ( |
59 | atomics::detail::addressof(value.to), |
60 | atomics::detail::addressof(from), |
61 | (sizeof(From) < sizeof(To) ? sizeof(From) : sizeof(To)) |
62 | ); |
63 | return value.to; |
64 | } |
65 | |
66 | } // namespace detail |
67 | } // namespace atomics |
68 | } // namespace boost |
69 | |
70 | #if defined(BOOST_GCC) && (BOOST_GCC+0) >= 40600 |
71 | #pragma GCC diagnostic pop |
72 | #endif |
73 | |
74 | #endif // BOOST_ATOMIC_DETAIL_BITWISE_CAST_HPP_INCLUDED_ |
75 | |