| The Gaudi Framework v27r1

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