| # Gaudi Framework, version v22r4 |

Home |  | Generated: Fri Sep 2 2011

# KeyedObject.h

Go to the documentation of this file.

    00001 #ifndef GAUDIKERNEL_KEYEDOBJECT_H
    00002 #define GAUDIKERNEL_KEYEDOBJECT_H
    00003
    00004 namespace GaudiDict  {
    00005   template <class T> struct KeyedObjectDict;
    00006 }
    00007
    00008 // Framework include files
    00009 #include "GaudiKernel/ContainedObject.h"
    00010 #include "GaudiKernel/KeyedTraits.h"
    00011 #include "GaudiKernel/KeyedContainer.h"
    00012
    00027 template < class KEY >
    00028 class GAUDI_API KeyedObject: public ContainedObject {
    00029   friend struct GaudiDict::KeyedObjectDict<KEY>;
    00030 public:
    00032   typedef KEY key_type;
    00033
    00034 protected:
    00036   typedef typename Containers::key_traits<key_type> traits;
    00038 //#ifdef _WIN32
    00039 //  friend traits;
    00040 //#else
    00041   friend struct Containers::key_traits<key_type>;
    00042 //#endif
    00043
    00045   key_type m_key;
    00047   long     m_refCount;
    00049   bool     m_hasKey;
    00051   unsigned long addRef();
    00053   unsigned long release();
    00058   void setKey(const key_type& key);
    00059 public:
    00061   KeyedObject(): m_key(), m_refCount(0), m_hasKey(false) {                     }
    00065   KeyedObject(const key_type& kval):m_key(kval),m_refCount(0),m_hasKey(true) { }
    00067   virtual ~KeyedObject();
    00069   const key_type& key() const           {                     return m_key;    }
    00071   bool hasKey()   const                 {                     return m_hasKey; }
    00072   virtual long index() const            {    return traits::identifier(m_key); }
    00074   virtual StreamBuffer& serialize( StreamBuffer& s ) const;
    00076   virtual StreamBuffer& serialize( StreamBuffer& s );
    00077 private:
    00079   KeyedObject(const KeyedObject& copy) : ContainedObject(copy) {               }
    00080 };
    00081
    00082 /*
    00083  *
    00084  *
    00085  *  Inline code for keyed KeyedObject class
    00086  *
    00087  */
    00088
    00089 // Standard destructor.
    00090 template<class KEY> inline
    00091 KeyedObject<KEY>::~KeyedObject()
    00092 {
    00093   ObjectContainerBase* p = const_cast<ObjectContainerBase*>(parent());
    00094   if ( p )  {
    00095     setParent(0);
    00096     p->remove(this);
    00097   }
    00098 }
    00099
    00100 // Add reference to object (Increase reference counter).
    00101 template<class KEY> inline
    00102 unsigned long KeyedObject<KEY>::addRef() {
    00103   return ++m_refCount;
    00104 }
    00105
    00106 // Release reference. If the reference count is ZERO, delete the object.
    00107 template<class KEY> inline
    00108 unsigned long KeyedObject<KEY>::release() {
    00109   long cnt = --m_refCount;
    00110   if ( cnt <= 0 )  {
    00111     delete this;
    00112   }
    00113   return cnt;
    00114 }
    00115
    00116 /*  Set object key. The key for consistency reasons
    00117     can be set only once for the object. Any attempt to
    00118     redefine the key results in an exception.
    00119 */
    00120 template<class KEY> inline
    00121 void KeyedObject<KEY>::setKey(const key_type& key) {
    00122   if ( !m_hasKey )   {
    00123     m_key = key;
    00124     m_hasKey = true;
    00125     return;
    00126   }
    00127   Containers::cannotAssignObjectKey();
    00128 }
    00129
    00130 // Serialize the object for writing
    00131 template<class KEY> inline
    00132 StreamBuffer& KeyedObject<KEY>::serialize( StreamBuffer& s ) const {
    00133   return ContainedObject::serialize(s) << traits::identifier(m_key);
    00134 }
    00135
    00136 // Serialize the object for reading
    00137 template<class KEY> inline
    00138 StreamBuffer& KeyedObject<KEY>::serialize( StreamBuffer& s ) {
    00139   long k;
    00140   ContainedObject::serialize(s) >> k;
    00141   m_key = traits::makeKey(k);
    00142   m_hasKey = true;
    00143   return s;
    00144 }
    00145 #endif // GAUDIKERNEL_KEYEDOBJECT_H

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines