The Gaudi Framework  v33r0 (d5ea422b)
KeyedObject.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #ifndef GAUDIKERNEL_KEYEDOBJECT_H
12 #define GAUDIKERNEL_KEYEDOBJECT_H
13 
14 namespace GaudiDict {
15  template <class T>
17 }
18 
19 // Framework include files
23 
38 template <class KEY>
40  friend struct GaudiDict::KeyedObjectDict<KEY>;
41 
42 public:
44  typedef KEY key_type;
45 
46 protected:
50  //#ifdef _WIN32
51  // friend traits;
52  //#else
54  //#endif
55 
57  key_type m_key{};
59  long m_refCount = 0;
61  bool m_hasKey = false;
63  unsigned long addRef();
65  unsigned long release();
70  void setKey( const key_type& key );
71 
72 public:
74  KeyedObject() = default;
78  KeyedObject( const key_type& kval ) : m_key( kval ), m_refCount( 0 ), m_hasKey( true ) {}
80  ~KeyedObject() override;
82  const key_type& key() const { return m_key; }
84  bool hasKey() const { return m_hasKey; }
85  long index() const override { return traits::identifier( m_key ); }
87  StreamBuffer& serialize( StreamBuffer& s ) const override;
89  StreamBuffer& serialize( StreamBuffer& s ) override;
90 
91 private:
93  KeyedObject( const KeyedObject& copy ) : ContainedObject( copy ), m_refCount( 0 ), m_hasKey( true ) {}
94 };
95 
96 /*
97  *
98  *
99  * Inline code for keyed KeyedObject class
100  *
101  */
102 
103 // Standard destructor.
104 template <class KEY>
106  ObjectContainerBase* p = const_cast<ObjectContainerBase*>( parent() );
107  if ( p ) {
108  setParent( nullptr );
109  p->remove( this );
110  }
111 }
112 
113 // Add reference to object (Increase reference counter).
114 template <class KEY>
115 inline unsigned long KeyedObject<KEY>::addRef() {
116  return ++m_refCount;
117 }
118 
119 // Release reference. If the reference count is ZERO, delete the object.
120 template <class KEY>
121 inline unsigned long KeyedObject<KEY>::release() {
122  long cnt = --m_refCount;
123  if ( cnt <= 0 ) delete this;
124  return cnt;
125 }
126 
127 /* Set object key. The key for consistency reasons
128  can be set only once for the object. Any attempt to
129  redefine the key results in an exception.
130 */
131 template <class KEY>
132 inline void KeyedObject<KEY>::setKey( const key_type& key ) {
133  if ( !m_hasKey ) {
134  m_key = key;
135  m_hasKey = true;
136  return;
137  }
139 }
140 
141 // Serialize the object for writing
142 template <class KEY>
144  return ContainedObject::serialize( s ) << traits::identifier( m_key );
145 }
146 
147 // Serialize the object for reading
148 template <class KEY>
150  long k;
152  m_key = traits::makeKey( k );
153  m_hasKey = true;
154  return s;
155 }
156 #endif // GAUDIKERNEL_KEYEDOBJECT_H
virtual StreamBuffer & serialize(StreamBuffer &s) const
Serialize the object for writing.
static long addRef(obj_type *v)
Add reference counter to object when inserted into the container.
Definition: KeyedTraits.h:111
Definition of the templated KeyedObject class.
Definition: KeyedObject.h:39
The stream buffer is a small object collecting object data.
Definition: StreamBuffer.h:51
KEY key_type
Definition of the key-type to access object.
Definition: KeyedObject.h:44
KeyedObject(const KeyedObject &copy)
NOBODY may copy these objects.
Definition: KeyedObject.h:93
static key_type makeKey(long k)
Create key from its full integer representation.
Definition: KeyedTraits.h:89
KeyedObject(const key_type &kval)
Standard Constructor accepting the object's key.
Definition: KeyedObject.h:78
Key traits class.
Definition: KeyedTraits.h:47
long index() const override
Distance in the parent container.
Definition: KeyedObject.h:85
void setKey(const key_type &key)
Set object key.
Definition: KeyedObject.h:132
Containers::key_traits< key_type > traits
definition of the container key traits to be made friend
Definition: KeyedObject.h:48
StreamBuffer & serialize(StreamBuffer &s) const override
Serialize the object for writing.
Definition: KeyedObject.h:143
bool hasKey() const
Check if the object has a key assigned or not.
Definition: KeyedObject.h:84
unsigned long addRef()
Add reference to object (Increase reference counter).
Definition: KeyedObject.h:115
unsigned long release()
Release reference. If the reference count is ZERO, delete the object.
Definition: KeyedObject.h:121
static long identifier(const key_type &k)
Full unhashed key identifier.
Definition: KeyedTraits.h:92
All classes that their objects may be contained in an LHCb ObjectContainer (e.g.
GAUDI_API void cannotAssignObjectKey()
Function to be called when an object key cannot be assigned.
string s
Definition: gaudirun.py:328
virtual long remove(ContainedObject *value)=0
Release object from the container (the pointer will be removed from the container,...
static long release(obj_type *v)
Release reference to object.
Definition: KeyedTraits.h:113
~KeyedObject() override
Standard destructor.
Definition: KeyedObject.h:105
ObjectContainerBase is the base class for Gaudi container classes.
#define GAUDI_API
Definition: Kernel.h:81
const key_type & key() const
Retrieve Key of the object.
Definition: KeyedObject.h:82
static void setKey(obj_type *v, const key_type &k)
Set object key when inserted into the container.
Definition: KeyedTraits.h:96