File indexing completed on 2025-03-13 08:19:36
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #include <DD4hep/Printout.h>
0016 #include <DD4hep/Primitives.h>
0017 #include <Parsers/Parsers.h>
0018 #include <DD4hep/ComponentProperties.h>
0019
0020
0021 #include <stdexcept>
0022 #include <cstring>
0023
0024 using namespace dd4hep;
0025
0026
0027 std::string Property::type(const Property& property) {
0028 return type(property.grammar().type());
0029 }
0030
0031
0032 std::string Property::type(const std::type_info& typ) {
0033 return typeName(typ);
0034 }
0035
0036
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
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
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
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
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
0083 PropertyManager::PropertyManager() {
0084 }
0085
0086
0087 PropertyManager::~PropertyManager() {
0088 m_properties.clear();
0089 }
0090
0091
0092 size_t PropertyManager::size() const {
0093 return m_properties.size();
0094 }
0095
0096
0097 void PropertyManager::adopt(const PropertyManager& copy) {
0098 m_properties = copy.m_properties;
0099 }
0100
0101
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
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
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
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
0134 Property& PropertyManager::property(const std::string& name) {
0135 return (*verifyExistence(name)).second;
0136 }
0137
0138
0139 const Property& PropertyManager::property(const std::string& name) const {
0140 return (*verifyExistence(name)).second;
0141 }
0142
0143
0144 Property& PropertyManager::operator[](const std::string& name) {
0145 return (*verifyExistence(name)).second;
0146 }
0147
0148
0149 const Property& PropertyManager::operator[](const std::string& name) const {
0150 return (*verifyExistence(name)).second;
0151 }
0152
0153
0154 void PropertyManager::add(const std::string& name, const Property& prop) {
0155 verifyNonExistence(name);
0156 m_properties.emplace(name, prop);
0157 }
0158
0159
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
0167 PropertyConfigurable::PropertyConfigurable() {
0168 }
0169
0170
0171 PropertyConfigurable::~PropertyConfigurable() {
0172 }
0173
0174
0175 bool PropertyConfigurable::hasProperty(const std::string& nam) const {
0176 return m_properties.exists(nam);
0177 }
0178
0179
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
0196 template class Grammar<Property>;
0197 static auto s_registry = GrammarRegistry::pre_note<Property>(1);
0198 }
0199