File indexing completed on 2025-01-18 09:13:57
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #include <DD4hep/PluginTester.h>
0016 #include <DD4hep/InstanceCount.h>
0017 #include <DD4hep/Primitives.h>
0018
0019
0020 #include <stdexcept>
0021
0022 using namespace dd4hep;
0023
0024 namespace {
0025
0026 static int s_extensionID = 0;
0027
0028 PluginTester::ExtensionMap* extensionContainer(const std::type_info& typ) {
0029 static std::map<const std::type_info*, PluginTester::ExtensionMap> s_map;
0030 PluginTester::ExtensionMap& m = s_map[&typ];
0031 return &m;
0032 }
0033 }
0034
0035
0036 PluginTester::PluginTester() {
0037 extensionMap = extensionContainer(typeid(PluginTester));
0038 InstanceCount::increment(this);
0039 }
0040
0041
0042 PluginTester::~PluginTester() {
0043 clear();
0044 InstanceCount::decrement(this);
0045 }
0046
0047
0048 void PluginTester::clear(bool destroy) {
0049 for (Extensions::iterator i = extensions.begin(); i != extensions.end(); ++i) {
0050 void* ptr = (*i).second;
0051 if (ptr) {
0052 ExtensionMap::iterator j = extensionMap->find((*i).first.first);
0053 if (j != extensionMap->end()) {
0054 Entry& e = (*j).second;
0055 if (destroy && e.destruct)
0056 (*(e.destruct))(ptr);
0057 }
0058 }
0059 }
0060 extensions.clear();
0061 }
0062
0063
0064 void* PluginTester::addExtension(void* ptr, const std::string& name, const std::type_info& info, destruct_t dtor) {
0065 key_type key(&info,name);
0066 Extensions::iterator j = extensions.find(key);
0067 if (j == extensions.end()) {
0068 ExtensionMap::iterator i = extensionMap->find(&info);
0069 if (i == extensionMap->end()) {
0070 Entry entry;
0071 entry.destruct = dtor;
0072 entry.id = ++s_extensionID;
0073 extensionMap->emplace(&info, entry);
0074 }
0075 return extensions[key] = ptr;
0076 }
0077 throw std::runtime_error("dd4hep: addExtension: Object already has an extension "+name+
0078 " of type:" + typeName(info) + ".");
0079 }
0080
0081
0082 void* PluginTester::removeExtension(const std::string& name, const std::type_info& info, bool destroy) {
0083 key_type key(&info,name);
0084 Extensions::iterator j = extensions.find(key);
0085 if (j != extensions.end()) {
0086 void *ptr = (*j).second;
0087 if ( destroy ) {
0088 ExtensionMap::iterator i = extensionMap->find(&info);
0089 if (i != extensionMap->end()) {
0090 Entry& e = (*i).second;
0091 (*e.destruct)((*j).second);
0092 ptr = 0;
0093 }
0094 }
0095 extensions.erase(j);
0096 return ptr;
0097 }
0098 throw std::runtime_error("dd4hep: removeExtension: The object "+name+
0099 " of type " + typeName(info) + " is not present.");
0100 }
0101
0102
0103 void* PluginTester::extension(const std::string& name, const std::type_info& info, bool alert) const {
0104 key_type key(&info,name);
0105 Extensions::const_iterator j = extensions.find(key);
0106 if (j != extensions.end()) {
0107 return (*j).second;
0108 }
0109 else if ( !alert )
0110 return 0;
0111 throw std::runtime_error("dd4hep: extension: Object has no extension "+name+
0112 " of type:" + typeName(info) + ".");
0113 }
0114