DD4hep  1.31.0
Detector Description Toolkit for High Energy Physics
ObjectExtensions.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
16 #include <DD4hep/InstanceCount.h>
17 #include <DD4hep/Primitives.h>
18 #include <DD4hep/Printout.h>
19 
20 using namespace dd4hep;
21 
22 #define EXTENSION_DEBUG 0
23 
24 namespace {
25  std::string obj_type(void* ptr) {
27  return typeName(typeid(*o));
28  }
29 }
30 
32 ObjectExtensions::ObjectExtensions(const std::type_info& /* parent_type */) {
34 }
35 
38  clear();
40 }
41 
44  extensions = source.extensions;
45  source.extensions.clear();
46 }
47 
49 void ObjectExtensions::clear(bool destroy) {
50  for( const auto& i : extensions ) {
51  if ( i.second ) {
52  if ( destroy ) i.second->destruct();
53  delete i.second;
54  }
55  }
56  extensions.clear();
57 }
58 
60 void ObjectExtensions::copyFrom(const std::map<unsigned long long int,ExtensionEntry*>& ext, void* arg) {
61  for( const auto& i : ext ) {
62  extensions[i.first] = i.second->clone(arg);
63  }
64 }
65 
67 void* ObjectExtensions::addExtension(unsigned long long int key, ExtensionEntry* e) {
68  if ( e ) {
69  if ( e->object() ) {
70  auto j = extensions.find(key);
71  if (j == extensions.end()) {
72 #if EXTENSION_DEBUG
73  auto* p = e->object();
74  ExtensionEntry* ptr = (ExtensionEntry*)e;
75  printout(ALWAYS,"addExtension","+++ Add extension with key: %016llX --> %p [%s]",
76  key, p, typeName(typeid(*ptr)).c_str());
77 #endif
78  extensions[key] = e;
79  return e->object();
80  }
81  except("ObjectExtensions::addExtension","Object already has an extension of type: %s.",obj_type(e->object()).c_str());
82  }
83  except("ObjectExtensions::addExtension","Invalid extension object for key %016llX!",key);
84  }
85  except("ObjectExtensions::addExtension","Invalid extension entry for key %016llX!",key);
86  return nullptr;
87 }
88 
90 void* ObjectExtensions::removeExtension(unsigned long long int key, bool destroy) {
91  auto j = extensions.find(key);
92  if ( j != extensions.end() ) {
93  void* ptr = (*j).second->object();
94  if ( destroy ) {
95  (*j).second->destruct();
96  }
97  delete (*j).second;
98  extensions.erase(j);
99  return ptr;
100  }
101  except("ObjectExtensions::removeExtension","The object of type %016llX is not present.",key);
102  return nullptr;
103 }
104 
106 void* ObjectExtensions::extension(unsigned long long int key) const {
107  const auto j = extensions.find(key);
108 #if EXTENSION_DEBUG
109  printout(ALWAYS,"extension","+++ Get extension with key: %016llX", key);
110 #endif
111  if (j != extensions.end()) {
112  return (*j).second->object();
113  }
114  except("ObjectExtensions::extension","The object has no extension of type %016llX.",key);
115  return nullptr;
116 }
117 
119 void* ObjectExtensions::extension(unsigned long long int key, bool alert) const {
120  const auto j = extensions.find(key);
121 #if EXTENSION_DEBUG
122  printout(ALWAYS,"extension","+++ Get extension with key: %016llX", key);
123 #endif
124  if (j != extensions.end()) {
125  return (*j).second->object();
126  }
127  else if ( !alert )
128  return 0;
129  except("ObjectExtensions::extension","The object has no extension of type %016llX.",key);
130  return nullptr;
131 }
dd4hep::ExtensionEntry
Definition of the extension entry interface class.
Definition: ExtensionEntry.h:40
dd4hep::ObjectExtensions
Implementation of an object supporting arbitrary user extensions.
Definition: ObjectExtensions.h:33
ObjectExtensions.h
dd4hep::ObjectExtensions::removeExtension
void * removeExtension(unsigned long long int key, bool destroy)
Remove an existing extension object from the instance.
Definition: ObjectExtensions.cpp:90
dd4hep::InstanceCount::increment
static void increment(T *)
Increment count according to type information.
Definition: InstanceCount.h:98
dd4hep::ObjectExtensions::ObjectExtensions
ObjectExtensions(const std::type_info &parent_type)
Default constructor.
Definition: ObjectExtensions.cpp:32
dd4hep::ObjectExtensions::extensions
std::map< unsigned long long int, ExtensionEntry * > extensions
The extensions object.
Definition: ObjectExtensions.h:36
dd4hep::InstanceCount::decrement
static void decrement(T *)
Decrement count according to type information.
Definition: InstanceCount.h:102
dd4hep::ExtensionEntry::object
virtual void * object() const =0
Virtual object accessor.
dd4hep::ObjectExtensions::move
void move(ObjectExtensions &copy)
Move extensions to target object.
Definition: ObjectExtensions.cpp:43
dd4hep::ObjectExtensions::~ObjectExtensions
virtual ~ObjectExtensions()
Default destructor.
Definition: ObjectExtensions.cpp:37
Primitives.h
key
unsigned char key
Definition: AlignmentsCalculator.cpp:69
dd4hep::ObjectExtensions::clear
void clear(bool destroy=true)
Clear all extensions.
Definition: ObjectExtensions.cpp:49
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
InstanceCount.h
dd4hep::ObjectExtensions::copyFrom
void copyFrom(const std::map< unsigned long long int, ExtensionEntry * > &ext, void *arg)
Copy object extensions from another object. Hosting type must be identical!
Definition: ObjectExtensions.cpp:60
Printout.h
dd4hep::ObjectExtensions::addExtension
void * addExtension(unsigned long long int key, ExtensionEntry *entry)
Add an extension object to the detector element.
Definition: ObjectExtensions.cpp:67
dd4hep::ObjectExtensions::extension
void * extension(unsigned long long int key, bool alert) const
Access an existing extension object from the detector element.
Definition: ObjectExtensions.cpp:119