The Gaudi Framework  v30r3 (a5ef0a68)
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>
8 }
9 
10 // Framework include files
14 
29 template <class KEY>
31 {
32  friend struct GaudiDict::KeyedObjectDict<KEY>;
33 
34 public:
36  typedef KEY key_type;
37 
38 protected:
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>
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  }
135 }
136 
137 // Serialize the object for writing
138 template <class KEY>
140 {
141  return ContainedObject::serialize( s ) << traits::identifier( m_key );
142 }
143 
144 // Serialize the object for reading
145 template <class KEY>
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
static long addRef(obj_type *v)
Add reference counter to object when inserted into the container.
Definition: KeyedTraits.h:104
Definition of the templated KeyedObject class.
Definition: KeyedObject.h:30
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:36
KeyedObject(const KeyedObject &copy)
NOBODY may copy these objects.
Definition: KeyedObject.h:85
static key_type makeKey(long k)
Create key from its full integer representation.
Definition: KeyedTraits.h:80
KeyedObject(const key_type &kval)
Standard Constructor accepting the object&#39;s key.
Definition: KeyedObject.h:70
virtual StreamBuffer & serialize(StreamBuffer &s) const
Serialize the object for writing.
Key traits class.
Definition: KeyedTraits.h:38
void setKey(const key_type &key)
Set object key.
Definition: KeyedObject.h:127
Containers::key_traits< key_type > traits
definition of the container key traits to be made friend
Definition: KeyedObject.h:40
const key_type & key() const
Retrieve Key of the object.
Definition: KeyedObject.h:74
unsigned long addRef()
Add reference to object (Increase reference counter).
Definition: KeyedObject.h:108
unsigned long release()
Release reference. If the reference count is ZERO, delete the object.
Definition: KeyedObject.h:115
static long identifier(const key_type &k)
Full unhashed key identifier.
Definition: KeyedTraits.h:83
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:76
StreamBuffer & serialize(StreamBuffer &s) const override
Serialize the object for writing.
Definition: KeyedObject.h:139
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:77
string s
Definition: gaudirun.py:253
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:106
~KeyedObject() override
Standard destructor.
Definition: KeyedObject.h:97
ObjectContainerBase is the base class for Gaudi container classes.
#define GAUDI_API
Definition: Kernel.h:104
static void setKey(obj_type *v, const key_type &k)
Set object key when inserted into the container.
Definition: KeyedTraits.h:87