| # Gaudi Framework, version v22r0 |

Home |  | Generated: 9 Feb 2011

# 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> getter ;
    00063   return getter ( *this    ,
    00064                   service  ,
    00065                   fullTESLocation ( location , useRootInTES ) ) ;
    00066 }
    00067 // ============================================================================
    00068 // check the existence of objects in Gaudi Transient Store
    00069 // ============================================================================
    00070 template < class PBASE >
    00071 template < class TYPE  >
    00072 inline bool GaudiCommon<PBASE>::exist( IDataProviderSvc*  service  ,
    00073                                        const std::string& location ,
    00074                                        const bool useRootInTES ) const
    00075 {
    00076   // check the environment
    00077   Assert( 0 != service , "exist():: IDataProvider* points to NULL!"      ) ;
    00078   // check the data object
    00079   Gaudi::Utils::CheckData<TYPE> checker ;
    00080   return checker ( service,
    00081                    fullTESLocation ( location , useRootInTES ) ) ;
    00082 }
    00083 // ============================================================================
    00084 // get the existing object from Gaudi Event Transient store
    00085 // or create new object register in in TES and return if object
    00086 // does not exist
    00087 // ============================================================================
    00088 template <class PBASE>
    00089 template <class TYPE,class TYPE2>
    00090 inline typename Gaudi::Utils::GetData<TYPE>::return_type
    00091 GaudiCommon<PBASE>::getOrCreate( IDataProviderSvc*  service  ,
    00092                                  const std::string& location ,
    00093                                  const bool useRootInTES  ) const
    00094 {
    00095   // check the environment
    00096   Assert ( 0 != service , "getOrCreate():: svc points to NULL!" ) ;
    00097   // get the helper object
    00098   Gaudi::Utils::GetOrCreateData<TYPE,TYPE2> getter ;
    00099   return getter ( *this                                     ,
    00100                   service                                   ,
    00101                   fullTESLocation( location, useRootInTES ) ,
    00102                   location                                  ) ;
    00103 }
    00104 // ============================================================================
    00105 // the useful method for location of tools.
    00106 // ============================================================================
    00107 template < class PBASE >
    00108 template < class TOOL  >
    00109 inline TOOL* GaudiCommon<PBASE>::tool( const std::string& type           ,
    00110                                        const std::string& name           ,
    00111                                        const IInterface*  parent         ,
    00112                                        bool               create         ) const
    00113 {
    00114   TOOL* Tool = 0 ;
    00115   // for empty names delegate to another method
    00116   if ( name.empty() )
    00117   {
    00118     Tool = tool<TOOL>( type , parent , create ) ;
    00119   }
    00120   else
    00121   {
    00122     Assert( this->toolSvc() != 0, "tool():: IToolSvc* points to NULL!" ) ;
    00123     // get the tool from Tool Service
    00124     const StatusCode sc =
    00125       this->toolSvc()->retrieveTool ( type , name , Tool , parent , create ) ;
    00126     if ( sc.isFailure() )
    00127     { Exception("tool():: Could not retrieve Tool '" + type + "'/'" + name + "'", sc ) ; }
    00128     if ( 0 == Tool )
    00129     { Exception("tool():: Could not retrieve Tool '" + type + "'/'" + name + "'"     ) ; }
    00130     // add the tool into list of known tools to be properly released
    00131     addToToolList( Tool );
    00132   }
    00133   // return *VALID* located tool
    00134   return Tool ;
    00135 }
    00136 // ============================================================================
    00137 // the useful method for location of tools.
    00138 // ============================================================================
    00139 template < class PBASE >
    00140 template < class TOOL  >
    00141 inline TOOL* GaudiCommon<PBASE>::tool( const std::string& type   ,
    00142                                        const IInterface*  parent ,
    00143                                        bool               create ) const
    00144 {
    00145   // check the environment
    00146   Assert ( PBASE::toolSvc() != 0, "IToolSvc* points to NULL!" );
    00147   // retrieve the tool from Tool Service
    00148   TOOL* Tool = 0 ;
    00149   const StatusCode sc =
    00150     this->toolSvc() -> retrieveTool ( type, Tool, parent , create   );
    00151   if ( sc.isFailure() )
    00152   { Exception("tool():: Could not retrieve Tool '" + type + "'", sc ) ; }
    00153   if ( 0 == Tool )
    00154   { Exception("tool():: Could not retrieve Tool '" + type + "'"     ) ; }
    00155   // add the tool into the list of known tools to be properly released
    00156   addToToolList( Tool );
    00157   // return *VALID* located tool
    00158   return Tool ;
    00159 }
    00160 // ============================================================================
    00161 // the useful method for location of services
    00162 // ============================================================================
    00163 template < class PBASE   >
    00164 template < class SERVICE >
    00165 inline SmartIF<SERVICE> GaudiCommon<PBASE>::svc( const std::string& name   ,
    00166                                                   const bool         create ) const
    00167 {
    00168   Assert ( this->svcLoc() != 0, "ISvcLocator* points to NULL!" );
    00169   SmartIF<SERVICE> s;
    00170   // check if we already have this service
    00171   Services::iterator it = m_services.find(name);
    00172   if (it != m_services.end()) {
    00173     // Try to get the requested interface
    00174     s = it->second;
    00175     // check the results
    00176     if ( !s.isValid() ) {
    00177       Exception ("svc():: Could not retrieve Svc '" + name + "'", StatusCode::FAILURE);
    00178     }
    00179   } else {
    00180     SmartIF<IService>& baseSvc = this->svcLoc()->service(name, create);
    00181     // Try to get the requested interface
    00182     s = baseSvc;
    00183     // check the results
    00184     if ( !baseSvc.isValid() || !s.isValid() ) {
    00185       Exception ("svc():: Could not retrieve Svc '" + name + "'", StatusCode::FAILURE);
    00186     }
    00187     // add the tool into list of known tools, to be properly released
    00188     addToServiceList(baseSvc);
    00189   }
    00190   // return *VALID* located service
    00191   return s;
    00192 }
    00193 // ============================================================================
    00194 // Short-cut  to get a pointer to the UpdateManagerSvc
    00195 // ============================================================================
    00196 template <class PBASE>
    00197 inline IUpdateManagerSvc *
    00198 GaudiCommon<PBASE>::updMgrSvc() const
    00199 {
    00200   if ( !m_updMgrSvc )
    00201   { m_updMgrSvc = svc<IUpdateManagerSvc>("UpdateManagerSvc",true); }
    00202   return m_updMgrSvc ;
    00203 }
    00204 // ============================================================================
    00205 // Short-cut  to get a pointer to the
    00206 // ============================================================================
    00207 template <class PBASE>
    00208 inline IDataProviderSvc *
    00209 GaudiCommon<PBASE>::fastContainersSvc() const
    00210 {
    00211   if ( !m_fastContainersSvc )
    00212   { m_fastContainersSvc = svc<IDataProviderSvc>("FastContainersSvc",true); }
    00213   return m_fastContainersSvc ;
    00214 }
    00215 // ============================================================================
    00216 // Get a fast container
    00217 // ============================================================================
    00218 template < class PBASE   >
    00219 template < class T >
    00220 inline TransientFastContainer<T> *
    00221 GaudiCommon<PBASE>::getFastContainer( const std::string &location,
    00222                                       typename TransientFastContainer<T>::size_type initial )
    00223 {
    00224   typedef TransientFastContainer<T> container_type;
    00225
    00226   IDataProviderSvc* svc = fastContainersSvc();
    00227   Assert( 0 != svc , "getFastContainer(): cannot get FastContainersSvc" );
    00228
    00229   container_type *ptr = NULL;
    00230   SmartDataPtr<container_type> obj( svc, location );
    00231   if (!obj){
    00232     ptr = new container_type(initial);
    00233     StatusCode status = svc->registerObject(location,ptr);
    00234     if ( !status.isSuccess() ){
    00235       Exception("getFastContainer(): cannot register '" +
    00236                 System::typeinfoName( typeid( *ptr ) ) +
    00237                 "' at address '" + location + "'"  , status );
    00238     }
    00239   } else {
    00240     ptr = obj;
    00241     if ( !ptr ){
    00242       Exception("getFastContainer(): No valid container at '" + location + "'");
    00243     }
    00244   }
    00245
    00246   return ptr;
    00247 }
    00248 // ============================================================================
    00249 // predefined configurable message stream for the effective printouts
    00250 // ============================================================================
    00251 template <class PBASE>
    00252 inline MsgStream&
    00253 GaudiCommon<PBASE>::msgStream ( const MSG::Level level ) const
    00254 {
    00255   if ( !m_msgStream )
    00256   { m_msgStream = new MsgStream ( PBASE::msgSvc() , this->name() ) ; }
    00257   return *m_msgStream << level ;
    00258 }
    00259 // ============================================================================
    00260 // Assertion - throw exception, if condition is not fulfilled
    00261 // ============================================================================
    00262 template <class PBASE>
    00263 inline void GaudiCommon<PBASE>::Assert( const bool         ok  ,
    00264                                         const std::string& msg ,
    00265                                         const StatusCode   sc  ) const
    00266 {
    00267   if (!ok) Exception( msg , sc );
    00268 }
    00269 // ============================================================================
    00270 // Delete the current message stream object
    00271 // ============================================================================
    00272 template <class PBASE>
    00273 inline void GaudiCommon<PBASE>::resetMsgStream() const
    00274 {
    00275   if ( 0 != m_msgStream ) { delete m_msgStream; m_msgStream = 0; }
    00276 }
    00277 // ============================================================================
    00278 // Assertion - throw exception, if condition is not fulfilled
    00279 // ============================================================================
    00280 template <class PBASE>
    00281 inline void GaudiCommon<PBASE>::Assert( const bool        ok  ,
    00282                                         const char*       msg ,
    00283                                         const StatusCode  sc  ) const
    00284 {
    00285   if (!ok) Exception( msg , sc );
    00286 }
    00287 // ============================================================================
    00301 // ============================================================================
    00302 #define ALG_ERROR( message , code )                                     \
    00303   ( Error( message                                   +                  \
    00304            std::string             ( " [ at line " ) +                  \
    00305            GaudiAlg::fileLine      (   __LINE__    ) +                  \
    00306            std::string             ( " in file '"  ) +                  \
    00307            std::string             (   __FILE__    ) + "']" , code ) )
    00308
    00309 // ============================================================================
    00310 // The END
    00311 // ============================================================================
    00312 #endif // GAUDIALG_GAUDICOMMONIMP_H

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines