Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-21 10:00:33

0001 /***********************************************************************************\
0002 * (c) Copyright 1998-2019 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_KEYEDOBJECT_H
0012 #define GAUDIKERNEL_KEYEDOBJECT_H
0013 
0014 namespace GaudiDict {
0015   template <class T>
0016   struct KeyedObjectDict;
0017 }
0018 
0019 // Framework include files
0020 #include "GaudiKernel/ContainedObject.h"
0021 #include "GaudiKernel/KeyedContainer.h"
0022 #include "GaudiKernel/KeyedTraits.h"
0023 
0024 /** Definition of the templated KeyedObject class.
0025  *
0026  *  This object, which is of the basic containedObject
0027  *  type allows to be identified by key.
0028  *
0029  *  This implementation uses a reference count mechanism
0030  *  for insertion into multiple containers; Once the
0031  *  reference count is NULL, the object will automatically
0032  *  be deleted.
0033  *
0034  *  @author   M.Frank CERN/LHCb
0035  *  @version  1.0
0036  *
0037  */
0038 template <class KEY>
0039 class GAUDI_API KeyedObject : public ContainedObject {
0040   friend struct GaudiDict::KeyedObjectDict<KEY>;
0041 
0042 public:
0043   /// Definition of the key-type to access object
0044   typedef KEY key_type;
0045 
0046 protected:
0047   /// definition of the container key traits to be made friend
0048   typedef typename Containers::key_traits<key_type> traits;
0049   /// Allow the container traits to access full object properties
0050   // #ifdef _WIN32
0051   //   friend traits;
0052   // #else
0053   friend struct Containers::key_traits<key_type>;
0054   // #endif
0055 
0056   /// Object Key
0057   key_type m_key{};
0058   /// Reference counter
0059   long m_refCount = 0;
0060   /// Boolean to indicate wether a key was already assigned.
0061   bool m_hasKey = false;
0062   /// Add reference to object (Increase reference counter).
0063   unsigned long addRef();
0064   /// Release reference. If the reference count is ZERO, delete the object.
0065   unsigned long release();
0066   /** Set object key. The key for consistency reasons
0067       can be set only once for the object. Any attempt to
0068       redefine the key results in an exception.
0069   */
0070   void setKey( const key_type& key );
0071 
0072 public:
0073   /// Standard Constructor. The object key is preset to the invalid value.
0074   KeyedObject() = default;
0075   /** Standard Constructor accepting the object's key.
0076       The key must be valid and cannot be changed later.
0077   */
0078   KeyedObject( const key_type& kval ) : m_key( kval ), m_refCount( 0 ), m_hasKey( true ) {}
0079   /// Standard destructor.
0080   ~KeyedObject() override;
0081   /// Retrieve Key of the object.
0082   const key_type& key() const { return m_key; }
0083   /// Check if the object has a key assigned or not.
0084   bool hasKey() const { return m_hasKey; } /// Distance in the parent container
0085   long index() const override { return traits::identifier( m_key ); }
0086   /// Serialize the object for writing
0087   StreamBuffer& serialize( StreamBuffer& s ) const override;
0088   /// Serialize the object for reading
0089   StreamBuffer& serialize( StreamBuffer& s ) override;
0090 
0091 private:
0092   /// NOBODY may copy these objects
0093   KeyedObject( const KeyedObject& copy ) : ContainedObject( copy ), m_refCount( 0 ), m_hasKey( true ) {}
0094 };
0095 
0096 /*
0097  *
0098  *
0099  *  Inline code for keyed KeyedObject class
0100  *
0101  */
0102 
0103 // Standard destructor.
0104 template <class KEY>
0105 inline KeyedObject<KEY>::~KeyedObject() {
0106   ObjectContainerBase* p = const_cast<ObjectContainerBase*>( parent() );
0107   if ( p ) {
0108     setParent( nullptr );
0109     p->remove( this );
0110   }
0111 }
0112 
0113 // Add reference to object (Increase reference counter).
0114 template <class KEY>
0115 inline unsigned long KeyedObject<KEY>::addRef() {
0116   return ++m_refCount;
0117 }
0118 
0119 // Release reference. If the reference count is ZERO, delete the object.
0120 template <class KEY>
0121 inline unsigned long KeyedObject<KEY>::release() {
0122   long cnt = --m_refCount;
0123   if ( cnt <= 0 ) delete this;
0124   return cnt;
0125 }
0126 
0127 /*  Set object key. The key for consistency reasons
0128     can be set only once for the object. Any attempt to
0129     redefine the key results in an exception.
0130 */
0131 template <class KEY>
0132 inline void KeyedObject<KEY>::setKey( const key_type& key ) {
0133   if ( !m_hasKey ) {
0134     m_key    = key;
0135     m_hasKey = true;
0136     return;
0137   }
0138   Containers::cannotAssignObjectKey();
0139 }
0140 
0141 // Serialize the object for writing
0142 template <class KEY>
0143 inline StreamBuffer& KeyedObject<KEY>::serialize( StreamBuffer& s ) const {
0144   return ContainedObject::serialize( s ) << traits::identifier( m_key );
0145 }
0146 
0147 // Serialize the object for reading
0148 template <class KEY>
0149 inline StreamBuffer& KeyedObject<KEY>::serialize( StreamBuffer& s ) {
0150   long k;
0151   ContainedObject::serialize( s ) >> k;
0152   m_key    = traits::makeKey( k );
0153   m_hasKey = true;
0154   return s;
0155 }
0156 #endif // GAUDIKERNEL_KEYEDOBJECT_H