Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  master (d98a2936)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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  friend struct Containers::key_traits<key_type>;
49 
51  key_type m_key{};
53  long m_refCount = 0;
55  bool m_hasKey = false;
57  unsigned long addRef();
59  unsigned long release();
64  void setKey( const key_type& key );
65 
66 public:
68  KeyedObject() = default;
72  KeyedObject( const key_type& kval ) : m_key( kval ), m_refCount( 0 ), m_hasKey( true ) {}
74  ~KeyedObject() override;
76  const key_type& key() const { return m_key; }
78  bool hasKey() const { return m_hasKey; }
79  long index() const override { return traits::identifier( m_key ); }
81  StreamBuffer& serialize( StreamBuffer& s ) const override;
84 
85 private:
87  KeyedObject( const KeyedObject& copy ) : ContainedObject( copy ), m_refCount( 0 ), m_hasKey( true ) {}
88 };
89 
90 /*
91  *
92  *
93  * Inline code for keyed KeyedObject class
94  *
95  */
96 
97 // Standard destructor.
98 template <class KEY>
100  ObjectContainerBase* p = const_cast<ObjectContainerBase*>( parent() );
101  if ( p ) {
102  setParent( nullptr );
103  p->remove( this );
104  }
105 }
106 
107 // Add reference to object (Increase reference counter).
108 template <class KEY>
109 inline unsigned long KeyedObject<KEY>::addRef() {
110  return ++m_refCount;
111 }
112 
113 // Release reference. If the reference count is ZERO, delete the object.
114 template <class KEY>
115 inline unsigned long KeyedObject<KEY>::release() {
116  long cnt = --m_refCount;
117  if ( cnt <= 0 ) delete this;
118  return cnt;
119 }
120 
121 /* Set object key. The key for consistency reasons
122  can be set only once for the object. Any attempt to
123  redefine the key results in an exception.
124 */
125 template <class KEY>
126 inline void KeyedObject<KEY>::setKey( const key_type& key ) {
127  if ( !m_hasKey ) {
128  m_key = key;
129  m_hasKey = true;
130  return;
131  }
133 }
134 
135 // Serialize the object for writing
136 template <class KEY>
138  return ContainedObject::serialize( s ) << traits::identifier( m_key );
139 }
140 
141 // Serialize the object for reading
142 template <class KEY>
144  long k;
146  m_key = traits::makeKey( k );
147  m_hasKey = true;
148  return s;
149 }
KeyedObject::addRef
unsigned long addRef()
Add reference to object (Increase reference counter).
Definition: KeyedObject.h:109
KeyedObject::index
long index() const override
Distance in the parent container.
Definition: KeyedObject.h:79
KeyedObject::setKey
void setKey(const key_type &key)
Set object key.
Definition: KeyedObject.h:126
KeyedObject::serialize
StreamBuffer & serialize(StreamBuffer &s) const override
Serialize the object for writing.
Definition: KeyedObject.h:137
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:72
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:78
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:87
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:143
KeyedObject::key
const key_type & key() const
Retrieve Key of the object.
Definition: KeyedObject.h:76
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:99
KeyedObject::release
unsigned long release()
Release reference. If the reference count is ZERO, delete the object.
Definition: KeyedObject.h:115
ProduceConsume.key
key
Definition: ProduceConsume.py:84
GaudiDict::KeyedObjectDict
Definition: KeyedObject.h:19
ContainedObject.h
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:49
ContainedObject
Definition: ContainedObject.h:37
ContainedObject::serialize
virtual StreamBuffer & serialize(StreamBuffer &s) const
Serialize the object for writing.
Definition: ContainedObject.h:66