File indexing completed on 2025-06-30 07:54:19
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #include <DD4hep/ObjectExtensions.h>
0016 #include <DD4hep/InstanceCount.h>
0017 #include <DD4hep/Primitives.h>
0018 #include <DD4hep/Printout.h>
0019
0020 using namespace dd4hep;
0021
0022 #define EXTENSION_DEBUG 0
0023
0024 namespace {
0025 std::string obj_type(void* ptr) {
0026 ObjectExtensions* o = (ObjectExtensions*)ptr;
0027 return typeName(typeid(*o));
0028 }
0029 }
0030
0031
0032 ObjectExtensions::ObjectExtensions(const std::type_info& ) {
0033 InstanceCount::increment(this);
0034 }
0035
0036
0037 ObjectExtensions::~ObjectExtensions() {
0038 clear();
0039 InstanceCount::decrement(this);
0040 }
0041
0042
0043 void ObjectExtensions::move(ObjectExtensions& source) {
0044 extensions = source.extensions;
0045 source.extensions.clear();
0046 }
0047
0048
0049 void ObjectExtensions::clear(bool destroy) {
0050 for( const auto& i : extensions ) {
0051 if ( i.second ) {
0052 if ( destroy ) i.second->destruct();
0053 delete i.second;
0054 }
0055 }
0056 extensions.clear();
0057 }
0058
0059
0060 void ObjectExtensions::copyFrom(const std::map<unsigned long long int,ExtensionEntry*>& ext, void* arg) {
0061 for( const auto& i : ext ) {
0062 extensions[i.first] = i.second->clone(arg);
0063 }
0064 }
0065
0066
0067 void* ObjectExtensions::addExtension(unsigned long long int key, ExtensionEntry* e) {
0068 if ( e ) {
0069 if ( e->object() ) {
0070 auto j = extensions.find(key);
0071 if (j == extensions.end()) {
0072 #if EXTENSION_DEBUG
0073 auto* p = e->object();
0074 ExtensionEntry* ptr = (ExtensionEntry*)e;
0075 printout(ALWAYS,"addExtension","+++ Add extension with key: %016llX --> %p [%s]",
0076 key, p, typeName(typeid(*ptr)).c_str());
0077 #endif
0078 extensions[key] = e;
0079 return e->object();
0080 }
0081 except("ObjectExtensions::addExtension","Object already has an extension of type: %s.",obj_type(e->object()).c_str());
0082 }
0083 except("ObjectExtensions::addExtension","Invalid extension object for key %016llX!",key);
0084 }
0085 except("ObjectExtensions::addExtension","Invalid extension entry for key %016llX!",key);
0086 return nullptr;
0087 }
0088
0089
0090 void* ObjectExtensions::removeExtension(unsigned long long int key, bool destroy) {
0091 auto j = extensions.find(key);
0092 if ( j != extensions.end() ) {
0093 void* ptr = (*j).second->object();
0094 if ( destroy ) {
0095 (*j).second->destruct();
0096 }
0097 delete (*j).second;
0098 extensions.erase(j);
0099 return ptr;
0100 }
0101 except("ObjectExtensions::removeExtension","The object of type %016llX is not present.",key);
0102 return nullptr;
0103 }
0104
0105
0106 void* ObjectExtensions::extension(unsigned long long int key) const {
0107 const auto j = extensions.find(key);
0108 #if EXTENSION_DEBUG
0109 printout(ALWAYS,"extension","+++ Get extension with key: %016llX", key);
0110 #endif
0111 if (j != extensions.end()) {
0112 return (*j).second->object();
0113 }
0114 except("ObjectExtensions::extension","The object has no extension of type %016llX.",key);
0115 return nullptr;
0116 }
0117
0118
0119 void* ObjectExtensions::extension(unsigned long long int key, bool alert) const {
0120 const auto j = extensions.find(key);
0121 #if EXTENSION_DEBUG
0122 printout(ALWAYS,"extension","+++ Get extension with key: %016llX", key);
0123 #endif
0124 if (j != extensions.end()) {
0125 return (*j).second->object();
0126 }
0127 else if ( !alert )
0128 return 0;
0129 except("ObjectExtensions::extension","The object has no extension of type %016llX.",key);
0130 return nullptr;
0131 }