Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-22 10:32:44

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 #ifndef DD4HEP_CONDITIONANY_H
0014 #define DD4HEP_CONDITIONANY_H
0015 
0016 // Framework include files
0017 #include <DD4hep/Conditions.h>
0018 
0019 // C/C++ include files
0020 #include <any>
0021 
0022 /// Namespace for the AIDA detector description toolkit
0023 namespace dd4hep {
0024 
0025   /// Conditions internal namespace
0026   namespace detail  {
0027     class ConditionObject;
0028   }
0029 
0030   /// Main condition object handle.
0031   /**
0032    *  Convenience handle for specialized Condition objects
0033    *  with a concrete payload of std::any.
0034    *
0035    *  Note:
0036    *  Conditions may be shared between several DetElement objects.
0037    *
0038    *  \author  M.Frank
0039    *  \version 1.0
0040    *  \ingroup DD4HEP_CONDITIONS
0041    */
0042   class ConditionAny : public Handle<detail::ConditionObject> {
0043   public:
0044     /// Forward definition of the key type
0045     using key_type     = Condition::key_type;
0046     /// High part of the key identifies the detector element
0047     using detkey_type  = Condition::detkey_type;
0048     /// Low part of the key identifies the item identifier
0049     using itemkey_type = Condition::itemkey_type;
0050     /// Forward definition of the object properties
0051     using mask_type    = Condition::mask_type;
0052 
0053   private:
0054     /// Verify that underlying data are either invalid of contain an instance of std::any.
0055     void use_data(detail::ConditionObject* obj);
0056 
0057   public:
0058 
0059     /// Default constructor
0060     ConditionAny() = default;
0061     /// Move constructor
0062     ConditionAny(ConditionAny&& c) = default;
0063     /// Move constructor from Condition
0064     ConditionAny(Condition&& c);
0065     /// Copy constructor
0066     ConditionAny(const ConditionAny& c) = default;
0067     /// Copy constructor from Condition
0068     ConditionAny(const Condition& c);
0069     /// Initializing constructor
0070     ConditionAny(Object* p);
0071     /// Constructor from another handle
0072     template <typename Q> ConditionAny(const Handle<Q>& e);
0073     /// Initializing constructor for a initialized std::any payload
0074     ConditionAny(key_type hash_key);
0075     /// Initializing constructor for a initialized and filled std::any payload
0076     template <typename PAYLOAD> ConditionAny(key_type hash_key, PAYLOAD&& data);
0077     /// Initializing constructor for a initialized std::any payload
0078     ConditionAny(const std::string& name, const std::string& type);
0079     /// Initializing constructor for a initialized and filled std::any payload
0080     template <typename PAYLOAD> ConditionAny(const std::string& name, const std::string& type, PAYLOAD&& data);
0081 
0082     /// Assignment move operator
0083     template <typename Q> ConditionAny& operator=(Handle<Q>&& c);
0084     /// Assignment copy operator
0085     template <typename Q> ConditionAny& operator=(const Handle<Q>& c);
0086     /// Assignment move operator
0087     ConditionAny& operator=(Condition&& c);
0088     /// Assignment copy operator
0089     ConditionAny& operator=(const Condition& c);
0090     /// Assignment move operator
0091     ConditionAny& operator=(ConditionAny&& c) = default;
0092     /// Assignment copy operator
0093     ConditionAny& operator=(const ConditionAny& c) = default;
0094 
0095     /** Interval of validity            */
0096     /// Access the IOV type
0097     const IOVType& iovType()  const;
0098     /// Access the IOV block
0099     const IOV& iov()  const;
0100 
0101     /** Conditions identification using integer keys.   */
0102     /// Hash identifier
0103     key_type key()  const;
0104     /// DetElement part of the identifier
0105     detkey_type  detector_key()  const;
0106     /// Item part of the identifier
0107     itemkey_type item_key()  const;
0108 
0109     /// Flag operations: Get condition flags
0110     mask_type flags()  const;
0111     /// Flag operations: Set a conditons flag
0112     void setFlag(mask_type option);
0113     /// Flag operations: UN-Set a conditons flag
0114     void unFlag(mask_type option);
0115     /// Flag operations: Test for a given a conditons flag
0116     bool testFlag(mask_type option) const;
0117 
0118     /// Generic getter. Specify the exact type, not a polymorph type
0119     std::any& get();
0120     /// Generic getter (const version). Specify the exact type, not a polymorph type
0121     const std::any& get() const;
0122 
0123     /// Checks whether the object contains a value
0124     bool has_value()   const;
0125     /// Access to the type information
0126     const std::type_info& any_type() const;
0127     /// Access to the type information as string
0128     const std::string     any_type_name() const;
0129     /// Access the contained object inside std::any
0130     template <typename T> T& as();
0131     /// Access the contained object inside std::any
0132     template <typename T> const T& as() const;
0133     /// Access a copy of the contained object inside std::any
0134     template <typename T> T value();
0135     /// Access the contained object inside std::any
0136     template <typename T> T* pointer();
0137     /// Access the contained object inside std::any
0138     template <typename T> const T* pointer() const;
0139   };
0140 
0141   /// Move constructor from Condition
0142   inline ConditionAny::ConditionAny(Condition&& c) : Handle<detail::ConditionObject>() {
0143     this->use_data(c.ptr());
0144   }
0145 
0146   /// Copy constructor from Condition
0147   inline ConditionAny::ConditionAny(const Condition& c) : Handle<detail::ConditionObject>()  {
0148     this->use_data(c.ptr());
0149   }
0150 
0151   /// Construct conditions object and bind the data
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   /// Construct conditions object and bind the payload
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   /// Assignment move operator
0168   template <typename Q> inline ConditionAny& ConditionAny::operator=(Handle<Q>&& c)  {
0169     this->use_data(c.ptr());
0170     return *this;
0171   }
0172 
0173   /// Assignment copy operator
0174   template <typename Q> inline ConditionAny& ConditionAny::operator=(const Handle<Q>& c)   {
0175     this->use_data(c.ptr());
0176     return *this;
0177   }
0178 
0179   /// Assignment move operator
0180   inline ConditionAny& ConditionAny::operator=(Condition&& c)  {
0181     this->use_data(c.ptr());
0182     return *this;
0183   }
0184 
0185   /// Assignment copy operator
0186   inline ConditionAny& ConditionAny::operator=(const Condition& c)   {
0187     this->use_data(c.ptr());
0188     return *this;
0189   }
0190 
0191   /// Access the contained object inside std::any
0192   template <typename T> inline T ConditionAny::value() {
0193     return std::any_cast<T>(this->get());
0194   }
0195 
0196   /// Access the contained object inside std::any
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   /// Access the contained object inside std::any
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   /// Access the contained object inside std::any
0211   template <typename T> inline T* ConditionAny::pointer() {
0212     return isValid() ? std::any_cast<T>(&this->get()) : nullptr;
0213   }
0214 
0215   /// Access the contained object inside std::any
0216   template <typename T> inline const T* ConditionAny::pointer() const {
0217     return isValid() ? std::any_cast<const T>(&this->get()) : nullptr;
0218   }
0219 
0220 }          /* End namespace dd4hep                   */
0221 #endif // DD4HEP_CONDITIONANY_H