DD4hep  1.30.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 namespace {
23  std::string obj_type(void* ptr) {
25  return typeName(typeid(*o));
26  }
27 }
28 
30 ObjectExtensions::ObjectExtensions(const std::type_info& /* parent_type */) {
32 }
33 
36  clear();
38 }
39 
42  extensions = source.extensions;
43  source.extensions.clear();
44 }
45 
47 void ObjectExtensions::clear(bool destroy) {
48  for( const auto& i : extensions ) {
49  if ( i.second ) {
50  if ( destroy ) i.second->destruct();
51  delete i.second;
52  }
53  }
54  extensions.clear();
55 }
56 
58 void ObjectExtensions::copyFrom(const std::map<unsigned long long int,ExtensionEntry*>& ext, void* arg) {
59  for( const auto& i : ext ) {
60  extensions[i.first] = i.second->clone(arg);
61  }
62 }
63 
65 void* ObjectExtensions::addExtension(unsigned long long int key, ExtensionEntry* e) {
66  if ( e ) {
67  if ( e->object() ) {
68  auto j = extensions.find(key);
69  if (j == extensions.end()) {
70  extensions[key] = e;
71  return e->object();
72  }
73  except("ObjectExtensions::addExtension","Object already has an extension of type: %s.",obj_type(e->object()).c_str());
74  }
75  except("ObjectExtensions::addExtension","Invalid extension object for key %016llX!",key);
76  }
77  except("ObjectExtensions::addExtension","Invalid extension entry for key %016llX!",key);
78  return nullptr;
79 }
80 
82 void* ObjectExtensions::removeExtension(unsigned long long int key, bool destroy) {
83  auto j = extensions.find(key);
84  if ( j != extensions.end() ) {
85  void* ptr = (*j).second->object();
86  if ( destroy ) {
87  (*j).second->destruct();
88  }
89  delete (*j).second;
90  extensions.erase(j);
91  return ptr;
92  }
93  except("ObjectExtensions::removeExtension","The object of type %016llX is not present.",key);
94  return nullptr;
95 }
96 
98 void* ObjectExtensions::extension(unsigned long long int key) const {
99  const auto j = extensions.find(key);
100  if (j != extensions.end()) {
101  return (*j).second->object();
102  }
103  except("ObjectExtensions::extension","The object has no extension of type %016llX.",key);
104  return nullptr;
105 }
106 
108 void* ObjectExtensions::extension(unsigned long long int key, bool alert) const {
109  const auto j = extensions.find(key);
110  if (j != extensions.end()) {
111  return (*j).second->object();
112  }
113  else if ( !alert )
114  return 0;
115  except("ObjectExtensions::extension","The object has no extension of type %016llX.",key);
116  return nullptr;
117 }
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:82
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:30
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:41
dd4hep::ObjectExtensions::~ObjectExtensions
virtual ~ObjectExtensions()
Default destructor.
Definition: ObjectExtensions.cpp:35
Primitives.h
key
unsigned char key
Definition: AlignmentsCalculator.cpp:69
dd4hep::ObjectExtensions::clear
void clear(bool destroy=true)
Clear all extensions.
Definition: ObjectExtensions.cpp:47
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:58
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:65
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:108