ATLAS Offline Software
ServiceStore.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
3 */
4 
8 
9 
10 
11 // Local include(s):
13 #include "AsgServices/AsgService.h"
16 
17 // System include(s):
18 #include <map>
19 #include <iostream>
20 #include <mutex>
21 
23 typedef std::map< std::string, std::weak_ptr<asg::IAsgService> > ServiceMap_t;
24 
25 namespace
26 {
28  static ServiceMap_t s_services ATLAS_THREAD_SAFE;
29  std::mutex s_serviceMutex;
30 }
31 
32 namespace asg
33 {
34  StatusCode ServiceStore::
35  put( const std::shared_ptr<IAsgService>& pservice )
36  {
37  using namespace msgComponentConfig;
38 
39  // Start with a little sanity check:
40  if( ! pservice )
41  {
42  ANA_MSG_ERROR( "asg::ServiceStore::put " <<
43  "Received a null pointer" );
44  return StatusCode::FAILURE;
45  }
46 
47  // Get and check the name of the service:
48  const std::string& name = pservice->name();
49  if( ! name.size() )
50  {
51  ANA_MSG_ERROR( "asg::ServiceStore::put " <<
52  "The received service doesn't have a name" );
53  return StatusCode::FAILURE;
54  }
55 
56  std::lock_guard<std::mutex> lock (s_serviceMutex);
57 
58  auto& currentService = s_services[name];
59  if (currentService.lock())
60  {
61  ANA_MSG_ERROR ("Service with name \""
62  << name << "\" already registered");
63  return StatusCode::FAILURE;
64  }
65 
66  currentService = pservice;
67  return StatusCode::SUCCESS;
68  }
69 
70 
71 
72  std::shared_ptr<IAsgService> ServiceStore::
73  get( const std::string& name, bool silent )
74  {
75  using namespace msgToolHandle;
76 
77  std::lock_guard<std::mutex> lock (s_serviceMutex);
78  auto iservice = s_services.find( name );
79 
80  std::shared_ptr<IAsgService> result;
81  if( iservice != s_services.end() )
82  result = iservice->second.lock();
83 
84  if( ! silent && !result )
85  ANA_MSG_ERROR ("Service with name \"" << name << "\" not found");
86  return result;
87  }
88 
89 
90 
91  void ServiceStore::dumpServiceConfig ()
92  {
93  using namespace asg::msgProperty;
94 
95  // I am first putting everything into a map, so that the error
96  // messages don't interleave the property values
97  std::map<std::string,std::map<std::string,std::string> > properties;
98 
99  std::lock_guard<std::mutex> lock (s_serviceMutex);
100  for (auto& service : s_services)
101  {
102  auto myservice = service.second.lock();
103  if (myservice)
104  {
105  auto& myproperties = properties[service.first];
106  asg::IAsgService *myservicePtr = myservice.get(); // avoid Wpotentially-evaluated-expression from typeid
107  myproperties[""] = std::string (typeid(*myservicePtr).name()) + "/" + service.first;
108  for (auto& property : dynamic_cast<const AsgService*>(myservice.get())->getPropertyMgr()->getProperties())
109  {
110  std::string asString;
111  if (property.second->getString (asString).isFailure())
112  {
113  ANA_MSG_ERROR ("on property " << property.first << " for service " << service.first);
114  myproperties[property.first] = "<<invalid>>";
115  } else
116  myproperties[property.first] = asString;
117  }
118  }
119  }
120  // Using std::cout here intentionally to bypass the messaging
121  // system to avoid accidental changes to the formatting. This
122  // could be updated in the future to allow the user to pass in the
123  // stream to output to, if desired.
124  for (const auto& myproperties : properties)
125  {
126  for (auto& property : myproperties.second)
127  {
128  std::cout << myproperties.first;
129  if (!property.first.empty())
130  std::cout << "." << property.first;
131  std::cout << " = " << property.second << "\n";
132  }
133  }
134  std::cout << std::flush;
135  }
136 
137 } // namespace asg
get_generator_info.result
result
Definition: get_generator_info.py:21
AsgService.h
python.TestDriveDummies.properties
dictionary properties
Definition: TestDriveDummies.py:14
FullCPAlgorithmsTest_eljob.flush
flush
Definition: FullCPAlgorithmsTest_eljob.py:182
asg::IAsgService
Base class for the dual-use service interface classes.
Definition: IAsgService.h:26
BeamSpot::mutex
std::mutex mutex
Definition: InDetBeamSpotVertex.cxx:18
asg
Definition: DataHandleTestTool.h:28
ANA_MSG_ERROR
#define ANA_MSG_ERROR(xmsg)
Macro printing error messages.
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:294
ServiceMap_t
std::map< std::string, std::weak_ptr< asg::IAsgService > > ServiceMap_t
Convenience type definition.
Definition: ServiceStore.cxx:23
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ServiceStore.h
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
python.trfDecorators.silent
def silent(func)
Redirect stdout/err to /dev/null Useful wrapper to get rid of ROOT verbosity...
Definition: trfDecorators.py:24
MsgStreamMacros.h
get
T * get(TKey *tobj)
get a TObject* from a TKey* (why can't a TObject be a TKey?)
Definition: hcg.cxx:127
ATLAS_THREAD_SAFE
#define ATLAS_THREAD_SAFE
Definition: checker_macros.h:211
MessageCheckAsgTools.h