ATLAS Offline Software
CloneService.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 
7 #include <cassert>
8 #include <vector>
9 
10 #include "GaudiKernel/ISvcLocator.h"
11 #include "GaudiKernel/ISvcManager.h"
12 #include "GaudiKernel/MsgStream.h"
13 #include "GaudiKernel/Service.h"
14 #include "GaudiKernel/SmartIF.h"
15 #include "GaudiKernel/System.h"
16 
17 namespace CloneService {
19  StatusCode clone(const IService* iParent, const std::string& childName,
20  Service*& child)
21  {
22  //FIXME this only creates an uninitialized SERVICE.
23  //FIXME it would be better if one could say parent->clone()
24  //FIXME (its default implementation could be this one)
25 
26  //check the parent pointer is valid
27  const Service* parent(0);
28  parent = dynamic_cast<const Service*>(iParent);
29  if (0 == parent) {
30  std::cout << " CloneService:clone ERROR: parent " << iParent->name()
31  << " is not a service!" << std::endl;
32  return StatusCode::FAILURE;
33  }
34 
35 
36  // can we access the Service locator interface?
37  ISvcLocator* svcLoc(parent->serviceLocator());
38  assert (0 != svcLoc);
39 
40  MsgStream mlog( parent->msgSvc(), "CloneService::clone" );
41 
42  //create the child unless it is already there
43  //if the service is not there and can't be created barf
44  IService* pIS(0);
45  const bool DONOTCREATE(false);
46  if ((svcLoc->getService(childName, pIS, DONOTCREATE)).isSuccess() &&
47  0 != (child = dynamic_cast<Service*>(pIS))) {
48  mlog << MSG::DEBUG
49  << "Found service " << childName
50  << endmsg;
51  } else {
52  // can we access the Service Manager interface?
53  SmartIF<ISvcManager> svcMgr(svcLoc);
54  if ( !svcMgr.isValid() ) {
55  mlog << MSG::FATAL
56  << "ISvcManager interface not found by serviceLocator."
57  << endmsg;
58  return StatusCode::FAILURE;
59  }
60  const std::string& parentType = System::typeinfoName(typeid(*parent));
61  if (svcMgr->createService(parentType, childName,
62  pIS).isSuccess() &&
63  0 != (child = dynamic_cast<Service*>(pIS))) {
64  mlog << MSG::DEBUG
65  << "Created service " << childName << " of type " << parentType
66  << endmsg;
67  } else {
68  mlog << MSG::ERROR
69  << "Failed to create " << childName << " of type " << parentType
70  << endmsg;
71  return StatusCode::FAILURE;
72  }
73 
74  }
75  //now copy parent's properties into child
76  std::vector<Gaudi::Details::PropertyBase*>::const_iterator iProp(parent->getProperties().begin());
77  std::vector<Gaudi::Details::PropertyBase*>::const_iterator eProp(parent->getProperties().end());
78  while (iProp != eProp &&
79  (child->setProperty(**iProp)).isSuccess()) ++iProp;
80  if (iProp != eProp) {
81  mlog << MSG::ERROR
82  << "Failed to set child property " << (**iProp).name()
83  << endmsg;
84  return StatusCode::FAILURE;
85  } else {
86  mlog << MSG::DEBUG
87  << childName << " properties set"
88  << endmsg;
89  }
90 
91  //finally put the service in the same state as the parent
92  //FIXME should we handle the case in which the parent is RUNNING?
93  if (Gaudi::StateMachine::INITIALIZED == parent->FSMState() &&
94  Gaudi::StateMachine::CONFIGURED == child->FSMState())
95  return child->sysInitialize();
96  else {
97  mlog << MSG::DEBUG
98  << "Did not initialize " << childName
99  << endmsg;
100  return StatusCode::SUCCESS;
101  }
102  }
103 }
python.Constants.FATAL
int FATAL
Definition: Control/AthenaCommon/python/Constants.py:19
StdJOSetup.svcMgr
svcMgr
Definition: StdJOSetup.py:33
CloneService
Definition: CloneService.h:11
python.FakeAthena.Service
def Service(name)
Definition: FakeAthena.py:38
python.InDetPriVxFinderConfig.mlog
mlog
Definition: InDetPriVxFinderConfig.py:134
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
test_pyathena.parent
parent
Definition: test_pyathena.py:15
CloneService.h
CloneService::clone
StatusCode clone(const IService *parent, const std::string &childName, Service *&child)
given a reference to a parent svc sets a reference to a cloned child
Definition: CloneService.cxx:19
DEBUG
#define DEBUG
Definition: page_access.h:11