Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-06 09:38:30

0001 // -*- C++ -*-
0002 //
0003 // ReferenceCounted.h is a part of ThePEG - Toolkit for HEP Event Generation
0004 // Copyright (C) 1999-2019 Leif Lonnblad
0005 //
0006 // ThePEG is licenced under version 3 of the GPL, see COPYING for details.
0007 // Please respect the MCnet academic guidelines, see GUIDELINES for details.
0008 //
0009 #ifndef ThePEG_ReferenceCounted_H
0010 #define ThePEG_ReferenceCounted_H
0011 // This is the declaration of the ReferenceCounted class.
0012 
0013 
0014 #include "RCPtr.fh"
0015 #include "ThePEG/Persistency/PersistentIStream.fh"
0016 
0017 namespace ThePEG {
0018 namespace Pointer {
0019 
0020 /**
0021  * ReferenceCounted must be the (virtual) base class of all
0022  * classes which may be pointed to by the RCPtr smart
0023  * pointer class. It keeps track of all RCPtr and
0024  * ConstRCPtr pointers which are currently pointing to an
0025  * object.
0026  *
0027  * @see RCPtr
0028  * @see ConstRCPtr
0029  */
0030 class ReferenceCounted {
0031 
0032   /** The RCPtrBase class needs to acces the private parts of ReferenceCounted. */
0033   friend class RCPtrBase;
0034   friend class ThePEG::PersistentIStream;
0035 
0036 public:
0037 
0038   /**
0039    * The integer type used for counting.
0040    */
0041   typedef unsigned int CounterType;
0042 
0043 protected:
0044 
0045   /** @name Standard constructors and assignment. */
0046   //@{
0047   /**
0048    * Default constructor.
0049    */
0050   ReferenceCounted() 
0051     : uniqueId(++objectCounter), 
0052       theReferenceCounter(CounterType(1)) {}
0053 
0054   /**
0055    * Copy-constructor.
0056    */
0057   ReferenceCounted(const ReferenceCounted &)
0058     : uniqueId(++objectCounter), 
0059       theReferenceCounter(CounterType(1)) {}
0060 
0061   /**
0062    * Assignment.
0063    */
0064   ReferenceCounted & operator=(const ReferenceCounted &)
0065   {
0066     return *this;
0067   }
0068     
0069   //@}
0070 
0071 public:
0072 
0073   /**
0074    * Return the reference count.
0075    */
0076   CounterType referenceCount() const 
0077   { 
0078     return theReferenceCounter; 
0079   }
0080 
0081 private:
0082 
0083   /**
0084    * Increment the reference count.
0085    */
0086   void incrementReferenceCount() const 
0087   { 
0088     ++theReferenceCounter; 
0089   }
0090 
0091   /**
0092    * Decrement with the reference count.
0093    */
0094   bool decrementReferenceCount() const 
0095   {
0096     return !--theReferenceCounter;
0097   }
0098 
0099 public:
0100 
0101   /**
0102    * The unique ID. Can be used as sorting criterion, e.g. for set<> and map<>.
0103    */
0104   const unsigned long uniqueId;
0105 
0106 private:
0107 
0108   /** 
0109    * A counter for issuing unique IDs. It will overflow back to 0 eventually,
0110    * but it is very unlikely that two identical IDs show up in the same event.
0111    */
0112   static unsigned long objectCounter;
0113 
0114   /**
0115    * The reference count.
0116    */
0117   mutable CounterType theReferenceCounter;
0118 
0119 };
0120 
0121 
0122 }
0123 }
0124 
0125 #endif /* ThePEG_ReferenceCounted_H */
0126