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-2026 CERN for the benefit of the ATLAS collaboration
4*/
190
191#ifndef ATHENAKERNEL_BASEINFO_H
192#define ATHENAKERNEL_BASEINFO_H
193
195#include "AthenaKernel/Bases.h"
196#include "GaudiKernel/ClassID.h"
197#include <vector>
198#include <typeinfo>
199#include <type_traits>
200
201
202//===========================================================================
203// Macros used to declare base class information.
204//
205
218#define SG_VIRTUAL(T) Virtual<T>
219
220
231#define SG_BASES(D, ...) \
232 namespace SG { \
233 template<> struct Bases<D >{ \
234 using bases = BaseList<__VA_ARGS__>; \
235 }; \
236 template struct RegisterBaseInit<D >; \
237 template struct BaseInit<D >; \
238} struct sg_dummy // to swallow semicolon
239
240
250#define SG_BASE(D, B) SG_BASES(D, B)
251
252
263#define SG_BASES1(D, B) SG_BASES(D, B)
264
265
277#define SG_BASES2(D, B1, B2) SG_BASES(D, B1, B2)
278
279
292#define SG_BASES3(D, B1, B2, B3) SG_BASES(D, B1, B2, B3)
293
294
308#define SG_BASES4(D, B1, B2, B3, B4) SG_BASES(D, B1, B2, B3, B4)
309
310
332#define SG_ADD_BASE(D, B) \
333 namespace SG { \
334 template struct AddBaseInit<D, B >; \
335} struct sg_dummy // to swallow semicolon
336
337
343#define SG_ADD_COPY_CONVERSION(D, C) \
344 namespace SG { \
345 template struct AddCopyConversionInit<D, C >; \
346} struct sg_dummy // to swallow semicolon
347
348
349
350namespace SG {
351
352
370template <class T>
371struct BaseType;
372
373
374// Forward declaration.
375template <class T>
377
378
379struct BaseInfoBaseImpl;
380
381
382//===========================================================================
383// Copy conversion declarations.
384//
385
386
391{
392public:
395
397 virtual void* create() const = 0;
398
400 virtual void destroy (void* p) const = 0;
401
404 virtual void convertUntyped (const void* src, void* dst) const = 0;
405};
406
407
412template <class SRC, class DST>
414 : public CopyConversionBase
415{
416public:
418 typedef DST target_type;
419
421 virtual void* create() const override { return new DST; }
422
424 virtual void destroy (void* p) const override { delete reinterpret_cast<DST*>(p); }
425
428 virtual void convertUntyped (const void* src, void* dst) const override
429 {
430 convert (*reinterpret_cast<const SRC*>(src),
431 *reinterpret_cast<DST*>(dst));
432 }
433
437 virtual void convert (const SRC& src, DST& dst) const = 0;
438};
439
440
441//===========================================================================
442// Base class declaration.
443// This factors out the part of the implementation that doesn't
444// depend on the template parameter @a T.
445//
446
447
448template <class D, class B>
450template <class D, class B>
452
453
458{
459public:
460 template <class D, class B>
461 friend struct RegisterAddBaseInit;
462 template <class D, class B>
464
465
466 using destroy_fn = void (void*);
467
468
472 CLID clid() const;
473
474
478 const std::type_info& typeinfo() const;
479
480
489 void* cast (void* p, CLID clid) const;
490
499 void* cast (void* p, const std::type_info& tinfo) const;
500
501
511 void* castTo (void* p, CLID clid) const;
512
522 void* castTo (void* p, const std::type_info& tinfo) const;
523
524
526 typedef void* castfn_t (void* p);
527
528
537 castfn_t* castfn (CLID clid) const;
538
539
548 castfn_t* castfn (const std::type_info& tinfo) const;
549
558 castfn_t* castfnTo (CLID clid) const;
559
560
569 castfn_t* castfnTo (const std::type_info& tinfo) const;
570
571
576 const std::vector<CLID>& get_bases () const;
577
582 std::vector<const std::type_info*> get_ti_bases () const;
583
590 bool is_base (CLID clid) const;
591
598 bool is_base (const std::type_info& tinfo) const;
599
600
607 bool is_virtual (CLID clid) const;
608
615 bool is_virtual (const std::type_info& tinfo) const;
616
617
624 const CopyConversionBase* copy_conversion (const std::type_info& tinfo) const;
625
626
634
635
640 void destroy (void* p) const;
641
642
650 void add_copy_conversion (const std::type_info& tinfo,
651 const CopyConversionBase* cnv);
652
653
660 std::vector<CLID> get_copy_conversions() const;
661
662
663#ifndef __REFLEX__
674 void add_info (const std::type_info& tinfo,
675 castfn_t* converter,
676 castfn_t* converterTo,
677 bool is_virtual);
678#endif
679
680
687 static const BaseInfoBase* find (CLID clid);
688
689
697 static const BaseInfoBase* find (const std::type_info& tinfo);
698
699
701 typedef void init_func_t (BaseInfoBase* bib);
702
703
704#ifndef __REFLEX__
710 static void addInit (const std::type_info* tinfo,
711 init_func_t* init_func);
712#endif
713
714
718 void maybeInit();
719
720
721protected:
727 BaseInfoBase (const std::type_info& tinfo, destroy_fn* destroy);
728
729
734
735
736private:
744 static BaseInfoBase* find1 (const std::type_info& tinfo);
745
746
749
752};
753
754
755//===========================================================================
756// The templated @c BaseInfo class.
757//
758
763template <class T>
765{
766public:
767 template <class D, class B>
769 template <class D, class B>
770 friend struct RegisterAddBaseInit;
771
772
781 static void* cast (T* p, CLID clid);
782
791 static void* cast (T* p, const std::type_info& tinfo);
792
793
803 static T* castTo (void* p, CLID clid);
804
814 static T* castTo (void* p, const std::type_info& tinfo);
815
816
826
827
836 static BaseInfoBase::castfn_t* castfn (const std::type_info& tinfo);
837
838
848
849
858 static BaseInfoBase::castfn_t* castfnTo (const std::type_info& tinfo);
859
860
865 static const std::vector<CLID>& get_bases ();
866
867
872 static std::vector<const std::type_info*> get_ti_bases ();
873
874
881 static bool is_base (CLID clid);
882
889 static bool is_base (const std::type_info& tinfo);
890
891
898 static bool is_virtual (CLID clid);
899
906 static bool is_virtual (const std::type_info& tinfo);
907
908
913 static void destroy (T* p);
914
915
919 static const BaseInfoBase& baseinfo ();
920
921
922 static void maybeInit ();
923
924
925private:
929
931};
932
933
934} // namespace SG
935
936
938
939
940#endif // not ATHENAKERNEL_BASEINFO_H
941
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:602
BaseInfoBase(const std::type_info &tinfo, destroy_fn *destroy)
Constructor.
Definition BaseInfo.cxx:543
const CopyConversionBase * copy_conversion(const std::type_info &tinfo) const
Search for a copy conversion to tinfo.
Definition BaseInfo.cxx:407
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:493
BaseInfoBase(const BaseInfoBase &)
void add_copy_conversion(const std::type_info &tinfo, const CopyConversionBase *cnv)
Add a new copy conversion.
Definition BaseInfo.cxx:454
void maybeInit()
Run initializations for this class, if needed.
Definition BaseInfo.cxx:714
void init_func_t(BaseInfoBase *bib)
Type for an initialization function.
BaseInfoBase & operator=(const BaseInfoBase &)
~BaseInfoBase()
Destructor.
Definition BaseInfo.cxx:567
std::vector< const std::type_info * > get_ti_bases() const
Return the type_info's of all known bases of T.
Definition BaseInfo.cxx:328
castfn_t * castfnTo(CLID clid) const
Return a function for casting to a derived pointer.
Definition BaseInfo.cxx:274
void * castTo(void *p, CLID clid) const
Cast to a derived pointer.
Definition BaseInfo.cxx:203
CLID clid() const
Return the CLID for this class.
Definition BaseInfo.cxx:143
static void addInit(const std::type_info *tinfo, init_func_t *init_func)
Register an initialization function.
Definition BaseInfo.cxx:689
void destroy(void *p) const
Delete an instance of the described type.
Definition BaseInfo.cxx:438
static const BaseInfoBase * find(CLID clid)
Find the BaseInfoBase instance for clid.
Definition BaseInfo.cxx:586
const std::type_info & typeinfo() const
Return the std::type_info for this class.
Definition BaseInfo.cxx:153
void * cast(void *p, CLID clid) const
Cast to a base pointer.
Definition BaseInfo.cxx:168
castfn_t * castfn(CLID clid) const
Return a function for casting to a base pointer.
Definition BaseInfo.cxx:238
const std::vector< CLID > & get_bases() const
Return the class IDs of all known bases of T (that have class IDs).
Definition BaseInfo.cxx:306
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:346
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:375
std::vector< CLID > get_copy_conversions() const
Return known copy conversions.
Definition BaseInfo.cxx:469
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 void destroy(T *p)
Delete an instance of the described type.
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.
Helper metafunction to get base class types.