• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright Antony Polukhin, 2016-2020.
2 //
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)
6 
7 #ifndef BOOST_STACKTRACE_DETAIL_TO_HEX_ARRAY_HPP
8 #define BOOST_STACKTRACE_DETAIL_TO_HEX_ARRAY_HPP
9 
10 #include <boost/config.hpp>
11 #ifdef BOOST_HAS_PRAGMA_ONCE
12 #   pragma once
13 #endif
14 
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>
19 
20 namespace boost { namespace stacktrace { namespace detail {
21 
22 BOOST_STATIC_CONSTEXPR char to_hex_array_bytes[] = "0123456789ABCDEF";
23 
24 template <class T>
to_hex_array(T addr)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"};
27     ret.back() = '\0';
28     BOOST_STATIC_ASSERT_MSG(!boost::is_pointer<T>::value, "");
29 
30     const std::size_t s = sizeof(T);
31 
32     char* out = ret.data() + s * 2 + 1;
33 
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];
37         -- out;
38         *out = to_hex_array_bytes[tmp_addr >> 4];
39         -- out;
40         addr >>= 8;
41     }
42 
43     return ret;
44 }
45 
to_hex_array(const void * addr)46 inline boost::array<char, 2 + sizeof(void*) * 2 + 1> to_hex_array(const void* addr) BOOST_NOEXCEPT {
47     return to_hex_array(
48         reinterpret_cast< boost::make_unsigned<std::ptrdiff_t>::type >(addr)
49     );
50 }
51 
52 }}} // namespace boost::stacktrace::detail
53 
54 #endif // BOOST_STACKTRACE_DETAIL_TO_HEX_ARRAY_HPP
55