ATLAS Offline Software
Loading...
Searching...
No Matches
VP1ToolAccessHelper.icc
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
5#include "VP1Base/VP1Msg.h"
6
7template <class toolT>
8inline 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 (VP1Msg::verbose())
73 messageVerbose("Returning "+str(thetool)+" tool pointer.");
74 //We need the cast to void ptr to work around some WEIRD pointer
75 //values introduced by a more straight-forward casting:
76 void* voidptr = static_cast<void*>(thetool);
77 IAlgTool* algptr = static_cast<IAlgTool*>(voidptr);
78 if (VP1Msg::verbose())
79 messageVerbose("getToolPointer(..) Storing ptr = "+str(algptr));
80 m_toolname2pointer[id] = algptr;
81 return thetool;
82 } else {
83 //Not first attempt:
84 if (!it->second) {
85 messageVerbose("getToolPointer(..) Returning null tool pointer since previous retrieval attempts failed)).");
86 return 0;
87 }
88 toolT* pointer = static_cast<toolT*>(static_cast<void*>(it->second));
89 if (!pointer) {
90 //Fixme: Respect silent flag here?
91 message("getToolPointer(..) ERROR: Could not cast IAlgTool pointer "+str(it->second)
92 +" to type "+QString(typeid(toolT*).name())+". Returning 0.");
93 return 0;
94 }
95 if (VP1Msg::verbose())
96 messageVerbose("getToolPointer(..) Retrieved tool pointer ("+str(pointer)+") succesfully");
97 return pointer;
98 }
99}