DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
PluginTester.cpp
Go to the documentation of this file.
1 //==========================================================================
2 // AIDA Detector description implementation
3 //--------------------------------------------------------------------------
4 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
5 // All rights reserved.
6 //
7 // For the licensing terms see $DD4hepINSTALL/LICENSE.
8 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
9 //
10 // Author : M.Frank
11 //
12 //==========================================================================
13 
14 // Framework include files
15 #include <DD4hep/PluginTester.h>
16 #include <DD4hep/InstanceCount.h>
17 #include <DD4hep/Primitives.h>
18 
19 // C/C++ include files
20 #include <stdexcept>
21 
22 using namespace dd4hep;
23 
24 namespace {
25 
26  static int s_extensionID = 0;
27 
28  PluginTester::ExtensionMap* extensionContainer(const std::type_info& typ) {
29  static std::map<const std::type_info*, PluginTester::ExtensionMap> s_map;
30  PluginTester::ExtensionMap& m = s_map[&typ];
31  return &m;
32  }
33 }
34 
37  extensionMap = extensionContainer(typeid(PluginTester));
39 }
40 
43  clear();
45 }
46 
48 void PluginTester::clear(bool destroy) {
49  for (Extensions::iterator i = extensions.begin(); i != extensions.end(); ++i) {
50  void* ptr = (*i).second;
51  if (ptr) {
52  ExtensionMap::iterator j = extensionMap->find((*i).first.first);
53  if (j != extensionMap->end()) {
54  Entry& e = (*j).second;
55  if (destroy && e.destruct)
56  (*(e.destruct))(ptr);
57  }
58  }
59  }
60  extensions.clear();
61 }
62 
64 void* PluginTester::addExtension(void* ptr, const std::string& name, const std::type_info& info, destruct_t dtor) {
65  key_type key(&info,name);
66  Extensions::iterator j = extensions.find(key);
67  if (j == extensions.end()) {
68  ExtensionMap::iterator i = extensionMap->find(&info);
69  if (i == extensionMap->end()) {
70  Entry entry;
71  entry.destruct = dtor;
72  entry.id = ++s_extensionID;
73  extensionMap->emplace(&info, entry);
74  }
75  return extensions[key] = ptr;
76  }
77  throw std::runtime_error("dd4hep: addExtension: Object already has an extension "+name+
78  " of type:" + typeName(info) + ".");
79 }
80 
82 void* PluginTester::removeExtension(const std::string& name, const std::type_info& info, bool destroy) {
83  key_type key(&info,name);
84  Extensions::iterator j = extensions.find(key);
85  if (j != extensions.end()) {
86  void *ptr = (*j).second;
87  if ( destroy ) {
88  ExtensionMap::iterator i = extensionMap->find(&info);
89  if (i != extensionMap->end()) {
90  Entry& e = (*i).second;
91  (*e.destruct)((*j).second);
92  ptr = 0;
93  }
94  }
95  extensions.erase(j);
96  return ptr;
97  }
98  throw std::runtime_error("dd4hep: removeExtension: The object "+name+
99  " of type " + typeName(info) + " is not present.");
100 }
101 
103 void* PluginTester::extension(const std::string& name, const std::type_info& info, bool alert) const {
104  key_type key(&info,name);
105  Extensions::const_iterator j = extensions.find(key);
106  if (j != extensions.end()) {
107  return (*j).second;
108  }
109  else if ( !alert )
110  return 0;
111  throw std::runtime_error("dd4hep: extension: Object has no extension "+name+
112  " of type:" + typeName(info) + ".");
113 }
114 
dd4hep::PluginTester::clear
void clear(bool destroy=true)
Clear all extensions.
Definition: PluginTester.cpp:48
dd4hep::PluginTester::key_type
std::pair< const std::type_info *, std::string > key_type
Definition of the extension type.
Definition: PluginTester.h:33
dd4hep::PluginTester::PluginTester
PluginTester()
Default constructor.
Definition: PluginTester.cpp:36
dd4hep::PluginTester::~PluginTester
virtual ~PluginTester()
Default destructor.
Definition: PluginTester.cpp:42
dd4hep::info
std::size_t info(const std::string &src, const std::string &msg)
Definition: RootDictionary.h:65
dd4hep::InstanceCount::increment
static void increment(T *)
Increment count according to type information.
Definition: InstanceCount.h:98
dd4hep::PluginTester::removeExtension
void * removeExtension(const std::string &name, const std::type_info &info, bool destroy)
Remove an existing extension object from the instance.
Definition: PluginTester.cpp:82
dd4hep::PluginTester::extensionMap
ExtensionMap * extensionMap
Pointer to the extension map.
Definition: PluginTester.h:49
dd4hep::PluginTester::ExtensionMap
std::map< const std::type_info *, Entry > ExtensionMap
Definition: PluginTester.h:44
dd4hep::PluginTester::extension
void * extension(const std::string &name, const std::type_info &info, bool alert) const
Access an existing extension object from the detector element.
Definition: PluginTester.cpp:103
dd4hep::PluginTester::addExtension
void * addExtension(void *ptr, const std::string &name, const std::type_info &info, destruct_t dtor)
Add an extension object to the detector element.
Definition: PluginTester.cpp:64
dd4hep::InstanceCount::decrement
static void decrement(T *)
Decrement count according to type information.
Definition: InstanceCount.h:102
Primitives.h
key
unsigned char key
Definition: AlignmentsCalculator.cpp:69
dd4hep::PluginTester
Helper class to ease testing of plugins: Effectively a container of object extensions.
Definition: PluginTester.h:30
dd4hep::PluginTester::Entry::id
int id
Definition: PluginTester.h:42
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
PluginTester.h
dd4hep::PluginTester::Entry
Defintiion of the extension entry.
Definition: PluginTester.h:40
InstanceCount.h
dd4hep::PluginTester::Entry::destruct
destruct_t destruct
Definition: PluginTester.h:41
dd4hep::PluginTester::extensions
Extensions extensions
The extensions object.
Definition: PluginTester.h:47