ATLAS Offline Software
Control/AthenaKernel/AthenaKernel/BaseInfo.h
Go to the documentation of this file.
1 // This file's extension implies that it's C, but it's really -*- C++ -*-.
2 /*
3  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
4 */
184 #ifndef ATHENAKERNEL_BASEINFO_H
185 #define ATHENAKERNEL_BASEINFO_H
186 
187 #include "CxxUtils/checker_macros.h"
188 #include "AthenaKernel/Bases.h"
189 #include "GaudiKernel/ClassID.h"
190 #include <vector>
191 #include <typeinfo>
192 #include <type_traits>
193 
194 
195 //===========================================================================
196 // Macros used to declare base class information.
197 //
198 
211 #define SG_VIRTUAL(T) Virtual<T>
212 
213 
224 #define SG_BASES(D, ...) \
225  namespace SG { \
226  template<> struct Bases<D >{ \
227  using bases = BaseList<__VA_ARGS__>; \
228  }; \
229  template struct RegisterBaseInit<D >; \
230  template struct BaseInit<D >; \
231 } struct sg_dummy // to swallow semicolon
232 
233 
243 #define SG_BASE(D, B) SG_BASES(D, B)
244 
245 
256 #define SG_BASES1(D, B) SG_BASES(D, B)
257 
258 
270 #define SG_BASES2(D, B1, B2) SG_BASES(D, B1, B2)
271 
272 
285 #define SG_BASES3(D, B1, B2, B3) SG_BASES(D, B1, B2, B3)
286 
287 
301 #define SG_BASES4(D, B1, B2, B3, B4) SG_BASES(D, B1, B2, B3, B4)
302 
303 
325 #define SG_ADD_BASE(D, B) \
326  namespace SG { \
327  template struct AddBaseInit<D, B >; \
328 } struct sg_dummy // to swallow semicolon
329 
330 
336 #define SG_ADD_COPY_CONVERSION(D, C) \
337  namespace SG { \
338  template struct AddCopyConversionInit<D, C >; \
339 } struct sg_dummy // to swallow semicolon
340 
341 
342 
343 namespace SG {
344 
345 
363 template <class T>
364 struct BaseType;
365 
366 
367 // Forward declaration.
368 template <class T>
370 
371 
372 struct BaseInfoBaseImpl;
373 
374 
375 //===========================================================================
376 // Copy conversion declarations.
377 //
378 
379 
384 {
385 public:
387  virtual ~CopyConversionBase() {}
388 
390  virtual void* create() const = 0;
391 
393  virtual void destroy (void* p) const = 0;
394 
397  virtual void convertUntyped (const void* src, void* dst) const = 0;
398 };
399 
400 
405 template <class SRC, class DST>
407  : public CopyConversionBase
408 {
409 public:
411  typedef DST target_type;
412 
414  virtual void* create() const { return new DST; }
415 
417  virtual void destroy (void* p) const { delete reinterpret_cast<DST*>(p); }
418 
421  virtual void convertUntyped (const void* src, void* dst) const
422  {
423  convert (*reinterpret_cast<const SRC*>(src),
424  *reinterpret_cast<DST*>(dst));
425  }
426 
430  virtual void convert (const SRC& src, DST& dst) const = 0;
431 };
432 
433 
434 //===========================================================================
435 // Base class declaration.
436 // This factors out the part of the implementation that doesn't
437 // depend on the template parameter @a T.
438 //
439 
440 
441 template <class D, class B>
443 template <class D, class B>
445 
446 
451 {
452 public:
453  template <class D, class B>
454  friend struct RegisterAddBaseInit;
455  template <class D, class B>
457 
458 
462  CLID clid() const;
463 
464 
468  const std::type_info& typeinfo() const;
469 
470 
479  void* cast (void* p, CLID clid) const;
480 
489  void* cast (void* p, const std::type_info& tinfo) const;
490 
491 
501  void* castTo (void* p, CLID clid) const;
502 
512  void* castTo (void* p, const std::type_info& tinfo) const;
513 
514 
516  typedef void* castfn_t (void* p);
517 
518 
519  // gcc 4.3 complains about the code genreflex generates for these.
520  // They're not useful from python anyway, so just suppress them.
521 #ifndef __REFLEX__
522 
530  castfn_t* castfn (CLID clid) const;
531 
532 
541  castfn_t* castfn (const std::type_info& tinfo) const;
542 
551  castfn_t* castfnTo (CLID clid) const;
552 
553 
562  castfn_t* castfnTo (const std::type_info& tinfo) const;
563 #endif
564 
565 
570  const std::vector<CLID>& get_bases () const;
571 
576  std::vector<const std::type_info*> get_ti_bases () const;
577 
584  bool is_base (CLID clid) const;
585 
592  bool is_base (const std::type_info& tinfo) const;
593 
594 
601  bool is_virtual (CLID clid) const;
602 
609  bool is_virtual (const std::type_info& tinfo) const;
610 
611 
618  const CopyConversionBase* copy_conversion (const std::type_info& tinfo) const;
619 
620 
628 
629 
637  void add_copy_conversion (const std::type_info& tinfo,
638  const CopyConversionBase* cnv);
639 
640 
647  std::vector<CLID> get_copy_conversions() const;
648 
649 
650 #ifndef __REFLEX__
651 
661  void add_info (const std::type_info& tinfo,
662  castfn_t* converter,
663  castfn_t* converterTo,
664  bool is_virtual);
665 #endif
666 
667 
674  static const BaseInfoBase* find (CLID clid);
675 
676 
684  static const BaseInfoBase* find (const std::type_info& tinfo);
685 
686 
688  typedef void init_func_t (BaseInfoBase* bib);
689 
690 
691 #ifndef __REFLEX__
692 
697  static void addInit (const std::type_info* tinfo,
698  init_func_t* init_func);
699 #endif
700 
701 
705  void maybeInit();
706 
707 
708 protected:
713  BaseInfoBase (const std::type_info& tinfo);
714 
715 
719  ~BaseInfoBase();
720 
721 
722 private:
730  static BaseInfoBase* find1 (const std::type_info& tinfo);
731 
732 
735 
738 };
739 
740 
741 //===========================================================================
742 // The templated @c BaseInfo class.
743 //
744 
749 template <class T>
750 class BaseInfo
751 {
752 public:
753  template <class D, class B>
755  template <class D, class B>
756  friend struct RegisterAddBaseInit;
757 
758 
767  static void* cast (T* p, CLID clid);
768 
777  static void* cast (T* p, const std::type_info& tinfo);
778 
779 
789  static T* castTo (void* p, CLID clid);
790 
800  static T* castTo (void* p, const std::type_info& tinfo);
801 
802 
812 
813 
822  static BaseInfoBase::castfn_t* castfn (const std::type_info& tinfo);
823 
824 
834 
835 
844  static BaseInfoBase::castfn_t* castfnTo (const std::type_info& tinfo);
845 
846 
851  static const std::vector<CLID>& get_bases ();
852 
853 
858  static std::vector<const std::type_info*> get_ti_bases ();
859 
860 
867  static bool is_base (CLID clid);
868 
875  static bool is_base (const std::type_info& tinfo);
876 
877 
884  static bool is_virtual (CLID clid);
885 
892  static bool is_virtual (const std::type_info& tinfo);
893 
894 
898  static const BaseInfoBase& baseinfo ();
899 
900 
901  static void maybeInit ();
902 
903 
904 private:
908 
910 };
911 
912 
913 } // namespace SG
914 
915 
916 #include "AthenaKernel/BaseInfo.icc"
917 
918 
919 #endif // not ATHENAKERNEL_BASEINFO_H
920 
SG::BaseInfo::is_base
static bool is_base(const std::type_info &tinfo)
Return true if tinfo is the std::type_info of a class that is known to be a base of T.
SG::BaseInfoBase::find1
static BaseInfoBase * find1(const std::type_info &tinfo)
Helper for find.
Definition: BaseInfo.cxx:585
SG::BaseInfo::cast
static void * cast(T *p, const std::type_info &tinfo)
Cast to a base pointer.
SG::BaseInfo::instance
static BaseInfoImpl< T > & instance()
Return a reference to the (singleton) implementation object for this class.
SG::BaseInfo::get_bases
static const std::vector< CLID > & get_bases()
Return the class IDs of all known bases of T (that have class IDs).
SG::CopyConversionBase::create
virtual void * create() const =0
Create an instance of the destination class.
SG
Forward declaration.
Definition: CaloCellPacker_400_500.h:32
WriteCellNoiseToCool.src
src
Definition: WriteCellNoiseToCool.py:513
SG::CopyConversion
Base class for copy conversions, templated on source and destination classes.
Definition: Control/AthenaKernel/AthenaKernel/BaseInfo.h:408
SG::BaseInfo::castfn
static BaseInfoBase::castfn_t * castfn(CLID clid)
Return a function for casting to a base pointer.
SG::BaseInfoBase::~BaseInfoBase
~BaseInfoBase()
Destructor.
Definition: BaseInfo.cxx:550
SG::BaseInfoBase::maybeInit
void maybeInit()
Run initializations for this class, if needed.
Definition: BaseInfo.cxx:697
SG::BaseInfoBase::BaseInfoBase
BaseInfoBase(const BaseInfoBase &)
SG::BaseInfoBase::init_func_t
void init_func_t(BaseInfoBase *bib)
Type for an initialization function.
Definition: Control/AthenaKernel/AthenaKernel/BaseInfo.h:688
SG::BaseInfoBase::get_ti_bases
std::vector< const std::type_info * > get_ti_bases() const
Return the type_info's of all known bases of T.
Definition: BaseInfo.cxx:326
SG::BaseInfoBase::castfnTo
castfn_t * castfnTo(CLID clid) const
Return a function for casting to a derived pointer.
Definition: BaseInfo.cxx:272
SG::BaseInfo::get_ti_bases
static std::vector< const std::type_info * > get_ti_bases()
Return the type_info's of all known bases of T.
SG::BaseInfo::maybeInit
static void maybeInit()
SG::BaseInfoBase::get_bases
const std::vector< CLID > & get_bases() const
Return the class IDs of all known bases of T (that have class IDs).
Definition: BaseInfo.cxx:304
SG::BaseInfoBase::addInit
static void addInit(const std::type_info *tinfo, init_func_t *init_func)
Register an initialization function.
Definition: BaseInfo.cxx:672
SG::BaseInfo::is_virtual
static bool is_virtual(CLID clid)
Return true if clid is the ID of a class that is known to be a virtual base of T.
SG::BaseInfo::ATLAS_THREAD_SAFE
static BaseInfoImpl< T > s_instance ATLAS_THREAD_SAFE
Definition: Control/AthenaKernel/AthenaKernel/BaseInfo.h:909
SG::BaseInfoBase::cast
void * cast(void *p, CLID clid) const
Cast to a base pointer.
Definition: BaseInfo.cxx:166
SG::BaseInfoBase::m_impl
BaseInfoBaseImpl * m_impl
Pointer to internal state.
Definition: Control/AthenaKernel/AthenaKernel/BaseInfo.h:734
SG::BaseInfoBaseImpl
Definition: BaseInfo.cxx:29
SG::BaseInfo::castTo
static T * castTo(void *p, CLID clid)
Cast to a derived pointer.
SG::RegisterAddBaseInit
Definition: Control/AthenaKernel/AthenaKernel/BaseInfo.h:442
SG::CopyConversionBase::destroy
virtual void destroy(void *p) const =0
Destroy an instance of the destination class.
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
SG::CopyConversion::target_type
DST target_type
The destination class.
Definition: Control/AthenaKernel/AthenaKernel/BaseInfo.h:411
SG::CopyConversionBase
Base class for copy conversions.
Definition: Control/AthenaKernel/AthenaKernel/BaseInfo.h:384
SG::CopyConversion::convert
virtual void convert(const SRC &src, DST &dst) const =0
Convert the contents of an instance of the source class SRC to an instance of the destination class D...
SG::CopyConversion::convertUntyped
virtual void convertUntyped(const void *src, void *dst) const
Convert the contents of an instance of the source class SRC to an instance of the destination class D...
Definition: Control/AthenaKernel/AthenaKernel/BaseInfo.h:421
SG::BaseInfo::is_base
static bool is_base(CLID clid)
Return true if clid is the ID of a class that is known to be a base of T.
SG::BaseInfo
Provide an interface for finding inheritance information at run time.
Definition: Control/AthenaKernel/AthenaKernel/BaseInfo.h:751
SG::BaseInfo::castTo
static T * castTo(void *p, const std::type_info &tinfo)
Cast to a derived pointer.
SG::BaseInfoBase::get_copy_conversions
std::vector< CLID > get_copy_conversions() const
Return known copy conversions.
Definition: BaseInfo.cxx:455
SG::BaseInfoBase::copy_conversion
const CopyConversionBase * copy_conversion(const std::type_info &tinfo) const
Search for a copy conversion to tinfo.
Definition: BaseInfo.cxx:405
SG::CopyConversion::create
virtual void * create() const
Create an instance of the destination class.
Definition: Control/AthenaKernel/AthenaKernel/BaseInfo.h:414
SG::BaseInfoImpl
Definition: Control/AthenaKernel/AthenaKernel/BaseInfo.h:369
SG::BaseInfo::is_virtual
static bool is_virtual(const std::type_info &tinfo)
Return true if tinfo is the std::type_info of a class that is known to be a virtual base of T.
SG::BaseInfoBase::castTo
void * castTo(void *p, CLID clid) const
Cast to a derived pointer.
Definition: BaseInfo.cxx:201
CLID
uint32_t CLID
The Class ID type.
Definition: Event/xAOD/xAODCore/xAODCore/ClassID_traits.h:47
SG::BaseInfoBase::castfn_t
void * castfn_t(void *p)
Type of a pointer conversion function.
Definition: Control/AthenaKernel/AthenaKernel/BaseInfo.h:516
SG::BaseInfo::castfnTo
static BaseInfoBase::castfn_t * castfnTo(CLID clid)
Return a function for casting to a derived pointer.
SG::BaseInfoBase::BaseInfoBase
BaseInfoBase(const std::type_info &tinfo)
Constructor.
Definition: BaseInfo.cxx:528
SG::BaseInfo::castfnTo
static BaseInfoBase::castfn_t * castfnTo(const std::type_info &tinfo)
Return a function for casting to a derived pointer.
SG::BaseInfoBase::clid
CLID clid() const
Return the CLID for this class.
Definition: BaseInfo.cxx:141
SG::BaseInfoBase::operator=
BaseInfoBase & operator=(const BaseInfoBase &)
SG::BaseInfoBase::find
static const BaseInfoBase * find(CLID clid)
Find the BaseInfoBase instance for clid.
Definition: BaseInfo.cxx:569
SG::BaseInfo::cast
static void * cast(T *p, CLID clid)
Cast to a base pointer.
BaseInfo.icc
SG::CopyConversionBase::~CopyConversionBase
virtual ~CopyConversionBase()
Destructor.
Definition: Control/AthenaKernel/AthenaKernel/BaseInfo.h:387
SG::BaseInfo::castfn
static BaseInfoBase::castfn_t * castfn(const std::type_info &tinfo)
Return a function for casting to a base pointer.
SG::BaseInfoBase
The non-template portion of the BaseInfo implementation.
Definition: Control/AthenaKernel/AthenaKernel/BaseInfo.h:451
SG::BaseInfoBase::is_virtual
bool is_virtual(CLID clid) const
Return true if clid is the ID of a class that is known to be a virtual base of T.
Definition: BaseInfo.cxx:373
SG::BaseInfo::baseinfo
static const BaseInfoBase & baseinfo()
Return the non-templated BaseInfoBase object for this type.
SG::CopyConversionBase::convertUntyped
virtual void convertUntyped(const void *src, void *dst) const =0
Convert the contents of an instance of the source class SRC to an instance of the destination class D...
SG::BaseInfoBase::typeinfo
const std::type_info & typeinfo() const
Return the std::type_info for this class.
Definition: BaseInfo.cxx:151
checker_macros.h
Define macros for attributes used to control the static checker.
SG::BaseInfoBase::castfn
castfn_t * castfn(CLID clid) const
Return a function for casting to a base pointer.
Definition: BaseInfo.cxx:236
SG::BaseInfoBase::add_info
void add_info(const std::type_info &tinfo, castfn_t *converter, castfn_t *converterTo, bool is_virtual)
Add information about one base class.
Definition: BaseInfo.cxx:479
SG::BaseType
Helper metafunction to get base class types.
Definition: Control/AthenaKernel/AthenaKernel/BaseInfo.h:364
TSU::T
unsigned long long T
Definition: L1TopoDataTypes.h:35
SG::BaseInfoBase::add_copy_conversion
void add_copy_conversion(const std::type_info &tinfo, const CopyConversionBase *cnv)
Add a new copy conversion.
Definition: BaseInfo.cxx:440
Bases.h
Traits class for representing derivation.
SG::CopyConversion::destroy
virtual void destroy(void *p) const
Destroy an instance of the destination class.
Definition: Control/AthenaKernel/AthenaKernel/BaseInfo.h:417
SG::RegisterAddCopyConversionInit
Definition: Control/AthenaKernel/AthenaKernel/BaseInfo.h:444
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