File indexing completed on 2025-02-22 10:32:44
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #ifndef DD4HEP_CONDITIONANY_H
0014 #define DD4HEP_CONDITIONANY_H
0015
0016
0017 #include <DD4hep/Conditions.h>
0018
0019
0020 #include <any>
0021
0022
0023 namespace dd4hep {
0024
0025
0026 namespace detail {
0027 class ConditionObject;
0028 }
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042 class ConditionAny : public Handle<detail::ConditionObject> {
0043 public:
0044
0045 using key_type = Condition::key_type;
0046
0047 using detkey_type = Condition::detkey_type;
0048
0049 using itemkey_type = Condition::itemkey_type;
0050
0051 using mask_type = Condition::mask_type;
0052
0053 private:
0054
0055 void use_data(detail::ConditionObject* obj);
0056
0057 public:
0058
0059
0060 ConditionAny() = default;
0061
0062 ConditionAny(ConditionAny&& c) = default;
0063
0064 ConditionAny(Condition&& c);
0065
0066 ConditionAny(const ConditionAny& c) = default;
0067
0068 ConditionAny(const Condition& c);
0069
0070 ConditionAny(Object* p);
0071
0072 template <typename Q> ConditionAny(const Handle<Q>& e);
0073
0074 ConditionAny(key_type hash_key);
0075
0076 template <typename PAYLOAD> ConditionAny(key_type hash_key, PAYLOAD&& data);
0077
0078 ConditionAny(const std::string& name, const std::string& type);
0079
0080 template <typename PAYLOAD> ConditionAny(const std::string& name, const std::string& type, PAYLOAD&& data);
0081
0082
0083 template <typename Q> ConditionAny& operator=(Handle<Q>&& c);
0084
0085 template <typename Q> ConditionAny& operator=(const Handle<Q>& c);
0086
0087 ConditionAny& operator=(Condition&& c);
0088
0089 ConditionAny& operator=(const Condition& c);
0090
0091 ConditionAny& operator=(ConditionAny&& c) = default;
0092
0093 ConditionAny& operator=(const ConditionAny& c) = default;
0094
0095
0096
0097 const IOVType& iovType() const;
0098
0099 const IOV& iov() const;
0100
0101
0102
0103 key_type key() const;
0104
0105 detkey_type detector_key() const;
0106
0107 itemkey_type item_key() const;
0108
0109
0110 mask_type flags() const;
0111
0112 void setFlag(mask_type option);
0113
0114 void unFlag(mask_type option);
0115
0116 bool testFlag(mask_type option) const;
0117
0118
0119 std::any& get();
0120
0121 const std::any& get() const;
0122
0123
0124 bool has_value() const;
0125
0126 const std::type_info& any_type() const;
0127
0128 const std::string any_type_name() const;
0129
0130 template <typename T> T& as();
0131
0132 template <typename T> const T& as() const;
0133
0134 template <typename T> T value();
0135
0136 template <typename T> T* pointer();
0137
0138 template <typename T> const T* pointer() const;
0139 };
0140
0141
0142 inline ConditionAny::ConditionAny(Condition&& c) : Handle<detail::ConditionObject>() {
0143 this->use_data(c.ptr());
0144 }
0145
0146
0147 inline ConditionAny::ConditionAny(const Condition& c) : Handle<detail::ConditionObject>() {
0148 this->use_data(c.ptr());
0149 }
0150
0151
0152 template <typename PAYLOAD> inline
0153 ConditionAny::ConditionAny(key_type hash_key, PAYLOAD&& payload) {
0154 ConditionAny c(hash_key);
0155 c.get() = std::move(payload);
0156 this->m_element = c.ptr();
0157 }
0158
0159
0160 template <typename PAYLOAD> inline
0161 ConditionAny::ConditionAny(const std::string& name, const std::string& type, PAYLOAD&& payload) {
0162 ConditionAny c(name, type);
0163 c.get() = std::move(payload);
0164 this->m_element = c.ptr();
0165 }
0166
0167
0168 template <typename Q> inline ConditionAny& ConditionAny::operator=(Handle<Q>&& c) {
0169 this->use_data(c.ptr());
0170 return *this;
0171 }
0172
0173
0174 template <typename Q> inline ConditionAny& ConditionAny::operator=(const Handle<Q>& c) {
0175 this->use_data(c.ptr());
0176 return *this;
0177 }
0178
0179
0180 inline ConditionAny& ConditionAny::operator=(Condition&& c) {
0181 this->use_data(c.ptr());
0182 return *this;
0183 }
0184
0185
0186 inline ConditionAny& ConditionAny::operator=(const Condition& c) {
0187 this->use_data(c.ptr());
0188 return *this;
0189 }
0190
0191
0192 template <typename T> inline T ConditionAny::value() {
0193 return std::any_cast<T>(this->get());
0194 }
0195
0196
0197 template <typename T> inline T& ConditionAny::as() {
0198 T* ptr_payload = std::any_cast<T>(&this->get());
0199 if ( ptr_payload ) return *ptr_payload;
0200 throw std::runtime_error("ConditionAny: Cannot access value of std::any as a reference to "+typeName(typeid(T)));
0201 }
0202
0203
0204 template <typename T> inline const T& ConditionAny::as() const {
0205 const T* ptr_payload = std::any_cast<T>(&this->get());
0206 if ( ptr_payload ) return *ptr_payload;
0207 throw std::runtime_error("ConditionAny: Cannot access value of std::any as a reference to "+typeName(typeid(T)));
0208 }
0209
0210
0211 template <typename T> inline T* ConditionAny::pointer() {
0212 return isValid() ? std::any_cast<T>(&this->get()) : nullptr;
0213 }
0214
0215
0216 template <typename T> inline const T* ConditionAny::pointer() const {
0217 return isValid() ? std::any_cast<const T>(&this->get()) : nullptr;
0218 }
0219
0220 }
0221 #endif