The Gaudi Framework  master (181af51f)
Loading...
Searching...
No Matches
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
17namespace GaudiDict {
18 template <class T>
20}
21
36template <class KEY>
38 friend struct GaudiDict::KeyedObjectDict<KEY>;
39
40public:
42 typedef KEY key_type;
43
44protected:
48 friend struct Containers::key_traits<key_type>;
49
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
66public:
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
85private:
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.
98template <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).
108template <class KEY>
109inline unsigned long KeyedObject<KEY>::addRef() {
110 return ++m_refCount;
111}
112
113// Release reference. If the reference count is ZERO, delete the object.
114template <class KEY>
115inline 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*/
125template <class KEY>
126inline 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
136template <class KEY>
140
141// Serialize the object for reading
142template <class KEY>
144 long k;
146 m_key = traits::makeKey( k );
147 m_hasKey = true;
148 return s;
149}
#define GAUDI_API
Definition Kernel.h:49
void setParent(ObjectContainerBase *value)
Update parent member.
ContainedObject()=default
Constructors.
virtual StreamBuffer & serialize(StreamBuffer &s) const
Serialize the object for writing.
const ObjectContainerBase * parent() const
Access to parent object.
unsigned long addRef()
Add reference to object (Increase reference counter).
KeyedObject()=default
Standard Constructor. The object key is preset to the invalid value.
long index() const override
Distance in the parent container.
Definition KeyedObject.h:79
void setKey(const key_type &key)
Set object key.
const key_type & key() const
Definition KeyedObject.h:76
KeyedObject(const KeyedObject &copy)
NOBODY may copy these objects.
Definition KeyedObject.h:87
StreamBuffer & serialize(StreamBuffer &s) const override
Serialize the object for writing.
KeyedObject(const key_type &kval)
Standard Constructor accepting the object's key.
Definition KeyedObject.h:72
unsigned long release()
Release reference. If the reference count is ZERO, delete the object.
~KeyedObject() override
Standard destructor.
Definition KeyedObject.h:99
StreamBuffer & serialize(StreamBuffer &s) override
Serialize the object for reading.
bool hasKey() const
Check if the object has a key assigned or not.
Definition KeyedObject.h:78
Containers::key_traits< key_type > traits
Definition KeyedObject.h:46
KEY key_type
Definition of the key-type to access object.
Definition KeyedObject.h:42
ObjectContainerBase is the base class for Gaudi container classes.
virtual long remove(ContainedObject *value)=0
Release object from the container (the pointer will be removed from the container,...
The stream buffer is a small object collecting object data.
GAUDI_API void cannotAssignObjectKey()
Function to be called when an object key cannot be assigned.
Key traits class.
Definition KeyedTraits.h:75
static long identifier(const key_type &k)
Definition KeyedTraits.h:87