Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:13:56

0001 //==========================================================================
0002 //  AIDA Detector description implementation 
0003 //--------------------------------------------------------------------------
0004 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
0005 // All rights reserved.
0006 //
0007 // For the licensing terms see $DD4hepINSTALL/LICENSE.
0008 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
0009 //
0010 // Author     : M.Frank
0011 //
0012 //==========================================================================
0013 
0014 // Framework includes
0015 #include <DD4hep/Printout.h>
0016 #include <DD4hep/Primitives.h>
0017 #include <DD4hep/OpaqueData.h>
0018 #include <DD4hep/InstanceCount.h>
0019 
0020 // C/C++ header files
0021 #include <cstring>
0022 
0023 using namespace dd4hep;
0024 
0025 /// Create data block from string representation
0026 bool OpaqueData::fromString(const std::string& rep)   {
0027   if ( pointer && grammar )  {
0028     return grammar->fromString(pointer,rep);
0029   }
0030   throw std::runtime_error("Opaque data block is unbound. Cannot parse string representation.");
0031 }
0032 
0033 /// Create string representation of the data block
0034 std::string OpaqueData::str()  const  {
0035   if ( pointer && grammar )  {
0036     return grammar->str(pointer);
0037   }
0038   throw std::runtime_error("Opaque data block is unbound. Cannot create string representation.");
0039 }
0040 
0041 /// Access type id of the condition
0042 const std::type_info& OpaqueData::typeInfo() const  {
0043   if ( pointer && grammar ) {
0044     return grammar->type();
0045   }
0046   throw std::runtime_error("Opaque data block is unbound. Cannot determine type information!");
0047 }
0048 
0049 /// Access type name of the condition data block
0050 const std::string& OpaqueData::dataType() const   {
0051   if ( pointer && grammar ) {
0052     return grammar->type_name();
0053   }
0054   throw std::runtime_error("Opaque data block is unbound. Cannot determine type information!"); 
0055 }
0056 
0057 /// Standard initializing constructor
0058 OpaqueDataBlock::OpaqueDataBlock() : OpaqueData(), type(0)   {
0059   InstanceCount::increment(this);
0060 }
0061 
0062 /// Copy constructor
0063 OpaqueDataBlock::OpaqueDataBlock(const OpaqueDataBlock& c) 
0064   : OpaqueData(c), type(c.type)   {
0065   grammar = 0;
0066   pointer = 0;
0067   this->bind(c.grammar);
0068   if ( this->grammar->specialization.copy )  {
0069     this->grammar->specialization.copy(pointer, c.pointer);
0070   }
0071   else  {
0072     except("OpaqueDataBlock","Grammar type %s does not support object copy. Operation not allowed.",
0073            this->grammar->type_name().c_str());
0074   }
0075   InstanceCount::increment(this);
0076 }
0077 
0078 /// Standard Destructor
0079 OpaqueDataBlock::~OpaqueDataBlock()   {
0080   if ( pointer && (type&EXTERN_DATA) != EXTERN_DATA )  {
0081     grammar->destruct(pointer);
0082     if ( (type&ALLOC_DATA) == ALLOC_DATA ) ::operator delete(pointer);
0083   }
0084   pointer = 0;
0085   grammar = 0;
0086   InstanceCount::decrement(this);
0087 }
0088 
0089 /// Copy constructor
0090 OpaqueDataBlock& OpaqueDataBlock::operator=(const OpaqueDataBlock& c)   {
0091   if ( this != &c )  {
0092     if ( grammar == c.grammar )   {
0093       if ( pointer )  {
0094         if ( grammar ) grammar->destruct(pointer);
0095         if ( (type&ALLOC_DATA) == ALLOC_DATA ) ::operator delete(pointer);
0096       }
0097       pointer = 0;
0098       grammar = 0;
0099     }
0100     if ( grammar == 0 )  {
0101       this->OpaqueData::operator=(c);
0102       type = c.type;
0103       grammar = 0;
0104       if ( c.grammar )   {
0105         if ( !c.grammar->specialization.copy )  {
0106           except("OpaqueDataBlock","Grammar type %s does not support object copy. Operation not allowed.",
0107                  c.grammar->type_name().c_str());
0108         }
0109         bind(c.grammar);
0110         grammar->specialization.copy(pointer,c.pointer);
0111         return *this;
0112       }
0113       else if ( (c.type&EXTERN_DATA) == EXTERN_DATA )   {
0114         pointer = c.pointer;
0115         return *this;
0116       }
0117     }
0118     except("OpaqueData","You may not bind opaque data multiple times!");
0119   }
0120   return *this;
0121 }
0122 
0123 /// Bind data value
0124 void* OpaqueDataBlock::bind(const BasicGrammar* g)   {
0125   if ( (type&EXTERN_DATA) == EXTERN_DATA )  {
0126     except("OpaqueData","Extern data may not be bound!");
0127   }
0128   else if ( !grammar )  {
0129     size_t len = g->sizeOf();
0130     grammar  = g;
0131     (len > sizeof(data))
0132       ? (pointer=::operator new(len),type=ALLOC_DATA)
0133       : (pointer=data,type=PLAIN_DATA);
0134     return pointer;
0135   }
0136   else if ( grammar == g )  {
0137     // We cannot ingore secondary requests for data bindings.
0138     // This leads to memory leaks in the caller!
0139     except("OpaqueData","You may not bind opaque multiple times!");
0140   }
0141   typeinfoCheck(grammar->type(),g->type(),"Opaque data blocks may not be assigned.");
0142   return 0;
0143 }
0144 
0145 /// Bind external data value to the pointer
0146 void OpaqueDataBlock::bindExtern(void* ptr, const BasicGrammar* gr)    {
0147   if ( grammar != 0 && type != EXTERN_DATA )  {
0148     // We cannot ingore secondary requests for data bindings.
0149     // This leads to memory leaks in the caller!
0150     except("OpaqueData","You may not bind opaque data multiple times!");
0151   }
0152   pointer = ptr;
0153   grammar = gr;
0154   type    = EXTERN_DATA;
0155 }
0156 
0157 /// Set data value
0158 void* OpaqueDataBlock::bind(void* ptr, size_t size, const BasicGrammar* g)   {
0159   if ( (type&EXTERN_DATA) == EXTERN_DATA )  {
0160     except("OpaqueData","Extern data may not be bound!");
0161   }
0162   else if ( !grammar )  {
0163     size_t len = g->sizeOf();
0164     grammar = g;
0165     if ( len <= size )
0166       pointer=ptr, type=STACK_DATA;
0167     else if ( len <= sizeof(data) )
0168       pointer=data, type=PLAIN_DATA;
0169     else 
0170       pointer=::operator new(len),type=ALLOC_DATA;
0171     return pointer;
0172   }
0173   else if ( grammar == g )  {
0174     // We cannot ingore secondary requests for data bindings.
0175     // This leads to memory leaks in the caller!
0176     except("OpaqueData","You may not bind opaque data multiple times!");
0177   }
0178   typeinfoCheck(grammar->type(),g->type(),"Opaque data blocks may not be assigned.");
0179   return 0;
0180 }
0181 
0182 #include <DD4hep/GrammarUnparsed.h>
0183 static auto s_registry = GrammarRegistry::pre_note<OpaqueDataBlock>(1);