Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-03-13 08:19:36

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 include files
0015 #include <DD4hep/Printout.h>
0016 #include <DD4hep/Primitives.h>
0017 #include <Parsers/Parsers.h>
0018 #include <DD4hep/ComponentProperties.h>
0019 
0020 // C/C++ include files
0021 #include <stdexcept>
0022 #include <cstring>
0023 
0024 using namespace dd4hep;
0025 
0026 /// Property type name
0027 std::string Property::type(const Property& property) {
0028   return type(property.grammar().type());
0029 }
0030 
0031 /// Property type name
0032 std::string Property::type(const std::type_info& typ) {
0033   return typeName(typ);
0034 }
0035 
0036 /// Property type name
0037 std::string Property::type() const {
0038   return Property::type(grammar().type());
0039 }
0040 
0041 const BasicGrammar& Property::grammar() const {
0042   if ( m_hdl )
0043     return *m_hdl;
0044   throw std::runtime_error("Attempt to access property grammar from invalid object.");
0045 }
0046 
0047 /// Conversion to string value
0048 std::string Property::str() const {
0049   if ( m_hdl && m_par ) {
0050     return m_hdl->str(m_par);
0051   }
0052   throw std::runtime_error("Attempt to access property grammar from invalid object.");
0053 }
0054 
0055 /// Conversion from string value
0056 const Property& Property::str(const std::string& input)   const {
0057   if ( m_hdl && m_par )   {
0058     m_hdl->fromString(m_par,input);
0059     return *this;
0060   }
0061   throw std::runtime_error("Attempt to access property grammar from invalid object.");
0062 }
0063 
0064 /// Conversion from string value
0065 Property& Property::str(const std::string& input)    {
0066   if ( m_hdl && m_par )   {
0067     m_hdl->fromString(m_par,input);
0068     return *this;
0069   }
0070   throw std::runtime_error("Attempt to access property grammar from invalid object.");
0071 }
0072 
0073 /// Assignment operator / set new balue
0074 Property& Property::operator=(const char* val) {
0075   if ( val ) {
0076     this->set < std::string > (val);
0077     return *this;
0078   }
0079   throw std::runtime_error("Attempt to set invalid string to property!");
0080 }
0081 
0082 /// Default constructor
0083 PropertyManager::PropertyManager() {
0084 }
0085 
0086 /// Default destructor
0087 PropertyManager::~PropertyManager() {
0088   m_properties.clear();
0089 }
0090 
0091 /// Access total number of properties
0092 size_t PropertyManager::size()  const   {
0093   return m_properties.size();
0094 }
0095 
0096 /// Import properties of another instance
0097 void PropertyManager::adopt(const PropertyManager& copy)   {
0098   m_properties = copy.m_properties;
0099 }
0100 
0101 /// Check for existence
0102 bool PropertyManager::exists(const std::string& name) const   {
0103   Properties::const_iterator i = m_properties.find(name);
0104   return i != m_properties.end();
0105 }
0106 
0107 /// Verify that this property does not exist (throw exception if the name was found)
0108 void PropertyManager::verifyNonExistence(const std::string& name) const {
0109   Properties::const_iterator i = m_properties.find(name);
0110   if (i == m_properties.end())
0111     return;
0112   throw std::runtime_error("The property:" + name + " already exists for this component.");
0113 }
0114 
0115 /// Verify that this property exists (throw exception if the name was not found)
0116 PropertyManager::Properties::const_iterator
0117 PropertyManager::verifyExistence(const std::string& name) const {
0118   Properties::const_iterator i = m_properties.find(name);
0119   if (i != m_properties.end())
0120     return i;
0121   throw std::runtime_error("PropertyManager: Unknown property:" + name);
0122 }
0123 
0124 /// Verify that this property exists (throw exception if the name was not found)
0125 PropertyManager::Properties::iterator
0126 PropertyManager::verifyExistence(const std::string& name) {
0127   Properties::iterator i = m_properties.find(name);
0128   if (i != m_properties.end())
0129     return i;
0130   throw std::runtime_error("PropertyManager: Unknown property:" + name);
0131 }
0132 
0133 /// Access property by name (CONST)
0134 Property& PropertyManager::property(const std::string& name) {
0135   return (*verifyExistence(name)).second;
0136 }
0137 
0138 /// Access property by name
0139 const Property& PropertyManager::property(const std::string& name) const {
0140   return (*verifyExistence(name)).second;
0141 }
0142 
0143 /// Access property by name
0144 Property& PropertyManager::operator[](const std::string& name) {
0145   return (*verifyExistence(name)).second;
0146 }
0147 
0148 /// Access property by name
0149 const Property& PropertyManager::operator[](const std::string& name) const {
0150   return (*verifyExistence(name)).second;
0151 }
0152 
0153 /// Add a new property
0154 void PropertyManager::add(const std::string& name, const Property& prop) {
0155   verifyNonExistence(name);
0156   m_properties.emplace(name, prop);
0157 }
0158 
0159 /// Dump string values
0160 void PropertyManager::dump() const {
0161   for (const auto& i : m_properties )
0162     printout(ALWAYS, "PropertyManager", "Property %s = %s",
0163              i.first.c_str(), i.second.str().c_str());
0164 }
0165 
0166 /// Standard PropertyConfigurable constructor
0167 PropertyConfigurable::PropertyConfigurable()  {
0168 }
0169 
0170 /// Default PropertyConfigurable destructor
0171 PropertyConfigurable::~PropertyConfigurable()   {
0172 }
0173 
0174 /// Check property for existence
0175 bool PropertyConfigurable::hasProperty(const std::string& nam) const    {
0176   return m_properties.exists(nam);
0177 }
0178 
0179 /// Access single property
0180 Property& PropertyConfigurable::property(const std::string& nam)   {
0181   return properties()[nam];
0182 }
0183 
0184 #include <DD4hep/GrammarParsed.h>
0185 namespace dd4hep { 
0186   namespace Parsers {
0187     template <> int parse(Property& result, const std::string& input) {
0188       result.str(input); 
0189       return 1;
0190     }
0191     template <> std::ostream& toStream(const Property& result, std::ostream& os) {
0192       return os << result.str();
0193     }
0194   }
0195   // Ensure the grammars are registered
0196   template class Grammar<Property>;
0197   static auto s_registry = GrammarRegistry::pre_note<Property>(1);
0198 }
0199