File indexing completed on 2025-02-21 10:00:33
0001
0002
0003
0004
0005
0006
0007
0008
0009
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
0020 #include "GaudiKernel/ContainedObject.h"
0021 #include "GaudiKernel/KeyedContainer.h"
0022 #include "GaudiKernel/KeyedTraits.h"
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038 template <class KEY>
0039 class GAUDI_API KeyedObject : public ContainedObject {
0040 friend struct GaudiDict::KeyedObjectDict<KEY>;
0041
0042 public:
0043
0044 typedef KEY key_type;
0045
0046 protected:
0047
0048 typedef typename Containers::key_traits<key_type> traits;
0049
0050
0051
0052
0053 friend struct Containers::key_traits<key_type>;
0054
0055
0056
0057 key_type m_key{};
0058
0059 long m_refCount = 0;
0060
0061 bool m_hasKey = false;
0062
0063 unsigned long addRef();
0064
0065 unsigned long release();
0066
0067
0068
0069
0070 void setKey( const key_type& key );
0071
0072 public:
0073
0074 KeyedObject() = default;
0075
0076
0077
0078 KeyedObject( const key_type& kval ) : m_key( kval ), m_refCount( 0 ), m_hasKey( true ) {}
0079
0080 ~KeyedObject() override;
0081
0082 const key_type& key() const { return m_key; }
0083
0084 bool hasKey() const { return m_hasKey; }
0085 long index() const override { return traits::identifier( m_key ); }
0086
0087 StreamBuffer& serialize( StreamBuffer& s ) const override;
0088
0089 StreamBuffer& serialize( StreamBuffer& s ) override;
0090
0091 private:
0092
0093 KeyedObject( const KeyedObject& copy ) : ContainedObject( copy ), m_refCount( 0 ), m_hasKey( true ) {}
0094 };
0095
0096
0097
0098
0099
0100
0101
0102
0103
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
0114 template <class KEY>
0115 inline unsigned long KeyedObject<KEY>::addRef() {
0116 return ++m_refCount;
0117 }
0118
0119
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
0128
0129
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
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
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