DD4hep - The AIDA detector description toolkit for high energy physics experiments
DD4hep  Rev:Unversioneddirectory
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PluginTester.cpp
Go to the documentation of this file.
1 //==========================================================================
2 // AIDA Detector description implementation for LCD
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  static int s_extensionID = 0;
26  PluginTester::ExtensionMap* extensionContainer(const std::type_info& typ) {
27  static std::map<const std::type_info*, PluginTester::ExtensionMap> s_map;
28  return &s_map[&typ];
29  }
30 }
31 
34  extensionMap = extensionContainer(typeid(*this));
36 }
37 
40  clear();
42 }
43 
45 void PluginTester::clear(bool destroy) {
46  for (Extensions::iterator i = extensions.begin(); i != extensions.end(); ++i) {
47  void* ptr = (*i).second;
48  if (ptr) {
49  ExtensionMap::iterator j = extensionMap->find((*i).first.first);
50  if (j != extensionMap->end()) {
51  Entry& e = (*j).second;
52  if (destroy && e.destruct)
53  (*(e.destruct))(ptr);
54  }
55  }
56  }
57  extensions.clear();
58 }
59 
61 void* PluginTester::addExtension(void* ptr, const std::string& name, const std::type_info& info, destruct_t dtor) {
62  key_type key(&info,name);
63  Extensions::iterator j = extensions.find(key);
64  if (j == extensions.end()) {
65  ExtensionMap::iterator i = extensionMap->find(&info);
66  if (i == extensionMap->end()) {
67  Entry entry;
68  entry.destruct = dtor;
69  entry.id = ++s_extensionID;
70  extensionMap->insert(make_pair(&info, entry));
71  }
72  return extensions[key] = ptr;
73  }
74  throw std::runtime_error("DD4hep: addExtension: Object already has an extension "+name+
75  " of type:" + typeName(info) + ".");
76 }
77 
79 void* PluginTester::removeExtension(const std::string& name, const std::type_info& info, bool destroy) {
80  key_type key(&info,name);
81  Extensions::iterator j = extensions.find(key);
82  if (j != extensions.end()) {
83  void *ptr = (*j).second;
84  if ( destroy ) {
85  ExtensionMap::iterator i = extensionMap->find(&info);
86  if (i != extensionMap->end()) {
87  Entry& e = (*i).second;
88  (*e.destruct)((*j).second);
89  ptr = 0;
90  }
91  }
92  extensions.erase(j);
93  return ptr;
94  }
95  throw std::runtime_error("DD4hep: removeExtension: The object "+name+
96  " of type " + typeName(info) + " is not present.");
97 }
98 
100 void* PluginTester::extension(const std::string& name, const std::type_info& info, bool alert) const {
101  key_type key(&info,name);
102  Extensions::const_iterator j = extensions.find(key);
103  if (j != extensions.end()) {
104  return (*j).second;
105  }
106  else if ( !alert )
107  return 0;
108  throw std::runtime_error("DD4hep: extension: Object has no extension "+name+
109  " of type:" + typeName(info) + ".");
110 }
111 
Defintiion of the extension entry.
Definition: PluginTester.h:36
virtual ~PluginTester()
Default destructor.
std::pair< const std::type_info *, std::string > key_type
Definition of the extension type.
Definition: PluginTester.h:31
int id
Definition: PluginTester.h:38
static void decrement(T *)
Decrement count according to type information.
void * removeExtension(const std::string &name, const std::type_info &info, bool destroy)
Remove an existing extension object from the instance.
std::string typeName(const std::type_info &type)
ABI information about type names.
Definition: Primitives.cpp:186
return e
Definition: Volumes.cpp:297
Extensions extensions
The extensions object.
Definition: PluginTester.h:43
std::map< const std::type_info *, Entry > ExtensionMap
Definition: PluginTester.h:40
void * extension(const std::string &name, const std::type_info &info, bool alert) const
Access an existing extension object from the detector element.
destruct_t destruct
Definition: PluginTester.h:37
ExtensionMap * extensionMap
Pointer to the extension map.
Definition: PluginTester.h:45
void * addExtension(void *ptr, const std::string &name, const std::type_info &info, destruct_t dtor)
Add an extension object to the detector element.
PluginTester()
Default constructor.
static void increment(T *)
Increment count according to type information.
Definition: InstanceCount.h:98
void clear(bool destroy=true)
Clear all extensions.