The Gaudi Framework  v28r2p1 (f1a77ff4)
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> struct KeyedObjectDict;
6 }
7 
8 // Framework include files
12 
27 template < class KEY >
29  friend struct GaudiDict::KeyedObjectDict<KEY>;
30 public:
32  typedef KEY key_type;
33 
34 protected:
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
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
120  if ( !m_hasKey ) {
121  m_key = key;
122  m_hasKey = true;
123  return;
124  }
126 }
127 
128 // Serialize the object for writing
129 template<class KEY> inline
132 }
133 
134 // Serialize the object for reading
135 template<class KEY> inline
137  long k;
139  m_key = traits::makeKey(k);
140  m_hasKey = true;
141  return s;
142 }
143 #endif // GAUDIKERNEL_KEYEDOBJECT_H
static long addRef(obj_type *v)
Add reference counter to object when inserted into the container.
Definition: KeyedTraits.h:92
Definition of the templated KeyedObject class.
Definition: KeyedObject.h:28
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:32
KeyedObject(const KeyedObject &copy)
NOBODY may copy these objects.
Definition: KeyedObject.h:79
static key_type makeKey(long k)
Create key from its full integer representation.
Definition: KeyedTraits.h:72
KeyedObject(const key_type &kval)
Standard Constructor accepting the object&#39;s key.
Definition: KeyedObject.h:65
virtual StreamBuffer & serialize(StreamBuffer &s) const
Serialize the object for writing.
Key traits class.
Definition: KeyedTraits.h:32
void setKey(const key_type &key)
Set object key.
Definition: KeyedObject.h:119
Containers::key_traits< key_type > traits
definition of the container key traits to be made friend
Definition: KeyedObject.h:36
const key_type & key() const
Retrieve Key of the object.
Definition: KeyedObject.h:69
unsigned long addRef()
Add reference to object (Increase reference counter).
Definition: KeyedObject.h:102
unsigned long release()
Release reference. If the reference count is ZERO, delete the object.
Definition: KeyedObject.h:108
static long identifier(const key_type &k)
Full unhashed key identifier.
Definition: KeyedTraits.h:75
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:71
StreamBuffer & serialize(StreamBuffer &s) const override
Serialize the object for writing.
Definition: KeyedObject.h:130
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:72
string s
Definition: gaudirun.py:245
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:94
~KeyedObject() override
Standard destructor.
Definition: KeyedObject.h:91
ObjectContainerBase is the base class for Gaudi container classes.
#define GAUDI_API
Definition: Kernel.h:107
static void setKey(obj_type *v, const key_type &k)
Set object key when inserted into the container.
Definition: KeyedTraits.h:79