ATLAS Offline Software
VP1ToolAccessHelper.icc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2018 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include "VP1Base/VP1Msg.h"
6 
7 template <class toolT>
8 inline toolT * VP1ToolAccessHelper::getToolPointer( const QString& tooltypeandname, bool silent, bool createIfNotExists )
9 {
10  //We use the typeid for dynamic type-checking, since, for unknown
11  //reasons, we have to static_cast through void pointers in order to
12  //keep correct pointers and avoid dynamic casts to null!
13  QString typeidstr(typeid(toolT*).name());
14 
15  if (VP1Msg::verbose())
16  messageVerbose("getToolPointer(..) called with tool type/name = "+tooltypeandname
17  +", with return typeid = "+typeidstr);
18  if (!m_toolsvc) {
19  messageVerbose("getToolPointer(..) WARNING does not have toolSvc pointer. Returning NULL.");
20  return 0;
21  }
22  if (tooltypeandname.isEmpty()||!tooltypeandname.contains('/')) {
23  messageDebug("getToolPointer(..) WARNING requested tool name has incorrect format ("+tooltypeandname+")!");
24  return 0;
25  }
26 
27  std::pair<QString,QString> id(tooltypeandname,typeidstr);
28  const typename std::map<std::pair<QString,QString>,IAlgTool*>::iterator it = m_toolname2pointer.find(id);
29  if (it==m_toolname2pointer.end()) {
30  //This is the first attempt at retrieval.
31 
32  //Fixme/todo: Should we check that tool exists before any attempts
33  //at retrieval are done??
34 
35  //Create tool handle:
36  ToolHandle<toolT> toolhandle( tooltypeandname.toStdString(),
37  0/*public tools only*/,
38  createIfNotExists );
39  bool exception = true;
40  bool retrieveok = false;
41  try {
42  retrieveok = !toolhandle.retrieve().isFailure();
43  exception = false;
44  } catch (const std::runtime_error& e) {
45  exception = true;
46  }
47  if (exception) {
48  if (silent)
49  messageVerbose("ToolHandle<>::retrieve() throws exception.");
50  else
51  messageDebug("ToolHandle<>::retrieve() throws exception.");
52  m_toolname2pointer[id] = 0;
53 
54  return 0;
55  }
56  if (!retrieveok) {
57  if (silent)
58  messageVerbose("getToolPointer(..) ERROR: Failed to retrieve tool: "+tooltypeandname);
59  else
60  message("getToolPointer(..) ERROR: Failed to retrieve tool: "+tooltypeandname);
61  m_toolname2pointer[id] = 0;
62  return 0;
63  }
64 
65  //Get pointer.
66  toolT * thetool = &(*toolhandle);
67  if (!thetool) {
68  message("getToolPointer(..) ERROR: Tool retrieve claimed to be succesful, but pointer is NULL!");
69  return 0;
70  }
71 
72  if (!isValidInterface(thetool)) {
73  message("getToolPointer(..) ERROR: Tool retrieved is not a valid interface!");
74  thetool = 0;
75  }
76  if (VP1Msg::verbose())
77  messageVerbose("Returning "+str(thetool)+" tool pointer.");
78  //We need the cast to void ptr to work around some WEIRD pointer
79  //values introduced by a more straight-forward casting:
80  void* voidptr = static_cast<void*>(thetool);
81  IAlgTool* algptr = static_cast<IAlgTool*>(voidptr);
82  if (VP1Msg::verbose())
83  messageVerbose("getToolPointer(..) Storing ptr = "+str(algptr));
84  m_toolname2pointer[id] = algptr;
85  return thetool;
86  } else {
87  //Not first attempt:
88  if (!it->second) {
89  messageVerbose("getToolPointer(..) Returning null tool pointer since previous retrieval attempts failed)).");
90  return 0;
91  }
92  toolT* pointer = static_cast<toolT*>(static_cast<void*>(it->second));
93  if (!pointer) {
94  //Fixme: Respect silent flag here?
95  message("getToolPointer(..) ERROR: Could not cast IAlgTool pointer "+str(it->second)
96  +" to type "+QString(typeid(toolT*).name())+". Returning 0.");
97  return 0;
98  }
99  if (VP1Msg::verbose())
100  messageVerbose("getToolPointer(..) Retrieved tool pointer ("+str(pointer)+") succesfully");
101  return pointer;
102  }
103 }