object_hex_dump.hpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. //Copyright (c) 2006-2009 Emil Dotchevski and Reverge Studios, Inc.
  2. //Distributed under the Boost Software License, Version 1.0. (See accompanying
  3. //file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. #ifndef BOOST_EXCEPTION_6F463AC838DF11DDA3E6909F56D89593
  5. #define BOOST_EXCEPTION_6F463AC838DF11DDA3E6909F56D89593
  6. #include <boost/exception/detail/type_info.hpp>
  7. #include <iomanip>
  8. #include <ios>
  9. #include <string>
  10. #include <sstream>
  11. #include <cstdlib>
  12. #ifndef BOOST_EXCEPTION_ENABLE_WARNINGS
  13. #if __GNUC__*100+__GNUC_MINOR__>301
  14. #pragma GCC system_header
  15. #endif
  16. #ifdef __clang__
  17. #pragma clang system_header
  18. #endif
  19. #ifdef _MSC_VER
  20. #pragma warning(push,1)
  21. #endif
  22. #endif
  23. namespace
  24. boost
  25. {
  26. namespace
  27. exception_detail
  28. {
  29. template <class T>
  30. inline
  31. std::string
  32. object_hex_dump( T const & x, std::size_t max_size=16 )
  33. {
  34. std::ostringstream s;
  35. s << "type: " << type_name<T>() << ", size: " << sizeof(T) << ", dump: ";
  36. std::size_t n=sizeof(T)>max_size?max_size:sizeof(T);
  37. s.fill('0');
  38. s.width(2);
  39. unsigned char const * b=reinterpret_cast<unsigned char const *>(&x);
  40. s << std::setw(2) << std::hex << (unsigned int)*b;
  41. for( unsigned char const * e=b+n; ++b!=e; )
  42. s << " " << std::setw(2) << std::hex << (unsigned int)*b;
  43. return s.str();
  44. }
  45. }
  46. }
  47. #if defined(_MSC_VER) && !defined(BOOST_EXCEPTION_ENABLE_WARNINGS)
  48. #pragma warning(pop)
  49. #endif
  50. #endif