diff --git a/DDCond/include/DDCond/ConditionsContent.h b/DDCond/include/DDCond/ConditionsContent.h index 28e8c5c74be6a811088b76017ff9320e64dac1c2..0cba04668544999d1851b3523a219dcbb358d29e 100644 --- a/DDCond/include/DDCond/ConditionsContent.h +++ b/DDCond/include/DDCond/ConditionsContent.h @@ -154,7 +154,7 @@ namespace dd4hep { addDependency(ConditionDependency* dep); /// Add a new conditions dependency (Built internally from arguments) std::pair<Condition::key_type, ConditionDependency*> - addDependency(DetElement de, Condition::itemkey_type item, ConditionUpdateCall* callback); + addDependency(DetElement de, Condition::itemkey_type item, std::shared_ptr<ConditionUpdateCall> callback); }; template <> inline diff --git a/DDCond/src/ConditionsContent.cpp b/DDCond/src/ConditionsContent.cpp index 4ba7ed38393df98cbdbdd9af634f7f392ee459ca..27c9e6df3c17c065c55b1a8d0aa5f38023763a7f 100644 --- a/DDCond/src/ConditionsContent.cpp +++ b/DDCond/src/ConditionsContent.cpp @@ -139,7 +139,7 @@ ConditionsContent::addDependency(ConditionDependency* dep) std::pair<Condition::key_type, ConditionDependency*> ConditionsContent::addDependency(DetElement de, Condition::itemkey_type item, - ConditionUpdateCall* callback) + std::shared_ptr<ConditionUpdateCall> callback) { ConditionDependency* dep = new ConditionDependency(de, item, callback); return addDependency(dep); diff --git a/DDCond/src/plugins/ConditionsUserPool.cpp b/DDCond/src/plugins/ConditionsUserPool.cpp index 842289d2d54cd9a5c2086e1e4067996b7b689949..e38d5b330fd0bee5b0ef54862e7f41ee412bb0ad 100644 --- a/DDCond/src/plugins/ConditionsUserPool.cpp +++ b/DDCond/src/plugins/ConditionsUserPool.cpp @@ -491,11 +491,11 @@ namespace { typedef pair<const Condition::key_type,ConditionsLoadInfo* > Info; typedef pair<const Condition::key_type,Condition> Cond2; - bool operator()(const Dep& a,const Cond& b) const { return a.first < b.first; } - bool operator()(const Cond& a,const Dep& b) const { return a.first < b.first; } + bool operator()(const Dep& a,const Cond& b) const { return a.first < b.first; } + bool operator()(const Cond& a,const Dep& b) const { return a.first < b.first; } - bool operator()(const Info& a,const Cond& b) const { return a.first < b.first; } - bool operator()(const Cond& a,const Info& b) const { return a.first < b.first; } + bool operator()(const Info& a,const Cond& b) const { return a.first < b.first; } + bool operator()(const Cond& a,const Info& b) const { return a.first < b.first; } bool operator()(const Info& a,const Cond2& b) const { return a.first < b.first; } bool operator()(const Cond2& a,const Info& b) const { return a.first < b.first; } diff --git a/DDCore/include/DD4hep/ConditionDerived.h b/DDCore/include/DD4hep/ConditionDerived.h index 4a06f670283df2da48c2a7367d589232f6b045b0..34d639633a555ba852a8a33a83ba354c74c65d5a 100644 --- a/DDCore/include/DD4hep/ConditionDerived.h +++ b/DDCore/include/DD4hep/ConditionDerived.h @@ -18,6 +18,9 @@ #include "DD4hep/Conditions.h" #include "DD4hep/detail/ConditionsInterna.h" +// C/C++ include files +#include <memory> + /// Namespace for the AIDA detector description toolkit namespace dd4hep { @@ -275,8 +278,6 @@ namespace dd4hep { */ class ConditionUpdateCall { protected: - /// Reference count - int m_refCount; /// Standard destructor ConditionUpdateCall(); /// No copy constructor @@ -287,16 +288,6 @@ namespace dd4hep { ConditionUpdateCall& operator=(const ConditionUpdateCall& copy) = delete; public: - /// Add use count to the object - ConditionUpdateCall* addRef() { - ++m_refCount; - return this; - } - /// Release object. May not be used any longer - void release() { - if ( --m_refCount <= 0 ) - delete this; - } /// Interface to client callback in order to update/create the condition virtual Condition operator()(const ConditionKey& target, ConditionUpdateContext& ctxt) = 0; @@ -308,25 +299,33 @@ namespace dd4hep { /** * Used by clients to update a condition. * + * Note: + * We later in DDCond have to do many many set intersections of references to these + * objects. For this reason we do not want to use std::shared_ptr<ConditionDependency>. + * For this reason we implement here a simple ref'counting mechanism, which later + * allows us to use bare pointers. + * * \author M.Frank * \version 1.0 * \ingroup DD4HEP_CONDITIONS */ class ConditionDependency { + friend std::default_delete<ConditionDependency>; protected: /// Reference count - int m_refCount; + int m_refCount {0}; + public: #ifdef DD4HEP_CONDITIONS_DEBUG /// Reference to the target's detector element - DetElement detector; + DetElement detector; #endif /// Key to the condition to be updated - ConditionKey target; + ConditionKey target {0}; /// Dependency keys this condition depends on - std::vector<ConditionKey> dependencies; + std::vector<ConditionKey> dependencies; /// Reference to the update callback. No auto pointer. callback may be shared - ConditionUpdateCall* callback; + std::shared_ptr<ConditionUpdateCall> callback; protected: /// Copy constructor @@ -338,11 +337,11 @@ namespace dd4hep { public: /// Initializing constructor used by builder - ConditionDependency(Condition::key_type key, ConditionUpdateCall* call); + ConditionDependency(Condition::key_type key, std::shared_ptr<ConditionUpdateCall> call); /// Initializing constructor used by builder - ConditionDependency(DetElement de, const std::string& item, ConditionUpdateCall* call); + ConditionDependency(DetElement de, const std::string& item, std::shared_ptr<ConditionUpdateCall> call); /// Initializing constructor used by builder - ConditionDependency(DetElement de, Condition::itemkey_type item_key, ConditionUpdateCall* call); + ConditionDependency(DetElement de, Condition::itemkey_type item_key, std::shared_ptr<ConditionUpdateCall> call); /// Default constructor ConditionDependency(); /// Access the dependency key @@ -366,16 +365,16 @@ namespace dd4hep { class DependencyBuilder { protected: /// The created dependency - ConditionDependency* m_dependency; + std::unique_ptr<ConditionDependency> m_dependency; public: /// Initializing constructor - DependencyBuilder(DetElement de, Condition::itemkey_type item_key, ConditionUpdateCall* call); + DependencyBuilder(DetElement de, Condition::itemkey_type item_key, std::shared_ptr<ConditionUpdateCall> call); /// Initializing constructor - DependencyBuilder(DetElement de, const std::string& item, ConditionUpdateCall* call); + DependencyBuilder(DetElement de, const std::string& item, std::shared_ptr<ConditionUpdateCall> call); /// Default destructor virtual ~DependencyBuilder(); /// Access underlying object directly - ConditionDependency* operator->() { return m_dependency; } + ConditionDependency* operator->() { return m_dependency.operator->(); } /// Add a new dependency void add(const ConditionKey& source_key); /// Release the created dependency and take ownership. diff --git a/DDCore/src/ConditionDerived.cpp b/DDCore/src/ConditionDerived.cpp index cb7aab80c2a262fc81ff06c061d1cec12edcc745..75c522c549267f9a5f854ac3bc6b375155ba0adb 100644 --- a/DDCore/src/ConditionDerived.cpp +++ b/DDCore/src/ConditionDerived.cpp @@ -102,7 +102,7 @@ size_t ConditionUpdateContext::registerMany(const IOV& iov_val, const std::vecto } /// Standard destructor -ConditionUpdateCall::ConditionUpdateCall() : m_refCount(1) { +ConditionUpdateCall::ConditionUpdateCall() { InstanceCount::increment(this); } @@ -133,9 +133,8 @@ void ConditionUpdateContext::accessFailure(const ConditionKey& key_value) const /// Initializing constructor ConditionDependency::ConditionDependency(Condition::key_type key, - ConditionUpdateCall* call) - : m_refCount(0), - target(key), callback(call) + std::shared_ptr<ConditionUpdateCall> call) + : m_refCount(0), target(key), callback(std::move(call)) { InstanceCount::increment(this); } @@ -143,12 +142,12 @@ ConditionDependency::ConditionDependency(Condition::key_type key, /// Initializing constructor ConditionDependency::ConditionDependency(DetElement de, Condition::itemkey_type item_key, - ConditionUpdateCall* call) - : m_refCount(0), + std::shared_ptr<ConditionUpdateCall> call) + : m_refCount(0), #ifdef DD4HEP_CONDITIONS_DEBUG - detector(de), + detector(de), #endif - target(de, item_key), callback(call) + target(de, item_key), callback(std::move(call)) { InstanceCount::increment(this); } @@ -156,15 +155,18 @@ ConditionDependency::ConditionDependency(DetElement de, /// Initializing constructor ConditionDependency::ConditionDependency(DetElement de, const std::string& item, - ConditionUpdateCall* call) - : m_refCount(0), /*detector(de),*/ target(de, item), callback(call) + std::shared_ptr<ConditionUpdateCall> call) + : +#ifdef DD4HEP_CONDITIONS_DEBUG + detector(de), +#endif + target(de, item), callback(std::move(call)) { InstanceCount::increment(this); } /// Default constructor ConditionDependency::ConditionDependency() - : m_refCount(0), target(0), callback(0) { InstanceCount::increment(this); } @@ -172,28 +174,26 @@ ConditionDependency::ConditionDependency() /// Default destructor ConditionDependency::~ConditionDependency() { InstanceCount::decrement(this); - detail::releasePtr(callback); } /// Initializing constructor DependencyBuilder::DependencyBuilder(DetElement de, unsigned int item_key, - ConditionUpdateCall* call) - : m_dependency(new ConditionDependency(de,item_key,call)) + std::shared_ptr<ConditionUpdateCall> call) + : m_dependency(new ConditionDependency(de,item_key,std::move(call))) { } /// Initializing constructor DependencyBuilder::DependencyBuilder(DetElement de, const std::string& item, - ConditionUpdateCall* call) - : m_dependency(new ConditionDependency(de,item,call)) + std::shared_ptr<ConditionUpdateCall> call) + : m_dependency(new ConditionDependency(de,item,std::move(call))) { } /// Default destructor DependencyBuilder::~DependencyBuilder() { - detail::releasePtr(m_dependency); } /// Add a new dependency @@ -208,11 +208,9 @@ void DependencyBuilder::add(const ConditionKey& source) { /// Release the created dependency and take ownership. ConditionDependency* DependencyBuilder::release() { if ( m_dependency ) { - ConditionDependency* tmp = m_dependency; - m_dependency = 0; - return tmp; + return m_dependency.release(); } except("Dependency","++ Invalid object. Cannot access built objects!"); - return m_dependency; // Not necessary, but need to satisfy compiler + return m_dependency.release(); // Not necessary, but need to satisfy compiler } diff --git a/DDG4/include/DDG4/Geant4AssemblyVolume.h b/DDG4/include/DDG4/Geant4AssemblyVolume.h index af3f010f8e3cbb630aac63f01337516005b759d9..bbe9f0ef877db71464a7c9b459769169b21ad58b 100644 --- a/DDG4/include/DDG4/Geant4AssemblyVolume.h +++ b/DDG4/include/DDG4/Geant4AssemblyVolume.h @@ -49,7 +49,7 @@ namespace dd4hep { } /// Default destructor - virtual ~Geant4AssemblyVolume() { + ~Geant4AssemblyVolume() { } //std::vector<G4AssemblyTriplet>& triplets() { return fTriplets; } diff --git a/DDG4/src/Geant4Converter.cpp b/DDG4/src/Geant4Converter.cpp index 63ea9c63bec8943458f96991d8e89b9c20f6bfae..727e476f76563dfe75a51f55f1297757b362a641 100644 --- a/DDG4/src/Geant4Converter.cpp +++ b/DDG4/src/Geant4Converter.cpp @@ -197,7 +197,7 @@ void Geant4AssemblyVolume::imprint(Geant4GeometryInfo& info, // Register the physical volume created by us so we can delete it later // - fPVStore.push_back( pvPlaced.first ); + //fPVStore.push_back( pvPlaced.first ); info.g4VolumeImprints[vol].push_back(make_pair(new_chain,pvPlaced.first)); #if 0 cout << " Assembly:Parent:" << parent->GetName() << " " << node->GetName() diff --git a/examples/Conditions/src/ConditionExampleObjects.cpp b/examples/Conditions/src/ConditionExampleObjects.cpp index 28375b0b5e926fb432a676f384cfc83ed779afcb..6043d6231af5c69ee1084171c25cc49ec2484375 100644 --- a/examples/Conditions/src/ConditionExampleObjects.cpp +++ b/examples/Conditions/src/ConditionExampleObjects.cpp @@ -114,16 +114,13 @@ void ConditionUpdate3::resolve(Condition target, ConditionUpdateContext& context ConditionsDependencyCreator::ConditionsDependencyCreator(ConditionsContent& c, PrintLevel p) : OutputLevel(p), content(c) { - call1 = new ConditionUpdate1(printLevel); - call2 = new ConditionUpdate2(printLevel); - call3 = new ConditionUpdate3(printLevel); + call1 = std::shared_ptr<ConditionUpdateCall>(new ConditionUpdate1(printLevel)); + call2 = std::shared_ptr<ConditionUpdateCall>(new ConditionUpdate2(printLevel)); + call3 = std::shared_ptr<ConditionUpdateCall>(new ConditionUpdate3(printLevel)); } /// Destructor ConditionsDependencyCreator::~ConditionsDependencyCreator() { - detail::releasePtr(call1); - detail::releasePtr(call2); - detail::releasePtr(call3); } /// Callback to process a single detector element @@ -132,12 +129,12 @@ int ConditionsDependencyCreator::operator()(DetElement de, int) const { ConditionKey target1(de,"derived_data/derived_1"); ConditionKey target2(de,"derived_data/derived_2"); ConditionKey target3(de,"derived_data/derived_3"); - DependencyBuilder build_1(de, target1.item_key(), call1->addRef()); - DependencyBuilder build_2(de, target2.item_key(), call2->addRef()); - DependencyBuilder build_3(de, target3.item_key(), call3->addRef()); - //DependencyBuilder build_1(de, "derived_data/derived_1", call1->addRef()); - //DependencyBuilder build_2(de, "derived_data/derived_2", call2->addRef()); - //DependencyBuilder build_3(de, "derived_data/derived_3", call3->addRef()); + DependencyBuilder build_1(de, target1.item_key(), call1); + DependencyBuilder build_2(de, target2.item_key(), call2); + DependencyBuilder build_3(de, target3.item_key(), call3); + //DependencyBuilder build_1(de, "derived_data/derived_1", call1); + //DependencyBuilder build_2(de, "derived_data/derived_2", call2); + //DependencyBuilder build_3(de, "derived_data/derived_3", call3); // Compute the derived stuff build_1.add(key); diff --git a/examples/Conditions/src/ConditionExampleObjects.h b/examples/Conditions/src/ConditionExampleObjects.h index 87cab0a7b89f0a918b1c1bef74bc23ac9d78f1eb..5bfe31a3e19fc93bd893024d008913fce22b995b 100644 --- a/examples/Conditions/src/ConditionExampleObjects.h +++ b/examples/Conditions/src/ConditionExampleObjects.h @@ -148,7 +148,7 @@ namespace dd4hep { /// Content object to be filled ConditionsContent& content; /// Three different update call types - ConditionUpdateCall *call1, *call2, *call3; + std::shared_ptr<ConditionUpdateCall> call1, call2, call3; /// Constructor ConditionsDependencyCreator(ConditionsContent& c, PrintLevel p); /// Destructor diff --git a/examples/DDDB/src/plugins/DDDBDerivedCondTest.cpp b/examples/DDDB/src/plugins/DDDBDerivedCondTest.cpp index d9f6b3f043fe0edce8d45541e4a49e4f994c8150..ead7d692068305e91ff8e80c1788645f7af9967e 100644 --- a/examples/DDDB/src/plugins/DDDBDerivedCondTest.cpp +++ b/examples/DDDB/src/plugins/DDDBDerivedCondTest.cpp @@ -283,9 +283,9 @@ namespace { ConditionKey target1(de,cond->name+"/derived_1"); ConditionKey target2(de,cond->name+"/derived_2"); ConditionKey target3(de,cond->name+"/derived_3"); - DependencyBuilder build_1(de, cond->name+"/derived_1", new ConditionUpdate1(m_context)); - DependencyBuilder build_2(de, cond->name+"/derived_2", new ConditionUpdate2(m_context)); - DependencyBuilder build_3(de, cond->name+"/derived_3", new ConditionUpdate3(m_context)); + DependencyBuilder build_1(de, cond->name+"/derived_1", make_shared<ConditionUpdate1>(m_context)); + DependencyBuilder build_2(de, cond->name+"/derived_2", make_shared<ConditionUpdate2>(m_context)); + DependencyBuilder build_3(de, cond->name+"/derived_3", make_shared<ConditionUpdate3>(m_context)); build_1.add(key); build_2.add(key); diff --git a/examples/DDDB/src/plugins/DeVeloServiceTest.cpp b/examples/DDDB/src/plugins/DeVeloServiceTest.cpp index 2e233fc07fea2dff32e5a26d5708d6a1fec0ad9a..64978eae48a15ef5f2bfe1912f4125dd19d43f9b 100644 --- a/examples/DDDB/src/plugins/DeVeloServiceTest.cpp +++ b/examples/DDDB/src/plugins/DeVeloServiceTest.cpp @@ -118,12 +118,12 @@ namespace { dd4hep::DetectorScanner(dd4hep::detElementsCollector(elts), m_de); dd4hep::cond::DependencyBuilder align_builder(dsc.world(), Keys::alignmentsComputedKey, - new DeAlignmentCall(m_de)); + make_shared<DeAlignmentCall>(m_de)); auto* dep = align_builder.release(); dep->target.hash = Keys::alignmentsComputedKey; m_service->addContent(content, dep); - std::unique_ptr<DeVeloStaticConditionCall> static_update(new DeVeloStaticConditionCall()); + auto static_update = make_shared<DeVeloStaticConditionCall>(); for(const auto& e : elts) { dd4hep::DetElement de = e.first; dd4hep::DDDB::DDDBCatalog* cat = de.extension<dd4hep::DDDB::DDDBCatalog>(); @@ -147,16 +147,15 @@ namespace { } } - dd4hep::cond::DependencyBuilder static_builder(de, Keys::staticKey, static_update->addRef()); + dd4hep::cond::DependencyBuilder static_builder(de, Keys::staticKey, static_update); m_service->addContent(content, static_builder.release()); - dd4hep::cond::ConditionUpdateCall* call = (e.first == m_de) - ? new DeVeloConditionCall(de, cat, m_context.get()) - : new DeVeloIOVConditionCall(de, cat, m_context.get()); + shared_ptr<dd4hep::cond::ConditionUpdateCall> call = ( e.first == m_de ) + ? make_shared<DeVeloConditionCall>(de, cat, m_context.get()) + : make_shared<DeVeloIOVConditionCall>(de, cat, m_context.get()); dd4hep::cond::DependencyBuilder iov_builder(de, Keys::deKey, call); m_service->addContent(content, iov_builder.release()); } - static_update.release()->release(); m_service->closeContent(content); manager.clear(); } diff --git a/examples/DDDB/src/plugins/DeVeloTest.cpp b/examples/DDDB/src/plugins/DeVeloTest.cpp index 4cc985c0dd141500fb87c133e83a40c35b3acc3d..73fc44503a184fc4163414fc587f787c5386a448 100644 --- a/examples/DDDB/src/plugins/DeVeloTest.cpp +++ b/examples/DDDB/src/plugins/DeVeloTest.cpp @@ -116,12 +116,12 @@ namespace { dd4hep::DetectorScanner(dd4hep::detElementsCollector(elts), m_de); dd4hep::cond::DependencyBuilder align_builder(m_detDesc.world(), Keys::alignmentsComputedKey, - new DeAlignmentCall(m_de)); + make_shared<DeAlignmentCall>(m_de)); auto* dep = align_builder.release(); dep->target.hash = Keys::alignmentsComputedKey; m_content->addDependency(dep); - std::unique_ptr<DeVeloStaticConditionCall> static_update(new DeVeloStaticConditionCall()); + auto static_update = make_shared<DeVeloStaticConditionCall>(); for(const auto& e : elts) { dd4hep::DetElement de = e.first; dd4hep::DDDB::DDDBCatalog* cat = de.extension<dd4hep::DDDB::DDDBCatalog>(); @@ -142,16 +142,15 @@ namespace { m_content->addDependency((*first).second); } - dd4hep::cond::DependencyBuilder static_builder(de, Keys::staticKey, static_update->addRef()); + dd4hep::cond::DependencyBuilder static_builder(de, Keys::staticKey, static_update); m_content->addDependency(static_builder.release()); - dd4hep::cond::ConditionUpdateCall* call = (e.first == m_de) - ? new DeVeloConditionCall(de, cat, m_context.get()) - : new DeVeloIOVConditionCall(de, cat, m_context.get()); + shared_ptr<dd4hep::cond::ConditionUpdateCall> call = (e.first == m_de) + ? make_shared<DeVeloConditionCall>(de, cat, m_context.get()) + : make_shared<DeVeloIOVConditionCall>(de, cat, m_context.get()); dd4hep::cond::DependencyBuilder iov_builder(de, Keys::deKey, call); m_content->addDependency(iov_builder.release()); } - static_update.release()->release(); m_manager.clear(); }