File indexing completed on 2025-01-18 09:55:20
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #ifndef DDDIGI_DIGIACTION_H
0014 #define DDDIGI_DIGIACTION_H
0015
0016
0017 #include <DD4hep/Printout.h>
0018 #include <DD4hep/ExtensionEntry.h>
0019 #include <DD4hep/ComponentProperties.h>
0020 #include <DDDigi/DigiContext.h>
0021
0022
0023 #include <string>
0024 #include <memory>
0025 #include <cstdarg>
0026 #include <cstdint>
0027
0028 #if defined(G__ROOT) || defined(__CLING__) || defined(__ROOTCLING__)
0029 #define DDDIGI_DEFINE_ACTION_DEFAULT_CTOR(action) public: action() = default;
0030 #else
0031 #define DDDIGI_DEFINE_ACTION_DEFAULT_CTOR(action) protected: action() = delete;
0032 #endif
0033
0034
0035
0036
0037
0038
0039 #define DDDIGI_DEFINE_ACTION_CONSTRUCTORS(action) \
0040 DDDIGI_DEFINE_ACTION_DEFAULT_CTOR(action) \
0041 protected: \
0042 action(action&& copy) = delete; \
0043 action(const action& copy) = delete; \
0044 action& operator=(action&& copy) = delete; \
0045 action& operator=(const action& copy) = delete
0046
0047
0048 namespace dd4hep {
0049
0050
0051 class ExtensionEntry;
0052
0053
0054 namespace digi {
0055
0056
0057
0058
0059
0060
0061
0062 class TypeName {
0063 public:
0064 std::string first;
0065 std::string second;
0066
0067 TypeName() = default;
0068
0069 TypeName(const TypeName& copy) = default;
0070
0071 TypeName(const std::pair<std::string, std::string>& c)
0072 : first(c.first), second(c.second) { }
0073
0074 TypeName(const std::string& typ, const std::string& nam)
0075 : first(typ), second(nam) { }
0076
0077 TypeName& operator=(const TypeName& copy) = default;
0078
0079 static TypeName split(const std::string& type_name);
0080
0081 static TypeName split(const std::string& type_name, const std::string& delim);
0082 };
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093 class DigiAction {
0094
0095 friend class DigiKernel;
0096
0097 public:
0098 using context_t = DigiContext;
0099 using kernel_t = DigiKernel;
0100 using extensions_t = std::map<uint64_t, std::unique_ptr<ExtensionEntry> >;
0101
0102 protected:
0103 template <typename T> struct Extension : public ExtensionEntry {
0104 T* ptr = 0;
0105
0106
0107 Extension() = delete;
0108
0109 Extension(T* p) : ptr(p) { }
0110
0111 Extension(const Extension& copy) = delete;
0112
0113 Extension& operator=(const Extension& copy) = delete;
0114
0115 Extension(Extension&& copy) = delete;
0116
0117 Extension& operator=(Extension&& copy) = delete;
0118
0119 virtual ~Extension() = default;
0120
0121 virtual void* object() const override { return ptr; }
0122
0123 virtual void destruct() const override { delete ptr; }
0124 virtual void* copy(void*) const override { invalidCall("copy"); return nullptr; }
0125 virtual ExtensionEntry* clone(void*) const override { invalidCall("clone"); return nullptr; }
0126 virtual unsigned long long int hash64() const override { return uint64_t(ptr); }
0127 };
0128
0129
0130
0131 #if defined(G__ROOT) || defined(__CLING__) || defined(__ROOTCLING__)
0132 const kernel_t* m_kernel;
0133
0134 public:
0135 const kernel_t* kernel() const {
0136 return m_kernel;
0137 }
0138
0139 protected:
0140 #else
0141 const kernel_t& m_kernel;
0142 #endif
0143
0144 std::string m_name;
0145
0146 PropertyManager m_properties;
0147
0148 extensions_t m_extensions;
0149
0150 long m_refCount = 1;
0151
0152 int m_outputLevel = 3;
0153
0154 protected:
0155
0156 DDDIGI_DEFINE_ACTION_CONSTRUCTORS(DigiAction);
0157
0158
0159 virtual ~DigiAction();
0160
0161 public:
0162
0163 DigiAction(const kernel_t& kernel, const std::string& nam);
0164
0165 long addRef();
0166
0167 long release();
0168
0169 const std::string& name() const {
0170 return m_name;
0171 }
0172
0173 const char* c_name() const {
0174 return m_name.c_str();
0175 }
0176
0177 void setName(const std::string& new_name) {
0178 m_name = new_name;
0179 }
0180
0181 PropertyManager& properties() {
0182 return m_properties;
0183 }
0184
0185 const PropertyManager& properties() const {
0186 return m_properties;
0187 }
0188
0189 PrintLevel outputLevel() const {
0190 return (PrintLevel)m_outputLevel;
0191 }
0192
0193 PrintLevel setOutputLevel(PrintLevel new_level);
0194
0195
0196
0197 template <typename T> DigiAction& declareProperty(const std::string& nam, T& val);
0198
0199 template <typename T> DigiAction& declareProperty(const char* nam, T& val);
0200
0201 template <typename T> DigiAction& addProperty(const std::string& nam, T& val);
0202
0203 template <typename T> DigiAction& addProperty(const char* nam, T& val);
0204
0205 bool hasProperty(const std::string& name) const;
0206
0207 Property& property(const std::string& name);
0208
0209 const Property& property(const std::string& name) const;
0210
0211 virtual void printProperties() const;
0212
0213
0214 virtual void adopt_property(DigiAction* action, const std::string& foreign_name, const std::string& local_name);
0215
0216
0217 virtual void adopt_tool(DigiAction* action, const std::string& typ);
0218
0219
0220 uint64_t addExtension(uint64_t key, std::unique_ptr<ExtensionEntry>&& e);
0221
0222
0223 void* extension(uint64_t key);
0224
0225
0226 template <typename T> uint64_t addExtension(T* c) {
0227 std::unique_ptr<ExtensionEntry> e = std::make_unique<Extension<T> >(c);
0228 return this->addExtension(uint64_t(c), std::move(e));
0229 }
0230
0231
0232
0233 void print(const char* fmt, ...) const;
0234
0235 std::string format(const char* fmt, ...) const;
0236
0237 void always(const char* fmt, ...) const;
0238
0239 void debug(const char* fmt, ...) const;
0240
0241 void info(const char* fmt, ...) const;
0242
0243 void warning(const char* fmt, ...) const;
0244
0245 void error(const char* fmt, ...) const;
0246
0247 bool return_error(bool return_value, const char* fmt, ...) const;
0248
0249 void fatal(const char* fmt, ...) const;
0250
0251 void except(const char* fmt, ...) const;
0252 };
0253
0254
0255 template <typename T> DigiAction& DigiAction::declareProperty(const std::string& nam, T& val) {
0256 this->m_properties.add(nam, val);
0257 return *this;
0258 }
0259
0260
0261 template <typename T> DigiAction& DigiAction::declareProperty(const char* nam, T& val) {
0262 this->m_properties.add(nam, val);
0263 return *this;
0264 }
0265
0266 template <typename T> DigiAction& DigiAction::addProperty(const std::string& nam, T& val) {
0267 void* ptr = ::operator new(sizeof(T));
0268 T* prop = new(ptr) T(val);
0269 this->m_properties.add(nam, *prop);
0270 this->addExtension(prop);
0271 return *this;
0272 }
0273
0274
0275 template <typename T> DigiAction& DigiAction::addProperty(const char* nam, T& val) {
0276 void* ptr = ::operator new(sizeof(T));
0277 T* prop = new(ptr) T(val);
0278 this->m_properties.add(nam, *prop);
0279 this->addExtension(prop);
0280 return *this;
0281 }
0282 }
0283 }
0284
0285 #endif