00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef _dgDebugStd_h_
00027 #define _dgDebugStd_h_
00028
00034 #include <list>
00035 #include <vector>
00036 #include <map>
00037 #include <typeinfo>
00038
00039 #include "dgConfig.h"
00040 #include "dgBoost.h"
00041 #include "dgDebug.h"
00042
00043 namespace DGD {
00044
00048 template <class First_type, class Second_type>
00049 inline channel& operator << ( channel& cnl,
00050 const std::pair<First_type, Second_type>& p ) {
00051 cnl << "[" << dgd << p.first << " " << dgd << p.second << "]";
00052 return cnl;
00053 }
00054
00060 template <>
00061 inline channel& operator << ( channel& cnl,
00062 const std::pair<int, char**>& params ) {
00063 cnl.wrap(false);
00064 for( int i = 0; i < params.first; ++i ) {
00065 cnl << params.second[i] << " ";
00066 }
00067 cnl.wrap(true);
00068 return cnl;
00069 }
00070
00081 template <class Item_type>
00082 inline channel& operator << ( channel& cnl,
00083 const std::list<Item_type>& l ) {
00084 cnl << "{" << dgd << incr << std::endl;
00085 for( DGD_TYPENAME std::list<Item_type>::const_iterator i = l.begin();
00086 i != l.end();
00087 ++i ) {
00088 cnl << (*i) << std::endl;
00089 }
00090 cnl << decr << "}";
00091 return cnl;
00092 }
00093
00104 template <class Item_type>
00105 inline channel& operator << ( channel& cnl,
00106 const std::vector<Item_type>& v ) {
00107 cnl << "{" << dgd << incr << std::endl;
00108 for( DGD_TYPENAME std::vector<Item_type>::const_iterator i = v.begin();
00109 i != v.end();
00110 ++i ) {
00111 cnl << (*i) << std::endl;
00112 }
00113 cnl << decr << "}";
00114 return cnl;
00115 }
00116
00128 template <class Key_type, class Value_type>
00129 inline channel& operator << ( channel& cnl,
00130 const std::map<Key_type, Value_type>& m ) {
00131 cnl << "{" << dgd << incr << std::endl;
00132 for( DGD_TYPENAME
00133 std::map<Key_type, Value_type>::const_iterator i = m.begin();
00134 i != m.end();
00135 ++i ) {
00136 cnl << (*i) << std::endl;
00137 }
00138 cnl << decr << "}";
00139 return cnl;
00140 }
00141
00155 template <class InputIter>
00156 channel &dgd_copy( InputIter begin, InputIter end,
00157 channel &ch, const char* sep = NULL ) {
00158 while( begin != end )
00159 ch << *begin++ << sep;
00160 return ch;
00161 }
00162
00174 template <class Circulator>
00175 channel &dgd_copy( Circulator begin, channel &ch, const char* sep = NULL ) {
00176 Circulator circ = begin;
00177 do {
00178 ch << *circ++ << (sep ? sep : " ");
00179 } while( begin != circ );
00180 return ch;
00181 }
00182
00188 template <class Iterator>
00189 struct dgd_sequence {
00190 Iterator m_begin;
00191 Iterator m_end;
00192 const char* m_separator;
00193
00194 dgd_sequence( const Iterator& begin,
00195 const Iterator& end,
00196 const char* separator = " " ) :
00197 m_begin(begin), m_end(end), m_separator(separator) {}
00198 dgd_sequence( const dgd_sequence<Iterator>& peer ) :
00199 m_begin(peer.m_begin), m_end(peer.m_end),
00200 m_separator(peer.m_separator) {}
00201 };
00202
00208 template <class Circulator>
00209 struct dgd_circle {
00210 Circulator m_begin;
00211 const char* m_separator;
00212
00213 dgd_circle( const Circulator& begin,
00214 const char* separator = " " ) :
00215 m_begin(begin), m_separator(separator) {}
00216 dgd_circle( const dgd_circle<Circulator>& peer ) :
00217 m_begin(peer.m_begin), m_separator(peer.m_separator) {}
00218 };
00219
00223 template <class Iterator>
00224 dgd_sequence<Iterator> dgd_make_sequence( const Iterator& begin,
00225 const Iterator& end,
00226 const char* separator = " " ) {
00227 return dgd_sequence<Iterator>( begin, end, separator );
00228 }
00229
00233 template <class Circulator>
00234 dgd_circle<Circulator> dgd_make_circle( const Circulator& begin,
00235 const char* separator = " " ) {
00236 return dgd_circle<Circulator>( begin, separator );
00237 }
00238
00242 template <class Iterator>
00243 inline channel& operator << ( channel& cnl,
00244 const dgd_sequence<Iterator>& seq ) {
00245 return dgd_copy( seq.m_begin, seq.m_end, cnl, seq.m_separator );
00246 }
00247
00251 template <class Circulator>
00252 inline channel& operator << ( channel& cnl,
00253 const dgd_circle<Circulator>& circle ) {
00254 return dgd_copy( circle.m_begin, cnl, circle.m_separator );
00255 }
00256
00263 template <class T>
00264 struct dgd_reference {
00265 const T* const m_pointer;
00266 const bool m_use_as_void;
00267 const unsigned int m_size;
00268
00269 dgd_reference() : m_pointer(NULL), m_use_as_void(false), m_size(0) {}
00270 explicit dgd_reference( const T* const p,
00271 const bool use_as_void = false,
00272 const unsigned int size = 0 ) :
00273 m_pointer( p ), m_use_as_void(use_as_void), m_size(size) {}
00274
00275 dgd_reference( const dgd_reference& peer ) :
00276 m_pointer( peer.m_pointer ),
00277 m_use_as_void( peer.m_use_as_void ),
00278 m_size( peer.m_size ) {}
00279 };
00280
00281
00297 template <class T>
00298 inline dgd_reference<T> mem_ref( const T* const p,
00299 const bool use_as_void = true,
00300 const unsigned int size = 0 ) {
00301 return dgd_reference<T>(p, use_as_void, size);
00302 }
00303
00310 template <class T>
00311 inline dgd_reference<T> mem( const T* const p,
00312 const unsigned int size = sizeof(T),
00313 const bool use_as_void = true ) {
00314 return dgd_reference<T>(p, use_as_void, size);
00315 }
00316
00321 template <class T>
00322 inline std::ostream& operator << ( std::ostream& cnl,
00323 const dgd_reference<T>& ptr ) {
00324 if( !ptr.m_use_as_void ) {
00325 cnl << "(" << typeid(T).name() << "*)";
00326 }
00327 std::ostream::fmtflags flags = cnl.flags();
00328 cnl << std::hex << (void*)ptr.m_pointer;
00329
00330 if( ptr.m_size > 0 ) {
00331 channel* dgd_cnl = dynamic_cast< channel* > ( &cnl );
00332 unsigned int width = channelbuf::DefaultMaxWidth;
00333
00334 if( dgd_cnl ) {
00335 if( dgd_cnl->wrap() ) {
00336 width = dgd_cnl->max_width() - dgd_cnl->indent();
00337 }
00338 }
00339
00340 unsigned int num_bytes_per_line =
00341 std::min( (unsigned int)16, (width - 10) / 4 );
00342 unsigned int bytes_written = 0, i;
00343 unsigned char* mem = (unsigned char*)ptr.m_pointer;
00344
00345 cnl << std::dec << " " << ptr.m_size
00346 << std::hex << " bytes" << std::endl;
00347 while(bytes_written < ptr.m_size) {
00348 cnl << (unsigned long)( mem + bytes_written ) << " ";
00349
00350 for(i = 0; i < num_bytes_per_line; i++) {
00351 if( bytes_written+i < ptr.m_size ) {
00352 cnl.width(2);
00353 cnl << (unsigned int)(0xff & mem[bytes_written+i]) << " ";
00354 } else {
00355 cnl << " ";
00356 }
00357 }
00358 cnl << " ";
00359 for( i = 0; i < num_bytes_per_line &&
00360 bytes_written+i < ptr.m_size; i++ ) {
00361 char c = mem[bytes_written+i];
00362 cnl << ((c >= ' ')?c:'.');
00363 }
00364 bytes_written += num_bytes_per_line;
00365 cnl << std::endl;
00366 }
00367 }
00368
00369 cnl.flags( flags );
00370 return cnl;
00371 }
00372
00378 template <class T>
00379 struct dgd_safe_ptr {
00380 const T* const m_pointer;
00381
00382 dgd_safe_ptr() : m_pointer(NULL) {}
00383 explicit dgd_safe_ptr( const T* const p ) : m_pointer( p ) {}
00384
00385 dgd_safe_ptr( const dgd_reference<T>& peer ) :
00386 m_pointer( peer.m_pointer ) {};
00387 };
00388
00392 template <class T>
00393 inline dgd_safe_ptr<T> ptr( const T* const p ) {
00394 return dgd_safe_ptr<T>(p);
00395 }
00396
00400 template <class T>
00401 inline channel& operator << ( channel& cnl,
00402 const dgd_safe_ptr<T>& ptr ) {
00403 if( ptr.m_pointer == NULL ) {
00404 cnl << "(" << typeid(T).name() << "*)NULL";
00405 } else {
00406 cnl << (const T&)(*ptr.m_pointer);
00407 }
00408
00409 return cnl;
00410 }
00411
00412 };
00413
00414 #endif
00415
00416
00417
00418
00419
00420
00421
00422