The Gaudi Framework  v33r1 (b1225454)
DLLClassManager.cpp
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 // Include files
12 #include "DLLClassManager.h"
17 #include "GaudiKernel/IService.h"
21 #include "GaudiKernel/MsgStream.h"
22 #include "GaudiKernel/System.h"
23 
24 #include "GaudiKernel/IAlgorithm.h"
25 
26 #include <cassert>
27 #include <iostream>
28 
29 // default creator
31  m_pOuter = iface;
33 
34  assert( m_svclocator.isValid() );
35 
36  addRef(); // Initial count set to 1
37 }
38 
39 // implementation of IClassManager::loadModule
40 StatusCode DLLClassManager::loadModule( const std::string& module, bool fireIncident ) {
41  // Access the message service if not yet done already
42  if ( !m_msgsvc ) m_msgsvc = m_svclocator;
43  MsgStream log( m_msgsvc, "DllClassManager" );
44 
45  std::string mod = module == "" ? System::moduleNameFull() : module;
46  if ( module == "NONE" ) return StatusCode::SUCCESS;
47 
48  void* libHandle = nullptr;
50  try {
51  status = System::loadDynamicLib( module, &libHandle ) ? StatusCode::SUCCESS : StatusCode::FAILURE;
52  } catch ( const std::exception& excpt ) {
53  if ( m_msgsvc ) { log << MSG::ERROR << "Exception whilst loading " << module << " : " << excpt.what() << endmsg; }
54  status = StatusCode::FAILURE;
55  }
56 
57  if ( status.isFailure() ) {
58  // DLL library not loaded
59  log << MSG::ERROR << "Could not load module " << module << endmsg;
60  log << MSG::ERROR << "System Error: " << System::getLastErrorString() << endmsg;
61  return StatusCode::FAILURE;
62  }
63  // FIXME this is a hack to avoid a very early call to moduleLoad from
64  // FIXME AppMgr::i_startup
65  if ( fireIncident && !module.empty() ) {
66  // now fire ModuleLoadedIncident
67  const bool CREATEIF( true );
68  auto pIncidentSvc = m_svclocator->service<IIncidentSvc>( "IncidentSvc", CREATEIF );
69  if ( !pIncidentSvc ) {
70  log << MSG::FATAL << "Can not locate IncidentSvc" << endmsg;
71  throw GaudiException( "Error retrieving IncidentSvc", "DLLClassManager::DLLClassManager", StatusCode::FAILURE );
72  }
73  pIncidentSvc->fireIncident( ModuleLoadedIncident( "DLLClassManager", module ) );
74  }
75 
76  return StatusCode::SUCCESS;
77 }
78 
79 // implementation of IInterface::queryInterface
80 StatusCode DLLClassManager::queryInterface( const InterfaceID& iid, void** pinterface ) {
81  // try local interfaces
82  StatusCode sc = base_class::queryInterface( iid, pinterface );
83  if ( sc.isSuccess() ) return sc;
84  // fall back on the owner
85  return m_pOuter->queryInterface( iid, pinterface );
86 }
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:34
T empty(T... args)
Define general base for Gaudi exception.
StatusCode loadModule(const std::string &module, bool fireIncident=true) override
GAUDI_API const std::string & moduleNameFull()
Get the full name of the (executable/DLL) file.
Definition: ModuleInfo.cpp:87
bool isValid() const
Allow for check if smart pointer is valid.
Definition: SmartIF.h:72
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
STL class.
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:86
Interface ID class.
Definition: IInterface.h:39
T what(T... args)
SmartIF< IMessageSvc > m_msgsvc
unsigned long addRef() override
Reference Interface instance.
Definition: implements.h:48
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:61
Definition of the basic interface.
Definition: IInterface.h:254
fired when a module (DLL) is loaded
bool isSuccess() const
Definition: StatusCode.h:365
STL class.
StatusCode queryInterface(const InterfaceID &iid, void **pinterface) override
implementation of IInterface::queryInterface
constexpr static const auto FAILURE
Definition: StatusCode.h:101
DLLClassManager(IInterface *iface)
SmartIF< ISvcLocator > m_svclocator
bool isFailure() const
Definition: StatusCode.h:145
GAUDI_API const std::string getLastErrorString()
Get last system error as string.
Definition: System.cpp:272
StatusCode queryInterface(const InterfaceID &ti, void **pp) override
Implementation of IInterface::queryInterface.
Definition: implements.h:30
SmartIF< IInterface > m_pOuter
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
The interface implemented by the IncidentSvc service.
Definition: IIncidentSvc.h:33
GAUDI_API unsigned long loadDynamicLib(const std::string &name, ImageHandle *handle)
Load dynamic link library.
Definition: System.cpp:145
virtual StatusCode queryInterface(const InterfaceID &ti, void **pp)=0
Set the void** to the pointer to the requested interface of the instance.