Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:10:25

0001 // @(#)root/minuit2:$Id$
0002 // Authors: M. Winkler, F. James, L. Moneta, A. Zsenei   2003-2005
0003 
0004 /**********************************************************************
0005  *                                                                    *
0006  * Copyright (c) 2005 LCG ROOT Math team,  CERN/PH-SFT                *
0007  *                                                                    *
0008  **********************************************************************/
0009 
0010 #ifndef ROOT_Minuit2_MnRefCountedPointer
0011 #define ROOT_Minuit2_MnRefCountedPointer
0012 
0013 #include "MnReferenceCounter.h"
0014 
0015 namespace ROOT {
0016 
0017 namespace Minuit2 {
0018 
0019 template <class T>
0020 class MnRefCountedPointer {
0021 
0022 public:
0023    // Default constructor needed for use inside array, vector, etc.
0024    MnRefCountedPointer() : fPtr(0), fCounter(nullptr) {}
0025 
0026    MnRefCountedPointer(T *pt) : fPtr(pt), fCounter(new MnReferenceCounter()) { AddReference(); }
0027 
0028    MnRefCountedPointer(const MnRefCountedPointer<T> &other) : fPtr(other.fPtr), fCounter(other.fCounter)
0029    {
0030       AddReference();
0031    }
0032 
0033    ~MnRefCountedPointer()
0034    {
0035       /*
0036       if(References() == 0) {
0037         if(fPtr) delete fPtr;
0038         if(fCounter) delete fCounter;
0039       }
0040       else RemoveReference();
0041       */
0042       if (References() != 0)
0043          RemoveReference();
0044    }
0045 
0046    bool IsValid() const { return fPtr != 0; }
0047 
0048    MnRefCountedPointer &operator=(const MnRefCountedPointer<T> &other)
0049    {
0050       if (this != &other && fPtr != other.fPtr) {
0051          RemoveReference();
0052          fPtr = other.fPtr;
0053          fCounter = other.fCounter;
0054          AddReference();
0055       }
0056       return *this;
0057    }
0058 
0059    MnRefCountedPointer &operator=(T *ptr)
0060    {
0061       if (fPtr != ptr) {
0062          fPtr = ptr;
0063          fCounter = new MnReferenceCounter();
0064       }
0065       return *this;
0066    }
0067 
0068    T *Get() const { return fPtr; }
0069 
0070    T *operator->() const
0071    {
0072       DoCheck();
0073       return fPtr;
0074    }
0075 
0076    T &operator*() const
0077    {
0078       DoCheck();
0079       return *fPtr;
0080    }
0081 
0082    bool operator==(const T *otherP) const { return fPtr == otherP; }
0083 
0084    bool operator<(const T *otherP) const { return fPtr < otherP; }
0085 
0086    unsigned int References() const { return fCounter->References(); }
0087 
0088    void AddReference() const { fCounter->AddReference(); }
0089 
0090    void RemoveReference()
0091    {
0092       fCounter->RemoveReference();
0093       if (References() == 0) {
0094          delete fPtr;
0095          fPtr = 0;
0096          delete fCounter;
0097          fCounter = nullptr;
0098       }
0099    }
0100 
0101 private:
0102    T *fPtr;
0103    MnReferenceCounter *fCounter;
0104 
0105 private:
0106    void DoCheck() const { assert(IsValid()); }
0107 };
0108 
0109 } // namespace Minuit2
0110 
0111 } // namespace ROOT
0112 
0113 #endif // ROOT_Minuit2_MnRefCountedPointer