From 51510e3f33737dc99c9550d6f4ca676dcfda25fe Mon Sep 17 00:00:00 2001
From: Markus FRANK <Markus.Frank@cern.ch>
Date: Thu, 9 Apr 2020 11:02:03 +0200
Subject: [PATCH] Support for conditions with non-default ctor

---
 DDCore/include/DD4hep/OpaqueData.h |  9 +++++++++
 DDCore/src/OpaqueData.cpp          | 12 ++++++++++++
 2 files changed, 21 insertions(+)

diff --git a/DDCore/include/DD4hep/OpaqueData.h b/DDCore/include/DD4hep/OpaqueData.h
index 194fb61da..49c3af537 100644
--- a/DDCore/include/DD4hep/OpaqueData.h
+++ b/DDCore/include/DD4hep/OpaqueData.h
@@ -122,6 +122,8 @@ namespace dd4hep {
     void* bind(const BasicGrammar* grammar);
     /// Bind data value in place
     void* bind(void* ptr, size_t len, const BasicGrammar* grammar);
+    /// Bind external data value to the pointer
+    void bindExtern(void* ptr, const BasicGrammar* grammar);
     /// Construct conditions object and bind the data
     template <typename T, typename... Args> T& construct(Args... args);
     /// Bind data value
@@ -132,6 +134,8 @@ namespace dd4hep {
     template <typename T> T& bind(const std::string& value);
     /// Bind data value
     template <typename T> T& bind(void* ptr, size_t len, const std::string& value);
+    /// Bind external data value to the pointer
+    template <typename T> void bindExtern(T* ptr);
   };
 
   /// Generic getter. Specify the exact type, not a polymorph type
@@ -183,5 +187,10 @@ namespace dd4hep {
     }
     return ret;
   }
+  /// Bind external data value to the pointer
+  template <typename T> void OpaqueDataBlock::bindExtern(T* ptr)    {
+    bindExtern(ptr, &BasicGrammar::instance<T>());
+  }
+
 }      /* End namespace dd4hep */
 #endif /* DD4HEP_OPAQUEDATA_H  */
diff --git a/DDCore/src/OpaqueData.cpp b/DDCore/src/OpaqueData.cpp
index 79b4ac291..04f19d6df 100644
--- a/DDCore/src/OpaqueData.cpp
+++ b/DDCore/src/OpaqueData.cpp
@@ -143,6 +143,18 @@ void* OpaqueDataBlock::bind(const BasicGrammar* g)   {
   return 0;
 }
 
+/// Bind external data value to the pointer
+void OpaqueDataBlock::bindExtern(void* ptr, const BasicGrammar* gr)    {
+  if ( grammar != 0 && type != EXTERN_DATA )  {
+    // We cannot ingore secondary requests for data bindings.
+    // This leads to memory leaks in the caller!
+    except("OpaqueData","You may not bind opaque data multiple times!");
+  }
+  pointer = ptr;
+  grammar = gr;
+  type    = EXTERN_DATA;
+}
+
 /// Set data value
 void* OpaqueDataBlock::bind(void* ptr, size_t size, const BasicGrammar* g)   {
   if ( (type&EXTERN_DATA) == EXTERN_DATA )  {
-- 
GitLab