2 Copyright (C) 2002-2018 CERN for the benefit of the ATLAS collaboration
5 #include "VP1Base/VP1Msg.h"
8 inline toolT * VP1ToolAccessHelper::getToolPointer( const QString& tooltypeandname, bool silent, bool createIfNotExists )
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());
15 if (VP1Msg::verbose())
16 messageVerbose("getToolPointer(..) called with tool type/name = "+tooltypeandname
17 +", with return typeid = "+typeidstr);
19 messageVerbose("getToolPointer(..) WARNING does not have toolSvc pointer. Returning NULL.");
22 if (tooltypeandname.isEmpty()||!tooltypeandname.contains('/')) {
23 messageDebug("getToolPointer(..) WARNING requested tool name has incorrect format ("+tooltypeandname+")!");
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.
32 //Fixme/todo: Should we check that tool exists before any attempts
33 //at retrieval are done??
36 ToolHandle<toolT> toolhandle( tooltypeandname.toStdString(),
37 0/*public tools only*/,
39 bool exception = true;
40 bool retrieveok = false;
42 retrieveok = !toolhandle.retrieve().isFailure();
44 } catch (const std::runtime_error& e) {
49 messageVerbose("ToolHandle<>::retrieve() throws exception.");
51 messageDebug("ToolHandle<>::retrieve() throws exception.");
52 m_toolname2pointer[id] = 0;
58 messageVerbose("getToolPointer(..) ERROR: Failed to retrieve tool: "+tooltypeandname);
60 message("getToolPointer(..) ERROR: Failed to retrieve tool: "+tooltypeandname);
61 m_toolname2pointer[id] = 0;
66 toolT * thetool = &(*toolhandle);
68 message("getToolPointer(..) ERROR: Tool retrieve claimed to be succesful, but pointer is NULL!");
72 if (!isValidInterface(thetool)) {
73 message("getToolPointer(..) ERROR: Tool retrieved is not a valid interface!");
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;
89 messageVerbose("getToolPointer(..) Returning null tool pointer since previous retrieval attempts failed)).");
92 toolT* pointer = static_cast<toolT*>(static_cast<void*>(it->second));
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.");
99 if (VP1Msg::verbose())
100 messageVerbose("getToolPointer(..) Retrieved tool pointer ("+str(pointer)+") succesfully");