| The Gaudi Framework v32r1 (f65d50dc)

KeyedObject.h
Go to the documentation of this file.
1 #ifndef GAUDIKERNEL_KEYEDOBJECT_H
2 #define GAUDIKERNEL_KEYEDOBJECT_H
3
4 namespace GaudiDict {
5 template <class T>
6 struct KeyedObjectDict;
7 }
8
9 // Framework include files
10 #include "GaudiKernel/ContainedObject.h"
11 #include "GaudiKernel/KeyedContainer.h"
12 #include "GaudiKernel/KeyedTraits.h"
13
28 template <class KEY>
29 class GAUDI_API KeyedObject : public ContainedObject {
30 friend struct GaudiDict::KeyedObjectDict<KEY>;
31
32 public:
34 typedef KEY key_type;
35
36 protected:
38 typedef typename Containers::key_traits<key_type> traits;
40 //#ifdef _WIN32
41 // friend traits;
42 //#else
43 friend struct Containers::key_traits<key_type>;
44 //#endif
45
47 key_type m_key{};
49 long m_refCount = 0;
51 bool m_hasKey = false;
53 unsigned long addRef();
55 unsigned long release();
60 void setKey( const key_type& key );
61
62 public:
64 KeyedObject() = default;
68 KeyedObject( const key_type& kval ) : m_key( kval ), m_refCount( 0 ), m_hasKey( true ) {}
70 ~KeyedObject() override;
72 const key_type& key() const { return m_key; }
74 bool hasKey() const { return m_hasKey; }
75 long index() const override { return traits::identifier( m_key ); }
77 StreamBuffer& serialize( StreamBuffer& s ) const override;
79 StreamBuffer& serialize( StreamBuffer& s ) override;
80
81 private:
83 KeyedObject( const KeyedObject& copy ) : ContainedObject( copy ), m_refCount( 0 ), m_hasKey( true ) {}
84 };
85
86 /*
87 *
88 *
89 * Inline code for keyed KeyedObject class
90 *
91 */
92
93 // Standard destructor.
94 template <class KEY>
95 inline KeyedObject<KEY>::~KeyedObject() {
96 ObjectContainerBase* p = const_cast<ObjectContainerBase*>( parent() );
97 if ( p ) {
98 setParent( nullptr );
99 p->remove( this );
100 }
101 }
102
103 // Add reference to object (Increase reference counter).
104 template <class KEY>
105 inline unsigned long KeyedObject<KEY>::addRef() {
106 return ++m_refCount;
107 }
108
109 // Release reference. If the reference count is ZERO, delete the object.
110 template <class KEY>
111 inline unsigned long KeyedObject<KEY>::release() {
112 long cnt = --m_refCount;
113 if ( cnt <= 0 ) delete this;
114 return cnt;
115 }
116
117 /* Set object key. The key for consistency reasons
118 can be set only once for the object. Any attempt to
119 redefine the key results in an exception.
120 */
121 template <class KEY>
122 inline void KeyedObject<KEY>::setKey( const key_type& key ) {
123 if ( !m_hasKey ) {
124 m_key = key;
125 m_hasKey = true;
126 return;
127 }
128 Containers::cannotAssignObjectKey();
129 }
130
131 // Serialize the object for writing
132 template <class KEY>
133 inline StreamBuffer& KeyedObject<KEY>::serialize( StreamBuffer& s ) const {
134 return ContainedObject::serialize( s ) << traits::identifier( m_key );
135 }
136
137 // Serialize the object for reading
138 template <class KEY>
139 inline StreamBuffer& KeyedObject<KEY>::serialize( StreamBuffer& s ) {
140 long k;
141 ContainedObject::serialize( s ) >> k;
142 m_key = traits::makeKey( k );
143 m_hasKey = true;
144 return s;
145 }
146 #endif // GAUDIKERNEL_KEYEDOBJECT_H
ContainedObject::serialize
virtual StreamBuffer & serialize(StreamBuffer &s) const
Serialize the object for writing.
Definition: ContainedObject.h:58
Containers::key_traits::addRef
static long addRef(obj_type *v)
Add reference counter to object when inserted into the container.
Definition: KeyedTraits.h:101
KeyedTraits.h
KeyedObject
Definition of the templated KeyedObject class.
Definition: KeyedObject.h:29
StreamBuffer
The stream buffer is a small object collecting object data.
Definition: StreamBuffer.h:41
KeyedObject::key_type
KEY key_type
Definition of the key-type to access object.
Definition: KeyedObject.h:34
KeyedObject::KeyedObject
KeyedObject(const KeyedObject &copy)
NOBODY may copy these objects.
Definition: KeyedObject.h:83
GaudiDict::KeyedObjectDict
Definition: KeyedObject.h:6
Containers::key_traits< DATATYPE::key_type >::makeKey
static key_type makeKey(long k)
Create key from its full integer representation.
Definition: KeyedTraits.h:79
KeyedObject::KeyedObject
KeyedObject(const key_type &kval)
Standard Constructor accepting the object's key.
Definition: KeyedObject.h:68
Containers::key_traits
Key traits class.
Definition: KeyedTraits.h:37
KeyedObject::index
long index() const override
Distance in the parent container.
Definition: KeyedObject.h:75
KeyedObject::setKey
void setKey(const key_type &key)
Set object key.
Definition: KeyedObject.h:122
KeyedObject::traits
Containers::key_traits< key_type > traits
definition of the container key traits to be made friend
Definition: KeyedObject.h:38
KeyedObject::serialize
StreamBuffer & serialize(StreamBuffer &s) const override
Serialize the object for writing.
Definition: KeyedObject.h:133
KeyedObject::hasKey
bool hasKey() const
Check if the object has a key assigned or not.
Definition: KeyedObject.h:74
KeyedObject::addRef
unsigned long addRef()
Add reference to object (Increase reference counter).
Definition: KeyedObject.h:105
KeyedObject::release
unsigned long release()
Release reference. If the reference count is ZERO, delete the object.
Definition: KeyedObject.h:111
Containers::key_traits< DATATYPE::key_type >::identifier
static long identifier(const key_type &k)
Full unhashed key identifier.
Definition: KeyedTraits.h:82
ContainedObject
All classes that their objects may be contained in an LHCb ObjectContainer (e.g.
Definition: ContainedObject.h:31
Containers::cannotAssignObjectKey
GAUDI_API void cannotAssignObjectKey()
Function to be called when an object key cannot be assigned.
Definition: KeyedObjectManager.cpp:69
gaudirun.s
string s
Definition: gaudirun.py:316
ObjectContainerBase::remove
virtual long remove(ContainedObject *value)=0
Release object from the container (the pointer will be removed from the container,...
Containers::key_traits::release
static long release(obj_type *v)
Release reference to object.
Definition: KeyedTraits.h:103
KeyedObject::~KeyedObject
~KeyedObject() override
Standard destructor.
Definition: KeyedObject.h:95
ContainedObject.h
ObjectContainerBase
ObjectContainerBase is the base class for Gaudi container classes.
Definition: ObjectContainerBase.h:19
GaudiDict
Definition: KeyedContainer.h:8
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:71
KeyedObject::key
const key_type & key() const
Retrieve Key of the object.
Definition: KeyedObject.h:72
KeyedContainer.h
Containers::key_traits::setKey
static void setKey(obj_type *v, const key_type &k)
Set object key when inserted into the container.
Definition: KeyedTraits.h:86