The Gaudi Framework  v33r0 (d5ea422b)
GaudiCommonImp.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #ifndef GAUDIALG_GAUDICOMMONIMP_H
12 #define GAUDIALG_GAUDICOMMONIMP_H 1
13 // ============================================================================
14 // Include files
15 // ============================================================================
16 #include <algorithm>
17 // ============================================================================
18 // GaudiAlg
19 // ============================================================================
20 #include "GaudiAlg/GaudiCommon.h"
21 #include "GaudiAlg/GetData.h"
22 // ============================================================================
30 // ============================================================================
31 // Templated access to the data in Gaudi Transient Store
32 // ============================================================================
33 template <class PBASE>
34 template <class TYPE>
36 GaudiCommon<PBASE>::get( IDataProviderSvc* service, const std::string& location, const bool useRootInTES ) const {
37  // check the environment
38  Assert( service, "get():: IDataProvider* points to NULL!" );
39  // get the helper object:
41  return getter( *this, service, this->fullTESLocation( location, useRootInTES ) );
42 }
43 // ============================================================================
44 // Templated access to the data in Gaudi Transient Store, no check on the content
45 // ============================================================================
46 template <class PBASE>
47 template <class TYPE>
50  const bool useRootInTES ) const {
51  // check the environment
52  Assert( service, "get():: IDataProvider* points to NULL!" );
53  // get the helper object:
55  return getter( *this, service, this->fullTESLocation( location, useRootInTES ), false );
56 }
57 // ============================================================================
58 // check the existence of objects in Gaudi Transient Store
59 // ============================================================================
60 template <class PBASE>
61 template <class TYPE>
63  const bool useRootInTES ) const {
64  // check the environment
65  Assert( service, "exist():: IDataProvider* points to NULL!" );
66  // check the data object
68  return checker( service, this->fullTESLocation( location, useRootInTES ) );
69 }
70 // ============================================================================
71 // get the existing object from Gaudi Event Transient store
72 // or create new object register in in TES and return if object
73 // does not exist
74 // ============================================================================
75 template <class PBASE>
76 template <class TYPE, class TYPE2>
79  const bool useRootInTES ) const {
80  // check the environment
81  Assert( service, "getOrCreate():: svc points to NULL!" );
82  // get the helper object
84  return getter( *this, service, this->fullTESLocation( location, useRootInTES ), location );
85 }
86 // ============================================================================
87 // the useful method for location of tools.
88 // ============================================================================
89 template <class PBASE>
90 template <class TOOL>
91 inline TOOL* GaudiCommon<PBASE>::tool( const std::string& type, const std::string& name, const IInterface* parent,
92  bool create ) const {
93  // for empty names delegate to another method
94  if ( name.empty() ) return tool<TOOL>( type, parent, create );
95  Assert( this->toolSvc(), "tool():: IToolSvc* points to NULL!" );
96  // get the tool from Tool Service
97  TOOL* Tool = nullptr;
98  const StatusCode sc = this->toolSvc()->retrieveTool( type, name, Tool, parent, create );
99  if ( sc.isFailure() ) { Exception( "tool():: Could not retrieve Tool '" + type + "'/'" + name + "'", sc ); }
100  if ( !Tool ) { Exception( "tool():: Could not retrieve Tool '" + type + "'/'" + name + "'" ); }
101  // insert tool into list of tools
102  PBASE::registerTool( Tool );
103  m_managedTools.push_back( Tool );
104  // return *VALID* located tool
105  return Tool;
106 }
107 // ============================================================================
108 // the useful method for location of tools.
109 // ============================================================================
110 template <class PBASE>
111 template <class TOOL>
112 inline TOOL* GaudiCommon<PBASE>::tool( const std::string& type, const IInterface* parent, bool create ) const {
113  // check the environment
114  Assert( PBASE::toolSvc(), "IToolSvc* points to NULL!" );
115  // retrieve the tool from Tool Service
116  TOOL* Tool = nullptr;
117  const StatusCode sc = this->toolSvc()->retrieveTool( type, Tool, parent, create );
118  if ( sc.isFailure() ) { Exception( "tool():: Could not retrieve Tool '" + type + "'", sc ); }
119  if ( !Tool ) { Exception( "tool():: Could not retrieve Tool '" + type + "'" ); }
120  // add the tool into the list of known tools to be properly released
121  PBASE::registerTool( Tool );
122  m_managedTools.push_back( Tool );
123  // return *VALID* located tool
124  return Tool;
125 }
126 // ============================================================================
127 // the useful method for location of services
128 // ============================================================================
129 template <class PBASE>
130 template <class SERVICE>
131 inline SmartIF<SERVICE> GaudiCommon<PBASE>::svc( const std::string& name, const bool create ) const {
132  Assert( this->svcLoc(), "ISvcLocator* points to NULL!" );
134  // check if we already have this service
135  auto it = std::lower_bound( std::begin( m_services ), std::end( m_services ), name, GaudiCommon_details::svc_lt );
136  if ( it != std::end( m_services ) && GaudiCommon_details::svc_eq( *it, name ) ) {
137  // Try to get the requested interface
138  s = *it;
139  // check the results
140  if ( !s ) { Exception( "svc():: Could not retrieve Svc '" + name + "'", StatusCode::FAILURE ); }
141  } else {
142  auto baseSvc = this->svcLoc()->service( name, create );
143  // Try to get the requested interface
144  s = baseSvc;
145  // check the results
146  if ( !baseSvc || !s ) { Exception( "svc():: Could not retrieve Svc '" + name + "'", StatusCode::FAILURE ); }
147  // add the tool into list of known tools, to be properly released
148  addToServiceList( baseSvc );
149  }
150  // return *VALID* located service
151  return s;
152 }
153 // ============================================================================
154 // Short-cut to get a pointer to the UpdateManagerSvc
155 // ============================================================================
156 template <class PBASE>
158  if ( !m_updMgrSvc ) { m_updMgrSvc = svc<IUpdateManagerSvc>( "UpdateManagerSvc", true ); }
159  return m_updMgrSvc;
160 }
161 // ============================================================================
162 // Assertion - throw exception, if condition is not fulfilled
163 // ============================================================================
164 template <class PBASE>
165 inline void GaudiCommon<PBASE>::Assert( const bool ok, const std::string& msg, const StatusCode sc ) const {
166  if ( !ok ) Exception( msg, sc );
167 }
168 // ============================================================================
169 // Assertion - throw exception, if condition is not fulfilled
170 // ============================================================================
171 template <class PBASE>
172 inline void GaudiCommon<PBASE>::Assert( const bool ok, const char* msg, const StatusCode sc ) const {
173  if ( !ok ) Exception( msg, sc );
174 }
175 // ============================================================================
189 // ============================================================================
190 #define ALG_ERROR( message, code ) \
191  ( Error( message + std::string( " [ at line " ) + std::to_string( __LINE__ ) + std::string( " in file '" ) + \
192  std::string( __FILE__ ) + "']", \
193  code ) )
194 
195 // ============================================================================
196 // The END
197 // ============================================================================
198 #endif // GAUDIALG_GAUDICOMMONIMP_H
IUpdateManagerSvc * updMgrSvc() const
Short-cut to locate the Update Manager Service.
Small smart pointer class with automatic reference counting for IInterface.
Definition: IConverter.h:25
Gaudi::Utils::GetData< TYPE >::return_type getIfExists(IDataProviderSvc *svc, const std::string &location, const bool useRootInTES=true) const
Quicker version of the get function which bypasses the check on the retrieved data.
TOOL * tool(const std::string &type, const std::string &name, const IInterface *parent=0, bool create=true) const
Useful method for the easy location of tools.
constexpr const struct GaudiCommon_details::svc_eq_t svc_eq
Helper structure for implementation of "get"-functions for GaudiCommon<BASE>
Definition: GaudiCommon.h:63
T end(T... args)
Data provider interface definition.
T lower_bound(T... args)
STL class.
void Assert(const bool ok, const std::string &message="", const StatusCode sc=StatusCode(StatusCode::FAILURE, true)) const
Assertion - throw exception if the given condition is not fulfilled.
constexpr const struct GaudiCommon_details::svc_lt_t svc_lt
Helper structure for implementation of "exists"-functions for GaudiCommon<BASE>
Definition: GetData.h:324
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:61
Definition of the basic interface.
Definition: IInterface.h:254
Gaudi::Utils::GetData< TYPE >::return_type get(IDataProviderSvc *svc, const std::string &location, const bool useRootInTES=true) const
Templated access to the data in Gaudi Transient Store.
SmartIF< SERVICE > svc(const std::string &name, const bool create=true) const
A useful method for the easy location of services.
Helper structure for implementation of "getOrCreate"-functions for GaudiCommon<BASE>
Definition: GetData.h:398
std::string fullTESLocation(std::string_view location, std::string_view rit)
Definition: FixTESPath.cpp:56
Interface class to the Update Manager service.
T begin(T... args)
string s
Definition: gaudirun.py:328
constexpr static const auto FAILURE
Definition: StatusCode.h:97
bool exist(IDataProviderSvc *svc, const std::string &location, const bool useRootInTES=true) const
Check the existence of a data object or container in the Gaudi Transient Event Store.
bool isFailure() const
Definition: StatusCode.h:141
Gaudi::Utils::GetData< TYPE >::return_type getOrCreate(IDataProviderSvc *svc, const std::string &location, const bool useRootInTES=true) const
Get the existing data object from Gaudi Event Transient store.