Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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>
7 }
8 
9 // Framework include files
13 
28 template <class KEY>
30  friend struct GaudiDict::KeyedObjectDict<KEY>;
31 
32 public:
34  typedef KEY key_type;
35 
36 protected:
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>
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  }
129 }
130 
131 // Serialize the object for writing
132 template <class KEY>
134  return ContainedObject::serialize( s ) << traits::identifier( m_key );
135 }
136 
137 // Serialize the object for reading
138 template <class KEY>
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
static long addRef(obj_type *v)
Add reference counter to object when inserted into the container.
Definition: KeyedTraits.h:101
Definition of the templated KeyedObject class.
Definition: KeyedObject.h:29
The stream buffer is a small object collecting object data.
Definition: StreamBuffer.h:41
KEY key_type
Definition of the key-type to access object.
Definition: KeyedObject.h:34
KeyedObject(const KeyedObject &copy)
NOBODY may copy these objects.
Definition: KeyedObject.h:83
static key_type makeKey(long k)
Create key from its full integer representation.
Definition: KeyedTraits.h:79
KeyedObject(const key_type &kval)
Standard Constructor accepting the object&#39;s key.
Definition: KeyedObject.h:68
virtual StreamBuffer & serialize(StreamBuffer &s) const
Serialize the object for writing.
Key traits class.
Definition: KeyedTraits.h:37
void setKey(const key_type &key)
Set object key.
Definition: KeyedObject.h:122
Containers::key_traits< key_type > traits
definition of the container key traits to be made friend
Definition: KeyedObject.h:38
const key_type & key() const
Retrieve Key of the object.
Definition: KeyedObject.h:72
unsigned long addRef()
Add reference to object (Increase reference counter).
Definition: KeyedObject.h:105
unsigned long release()
Release reference. If the reference count is ZERO, delete the object.
Definition: KeyedObject.h:111
static long identifier(const key_type &k)
Full unhashed key identifier.
Definition: KeyedTraits.h:82
All classes that their objects may be contained in an LHCb ObjectContainer (e.g.
bool hasKey() const
Check if the object has a key assigned or not.
Definition: KeyedObject.h:74
StreamBuffer & serialize(StreamBuffer &s) const override
Serialize the object for writing.
Definition: KeyedObject.h:133
GAUDI_API void cannotAssignObjectKey()
Function to be called when an object key cannot be assigned.
long index() const override
Distance in the parent container.
Definition: KeyedObject.h:75
string s
Definition: gaudirun.py:312
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).
static long release(obj_type *v)
Release reference to object.
Definition: KeyedTraits.h:103
~KeyedObject() override
Standard destructor.
Definition: KeyedObject.h:95
ObjectContainerBase is the base class for Gaudi container classes.
#define GAUDI_API
Definition: Kernel.h:71
static void setKey(obj_type *v, const key_type &k)
Set object key when inserted into the container.
Definition: KeyedTraits.h:86