1 // Copyright Antony Polukhin, 2016-2020.
3 // Distributed under the Boost Software License, Version 1.0. (See
4 // accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
7 #ifndef BOOST_STACKTRACE_DETAIL_TO_HEX_ARRAY_HPP
8 #define BOOST_STACKTRACE_DETAIL_TO_HEX_ARRAY_HPP
10 #include <boost/config.hpp>
11 #ifdef BOOST_HAS_PRAGMA_ONCE
15 #include <boost/array.hpp>
16 #include <boost/static_assert.hpp>
17 #include <boost/type_traits/is_pointer.hpp>
18 #include <boost/type_traits/make_unsigned.hpp>
20 namespace boost { namespace stacktrace { namespace detail {
22 BOOST_STATIC_CONSTEXPR char to_hex_array_bytes[] = "0123456789ABCDEF";
25 inline boost::array<char, 2 + sizeof(void*) * 2 + 1> to_hex_array(T addr) BOOST_NOEXCEPT {
26 boost::array<char, 2 + sizeof(void*) * 2 + 1> ret = {"0x"};
28 BOOST_STATIC_ASSERT_MSG(!boost::is_pointer<T>::value, "");
30 const std::size_t s = sizeof(T);
32 char* out = ret.data() + s * 2 + 1;
34 for (std::size_t i = 0; i < s; ++i) {
35 const unsigned char tmp_addr = (addr & 0xFFu);
36 *out = to_hex_array_bytes[tmp_addr & 0xF];
38 *out = to_hex_array_bytes[tmp_addr >> 4];
46 inline boost::array<char, 2 + sizeof(void*) * 2 + 1> to_hex_array(const void* addr) BOOST_NOEXCEPT {
48 reinterpret_cast< boost::make_unsigned<std::ptrdiff_t>::type >(addr)
52 }}} // namespace boost::stacktrace::detail
54 #endif // BOOST_STACKTRACE_DETAIL_TO_HEX_ARRAY_HPP