00001 // -*- C++ -*- 00002 // 00003 // ReferenceCounted.h is a part of ThePEG - Toolkit for HEP Event Generation 00004 // Copyright (C) 1999-2007 Leif Lonnblad 00005 // 00006 // ThePEG is licenced under version 2 of the GPL, see COPYING for details. 00007 // Please respect the MCnet academic guidelines, see GUIDELINES for details. 00008 // 00009 #ifndef ThePEG_ReferenceCounted_H 00010 #define ThePEG_ReferenceCounted_H 00011 // This is the declaration of the ReferenceCounted class. 00012 00013 00014 #include "RCPtr.fh" 00015 #include "ThePEG/Persistency/PersistentIStream.fh" 00016 00017 namespace ThePEG { 00018 namespace Pointer { 00019 00030 class ReferenceCounted { 00031 00033 friend class RCPtrBase; 00034 friend class ThePEG::PersistentIStream; 00035 00036 public: 00037 00041 typedef unsigned int CounterType; 00042 00043 protected: 00044 00050 ReferenceCounted() 00051 : uniqueId(++objectCounter), 00052 theReferenceCounter(CounterType(1)) {} 00053 00057 ReferenceCounted(const ReferenceCounted &) 00058 : uniqueId(++objectCounter), 00059 theReferenceCounter(CounterType(1)) {} 00060 00064 ReferenceCounted & operator=(const ReferenceCounted &) 00065 { 00066 return *this; 00067 } 00068 00070 00071 public: 00072 00076 CounterType referenceCount() const 00077 { 00078 return theReferenceCounter; 00079 } 00080 00081 private: 00082 00086 void incrementReferenceCount() const 00087 { 00088 ++theReferenceCounter; 00089 } 00090 00094 bool decrementReferenceCount() const 00095 { 00096 return !--theReferenceCounter; 00097 } 00098 00099 public: 00100 00104 const unsigned long uniqueId; 00105 00106 private: 00107 00112 static unsigned long objectCounter; 00113 00117 mutable CounterType theReferenceCounter; 00118 00119 }; 00120 00121 00122 } 00123 } 00124 00125 #endif /* ThePEG_ReferenceCounted_H */ 00126