object_hex_dump.hpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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. namespace
  12. boost
  13. {
  14. namespace
  15. exception_detail
  16. {
  17. template <class T>
  18. inline
  19. std::string
  20. object_hex_dump( T const & x, size_t max_size=16 )
  21. {
  22. std::ostringstream s;
  23. s << "type: " << type_name<T>() << ", size: " << sizeof(T) << ", dump: ";
  24. size_t n=sizeof(T)>max_size?max_size:sizeof(T);
  25. s.fill('0');
  26. s.width(2);
  27. unsigned char const * b=reinterpret_cast<unsigned char const *>(&x);
  28. s << std::setw(2) << std::hex << (unsigned int)*b;
  29. for( unsigned char const * e=b+n; ++b!=e; )
  30. s << " " << std::setw(2) << std::hex << (unsigned int)*b;
  31. return s.str();
  32. }
  33. }
  34. }
  35. #endif