| The Gaudi Framework v30r0 (c919700c)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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