The Gaudi Framework  v40r0 (475e45c1)
KeyedObject.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2025 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 #pragma once
12 
16 
17 namespace GaudiDict {
18  template <class T>
20 }
21 
36 template <class KEY>
38  friend struct GaudiDict::KeyedObjectDict<KEY>;
39 
40 public:
42  typedef KEY key_type;
43 
44 protected:
48  // #ifdef _WIN32
49  // friend traits;
50  // #else
51  friend struct Containers::key_traits<key_type>;
52  // #endif
53 
55  key_type m_key{};
57  long m_refCount = 0;
59  bool m_hasKey = false;
61  unsigned long addRef();
63  unsigned long release();
68  void setKey( const key_type& key );
69 
70 public:
72  KeyedObject() = default;
76  KeyedObject( const key_type& kval ) : m_key( kval ), m_refCount( 0 ), m_hasKey( true ) {}
78  ~KeyedObject() override;
80  const key_type& key() const { return m_key; }
82  bool hasKey() const { return m_hasKey; }
83  long index() const override { return traits::identifier( m_key ); }
85  StreamBuffer& serialize( StreamBuffer& s ) const override;
88 
89 private:
91  KeyedObject( const KeyedObject& copy ) : ContainedObject( copy ), m_refCount( 0 ), m_hasKey( true ) {}
92 };
93 
94 /*
95  *
96  *
97  * Inline code for keyed KeyedObject class
98  *
99  */
100 
101 // Standard destructor.
102 template <class KEY>
104  ObjectContainerBase* p = const_cast<ObjectContainerBase*>( parent() );
105  if ( p ) {
106  setParent( nullptr );
107  p->remove( this );
108  }
109 }
110 
111 // Add reference to object (Increase reference counter).
112 template <class KEY>
113 inline unsigned long KeyedObject<KEY>::addRef() {
114  return ++m_refCount;
115 }
116 
117 // Release reference. If the reference count is ZERO, delete the object.
118 template <class KEY>
119 inline unsigned long KeyedObject<KEY>::release() {
120  long cnt = --m_refCount;
121  if ( cnt <= 0 ) delete this;
122  return cnt;
123 }
124 
125 /* Set object key. The key for consistency reasons
126  can be set only once for the object. Any attempt to
127  redefine the key results in an exception.
128 */
129 template <class KEY>
130 inline void KeyedObject<KEY>::setKey( const key_type& key ) {
131  if ( !m_hasKey ) {
132  m_key = key;
133  m_hasKey = true;
134  return;
135  }
137 }
138 
139 // Serialize the object for writing
140 template <class KEY>
142  return ContainedObject::serialize( s ) << traits::identifier( m_key );
143 }
144 
145 // Serialize the object for reading
146 template <class KEY>
148  long k;
150  m_key = traits::makeKey( k );
151  m_hasKey = true;
152  return s;
153 }
KeyedObject::addRef
unsigned long addRef()
Add reference to object (Increase reference counter).
Definition: KeyedObject.h:113
KeyedObject::index
long index() const override
Distance in the parent container.
Definition: KeyedObject.h:83
KeyedObject::setKey
void setKey(const key_type &key)
Set object key.
Definition: KeyedObject.h:130
KeyedObject::serialize
StreamBuffer & serialize(StreamBuffer &s) const override
Serialize the object for writing.
Definition: KeyedObject.h:141
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:48
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:76
KeyedTraits.h
Containers::key_traits
Key traits class.
Definition: KeyedTraits.h:42
KeyedObject::traits
Containers::key_traits< key_type > traits
definition of the container key traits to be made friend
Definition: KeyedObject.h:46
KeyedObject
Definition of the templated KeyedObject class.
Definition: KeyedObject.h:37
KeyedObject::hasKey
bool hasKey() const
Check if the object has a key assigned or not.
Definition: KeyedObject.h:82
KeyedObject::key_type
KEY key_type
Definition of the key-type to access object.
Definition: KeyedObject.h:42
KeyedObject::KeyedObject
KeyedObject(const KeyedObject &copy)
NOBODY may copy these objects.
Definition: KeyedObject.h:91
ObjectContainerBase
Definition: ObjectContainerBase.h:26
GaudiDict
Definition: KeyedContainer.h:19
KeyedObject::serialize
StreamBuffer & serialize(StreamBuffer &s) override
Serialize the object for reading.
Definition: KeyedObject.h:147
KeyedObject::key
const key_type & key() const
Retrieve Key of the object.
Definition: KeyedObject.h:80
Containers::cannotAssignObjectKey
GAUDI_API void cannotAssignObjectKey()
Function to be called when an object key cannot be assigned.
Definition: KeyedObjectManager.cpp:78
KeyedObject::~KeyedObject
~KeyedObject() override
Standard destructor.
Definition: KeyedObject.h:103
KeyedObject::release
unsigned long release()
Release reference. If the reference count is ZERO, delete the object.
Definition: KeyedObject.h:119
ProduceConsume.key
key
Definition: ProduceConsume.py:84
GaudiDict::KeyedObjectDict
Definition: KeyedObject.h:19
ContainedObject.h
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:83
ContainedObject
Definition: ContainedObject.h:37
ContainedObject::serialize
virtual StreamBuffer & serialize(StreamBuffer &s) const
Serialize the object for writing.
Definition: ContainedObject.h:66