ATLAS Offline Software
TypeNameConversions.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // $Id$
16 #include "AthenaKernel/BaseInfo.h"
18 #include "GaudiKernel/IClassIDSvc.h"
19 #include "GaudiKernel/System.h"
20 #include "TClass.h"
21 
22 
23 namespace D3PD {
24 
25 
33 StatusCode nameToTypeinfo (const std::string& name,
34  std::type_info const* &ti,
35  const std::string& context,
37  /*= ServiceHandle<IClassIDSvc>("ClassIDSvc")*/)
38 {
39  ti = 0;
40 
41  // See if root knows the type.
42  const TClass* cls = TClass::GetClass (name.c_str());
43  if (cls) {
44  ti = cls->GetTypeInfo();
45  if (ti) return StatusCode::SUCCESS;
46  }
47 
48  // Try to look it up using BaseInfo. First need to convert to a CLID.
49  CLID clid;
50  if (nameToCLID (name, clid, context, clidsvc).isSuccess()) {
51  const SG::BaseInfoBase* bib = SG::BaseInfoBase::find (clid);
52  if (bib) {
53  ti = &bib->typeinfo();
54  return StatusCode::SUCCESS;
55  }
56  }
57 
58  REPORT_MESSAGE_WITH_CONTEXT (MSG::ERROR, context)
59  << "Can't convert type name " << name << " to type_info.";
60 
61  return StatusCode::FAILURE;
62 }
63 
64 
72 StatusCode nameToCLID (const std::string& name,
73  CLID& clid,
74  const std::string& context,
76  /*= ServiceHandle<IClassIDSvc>("ClassIDSvc")*/)
77 {
78  clid = CLID_NULL;
79  CHECK_WITH_CONTEXT( clidsvc.retrieve(), context );
80 
81  if (clidsvc->getIDOfTypeName (name, clid).isSuccess())
82  return StatusCode::SUCCESS;
83  if (clidsvc->getIDOfTypeInfoName (name, clid).isSuccess())
84  return StatusCode::SUCCESS;
85 
86  REPORT_MESSAGE_WITH_CONTEXT (MSG::ERROR, context)
87  << "Can't find CLID for type name " << name;
88  return StatusCode::FAILURE;
89 }
90 
91 
100  const std::string& name,
101  std::type_info const* &ti,
102  const std::string& context)
103 {
104  ti = 0;
105  const SG::BaseInfoBase* bib = SG::BaseInfoBase::find (clid);
106  if (bib) {
107  ti = &bib->typeinfo();
108  return StatusCode::SUCCESS;
109  }
110 
111  // Hmm.. no BaseInfo. Maybe Root knows it.
112  const TClass* cls = TClass::GetClass (name.c_str());
113  if (cls){
114  ti = cls->GetTypeInfo();
115  if (ti)
116  return StatusCode::SUCCESS;
117  }
118 
119  REPORT_MESSAGE_WITH_CONTEXT (MSG::ERROR, context)
120  << "Can't find typeinfo for CLID "
121  << clid << " (type " << name << ").";
122  return StatusCode::FAILURE;
123 }
124 
125 
130 std::string typeinfoToName (const std::type_info& ti)
131 {
132  // Handle fundamental types.
133  if (ti == typeid(float))
134  return "float";
135  else if (ti == typeid(double))
136  return "double";
137  else if (ti == typeid(int))
138  return "int";
139  else if (ti == typeid(bool))
140  return "bool";
141  else if (ti == typeid(char))
142  return "char";
143  else if (ti == typeid(signed char))
144  return "signed char";
145  else if (ti == typeid(unsigned char))
146  return "unsigned char";
147  else if (ti == typeid(short))
148  return "short";
149  else if (ti == typeid(unsigned short))
150  return "unsigned short";
151  else if (ti == typeid(unsigned int))
152  return "unsigned int";
153  else if (ti == typeid(long))
154  return "long";
155  else if (ti == typeid(unsigned long))
156  return "unsigned long";
157  else if (ti == typeid(long long))
158  return "long long";
159  else if (ti == typeid(unsigned long long))
160  return "unsigned long long";
161  else if (ti == typeid(long double))
162  return "long double";
163  else if (ti == typeid(wchar_t))
164  return "wchar_t";
165 
166  // Otherwise try to look it up with root.
167  std::string eltname;
168  TClass* eltcls = TClass::GetClass (ti);
169  if (eltcls) {
170  eltname = eltcls->GetName();
171  }
172  else {
173  std::string eltname = System::typeinfoName (ti);
174  eltcls = TClass::GetClass (eltname.c_str());
175  if (eltcls)
176  eltname = eltcls->GetName();
177  }
178  return eltname;
179 }
180 
181 
182 } // namespace D3PD
CHECK_WITH_CONTEXT
#define CHECK_WITH_CONTEXT(...)
Evaluate an expression and check for errors, with an explicitly specified context name.
Definition: Control/AthenaKernel/AthenaKernel/errorcheck.h:396
CaloClusterListBadChannel.cls
cls
Definition: CaloClusterListBadChannel.py:8
D3PD::nameToTypeinfo
StatusCode nameToTypeinfo(const std::string &name, std::type_info const *&ti, const std::string &context, ServiceHandle< IClassIDSvc > clidsvc=ServiceHandle< IClassIDSvc >("ClassIDSvc", "TypeNameConversions"))
Convert from a class name to a type_info.
Definition: TypeNameConversions.cxx:33
AthTPCnvSvc_test.clidsvc
clidsvc
Definition: AthTPCnvSvc_test.py:10
D3PD
Block filler tool for noisy FEB information.
Definition: InnerDetector/InDetMonitoring/InDetGlobalMonitoring/macros/EnhancedPrimaryVertexMonitoring/TrigD3PD/ChainGroup.h:21
BaseInfo.h
Provide an interface for finding inheritance information at run time.
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
D3PD::nameToCLID
StatusCode nameToCLID(const std::string &name, CLID &clid, const std::string &context, ServiceHandle< IClassIDSvc > clidsvc=ServiceHandle< IClassIDSvc >("ClassIDSvc", "TypeNameConversions"))
Convert from a class name to a CLID.
Definition: TypeNameConversions.cxx:72
D3PD::typeinfoToName
std::string typeinfoToName(const std::type_info &ti)
Convert from a type_info to a name.
Definition: TypeNameConversions.cxx:130
D3PD::clidToTypeinfo
StatusCode clidToTypeinfo(CLID clid, const std::string &nmae, std::type_info const *&ti, const std::string &context)
Convert from a CLID to a type_info; we also already know the name.
Definition: TypeNameConversions.cxx:99
CLID
uint32_t CLID
The Class ID type.
Definition: Event/xAOD/xAODCore/xAODCore/ClassID_traits.h:47
REPORT_MESSAGE_WITH_CONTEXT
#define REPORT_MESSAGE_WITH_CONTEXT(LVL, CONTEXT_NAME)
Report a message, with an explicitly specified context name.
Definition: Control/AthenaKernel/AthenaKernel/errorcheck.h:345
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
SG::BaseInfoBase::find
static const BaseInfoBase * find(CLID clid)
Find the BaseInfoBase instance for clid.
Definition: BaseInfo.cxx:569
errorcheck.h
Helpers for checking error return status codes and reporting errors.
SG::BaseInfoBase
The non-template portion of the BaseInfo implementation.
Definition: Control/AthenaKernel/AthenaKernel/BaseInfo.h:451
SG::BaseInfoBase::typeinfo
const std::type_info & typeinfo() const
Return the std::type_info for this class.
Definition: BaseInfo.cxx:151
TypeNameConversions.h
Utility functions for converting between type names, type_info, and CLIDs.
ServiceHandle< IClassIDSvc >