Chris@102: /* Chris@102: * Distributed under the Boost Software License, Version 1.0. Chris@102: * (See accompanying file LICENSE_1_0.txt or copy at Chris@102: * http://www.boost.org/LICENSE_1_0.txt) Chris@102: * Chris@102: * Copyright (c) 2009 Helge Bahmann Chris@102: * Copyright (c) 2012 Tim Blechmann Chris@102: * Copyright (c) 2013 - 2014 Andrey Semashev Chris@102: */ Chris@102: /*! Chris@102: * \file atomic/detail/casts.hpp Chris@102: * Chris@102: * This header defines \c union_cast and \c memcpy_cast used to convert between storage and value types Chris@102: */ Chris@102: Chris@102: #ifndef BOOST_ATOMIC_DETAIL_CASTS_HPP_INCLUDED_ Chris@102: #define BOOST_ATOMIC_DETAIL_CASTS_HPP_INCLUDED_ Chris@102: Chris@102: #include Chris@102: #include Chris@102: Chris@102: #ifdef BOOST_HAS_PRAGMA_ONCE Chris@102: #pragma once Chris@102: #endif Chris@102: Chris@102: namespace boost { Chris@102: namespace atomics { Chris@102: namespace detail { Chris@102: Chris@102: template< typename To, typename From > Chris@102: BOOST_FORCEINLINE To union_cast(From const& from) BOOST_NOEXCEPT Chris@102: { Chris@102: union Chris@102: { Chris@102: To as_to; Chris@102: From as_from; Chris@102: } Chris@102: caster = {}; Chris@102: caster.as_from = from; Chris@102: return caster.as_to; Chris@102: } Chris@102: Chris@102: template< typename To, typename From > Chris@102: BOOST_FORCEINLINE To memcpy_cast(From const& from) BOOST_NOEXCEPT Chris@102: { Chris@102: struct Chris@102: { Chris@102: To to; Chris@102: } Chris@102: value = {}; Chris@102: std::memcpy Chris@102: ( Chris@102: &reinterpret_cast< char& >(value.to), Chris@102: &reinterpret_cast< const char& >(from), Chris@102: (sizeof(From) < sizeof(To) ? sizeof(From) : sizeof(To)) Chris@102: ); Chris@102: return value.to; Chris@102: } Chris@102: Chris@102: } // namespace detail Chris@102: } // namespace atomics Chris@102: } // namespace boost Chris@102: Chris@102: #endif // BOOST_ATOMIC_DETAIL_CASTS_HPP_INCLUDED_