File indexing completed on 2025-01-18 09:13:56
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 namespace {
0023 std::string obj_type(void* ptr) {
0024 ObjectExtensions* o = (ObjectExtensions*)ptr;
0025 return typeName(typeid(*o));
0026 }
0027 }
0028
0029
0030 ObjectExtensions::ObjectExtensions(const std::type_info& ) {
0031 InstanceCount::increment(this);
0032 }
0033
0034
0035 ObjectExtensions::~ObjectExtensions() {
0036 clear();
0037 InstanceCount::decrement(this);
0038 }
0039
0040
0041 void ObjectExtensions::move(ObjectExtensions& source) {
0042 extensions = source.extensions;
0043 source.extensions.clear();
0044 }
0045
0046
0047 void ObjectExtensions::clear(bool destroy) {
0048 for( const auto& i : extensions ) {
0049 if ( i.second ) {
0050 if ( destroy ) i.second->destruct();
0051 delete i.second;
0052 }
0053 }
0054 extensions.clear();
0055 }
0056
0057
0058 void ObjectExtensions::copyFrom(const std::map<unsigned long long int,ExtensionEntry*>& ext, void* arg) {
0059 for( const auto& i : ext ) {
0060 extensions[i.first] = i.second->clone(arg);
0061 }
0062 }
0063
0064
0065 void* ObjectExtensions::addExtension(unsigned long long int key, ExtensionEntry* e) {
0066 if ( e ) {
0067 if ( e->object() ) {
0068 auto j = extensions.find(key);
0069 if (j == extensions.end()) {
0070 extensions[key] = e;
0071 return e->object();
0072 }
0073 except("ObjectExtensions::addExtension","Object already has an extension of type: %s.",obj_type(e->object()).c_str());
0074 }
0075 except("ObjectExtensions::addExtension","Invalid extension object for key %016llX!",key);
0076 }
0077 except("ObjectExtensions::addExtension","Invalid extension entry for key %016llX!",key);
0078 return nullptr;
0079 }
0080
0081
0082 void* ObjectExtensions::removeExtension(unsigned long long int key, bool destroy) {
0083 auto j = extensions.find(key);
0084 if ( j != extensions.end() ) {
0085 void* ptr = (*j).second->object();
0086 if ( destroy ) {
0087 (*j).second->destruct();
0088 }
0089 delete (*j).second;
0090 extensions.erase(j);
0091 return ptr;
0092 }
0093 except("ObjectExtensions::removeExtension","The object of type %016llX is not present.",key);
0094 return nullptr;
0095 }
0096
0097
0098 void* ObjectExtensions::extension(unsigned long long int key) const {
0099 const auto j = extensions.find(key);
0100 if (j != extensions.end()) {
0101 return (*j).second->object();
0102 }
0103 except("ObjectExtensions::extension","The object has no extension of type %016llX.",key);
0104 return nullptr;
0105 }
0106
0107
0108 void* ObjectExtensions::extension(unsigned long long int key, bool alert) const {
0109 const auto j = extensions.find(key);
0110 if (j != extensions.end()) {
0111 return (*j).second->object();
0112 }
0113 else if ( !alert )
0114 return 0;
0115 except("ObjectExtensions::extension","The object has no extension of type %016llX.",key);
0116 return nullptr;
0117 }