Stat.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007 #include <sstream>
00008
00009
00010
00011 #include "GaudiKernel/IStatSvc.h"
00012 #include "GaudiKernel/ICounterSvc.h"
00013 #include "GaudiKernel/Stat.h"
00014 #include "GaudiKernel/StatEntity.h"
00015
00016
00017
00018 #include "boost/format.hpp"
00019
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 Stat::Stat ( IStatSvc* svc ,
00045 const std::string& tag )
00046 : m_entity ( 0 )
00047 , m_tag ( tag )
00048 , m_group ( )
00049 , m_stat ( svc )
00050 , m_counter ( 0 )
00051 {
00052 if ( 0 != m_stat )
00053 {
00054 m_stat -> addRef() ;
00055
00056 const StatEntity* tmp = m_stat->stat ( tag ) ;
00057 if ( 0 == tmp )
00058 {
00059
00060 m_stat->stat ( tag , 0 ) ;
00061 tmp = m_stat->stat ( tag ) ;
00062 StatEntity* aux = const_cast<StatEntity*>( tmp );
00063 aux->reset () ;
00064 }
00065 m_entity = const_cast<StatEntity*> ( tmp ) ;
00066 }
00067 }
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086 Stat::Stat ( IStatSvc* svc ,
00087 const std::string& tag ,
00088 const double flag )
00089 : m_entity ( 0 )
00090 , m_tag ( tag )
00091 , m_group ( )
00092 , m_stat ( svc )
00093 , m_counter ( 0 )
00094 {
00095 if ( 0 != m_stat )
00096 {
00097 m_stat -> addRef() ;
00098 m_stat -> stat( tag , flag ) ;
00099
00100 m_entity = const_cast<StatEntity*>( m_stat -> stat ( tag ) ) ;
00101 }
00102 }
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112 Stat::Stat ( ICounterSvc* svc ,
00113 const std::string& group ,
00114 const std::string& name )
00115 : m_entity ( 0 )
00116 , m_tag ( name )
00117 , m_group ( group )
00118 , m_stat ( 0 )
00119 , m_counter ( svc )
00120 {
00121 if ( 0 != m_counter )
00122 {
00123 m_counter -> addRef() ;
00124
00125 m_entity = m_counter -> get ( group , name ) ;
00126
00127 if ( 0 == m_entity ) { m_counter -> create( group , name , 0 , m_entity ) ; }
00128 }
00129 }
00130
00131
00132
00133 Stat::Stat( const Stat& right )
00134 : m_entity ( right.m_entity )
00135 , m_tag ( right.m_tag )
00136 , m_group ( right.m_group )
00137 , m_stat ( right.m_stat )
00138 , m_counter ( right.m_counter )
00139 {
00140 if ( 0 != m_stat ) { m_stat -> addRef () ; }
00141 if ( 0 != m_counter ) { m_counter -> addRef () ; }
00142 }
00143
00144
00145
00146 Stat& Stat::operator=( const Stat& right)
00147 {
00148 if ( this == &right ) { return *this ; }
00149 m_entity = right.m_entity ;
00150 m_tag = right.m_tag ;
00151 m_group = right.m_group ;
00152 {
00153 IStatSvc* stat= right.m_stat ;
00154 if ( 0 != stat ) { stat -> addRef() ; }
00155 if ( 0 != m_stat ) { m_stat -> release() ; m_stat = 0 ; }
00156 m_stat = stat ;
00157 }
00158 {
00159 ICounterSvc* counter= right.m_counter ;
00160 if ( 0 != counter ) { counter -> addRef() ; }
00161 if ( 0 != m_counter ) { m_counter -> release() ; m_counter = 0 ; }
00162 m_counter = counter ;
00163 }
00164 return *this ;
00165 }
00166
00167
00168
00169 Stat::~Stat()
00170 {
00171 m_entity = 0 ;
00172 if ( 0 != m_stat ) { m_stat -> release() ; m_stat = 0 ; }
00173 if ( 0 != m_counter ) { m_counter -> release() ; m_counter = 0 ; }
00174 }
00175
00176
00177
00178 std::string Stat::toString() const
00179 {
00180 std::ostringstream ost ;
00181 print ( ost ) ;
00182 return ost.str () ;
00183 }
00184
00185
00186
00187
00188
00189 std::ostream& Stat::print( std::ostream& o ) const
00190 {
00191 if ( m_group.empty() && m_tag.empty() )
00192 { return 0 == m_entity ? ( o << "NULL" ) : ( o << m_entity ) ; }
00193 if ( !m_group.empty() )
00194 {
00195 if ( 0 != m_entity )
00196 {
00197 return o << boost::format(" %|1$15s|::%|2$-15s| %|32t|%3%")
00198 % ( "\"" + m_group ) % ( m_tag + "\"") % (*m_entity) ;
00199 }
00200 else
00201 {
00202 return o << boost::format(" %|1$15s|::%|2$-15s| %|32t|%NULL%")
00203 % ( "\"" + m_group ) % ( m_tag + "\"") ;
00204 }
00205 }
00206 if ( 0 != m_entity )
00207 {
00208 return o << boost::format(" %|1$=30s| %|32t|%2%")
00209 % ("\"" + m_tag + "\"" ) % (*m_entity) ;
00210 }
00211 return o << boost::format(" %|1$=30s| %|32t|%NULL%")
00212 % ( "\"" + m_tag + "\"" ) ;
00213 }
00214
00215
00216
00217 Stat operator+( const Stat& stat , const double value )
00218 { Stat s( stat ) ; s += value ; return s ; }
00219
00220
00221
00222 Stat operator-( const Stat& stat , const double value )
00223 { Stat s( stat ) ; s -= value ; return s ; }
00224
00225
00226
00227 Stat operator+( const double value , const Stat& stat )
00228 { Stat s( stat ) ; s += value ; return s ; }
00229
00230
00231 Stat operator+( const Stat& stat , const Stat& value )
00232 { Stat s( stat ) ; s += value ; return s ; }
00233
00234
00235 std::ostream& operator<<( std::ostream& stream , const Stat& stat )
00236 { return stat.print( stream ) ; }
00237
00238
00239
00240
00241