00001 // @(#)root/hist:$Id: TFitResultPtr.cxx 31261 2009-11-18 07:31:51Z moneta $ 00002 // Author: David Gonzalez Maline 12/11/09 00003 00004 /************************************************************************* 00005 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. * 00006 * All rights reserved. * 00007 * * 00008 * For the licensing terms see $ROOTSYS/LICENSE. * 00009 * For the list of contributors see $ROOTSYS/README/CREDITS. * 00010 *************************************************************************/ 00011 00012 #include "TFitResultPtr.h" 00013 #include "TFitResult.h" 00014 #include "TError.h" 00015 00016 /** 00017 TFitResultPtr provides an indirection to the TFitResult class and with a semantics 00018 identical to a TFitResult pointer, i.e. it is like a smart pointer to a TFitResult. 00019 In addition it provides an automatic comversion to an integer. In this way it can be 00020 returned from the TH1::Fit method and the change in TH1::Fit be backward compatible. 00021 The class 00022 00023 */ 00024 00025 ClassImp(TFitResultPtr) 00026 00027 TFitResultPtr::TFitResultPtr(TFitResult * p) : 00028 fStatus(-1), 00029 fPointer(p) 00030 { 00031 // constructor from a TFitResult pointer 00032 if (fPointer != 0) fStatus = fPointer->Status(); 00033 } 00034 00035 TFitResultPtr::TFitResultPtr(const TFitResultPtr& rhs) : 00036 fStatus(rhs.fStatus), fPointer(0) 00037 { 00038 // copy constructor - create a new TFitResult if needed 00039 if (rhs.fPointer != 0) fPointer = new TFitResult(*rhs); 00040 } 00041 00042 TFitResultPtr::~TFitResultPtr() 00043 { 00044 // destructor - delete the contained TFitResult pointer if needed 00045 if ( fPointer != 0) 00046 delete fPointer; 00047 } 00048 00049 00050 TFitResult& TFitResultPtr::operator*() const 00051 { 00052 // impelment the de-reference operator to make the class acts as a pointer to a TFitResult 00053 // assert in case the class does not contain a pointer to TFitResult 00054 if (fPointer == 0) { 00055 Error("TFitResultPtr","TFitResult is empty - use the fit option S"); 00056 return *(new TFitResult() ); 00057 } 00058 return *fPointer; 00059 } 00060 00061 TFitResult* TFitResultPtr::operator->() const 00062 { 00063 // implement the -> operator to make the class acts as a pointer to a TFitResult 00064 // assert in case the class does not contain a pointer to TFitResult 00065 if (fPointer == 0) { 00066 Error("TFitResultPtr","TFitResult is empty - use the fit option S"); 00067 return new TFitResult(); 00068 } 00069 return fPointer; 00070 } 00071 00072 00073 TFitResultPtr & TFitResultPtr::operator=(const TFitResultPtr& rhs) 00074 { 00075 // assignment operator 00076 // if needed copy the TFitResult object and delete previous one if existing 00077 if ( &rhs == this) return *this; // self assignment 00078 fStatus = rhs.fStatus; 00079 if ( fPointer ) delete fPointer; 00080 fPointer = 0; 00081 if (rhs.fPointer != 0) fPointer = new TFitResult(*rhs); 00082 return *this; 00083 } 00084