ATLAS Offline Software
Loading...
Searching...
No Matches
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-2025 CERN for the benefit of the ATLAS collaboration
4*/
183
184#ifndef ATHENAKERNEL_BASEINFO_H
185#define ATHENAKERNEL_BASEINFO_H
186
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
343namespace SG {
344
345
363template <class T>
364struct BaseType;
365
366
367// Forward declaration.
368template <class T>
370
371
372struct BaseInfoBaseImpl;
373
374
375//===========================================================================
376// Copy conversion declarations.
377//
378
379
384{
385public:
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
405template <class SRC, class DST>
407 : public CopyConversionBase
408{
409public:
411 typedef DST target_type;
412
414 virtual void* create() const override { return new DST; }
415
417 virtual void destroy (void* p) const override { delete reinterpret_cast<DST*>(p); }
418
421 virtual void convertUntyped (const void* src, void* dst) const override
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
441template <class D, class B>
443template <class D, class B>
445
446
451{
452public:
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__
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__
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__
697 static void addInit (const std::type_info* tinfo,
698 init_func_t* init_func);
699#endif
700
701
705 void maybeInit();
706
707
708protected:
713 BaseInfoBase (const std::type_info& tinfo);
714
715
720
721
722private:
730 static BaseInfoBase* find1 (const std::type_info& tinfo);
731
732
735
738};
739
740
741//===========================================================================
742// The templated @c BaseInfo class.
743//
744
749template <class T>
751{
752public:
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
904private:
908
910};
911
912
913} // namespace SG
914
915
917
918
919#endif // not ATHENAKERNEL_BASEINFO_H
920
Traits class for representing derivation.
uint32_t CLID
The Class ID type.
Define macros for attributes used to control the static checker.
The non-template portion of the BaseInfo implementation.
static BaseInfoBase * find1(const std::type_info &tinfo)
Helper for find.
Definition BaseInfo.cxx:586
const CopyConversionBase * copy_conversion(const std::type_info &tinfo) const
Search for a copy conversion to tinfo.
Definition BaseInfo.cxx:405
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
BaseInfoBase(const BaseInfoBase &)
void add_copy_conversion(const std::type_info &tinfo, const CopyConversionBase *cnv)
Add a new copy conversion.
Definition BaseInfo.cxx:440
void maybeInit()
Run initializations for this class, if needed.
Definition BaseInfo.cxx:698
void init_func_t(BaseInfoBase *bib)
Type for an initialization function.
BaseInfoBase & operator=(const BaseInfoBase &)
~BaseInfoBase()
Destructor.
Definition BaseInfo.cxx:551
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
castfn_t * castfnTo(CLID clid) const
Return a function for casting to a derived pointer.
Definition BaseInfo.cxx:272
void * castTo(void *p, CLID clid) const
Cast to a derived pointer.
Definition BaseInfo.cxx:201
CLID clid() const
Return the CLID for this class.
Definition BaseInfo.cxx:141
static void addInit(const std::type_info *tinfo, init_func_t *init_func)
Register an initialization function.
Definition BaseInfo.cxx:673
static const BaseInfoBase * find(CLID clid)
Find the BaseInfoBase instance for clid.
Definition BaseInfo.cxx:570
const std::type_info & typeinfo() const
Return the std::type_info for this class.
Definition BaseInfo.cxx:151
void * cast(void *p, CLID clid) const
Cast to a base pointer.
Definition BaseInfo.cxx:166
BaseInfoBase(const std::type_info &tinfo)
Constructor.
Definition BaseInfo.cxx:529
castfn_t * castfn(CLID clid) const
Return a function for casting to a base pointer.
Definition BaseInfo.cxx:236
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
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
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
std::vector< CLID > get_copy_conversions() const
Return known copy conversions.
Definition BaseInfo.cxx:455
BaseInfoBaseImpl * m_impl
Pointer to internal state.
void * castfn_t(void *p)
Type of a pointer conversion function.
Provide an interface for finding inheritance information at run time.
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.
static BaseInfoBase::castfn_t * castfnTo(CLID clid)
Return a function for casting to a derived pointer.
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.
static const std::vector< CLID > & get_bases()
Return the class IDs of all known bases of T (that have class IDs).
static BaseInfoBase::castfn_t * castfnTo(const std::type_info &tinfo)
Return a function for casting to a derived pointer.
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.
static std::vector< const std::type_info * > get_ti_bases()
Return the type_info's of all known bases of T.
static void maybeInit()
static BaseInfoBase::castfn_t * castfn(CLID clid)
Return a function for casting to a base pointer.
static BaseInfoBase::castfn_t * castfn(const std::type_info &tinfo)
Return a function for casting to a base pointer.
static void * cast(T *p, const std::type_info &tinfo)
Cast to a base pointer.
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.
static T * castTo(void *p, const std::type_info &tinfo)
Cast to a derived pointer.
static T * castTo(void *p, CLID clid)
Cast to a derived pointer.
static BaseInfoImpl< T > & instance()
Return a reference to the (singleton) implementation object for this class.
static BaseInfoImpl< T > s_instance ATLAS_THREAD_SAFE
static const BaseInfoBase & baseinfo()
Return the non-templated BaseInfoBase object for this type.
static void * cast(T *p, CLID clid)
Cast to a base pointer.
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...
virtual void * create() const =0
Create an instance of the destination class.
virtual void destroy(void *p) const =0
Destroy an instance of the destination class.
Base class for copy conversions, templated on source and destination classes.
virtual void convertUntyped(const void *src, void *dst) const override
Convert the contents of an instance of the source class SRC to an instance of the destination class D...
virtual void * create() const override
Create an instance of the destination class.
virtual void destroy(void *p) const override
Destroy an instance of the destination class.
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...
Forward declaration.