00001 #ifndef DVUTIL_DEBUG_DUMP_H 00002 #define DVUTIL_DEBUG_DUMP_H 00003 // $Id: debug_dump.h,v 1.4 2008/10/16 16:51:01 dvermeir Exp $ 00004 #include <iostream> 00005 #include <utility> // for std::pair 00006 #include <dvutil/iomanip.h> 00007 00008 namespace Dv { 00009 00010 /** Write a character buffer to a stream using only printable characters. 00011 * Non-printable chars are represented by a string of the form @a \\NNN 00012 * where @a NNN is the decimal code of the char. 00013 * @param os stream to print to 00014 * @param buf pointer to first char to print 00015 * @param n number of chars to print 00016 */ 00017 std::ostream& debug_write(std::ostream& os, const char* buf, size_t n); 00018 00019 /** Convenience type that allows to make iomanip objects 00020 * from buffers. */ 00021 typedef std::pair<const char*, size_t> Buffer; 00022 00023 /** Write a character buffer to a stream using only printable characters. 00024 * Non-printable chars are represented by a string of the form @a \\NNN 00025 * where @a NNN is the decimal code of the char. 00026 * @param os stream to print to 00027 * @param buf pair of buffer pointer and size 00028 * @sa Dv::debug_write(std::ostream&,const char*,size_t) 00029 */ 00030 inline std::ostream& debug_write(std::ostream& os, const Buffer& buf) { 00031 return debug_write(os, buf.first, buf.second); 00032 } 00033 00034 /** Iomanipulator type corresponding to buffers. */ 00035 typedef iomanip_c<Buffer> BufferDump; 00036 00037 /** Create an iomanip from a buffer with a size. This 00038 * supports code such as the following: 00039 * @code 00040 * const char* buf; 00041 * std::cout << "buf[1..20] = " << debug_dump(buf, 20) << std::endl; 00042 * @endcode 00043 * @param buf pointer to first char to print 00044 * @param size number of chars to print 00045 * @return iomanip 00046 */ 00047 inline BufferDump debug_dump(const char* buf, size_t size) { 00048 return BufferDump(debug_write, std::make_pair(buf, size)); 00049 } 00050 00051 /** Create an iomanip from an arbitrary object and a size. 00052 * This supports code such as the following: 00053 * @code 00054 * T object; 00055 * std::cout << "dump of t: " << debug_dump(object) << std::endl; 00056 * @endcode 00057 * @param t object to dump 00058 * @param size only the first size bytes will be dumped. 00059 * @return iomanip 00060 */ 00061 template<typename T> 00062 BufferDump debug_dump_t(const T& t, size_t size = sizeof(T)) { 00063 return debug_dump(reinterpret_cast<const char*>(&t), ( size <= sizeof(T) ? size : sizeof(T) )); 00064 } 00065 00066 } 00067 #endif
dvutil-1.0.10 | [ 5 December, 2009] |