All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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
42 //#endif
43 
47  long m_refCount;
49  bool m_hasKey;
51  unsigned long addRef();
53  unsigned long release();
58  void setKey(const key_type& key);
59 public:
61  KeyedObject(): m_key(), m_refCount(0), m_hasKey(false) { }
65  KeyedObject(const key_type& kval):m_key(kval),m_refCount(0),m_hasKey(true) { }
67  virtual ~KeyedObject();
69  const key_type& key() const { return m_key; }
71  bool hasKey() const { return m_hasKey; }
72  virtual long index() const { return traits::identifier(m_key); }
74  virtual StreamBuffer& serialize( StreamBuffer& s ) const;
76  virtual StreamBuffer& serialize( StreamBuffer& s );
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(0);
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 ) {
111  delete this;
112  }
113  return cnt;
114 }
115 
116 /* Set object key. The key for consistency reasons
117  can be set only once for the object. Any attempt to
118  redefine the key results in an exception.
119 */
120 template<class KEY> inline
122  if ( !m_hasKey ) {
123  m_key = key;
124  m_hasKey = true;
125  return;
126  }
128 }
129 
130 // Serialize the object for writing
131 template<class KEY> inline
134 }
135 
136 // Serialize the object for reading
137 template<class KEY> inline
139  long k;
141  m_key = traits::makeKey(k);
142  m_hasKey = true;
143  return s;
144 }
145 #endif // GAUDIKERNEL_KEYEDOBJECT_H
static long addRef(obj_type *v)
Add reference counter to object when inserted into the container.
Definition: KeyedTraits.h:93
Definition of the templated KeyedObject class.
Definition: KeyedObject.h:28
virtual long index() const
Distance in the parent container.
Definition: KeyedObject.h:72
The stream buffer is a small object collecting object data.
Definition: StreamBuffer.h:40
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
long m_refCount
Reference counter.
Definition: KeyedObject.h:47
static key_type makeKey(long k)
Create key from its full integer representation.
Definition: KeyedTraits.h:73
KeyedObject(const key_type &kval)
Standard Constructor accepting the object's key.
Definition: KeyedObject.h:65
virtual StreamBuffer & serialize(StreamBuffer &s) const
Serialize the object for writing.
Key traits class.
Definition: KeyedTraits.h:33
void setKey(const key_type &key)
Set object key.
Definition: KeyedObject.h:121
Containers::key_traits< key_type > traits
definition of the container key traits to be made friend
Definition: KeyedObject.h:36
virtual ~KeyedObject()
Standard destructor.
Definition: KeyedObject.h:91
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
key_type m_key
Object Key; It's initial value is not determined.
Definition: KeyedObject.h:45
static long identifier(const key_type &k)
Full unhashed key identifier.
Definition: KeyedTraits.h:76
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
GAUDI_API void cannotAssignObjectKey()
Function to be called when an object key cannot be assigned.
KeyedObject()
Standard Constructor. The object key is preset to the invalid value.
Definition: KeyedObject.h:61
bool m_hasKey
Boolean to indicate wether a key was already assigned.
Definition: KeyedObject.h:49
string s
Definition: gaudirun.py:210
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:95
ObjectContainerBase is the base class for Gaudi container classes.
#define GAUDI_API
Definition: Kernel.h:108
virtual StreamBuffer & serialize(StreamBuffer &s) const
Serialize the object for writing.
Definition: KeyedObject.h:132
static void setKey(obj_type *v, const key_type &k)
Set object key when inserted into the container.
Definition: KeyedTraits.h:80