Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TFitResultPtr.cxx
Go to the documentation of this file.
1 // @(#)root/hist:$Id$
2 // Author: David Gonzalez Maline 12/11/09
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #include "TFitResultPtr.h"
13 #include "TFitResult.h"
14 #include "TError.h"
15 
16 /** \class TFitResultPtr
17 Provides an indirection to the TFitResult class and with a semantics
18 identical to a TFitResult pointer, i.e. it is like a smart pointer to a TFitResult.
19 In addition it provides an automatic comversion to an integer. In this way it can be
20 returned from the TH1::Fit method and the change in TH1::Fit be backward compatible.
21  */
22 
23 ClassImp(TFitResultPtr);
24 
25 ////////////////////////////////////////////////////////////////////////////////
26 /// Constructor from a TFitResult pointer
27 
28 TFitResultPtr::TFitResultPtr(const std::shared_ptr<TFitResult> & p) :
29  fStatus(-1),
30  fPointer(p)
31 {
32  if (fPointer) fStatus = fPointer->Status();
33 }
34 
35 ////////////////////////////////////////////////////////////////////////////////
36 /// Constructor from a TFitResult pointer
37 
38 TFitResultPtr::TFitResultPtr(TFitResult * p) :
39  fStatus(-1),
40  fPointer(std::shared_ptr<TFitResult>(p))
41 {
42  if (fPointer) fStatus = fPointer->Status();
43 }
44 
45 TFitResultPtr::TFitResultPtr(const TFitResultPtr& rhs) :
46  fStatus(rhs.fStatus), fPointer(rhs.fPointer)
47 {
48 }
49 
50 ////////////////////////////////////////////////////////////////////////////////
51 /// Destructor. Delete the contained TFitResult pointer if needed
52 /// if ( fPointer != 0)
53 /// delete fPointer;
54 
55 TFitResultPtr::~TFitResultPtr()
56 {
57 }
58 
59 ////////////////////////////////////////////////////////////////////////////////
60 /// Implement the de-reference operator to make the class acts as a pointer to a TFitResult
61 /// assert in case the class does not contain a pointer to TFitResult
62 
63 TFitResult& TFitResultPtr::operator*() const
64 {
65  if (!fPointer) {
66  Error("TFitResultPtr","TFitResult is empty - use the fit option S");
67  }
68  return *fPointer;
69 }
70 
71 ////////////////////////////////////////////////////////////////////////////////
72 /// Implement the -> operator to make the class acts as a pointer to a TFitResult.
73 /// assert in case the class does not contain a pointer to TFitResult
74 
75 TFitResult* TFitResultPtr::operator->() const
76 {
77  if (!fPointer) {
78  Error("TFitResultPtr","TFitResult is empty - use the fit option S");
79  }
80  return fPointer.get();
81 }
82 
83 ////////////////////////////////////////////////////////////////////////////////
84 /// Return contained pointer
85 
86 TFitResult * TFitResultPtr::Get() const {
87  return fPointer.get();
88 }
89 
90 ////////////////////////////////////////////////////////////////////////////////
91 /// Assignment operator.
92 /// if needed copy the TFitResult object and delete previous one if existing
93 
94 TFitResultPtr & TFitResultPtr::operator=(const TFitResultPtr& rhs)
95 {
96  if ( &rhs == this) return *this; // self assignment
97  fStatus = rhs.fStatus;
98  fPointer = rhs.fPointer;
99  // if ( fPointer ) delete fPointer;
100  // fPointer = 0;
101  // if (rhs.fPointer != 0) fPointer = new TFitResult(*rhs);
102  return *this;
103 }
104 
105 ////////////////////////////////////////////////////////////////////////////////
106 /// Print the TFitResultPtr by printing its TFitResult.
107 
108 std::string cling::printValue(const TFitResultPtr* val) {
109  if (TFitResult* fr = val->Get())
110  return printValue(fr);
111  return "<nullptr TFitResult>";
112 }