diff --git a/DDCore/include/DD4hep/Detector.h b/DDCore/include/DD4hep/Detector.h index 14e0d664dbee57e567f33f19020e2805b9977387..6b814f76dd1852509a3bd33ebc6a4ce74fde4f4b 100644 --- a/DDCore/include/DD4hep/Detector.h +++ b/DDCore/include/DD4hep/Detector.h @@ -154,8 +154,8 @@ namespace DD4hep { } /// Access extension element by the type - template <class T> T* extension() const { - return (T*) i_extension(typeid(T)); + template <typename IFACE> IFACE* extension() const { + return (IFACE*) i_extension(typeid(IFACE)); } }; @@ -302,12 +302,10 @@ namespace DD4hep { CallbackSequence::checkTypes(typeid(IFACE), typeid(CONCRETE), dynamic_cast<IFACE*>(c)); return (IFACE*) i_addExtension(dynamic_cast<IFACE*>(c), typeid(IFACE), _copy<CONCRETE>, _delete<IFACE>); } - /// Access extension element by the type - template <class T> T* extension() const { - return (T*) i_extension(typeid(T)); + template <typename IFACE> IFACE* extension() const { + return (IFACE*) i_extension(typeid(IFACE)); } - /// Extend the detector element with an arbitrary callback template <typename Q, typename T> void callAtUpdate(unsigned int type, Q* pointer, diff --git a/DDCore/include/DD4hep/ObjectExtensions.h b/DDCore/include/DD4hep/ObjectExtensions.h index 959bfb0cc1dfb5742e3405336279f6bddd546256..db804a30bde2f5a176dc5831bf958ead697562e7 100644 --- a/DDCore/include/DD4hep/ObjectExtensions.h +++ b/DDCore/include/DD4hep/ObjectExtensions.h @@ -49,7 +49,11 @@ namespace DD4hep { /// Function to be passed as dtor if object should NOT be deleted! static void _noDelete(void*) {} - /// If the object SHOULD be deleted, use DD4hep::deletePtr<TYPE>! + + /// Templated destructor function + template <typename T> static void _delete(void* ptr) { + delete (T*) (ptr); + } public: /// Default constructor diff --git a/DDG4/include/DDG4/Geant4Context.h b/DDG4/include/DDG4/Geant4Context.h index 9139baf49e5645d8c1d7b479db4b8cfeb34cc41e..20fce51dd1589cba2546ade510bcc89a545e8739 100644 --- a/DDG4/include/DDG4/Geant4Context.h +++ b/DDG4/include/DDG4/Geant4Context.h @@ -70,6 +70,8 @@ namespace DD4hep { class Geant4Run : public ObjectExtensions { /// Reference to the original Geant4 run object const G4Run* m_run; + protected: + public: /// Intializing constructor Geant4Run(const G4Run* run); @@ -87,8 +89,8 @@ namespace DD4hep { return ObjectExtensions::addExtension(ptr,info,dtor); } /// Add user extension object. Ownership is transferred! - template <typename T> T* addExtension(T* ptr) { - return (T*)ObjectExtensions::addExtension(ptr,typeid(T),deletePtr<T>); + template <typename T> T* addExtension(T* ptr, bool take_ownership=true) { + return (T*)ObjectExtensions::addExtension(ptr,typeid(T),take_ownership ? _delete<T> : 0); } /// Access to type safe extension object. Exception is thrown if the object is invalid template <typename T> T* extension(bool alert=true) { @@ -132,8 +134,8 @@ namespace DD4hep { return ObjectExtensions::addExtension(ptr,info,dtor); } /// Add user extension object. Ownership is transferred and object deleted at the end of the event. - template <typename T> T* addExtension(T* ptr) { - return (T*)ObjectExtensions::addExtension(ptr,typeid(T),deletePtr<T>); + template <typename T> T* addExtension(T* ptr, bool take_ownership=true) { + return (T*)ObjectExtensions::addExtension(ptr,typeid(T),take_ownership ? _delete<T> : 0); } /// Access to type safe extension object. Exception is thrown if the object is invalid template <typename T> T* extension(bool alert=true) { diff --git a/DDG4/lcio/Geant4Output2LCIO.cpp b/DDG4/lcio/Geant4Output2LCIO.cpp index 3ff84154d55c0cb096ba12f6d8778e7af2643516..51b791c3ee5044b60557e1c677c747680813f352 100644 --- a/DDG4/lcio/Geant4Output2LCIO.cpp +++ b/DDG4/lcio/Geant4Output2LCIO.cpp @@ -82,6 +82,7 @@ namespace DD4hep { #include "DD4hep/InstanceCount.h" #include "DDG4/Geant4HitCollection.h" #include "DDG4/Geant4DataConversion.h" +#include "DDG4/Geant4Context.h" //#include "DDG4/Geant4Output2LCIO.h" #include "G4Event.hh" @@ -158,7 +159,8 @@ void Geant4Output2LCIO::begin(const G4Event* event){ //fg: fixme: should be this call (deleting the pointer in the end) but that does not compile ... // context()->event().addExtension<lcio::LCEventImpl>( e ); - context()->event().addExtension( e , typeid( lcio::LCEventImpl ), 0); + context()->event().addExtension<lcio::LCEventImpl>( e ); + //context()->event().addExtension( e , typeid( lcio::LCEventImpl ), 0); // std::cout << " ########### Geant4Output2LCIO::begin add new LCIO event event context " << std::endl ; }