From ade761c1aa52d3073959a494683c6379ebd7339a Mon Sep 17 00:00:00 2001
From: Markus Frank <Markus.Frank@cern.ch>
Date: Wed, 19 Oct 2016 10:34:49 +0200
Subject: [PATCH] Convert segmentations to DD4hep handles

---
 DDCore/include/DD4hep/Segmentations.h | 14 +++++++++++---
 DDCore/src/Segmentations.cpp          | 10 +++++-----
 2 files changed, 16 insertions(+), 8 deletions(-)

diff --git a/DDCore/include/DD4hep/Segmentations.h b/DDCore/include/DD4hep/Segmentations.h
index 9a98c85cf..0ae355c38 100644
--- a/DDCore/include/DD4hep/Segmentations.h
+++ b/DDCore/include/DD4hep/Segmentations.h
@@ -110,16 +110,24 @@ namespace DD4hep {
       /// Standard constructor
       SegmentationImplementation(DDSegmentation::BitField64* decoder);
       /// Default destructor
-      virtual ~SegmentationImplementation() {}
+      virtual ~SegmentationImplementation();
     };
 
+    /// Standard constructor
     template <typename IMP> inline
-    SegmentationImplementation<IMP>::SegmentationImplementation(DDSegmentation::BitField64* decoder)
-      :  SegmentationObject(0), implementation(decoder)
+    SegmentationImplementation<IMP>::SegmentationImplementation(DDSegmentation::BitField64* decode)
+      :  SegmentationObject(0), implementation(decode)
     {
       this->segmentation = &implementation;
     }
     
+    /// Default destructor
+    template <typename IMP> inline
+    SegmentationImplementation<IMP>::~SegmentationImplementation()
+    {
+      this->segmentation = 0; // Prevent deletion! We have the object aggregated!
+    }
+    
     /// Handle class supporting generic Segmentation of sensitive detectors
     /**
      *
diff --git a/DDCore/src/Segmentations.cpp b/DDCore/src/Segmentations.cpp
index 4578eac6a..d6f9aa67d 100644
--- a/DDCore/src/Segmentations.cpp
+++ b/DDCore/src/Segmentations.cpp
@@ -122,24 +122,24 @@ void SegmentationObject::neighbours(const CellID& cell, std::set<CellID>& nb) co
 }
 
 /// Constructor to used when creating a new object
-Segmentation::Segmentation(const string& typ, const string& nam, BitField64* decoder) : Handle<Implementation>()
+Segmentation::Segmentation(const string& typ, const string& nam, BitField64* dec) : Handle<Implementation>()
 {
   string type = "segmentation_constructor__"+typ;
-  SegmentationObject* obj = PluginService::Create<SegmentationObject*>(type, decoder);
+  SegmentationObject* obj = PluginService::Create<SegmentationObject*>(type, dec);
   if ( obj != 0 )  {
     assign(obj, nam, typ);
     if ( !nam.empty() ) obj->setName(nam);
+    return;
   }
 #if 0
   BaseSegmentation* s = DDSegmentation::SegmentationFactory::instance()->create(typ);
   if (s != 0) {
     assign(new Object(s), nam, "");
     if ( !nam.empty() ) s->setName(nam);
+    return;
   }
 #endif
-  else {
-    throw runtime_error("FAILED to create segmentation: " + typ + ". Missing factory method for: " + typ + "!");
-  }
+  except("Segmentation","FAILED to create segmentation: %s. [Missing factory]",typ.c_str());
 }
 
 /// Accessor: Segmentation type
-- 
GitLab