ATLAS Offline Software
Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
D3PD::TypeConverter Class Reference

This is helper for converting between pointers of different types, given dynamically by std::type_info. More...

#include <TypeConverter.h>

Collaboration diagram for D3PD::TypeConverter:

Public Member Functions

 TypeConverter ()
 Default constructor. More...
 
StatusCode init (const std::type_info &src_ti, const std::type_info &dst_ti)
 Initialize the converter. More...
 
StatusCode init (const std::type_info &src_ti, const std::vector< const std::type_info * > &dst_tis, size_t &which)
 Initialize the converter. More...
 
template<typename T >
StatusCode init (const std::type_info &src_ti)
 Alternate form of init, passing the destination type as a template argument. More...
 
const void * convertUntyped (const void *p) const
 Convert pointer. More...
 
template<class T >
const T * convert (const void *p) const
 Convert pointer. More...
 
bool isValid () const
 Test to see if this converter has been properly initialized. More...
 
const std::type_info & srcTypeinfo () const
 Return the configured source type. More...
 
const std::type_info & dstTypeinfo () const
 Return the configured destination type. More...
 
std::string srcName () const
 Return the name of the source type, or an empty string. More...
 
std::string dstName () const
 Return the name of the destination type, or an empty string. More...
 

Private Types

enum  Strategy { INVALID, IDENTICAL, SRC_BASE, DST_BASE }
 

Private Member Functions

StatusCode tryInit (const std::type_info &src_ti, const std::type_info &dst_ti)
 Initialize the converter. More...
 

Private Attributes

const std::type_info * m_srcTypeinfo
 The source type. More...
 
const std::type_info * m_dstTypeinfo
 The destination type. More...
 
const SG::BaseInfoBasem_srcBIB
 BaseInfo for the source type. More...
 
const SG::BaseInfoBasem_dstBIB
 BaseInfo for the destination type. More...
 
Strategy m_strategy
 Strategy to use for the conversion. More...
 

Detailed Description

This is helper for converting between pointers of different types, given dynamically by std::type_info.

The converter must first be initialized by calling init() with the source and destination types. If the conversion is allowable, then this will return SUCCESS, and isValid() will return true. Pointers can them be converted by calling convert() or convertUntyped().

The remaining methods retrieve the data that was passed to init().

Definition at line 44 of file TypeConverter.h.

Member Enumeration Documentation

◆ Strategy

Enumerator
INVALID 

No valid conversion has been configured.

IDENTICAL 

Source and destination types are identical.

SRC_BASE 

Destination is a base of source — use source BaseInfo.

DST_BASE 

Source is a base of destination — use destination BaseInfo.

Definition at line 185 of file TypeConverter.h.

185  {
187  INVALID,
188 
190  IDENTICAL,
191 
193  SRC_BASE,
194 
196  DST_BASE
197  };

Constructor & Destructor Documentation

◆ TypeConverter()

D3PD::TypeConverter::TypeConverter ( )

Default constructor.

This will create the object in an invalid state.

Definition at line 29 of file TypeConverter.cxx.

30  : m_srcTypeinfo (0),
31  m_dstTypeinfo (0),
32  m_srcBIB (0),
33  m_dstBIB (0),
35 {
36 }

Member Function Documentation

◆ convert()

template<class T >
const T* D3PD::TypeConverter::convert ( const void *  p) const

Convert pointer.

Parameters
Thepointer to convert, as a pointer to the source type.
Returns
The converted pointer, as a T*.

T must be the same as the previously configured destination type.

Will return 0 if the converter isn't properly initialized, or if the conversion fails (for a base -> derived conversion).

◆ convertUntyped()

const void * D3PD::TypeConverter::convertUntyped ( const void *  p) const

Convert pointer.

Parameters
Thepointer to convert, as a pointer to the source type.
Returns
The converted pointer, as a pointer to the destination type.

Will return 0 if the converter isn't properly initialized, or if the conversion fails (for a base -> derived conversion).

Definition at line 116 of file TypeConverter.cxx.

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 }

◆ dstName()

std::string D3PD::TypeConverter::dstName ( ) const

Return the name of the destination type, or an empty string.

Definition at line 151 of file TypeConverter.cxx.

152 {
153  if (!m_dstTypeinfo) return "";
154  return System::typeinfoName (*m_dstTypeinfo);
155 }

◆ dstTypeinfo()

const std::type_info& D3PD::TypeConverter::dstTypeinfo ( ) const

Return the configured destination type.

◆ init() [1/3]

template<typename T >
StatusCode D3PD::TypeConverter::init ( const std::type_info &  src_ti)

Alternate form of init, passing the destination type as a template argument.

Parameters
src_tiThe source type for the conversion.

The types should be for the pointer value types. I.e, if we want to convert from T* to U*, pass typeid(T) as src_ti and U as the template argument.

Returns FAILURE if the conversion is not allowable. Either derived -> base or base -> derived conversions are allowed.

◆ init() [2/3]

StatusCode D3PD::TypeConverter::init ( const std::type_info &  src_ti,
const std::type_info &  dst_ti 
)

Initialize the converter.

Parameters
src_tiThe source type for the conversion.
dst_tiThe destination type for the conversion.

The types should be for the pointer value types. I.e, if we want to convert from T* to U*, pass typeid(T) and typeid(U) as src_ti and dst_ti, respectively.

Returns FAILURE if the conversion is not allowable. Either derived -> base or base -> derived conversions are allowed.

Definition at line 51 of file TypeConverter.cxx.

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 }

◆ init() [3/3]

StatusCode D3PD::TypeConverter::init ( const std::type_info &  src_ti,
const std::vector< const std::type_info * > &  dst_tis,
size_t &  which 
)

Initialize the converter.

Parameters
src_tiThe source type for the conversion.
dst_tisList of possible destination types for the conversion.
[out]whichIndex of accepted conversion.

The types should be for the pointer value types. I.e, if we want to convert from T* to U*, pass typeid(T) and typeid(U) as src_ti and dst_ti, respectively.

All possible destination types are tried; the first one that succeeds is accepted, and WHICH is set to the index of that type. Either derived -> base or base -> derived conversions are allowed.

Returns FAILURE if no candidate conversions are allowable.

Definition at line 82 of file TypeConverter.cxx.

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 }

◆ isValid()

bool D3PD::TypeConverter::isValid ( ) const

Test to see if this converter has been properly initialized.

◆ srcName()

std::string D3PD::TypeConverter::srcName ( ) const

Return the name of the source type, or an empty string.

Definition at line 141 of file TypeConverter.cxx.

142 {
143  if (!m_srcTypeinfo) return "";
144  return System::typeinfoName (*m_srcTypeinfo);
145 }

◆ srcTypeinfo()

const std::type_info& D3PD::TypeConverter::srcTypeinfo ( ) const

Return the configured source type.

◆ tryInit()

StatusCode D3PD::TypeConverter::tryInit ( const std::type_info &  src_ti,
const std::type_info &  dst_ti 
)
private

Initialize the converter.

Parameters
src_tiThe source type for the conversion.
dst_tiThe destination type for the conversion.

The types should be for the pointer value types. I.e, if we want to convert from T* to U*, pass typeid(T) and typeid(U) as src_ti and dst_ti, respectively.

Returns FAILURE if the conversion is not allowable. Either derived -> base or base -> derived conversions are allowed.

Helper: this doesn't print error messages.

Definition at line 172 of file TypeConverter.cxx.

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 }

Member Data Documentation

◆ m_dstBIB

const SG::BaseInfoBase* D3PD::TypeConverter::m_dstBIB
private

BaseInfo for the destination type.

Definition at line 183 of file TypeConverter.h.

◆ m_dstTypeinfo

const std::type_info* D3PD::TypeConverter::m_dstTypeinfo
private

The destination type.

Definition at line 177 of file TypeConverter.h.

◆ m_srcBIB

const SG::BaseInfoBase* D3PD::TypeConverter::m_srcBIB
private

BaseInfo for the source type.

Definition at line 180 of file TypeConverter.h.

◆ m_srcTypeinfo

const std::type_info* D3PD::TypeConverter::m_srcTypeinfo
private

The source type.

Definition at line 174 of file TypeConverter.h.

◆ m_strategy

Strategy D3PD::TypeConverter::m_strategy
private

Strategy to use for the conversion.

Definition at line 200 of file TypeConverter.h.


The documentation for this class was generated from the following files:
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
D3PD::TypeConverter::m_dstTypeinfo
const std::type_info * m_dstTypeinfo
The destination type.
Definition: TypeConverter.h:177
SG::BaseInfoBase::cast
void * cast(void *p, CLID clid) const
Cast to a base pointer.
Definition: BaseInfo.cxx:166
D3PD::TypeConverter::srcName
std::string srcName() const
Return the name of the source type, or an empty string.
Definition: TypeConverter.cxx:141
D3PD::TypeConverter::m_srcTypeinfo
const std::type_info * m_srcTypeinfo
The source type.
Definition: TypeConverter.h:174
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
D3PD::TypeConverter::INVALID
@ INVALID
No valid conversion has been configured.
Definition: TypeConverter.h:187
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