Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members

diagnostic.hxx

00001 
00002 // 
00003 // Copyright (C) 2006 James M. Lawrence.  All rights reserved.
00004 // 
00005 // software: esve-1.0.3
00006 // file: esve/message/diagnostic.hxx
00007 // 
00008 // Redistribution and use in source and binary forms, with or without
00009 // modification, are permitted provided that the following conditions
00010 // are met:
00011 // 
00012 // 1. Redistributions of source code must retain the above copyright
00013 //    notice, this list of conditions and the following disclaimer.
00014 // 
00015 // 2. The origin of this software must not be misrepresented; you must
00016 //    not claim that you wrote the original software.  If you use this
00017 //    software in a product, an acknowledgment in the product 
00018 //    documentation would be appreciated but is not required.
00019 // 
00020 // 3. Altered source versions must be plainly marked as such, and must
00021 //    not be misrepresented as being the original software.
00022 // 
00023 // 4. The name of the author may not be used to endorse or promote
00024 //    products derived from this software without specific prior written
00025 //    permission.
00026 // 
00027 // THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY,
00028 // AND WITH NO CLAIM AS TO ITS SUITABILITY FOR ANY PURPOSE.  IN NO EVENT
00029 // SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM WHATSOEVER MADE IN CONNECTION
00030 // TO THIS SOFTWARE.
00031 // 
00033 
00034 #ifndef esve_message_diagnostic_hxx
00035 #define esve_message_diagnostic_hxx
00036 
00037 #if defined(ESVE_DIAGNOSTIC)
00038 
00039 #if defined(ESVE_DEMANGLE)
00040 #include <cxxabi.h>
00041 #endif
00042 
00043 #include <iostream>
00044 #include <string>
00045 #include <typeinfo>
00046 
00047 namespace esve { namespace message {
00048 
00049 namespace diagnostic {
00050 
00051 inline
00052 void
00053 indent()
00054 {
00055     std::cerr.fill(' ') ;
00056     std::cerr.width(24) ;
00057     std::cerr.setf(std::ios_base::right, std::ios_base::adjustfield) ;
00058 }
00059 
00060 template< typename Key >
00061 void
00062 key( const Key & k )
00063 {
00064     indent() ;
00065     std::cerr << k << ": " ;
00066 }
00067 
00068 template< typename Value >
00069 void
00070 value( const Value & v )
00071 {
00072     indent() ;
00073     std::cerr << " " << "  " << v << std::endl ;
00074 }
00075 
00076 template< typename Value >
00077 void
00078 pair( const std::string & k,
00079       const Value & v )
00080 {
00081     key(k) ;
00082     std::cerr << v << std::endl ;
00083 }
00084 
00085 template< typename Value1, typename Value2 >
00086 void
00087 triple( const std::string & k,
00088         const Value1 & v1,
00089         const Value2 & v2 )
00090 {
00091     key(k) ;
00092     std::cerr << v1 << '\t' << v2 << std::endl ;
00093 }
00094 
00095 #define esve_report(expr) \
00096     esve::message::diagnostic::pair(#expr, (expr))
00097 
00098 inline
00099 void
00100 header_begin( const std::string & message_name )
00101 {
00102     std::cerr.fill('_') ;
00103     std::cerr.width(79) ;
00104     std::cerr << "_" << std::endl ;
00105     std::cerr << message_name << "()" << std::endl ;
00106 }
00107 
00108 template< typename Sender, typename Receiver >
00109 void
00110 header( const std::string & message_name,
00111         const Sender & sender,
00112         const Receiver & receiver )
00113 {
00114     header_begin(message_name) ;
00115 #if defined(ESVE_DEMANGLE)
00116     int status = 0 ;
00117     triple("from",
00118            dynamic_cast<const void*>(&sender),
00119            abi::__cxa_demangle(typeid(sender).name(), 0, 0, &status)) ;
00120     triple("to",
00121            dynamic_cast<const void*>(&receiver),
00122            abi::__cxa_demangle(typeid(receiver).name(), 0, 0, &status)) ;
00123 #else
00124     triple("from",
00125            dynamic_cast<const void*>(&sender),
00126            typeid(sender).name()) ;
00127     triple("to",
00128            dynamic_cast<const void*>(&receiver),
00129            typeid(receiver).name()) ;
00130 #endif
00131 }
00132 
00133 template< typename Receiver >
00134 void
00135 header_receiver_only( const std::string & message_name,
00136                       const Receiver & receiver )
00137 {
00138     header_begin(message_name) ;
00139 #if defined(ESVE_DEMANGLE)
00140     int status = 0 ;
00141     triple("to",
00142            dynamic_cast<const void*>(&receiver),
00143            abi::__cxa_demangle(typeid(receiver).name(), 0, 0, &status)) ;
00144 #else
00145     triple("to",
00146            dynamic_cast<const void*>(&receiver),
00147            typeid(receiver).name()) ;
00148 #endif
00149 }
00150 
00151 } // namespace diagnostic
00152 
00153 }} // namespace esve::message
00154 
00155 #endif // defined(ESVE_DIAGNOSTIC)
00156 
00157 #endif

Generated on Tue May 30 11:40:52 2006 for esve by doxygen 1.3.4