Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-15 10:14:18

0001 /***********************************************************************************\
0002 * (c) Copyright 1998-2025 CERN for the benefit of the LHCb and ATLAS collaborations *
0003 *                                                                                   *
0004 * This software is distributed under the terms of the Apache version 2 licence,     *
0005 * copied verbatim in the file "LICENSE".                                            *
0006 *                                                                                   *
0007 * In applying this licence, CERN does not waive the privileges and immunities       *
0008 * granted to it by virtue of its status as an Intergovernmental Organization        *
0009 * or submit itself to any jurisdiction.                                             *
0010 \***********************************************************************************/
0011 #ifndef GAUDIKERNEL_DATAOBJID
0012 #define GAUDIKERNEL_DATAOBJID 1
0013 
0014 #include <GaudiKernel/ClassID.h>
0015 #include <GaudiKernel/StatusCode.h>
0016 
0017 #include <iostream>
0018 #include <memory>
0019 #include <mutex>
0020 #include <string>
0021 #include <unordered_set>
0022 
0023 //---------------------------------------------------------------------------
0024 
0025 /** DataObjID.h GaudiKernel/DataObjID.h
0026  *
0027  * Class used to identify an object in the Data Store, with fast lookup
0028  * using an hash
0029  *
0030  * Objects are identified via either a Gaudi style '/path/to/object' key,
0031  * or ATLAS style (ClassID, 'key') of ('ClassName', 'key')
0032  *
0033  * depending on the style, the hash is either a std::hash<string> of the path,
0034  * or a combination of the std::hash<string> of the key and the ClassID
0035  *
0036  * Collections of DataObjIDs are std::unordered_set<DataObjID> with a provided
0037  * hash function DataObjID_Hasher
0038  *
0039  * @author Charles Leggett
0040  * @date   2015-09-01
0041  */
0042 
0043 //---------------------------------------------------------------------------
0044 
0045 struct DataObjID_Hasher;
0046 class IClassIDSvc;
0047 
0048 class DataObjID {
0049 public:
0050   friend DataObjID_Hasher;
0051 
0052   DataObjID() = default;
0053   DataObjID( const DataObjID& other )
0054       : m_clid( other.m_clid ), m_hash( other.m_hash ), m_key( other.m_key ), m_className( other.m_className ) {}
0055 
0056   DataObjID( std::string key );
0057   DataObjID( const CLID& clid, std::string key );
0058   DataObjID( std::string className, std::string key );
0059 
0060   DataObjID& operator=( const DataObjID& other ) {
0061     m_clid      = other.m_clid;
0062     m_hash      = other.m_hash;
0063     m_key       = other.m_key;
0064     m_className = other.m_className;
0065     return *this;
0066   }
0067 
0068   /// only return the last part of the key
0069   const std::string& key() const { return m_key; }
0070 
0071   /// return the ClassName (if available)
0072   const std::string& className() const;
0073 
0074   /// combination of the key and the ClassName, mostly for debugging
0075   std::string fullKey() const;
0076 
0077   CLID        clid() const { return m_clid; }
0078   std::size_t hash() const { return m_hash; }
0079 
0080   void updateKey( std::string key );
0081 
0082   friend bool operator<( const DataObjID& lhs, const DataObjID& rhs ) { return lhs.m_hash < rhs.m_hash; }
0083   friend bool operator==( const DataObjID& lhs, const DataObjID& rhs ) { return lhs.m_hash == rhs.m_hash; }
0084   friend bool operator!=( const DataObjID& lhs, const DataObjID& rhs ) { return !( lhs == rhs ); }
0085 
0086   friend StatusCode    parse( DataObjID& dest, std::string_view src );
0087   friend std::ostream& toStream( const DataObjID& v, std::ostream& o );
0088   friend std::ostream& operator<<( std::ostream& os, const DataObjID& d ) { return toStream( d, os ); }
0089 
0090 private:
0091   void hashGen();
0092   void setClid();
0093 
0094   CLID        m_clid{ 0 };
0095   std::size_t m_hash{ 0 };
0096 
0097   std::string            m_key{ "INVALID" };
0098   mutable std::string    m_className;
0099   mutable std::once_flag m_setClassName;
0100 };
0101 
0102 inline DataObjID::DataObjID( std::string key ) : m_key( std::move( key ) ) { hashGen(); }
0103 
0104 inline DataObjID::DataObjID( const CLID& clid, std::string key ) : m_clid( clid ), m_key( std::move( key ) ) {
0105   hashGen();
0106 }
0107 
0108 inline DataObjID::DataObjID( std::string className, std::string key )
0109     : m_key( std::move( key ) ), m_className( std::move( className ) ) {
0110   setClid();
0111   hashGen();
0112 }
0113 
0114 inline void DataObjID::updateKey( std::string key ) {
0115   m_key = std::move( key );
0116   hashGen();
0117 }
0118 
0119 struct DataObjID_Hasher {
0120   std::size_t operator()( const DataObjID& k ) const { return k.m_hash; }
0121 };
0122 
0123 using DataObjIDColl   = std::unordered_set<DataObjID, DataObjID_Hasher>;
0124 using DataObjIDVector = std::vector<DataObjID>;
0125 
0126 namespace Gaudi {
0127   namespace Details {
0128     namespace Property {
0129       template <typename T>
0130       struct StringConverter;
0131 
0132       template <>
0133       struct StringConverter<DataObjIDColl> {
0134         std::string   toString( const DataObjIDColl& v );
0135         DataObjIDColl fromString( const DataObjIDColl&, const std::string& );
0136       };
0137 
0138       template <>
0139       struct StringConverter<DataObjIDVector> {
0140         std::string     toString( const DataObjIDVector& v );
0141         DataObjIDVector fromString( const DataObjIDVector&, const std::string& );
0142       };
0143     } // namespace Property
0144   }   // namespace Details
0145 } // namespace Gaudi
0146 
0147 #endif