ATLAS Offline Software
TypeConverter.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // $Id$
15 #include "AthenaKernel/BaseInfo.h"
17 #include "GaudiKernel/System.h"
18 #include <sstream>
19 
20 
21 namespace D3PD {
22 
23 
30  : m_srcTypeinfo (0),
31  m_dstTypeinfo (0),
32  m_srcBIB (0),
33  m_dstBIB (0),
34  m_strategy (INVALID)
35 {
36 }
37 
38 
51 StatusCode TypeConverter::init (const std::type_info& src_ti,
52  const std::type_info& dst_ti)
53 {
54  if (tryInit (src_ti, dst_ti).isFailure()) {
55  // Not a valid conversion.
56  REPORT_MESSAGE (MSG::ERROR)
57  << "Incompatible conversion: from " << srcName() << " to " << dstName();
58  return StatusCode::FAILURE;
59  }
60  return StatusCode::SUCCESS;
61 }
62 
63 
82 TypeConverter::init (const std::type_info& src_ti,
83  const std::vector<const std::type_info*>& dst_tis,
84  size_t& which)
85 {
86  size_t sz = dst_tis.size();
87  for (which = 0; which < sz; which++) {
88  if (tryInit (src_ti, *dst_tis[which]).isSuccess())
89  return StatusCode::SUCCESS;
90  }
91 
92  // Produce an error message listing the possible destination types.
93  std::ostringstream dst_names;
94  for (which = 0; which < sz; which++) {
95  if (which > 0)
96  dst_names << " ";
97  dst_names << System::typeinfoName (*dst_tis[which]);
98  }
99 
100  REPORT_MESSAGE (MSG::ERROR)
101  << "Incompatible conversion: from " << srcName() << " to one of ["
102  << dst_names.str() << "]";
103  return StatusCode::FAILURE;
104 }
105 
106 
107 
116 const void* TypeConverter::convertUntyped (const void* p) const
117 {
118  switch (m_strategy) {
119  case IDENTICAL:
120  return p;
121 
122  case SRC_BASE: {
123  // need non-const pointer
124  void* nonconst_p ATLAS_THREAD_SAFE = const_cast<void*> (p);
125  return m_srcBIB->cast (nonconst_p, *m_dstTypeinfo);
126  }
127  case DST_BASE: {
128  // need non-const pointer
129  void* nonconst_p ATLAS_THREAD_SAFE = const_cast<void*> (p);
130  return m_dstBIB->castTo (nonconst_p, *m_srcTypeinfo);
131  }
132  default:
133  std::abort();
134  }
135 }
136 
137 
141 std::string TypeConverter::srcName() const
142 {
143  if (!m_srcTypeinfo) return "";
144  return System::typeinfoName (*m_srcTypeinfo);
145 }
146 
147 
151 std::string TypeConverter::dstName() const
152 {
153  if (!m_dstTypeinfo) return "";
154  return System::typeinfoName (*m_dstTypeinfo);
155 }
156 
157 
172 StatusCode TypeConverter::tryInit (const std::type_info& src_ti,
173  const std::type_info& dst_ti)
174 {
175  // Find the source BaseInfo.
176  if (!m_srcBIB || m_srcBIB->typeinfo() != src_ti) {
177  m_srcTypeinfo = &src_ti;
178  m_srcBIB = SG::BaseInfoBase::find (src_ti);
179  }
180 
181  // Find the destination BaseInfo.
182  m_dstTypeinfo = &dst_ti;
183  m_dstBIB = SG::BaseInfoBase::find (dst_ti);
184 
185  // Test for source and destination types identical.
186  if (m_srcTypeinfo == m_dstTypeinfo) {
188  return StatusCode::SUCCESS;
189  }
190 
191  if (m_srcBIB && m_srcBIB->is_base (*m_dstTypeinfo)) {
192  // Destination is a base of source --- use source BaseInfo.
194  return StatusCode::SUCCESS;
195  }
196 
197  if (m_dstBIB && m_dstBIB->is_base (*m_srcTypeinfo)) {
198  // Source is a base of destination --- use destination BaseInfo.
200  return StatusCode::SUCCESS;
201  }
202 
203  return StatusCode::FAILURE;
204 }
205 
206 
207 } // namespace D3PD
fitman.sz
sz
Definition: fitman.py:527
python.PerfMonSerializer.p
def p
Definition: PerfMonSerializer.py:743
D3PD::TypeConverter::dstName
std::string dstName() const
Return the name of the destination type, or an empty string.
Definition: TypeConverter.cxx:151
D3PD::TypeConverter::SRC_BASE
@ SRC_BASE
Destination is a base of source — use source BaseInfo.
Definition: TypeConverter.h:193
D3PD::TypeConverter::m_strategy
Strategy m_strategy
Strategy to use for the conversion.
Definition: TypeConverter.h:200
TypeConverter.h
Helper to convert pointers.
D3PD::TypeConverter::m_dstTypeinfo
const std::type_info * m_dstTypeinfo
The destination type.
Definition: TypeConverter.h:177
CxxUtils::INVALID
@ INVALID
Definition: CachedValue.h:28
SG::BaseInfoBase::cast
void * cast(void *p, CLID clid) const
Cast to a base pointer.
Definition: BaseInfo.cxx:166
D3PD::TypeConverter::init
StatusCode init(const std::type_info &src_ti, const std::type_info &dst_ti)
Initialize the converter.
Definition: TypeConverter.cxx:51
D3PD::TypeConverter::srcName
std::string srcName() const
Return the name of the source type, or an empty string.
Definition: TypeConverter.cxx:141
D3PD::TypeConverter::convertUntyped
const void * convertUntyped(const void *p) const
Convert pointer.
Definition: TypeConverter.cxx:116
D3PD
Block filler tool for noisy FEB information.
Definition: InnerDetector/InDetMonitoring/InDetGlobalMonitoring/macros/EnhancedPrimaryVertexMonitoring/TrigD3PD/ChainGroup.h:21
D3PD::TypeConverter::m_srcTypeinfo
const std::type_info * m_srcTypeinfo
The source type.
Definition: TypeConverter.h:174
BaseInfo.h
Provide an interface for finding inheritance information at run time.
D3PD::TypeConverter::TypeConverter
TypeConverter()
Default constructor.
Definition: TypeConverter.cxx:29
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
SG::BaseInfoBase::castTo
void * castTo(void *p, CLID clid) const
Cast to a derived pointer.
Definition: BaseInfo.cxx:201
python.Utils.unixtools.which
def which(filename, env=os.environ)
UNIX-style which ---------------------------------------------------------—.
Definition: unixtools.py:39
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.
D3PD::TypeConverter::m_srcBIB
const SG::BaseInfoBase * m_srcBIB
BaseInfo for the source type.
Definition: TypeConverter.h:180
REPORT_MESSAGE
#define REPORT_MESSAGE(LVL)
Report a message.
Definition: Control/AthenaKernel/AthenaKernel/errorcheck.h:365
D3PD::TypeConverter::IDENTICAL
@ IDENTICAL
Source and destination types are identical.
Definition: TypeConverter.h:190
D3PD::TypeConverter::DST_BASE
@ DST_BASE
Source is a base of destination — use destination BaseInfo.
Definition: TypeConverter.h:196
ATLAS_THREAD_SAFE
#define ATLAS_THREAD_SAFE
Definition: checker_macros.h:211
SG::BaseInfoBase::typeinfo
const std::type_info & typeinfo() const
Return the std::type_info for this class.
Definition: BaseInfo.cxx:151
D3PD::TypeConverter::tryInit
StatusCode tryInit(const std::type_info &src_ti, const std::type_info &dst_ti)
Initialize the converter.
Definition: TypeConverter.cxx:172
D3PD::TypeConverter::m_dstBIB
const SG::BaseInfoBase * m_dstBIB
BaseInfo for the destination type.
Definition: TypeConverter.h:183
SG::BaseInfoBase::is_base
bool is_base(CLID clid) const
Return true if clid is the ID of a class that is known to be a base of T.
Definition: BaseInfo.cxx:344