The Gaudi Framework  master (37c0b60a)
KeyedObject.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #ifndef GAUDIKERNEL_KEYEDOBJECT_H
12 #define GAUDIKERNEL_KEYEDOBJECT_H
13 
14 namespace GaudiDict {
15  template <class T>
17 }
18 
19 // Framework include files
23 
38 template <class KEY>
40  friend struct GaudiDict::KeyedObjectDict<KEY>;
41 
42 public:
44  typedef KEY key_type;
45 
46 protected:
50  // #ifdef _WIN32
51  // friend traits;
52  // #else
53  friend struct Containers::key_traits<key_type>;
54  // #endif
55 
57  key_type m_key{};
59  long m_refCount = 0;
61  bool m_hasKey = false;
63  unsigned long addRef();
65  unsigned long release();
70  void setKey( const key_type& key );
71 
72 public:
74  KeyedObject() = default;
78  KeyedObject( const key_type& kval ) : m_key( kval ), m_refCount( 0 ), m_hasKey( true ) {}
80  ~KeyedObject() override;
82  const key_type& key() const { return m_key; }
84  bool hasKey() const { return m_hasKey; }
85  long index() const override { return traits::identifier( m_key ); }
87  StreamBuffer& serialize( StreamBuffer& s ) const override;
90 
91 private:
93  KeyedObject( const KeyedObject& copy ) : ContainedObject( copy ), m_refCount( 0 ), m_hasKey( true ) {}
94 };
95 
96 /*
97  *
98  *
99  * Inline code for keyed KeyedObject class
100  *
101  */
102 
103 // Standard destructor.
104 template <class KEY>
106  ObjectContainerBase* p = const_cast<ObjectContainerBase*>( parent() );
107  if ( p ) {
108  setParent( nullptr );
109  p->remove( this );
110  }
111 }
112 
113 // Add reference to object (Increase reference counter).
114 template <class KEY>
115 inline unsigned long KeyedObject<KEY>::addRef() {
116  return ++m_refCount;
117 }
118 
119 // Release reference. If the reference count is ZERO, delete the object.
120 template <class KEY>
121 inline unsigned long KeyedObject<KEY>::release() {
122  long cnt = --m_refCount;
123  if ( cnt <= 0 ) delete this;
124  return cnt;
125 }
126 
127 /* Set object key. The key for consistency reasons
128  can be set only once for the object. Any attempt to
129  redefine the key results in an exception.
130 */
131 template <class KEY>
132 inline void KeyedObject<KEY>::setKey( const key_type& key ) {
133  if ( !m_hasKey ) {
134  m_key = key;
135  m_hasKey = true;
136  return;
137  }
139 }
140 
141 // Serialize the object for writing
142 template <class KEY>
144  return ContainedObject::serialize( s ) << traits::identifier( m_key );
145 }
146 
147 // Serialize the object for reading
148 template <class KEY>
150  long k;
152  m_key = traits::makeKey( k );
153  m_hasKey = true;
154  return s;
155 }
156 #endif // GAUDIKERNEL_KEYEDOBJECT_H
KeyedObject::addRef
unsigned long addRef()
Add reference to object (Increase reference counter).
Definition: KeyedObject.h:115
KeyedObject::index
long index() const override
Distance in the parent container.
Definition: KeyedObject.h:85
KeyedObject::setKey
void setKey(const key_type &key)
Set object key.
Definition: KeyedObject.h:132
KeyedObject::serialize
StreamBuffer & serialize(StreamBuffer &s) const override
Serialize the object for writing.
Definition: KeyedObject.h:143
KeyedObject::KeyedObject
KeyedObject()=default
Standard Constructor. The object key is preset to the invalid value.
gaudirun.s
string s
Definition: gaudirun.py:346
KeyedContainer.h
StreamBuffer
Definition: StreamBuffer.h:52
ObjectContainerBase::remove
virtual long remove(ContainedObject *value)=0
Release object from the container (the pointer will be removed from the container,...
KeyedObject::KeyedObject
KeyedObject(const key_type &kval)
Standard Constructor accepting the object's key.
Definition: KeyedObject.h:78
KeyedTraits.h
Containers::key_traits
Key traits class.
Definition: KeyedTraits.h:47
KeyedObject::traits
Containers::key_traits< key_type > traits
definition of the container key traits to be made friend
Definition: KeyedObject.h:48
KeyedObject
Definition of the templated KeyedObject class.
Definition: KeyedObject.h:39
KeyedObject::hasKey
bool hasKey() const
Check if the object has a key assigned or not.
Definition: KeyedObject.h:84
KeyedObject::key_type
KEY key_type
Definition of the key-type to access object.
Definition: KeyedObject.h:44
KeyedObject::KeyedObject
KeyedObject(const KeyedObject &copy)
NOBODY may copy these objects.
Definition: KeyedObject.h:93
ObjectContainerBase
Definition: ObjectContainerBase.h:29
GaudiDict
Definition: KeyedContainer.h:18
KeyedObject::serialize
StreamBuffer & serialize(StreamBuffer &s) override
Serialize the object for reading.
Definition: KeyedObject.h:149
KeyedObject::key
const key_type & key() const
Retrieve Key of the object.
Definition: KeyedObject.h:82
Containers::cannotAssignObjectKey
GAUDI_API void cannotAssignObjectKey()
Function to be called when an object key cannot be assigned.
Definition: KeyedObjectManager.cpp:79
KeyedObject::~KeyedObject
~KeyedObject() override
Standard destructor.
Definition: KeyedObject.h:105
KeyedObject::release
unsigned long release()
Release reference. If the reference count is ZERO, delete the object.
Definition: KeyedObject.h:121
ProduceConsume.key
key
Definition: ProduceConsume.py:84
GaudiDict::KeyedObjectDict
Definition: KeyedObject.h:16
ContainedObject.h
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:81
ContainedObject
Definition: ContainedObject.h:41
ContainedObject::serialize
virtual StreamBuffer & serialize(StreamBuffer &s) const
Serialize the object for writing.
Definition: ContainedObject.h:70