object_hex_dump.hpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 UUID_6F463AC838DF11DDA3E6909F56D89593
  5. #define UUID_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. #if (__GNUC__*100+__GNUC_MINOR__>301) && !defined(BOOST_EXCEPTION_ENABLE_WARNINGS)
  13. #pragma GCC system_header
  14. #endif
  15. #if defined(_MSC_VER) && !defined(BOOST_EXCEPTION_ENABLE_WARNINGS)
  16. #pragma warning(push,1)
  17. #endif
  18. namespace
  19. boost
  20. {
  21. namespace
  22. exception_detail
  23. {
  24. template <class T>
  25. inline
  26. std::string
  27. object_hex_dump( T const & x, std::size_t max_size=16 )
  28. {
  29. std::ostringstream s;
  30. s << "type: " << type_name<T>() << ", size: " << sizeof(T) << ", dump: ";
  31. std::size_t n=sizeof(T)>max_size?max_size:sizeof(T);
  32. s.fill('0');
  33. s.width(2);
  34. unsigned char const * b=reinterpret_cast<unsigned char const *>(&x);
  35. s << std::setw(2) << std::hex << (unsigned int)*b;
  36. for( unsigned char const * e=b+n; ++b!=e; )
  37. s << " " << std::setw(2) << std::hex << (unsigned int)*b;
  38. return s.str();
  39. }
  40. }
  41. }
  42. #if defined(_MSC_VER) && !defined(BOOST_EXCEPTION_ENABLE_WARNINGS)
  43. #pragma warning(pop)
  44. #endif
  45. #endif