Gaudi Framework, version v21r6

Home   Generated: 11 Nov 2009

GaudiCommonImp.h

Go to the documentation of this file.
00001 // $Id: GaudiCommonImp.h,v 1.11 2008/10/10 08:06:33 marcocle Exp $
00002 // ============================================================================
00003 #ifndef GAUDIALG_GAUDICOMMONIMP_H
00004 #define GAUDIALG_GAUDICOMMONIMP_H 1
00005 // ============================================================================
00006 // Include files
00007 // ============================================================================
00008 // GaudiAlg
00009 // ============================================================================
00010 #include "GaudiAlg/GetData.h"
00011 #include "GaudiAlg/GaudiCommon.h"
00012 // ============================================================================
00020 // ============================================================================
00021 // Returns the full correct event location given the rootInTes settings
00022 // ============================================================================
00023 template < class PBASE >
00024 inline const std::string
00025 GaudiCommon<PBASE>::fullTESLocation( const std::string & location,
00026                                      const bool useRootInTES ) const
00027 {
00028   // The logic is:
00029   // if no R.I.T., give back location
00030   // if R.I.T., this is the mapping:
00031   // (note that R.I.T. contains a trailing '/')
00032   //  location       -> result
00033   //  -------------------------------------------------
00034   //  ""             -> R.I.T.[:-1]      ("rit")
00035   //  "/Event"       -> R.I.T.[:-1]      ("rit")
00036   //  "/Event/MyObj" -> R.I.T. + "MyObj" ("rit/MyObj")
00037   //  "MyObj"        -> R.I.T. + "MyObj" ("rit/MyObj")
00038   return ( !useRootInTES || rootInTES().empty() ?
00039            location
00040          :
00041            location.empty() || ( location == "/Event" ) ?
00042              rootInTES().substr(0,rootInTES().size()-1)
00043            :
00044              0 == location.find("/Event/") ?
00045                rootInTES() + location.substr(7)
00046              :
00047                rootInTES() + location );
00048 }
00049 // ============================================================================
00050 // Templated access to the data in Gaudi Transient Store
00051 // ============================================================================
00052 template < class PBASE >
00053 template < class TYPE  >
00054 inline typename Gaudi::Utils::GetData<TYPE>::return_type
00055 GaudiCommon<PBASE>::get( IDataProviderSvc*  service ,
00056                          const std::string& location,
00057                          const bool useRootInTES ) const
00058 {
00059   // check the environment
00060   Assert( 0 != service ,    "get():: IDataProvider* points to NULL!"      ) ;
00061   // get the helper object:
00062   Gaudi::Utils::GetData<TYPE> helper ;
00063   return helper ( *this , service ,
00064                   fullTESLocation ( location , useRootInTES ) ) ;
00065 }
00066 // ============================================================================
00067 // check the existence of objects in Gaudi Transient Store
00068 // ============================================================================
00069 template < class PBASE >
00070 template < class TYPE  >
00071 inline bool GaudiCommon<PBASE>::exist( IDataProviderSvc*  service  ,
00072                                        const std::string& location ,
00073                                        const bool useRootInTES ) const
00074 {
00075   // check the environment
00076   Assert( 0 != service , "exist():: IDataProvider* points to NULL!"      ) ;
00077   // check the data object
00078   const std::string & fullLoc = fullTESLocation( location, useRootInTES );
00079   SmartDataPtr<TYPE> obj( service , fullLoc ) ;
00080   return obj ? true : false ;
00081 }
00082 // ============================================================================
00083 // get the existing object from Gaudi Event Transient store
00084 // or create new object register in in TES and return if object
00085 // does not exist
00086 // ============================================================================
00087 template <class PBASE>
00088 template <class TYPE,class TYPE2>
00089 inline TYPE* GaudiCommon<PBASE>::getOrCreate( IDataProviderSvc* svc ,
00090                                               const std::string& location ,
00091                                               const bool useRootInTES  ) const
00092 {
00093   // check the environment
00094   Assert( 0 != svc , "getOrCreate():: svc points to NULL!" ) ;
00095   const std::string & fullLoc = fullTESLocation( location, useRootInTES );
00096   SmartDataPtr<TYPE> obj ( svc , fullLoc ) ;
00097   if ( !obj )
00098   {
00099     TYPE2* o = new TYPE2() ;
00100     put ( svc , o , location ) ; // do not use fullLoc here as put recreates it
00101     return o ;
00102   }
00103   TYPE* aux = obj ;
00104   if( !aux )
00105   { Exception ( "getOrCreate():: No valid data at '" + fullLoc + "'" ) ; }
00106   // return located *VALID* data
00107   return aux ;
00108 }
00109 // ============================================================================
00110 // the useful method for location of tools.
00111 // ============================================================================
00112 template < class PBASE >
00113 template < class TOOL  >
00114 inline TOOL* GaudiCommon<PBASE>::tool( const std::string& type           ,
00115                                        const std::string& name           ,
00116                                        const IInterface*  parent         ,
00117                                        bool               create         ) const
00118 {
00119   TOOL* Tool = 0 ;
00120   // for empty names delegate to another method
00121   if ( name.empty() )
00122   {
00123     Tool = tool<TOOL>( type , parent , create ) ;
00124   }
00125   else
00126   {
00127     Assert( this->toolSvc() != 0, "tool():: IToolSvc* points to NULL!" ) ;
00128     // get the tool from Tool Service
00129     const StatusCode sc =
00130       this->toolSvc()->retrieveTool ( type , name , Tool , parent , create ) ;
00131     if ( sc.isFailure() )
00132     { Exception("tool():: Could not retrieve Tool '" + type + "'/'" + name + "'", sc ) ; }
00133     if ( 0 == Tool )
00134     { Exception("tool():: Could not retrieve Tool '" + type + "'/'" + name + "'"     ) ; }
00135     // add the tool into list of known tools to be properly released
00136     addToToolList( Tool );
00137   }
00138   // return *VALID* located tool
00139   return Tool ;
00140 }
00141 // ============================================================================
00142 // the useful method for location of tools.
00143 // ============================================================================
00144 template < class PBASE >
00145 template < class TOOL  >
00146 inline TOOL* GaudiCommon<PBASE>::tool( const std::string& type   ,
00147                                        const IInterface*  parent ,
00148                                        bool               create ) const
00149 {
00150   // check the environment
00151   Assert ( PBASE::toolSvc() != 0, "IToolSvc* points to NULL!" );
00152   // retrieve the tool from Tool Service
00153   TOOL* Tool = 0 ;
00154   const StatusCode sc =
00155     this->toolSvc() -> retrieveTool ( type, Tool, parent , create   );
00156   if ( sc.isFailure() )
00157   { Exception("tool():: Could not retrieve Tool '" + type + "'", sc ) ; }
00158   if ( 0 == Tool )
00159   { Exception("tool():: Could not retrieve Tool '" + type + "'"     ) ; }
00160   // add the tool into the list of known tools to be properly released
00161   addToToolList( Tool );
00162   // return *VALID* located tool
00163   return Tool ;
00164 }
00165 // ============================================================================
00166 // the useful method for location of services
00167 // ============================================================================
00168 template < class PBASE   >
00169 template < class SERVICE >
00170 inline SmartIF<SERVICE> GaudiCommon<PBASE>::svc( const std::string& name   ,
00171                                                   const bool         create ) const
00172 {
00173   Assert ( this->svcLoc() != 0, "ISvcLocator* points to NULL!" );
00174   SmartIF<SERVICE> s;
00175   // check if we already have this service
00176   Services::iterator it = m_services.find(name);
00177   if (it != m_services.end()) {
00178     // Try to get the requested interface
00179     s = it->second;
00180     // check the results
00181     if ( !s.isValid() ) {
00182       Exception ("svc():: Could not retrieve Svc '" + name + "'", StatusCode::FAILURE);
00183     }
00184   } else {
00185     SmartIF<IService>& baseSvc = this->svcLoc()->service(name, create);
00186     // Try to get the requested interface
00187     s = baseSvc;
00188     // check the results
00189     if ( !baseSvc.isValid() || !s.isValid() ) {
00190       Exception ("svc():: Could not retrieve Svc '" + name + "'", StatusCode::FAILURE);
00191     }
00192     // add the tool into list of known tools, to be properly released
00193     addToServiceList(baseSvc);
00194   }
00195   // return *VALID* located service
00196   return s;
00197 }
00198 // ============================================================================
00199 // Short-cut  to get a pointer to the UpdateManagerSvc
00200 // ============================================================================
00201 template <class PBASE>
00202 inline IUpdateManagerSvc *
00203 GaudiCommon<PBASE>::updMgrSvc() const
00204 {
00205   if ( !m_updMgrSvc )
00206   { m_updMgrSvc = svc<IUpdateManagerSvc>("UpdateManagerSvc",true); }
00207   return m_updMgrSvc ;
00208 }
00209 // ============================================================================
00210 // Short-cut  to get a pointer to the
00211 // ============================================================================
00212 template <class PBASE>
00213 inline IDataProviderSvc *
00214 GaudiCommon<PBASE>::fastContainersSvc() const
00215 {
00216   if ( !m_fastContainersSvc )
00217   { m_fastContainersSvc = svc<IDataProviderSvc>("FastContainersSvc",true); }
00218   return m_fastContainersSvc ;
00219 }
00220 // ============================================================================
00221 // Get a fast container
00222 // ============================================================================
00223 template < class PBASE   >
00224 template < class T >
00225 inline TransientFastContainer<T> *
00226 GaudiCommon<PBASE>::getFastContainer( const std::string &location,
00227                                       typename TransientFastContainer<T>::size_type initial )
00228 {
00229   typedef TransientFastContainer<T> container_type;
00230 
00231   IDataProviderSvc* svc = fastContainersSvc();
00232   Assert( 0 != svc , "getFastContainer(): cannot get FastContainersSvc" );
00233 
00234   container_type *ptr = NULL;
00235   SmartDataPtr<container_type> obj( svc, location );
00236   if (!obj){
00237     ptr = new container_type(initial);
00238     StatusCode status = svc->registerObject(location,ptr);
00239     if ( !status.isSuccess() ){
00240       Exception("getFastContainer(): cannot register '" +
00241                 System::typeinfoName( typeid( *ptr ) ) +
00242                 "' at address '" + location + "'"  , status );
00243     }
00244   } else {
00245     ptr = obj;
00246     if ( !ptr ){
00247       Exception("getFastContainer(): No valid container at '" + location + "'");
00248     }
00249   }
00250   
00251   return ptr;
00252 }
00253 // ============================================================================
00254 // predefined configurable message stream for the effective printouts
00255 // ============================================================================
00256 template <class PBASE>
00257 inline MsgStream&
00258 GaudiCommon<PBASE>::msgStream ( const MSG::Level level ) const
00259 {
00260   if ( !m_msgStream )
00261   { m_msgStream = new MsgStream ( PBASE::msgSvc() , this->name() ) ; }
00262   return *m_msgStream << level ;
00263 }
00264 // ============================================================================
00265 // Assertion - throw exception, if condition is not fulfilled
00266 // ============================================================================
00267 template <class PBASE>
00268 inline StatusCode GaudiCommon<PBASE>::Assert( const bool         OK  ,
00269                                               const std::string& msg ,
00270                                               const StatusCode   sc  ) const
00271 {
00272   return ( OK ? StatusCode(StatusCode::SUCCESS, true) : Exception( msg , sc ) ) ;
00273 }
00274 // ============================================================================
00275 // Delete the current message stream object
00276 // ============================================================================
00277 template <class PBASE>
00278 inline void GaudiCommon<PBASE>::resetMsgStream() const
00279 {
00280   if ( 0 != m_msgStream ) { delete m_msgStream; m_msgStream = 0; }
00281 }
00282 // ============================================================================
00283 // Assertion - throw exception, if condition is not fulfilled
00284 // ============================================================================
00285 template <class PBASE>
00286 inline StatusCode
00287 GaudiCommon<PBASE>::Assert( const bool        OK  ,
00288                             const char*       msg ,
00289                             const StatusCode  sc  ) const
00290 {
00291   return OK ? StatusCode(StatusCode::SUCCESS, true) : Exception( msg , sc ) ;
00292 }
00293 // ============================================================================
00307 // ============================================================================
00308 #define ALG_ERROR( message , code )                                     \
00309   ( Error( message                                   +                  \
00310            std::string             ( " [ at line " ) +                  \
00311            GaudiAlg::fileLine      (   __LINE__    ) +                  \
00312            std::string             ( " in file '"  ) +                  \
00313            std::string             (   __FILE__    ) + "']" , code ) )
00314 
00315 // ============================================================================
00316 // The END 
00317 // ============================================================================
00318 #endif // GAUDIALG_GAUDICOMMONIMP_H

Generated at Wed Nov 11 16:22:56 2009 for Gaudi Framework, version v21r6 by Doxygen version 1.5.6 written by Dimitri van Heesch, © 1997-2004