Logo ROOT  
Reference Guide
TLeafObject.cxx
Go to the documentation of this file.
1 // @(#)root/tree:$Id$
2 // Author: Rene Brun 27/01/96
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 /** \class TLeafObject
13 \ingroup tree
14 
15 A TLeaf for a general object derived from TObject.
16 */
17 
18 #include "TLeafObject.h"
19 #include "TBranch.h"
20 #include "TBuffer.h"
21 #include "TClass.h"
22 #include "TMethodCall.h"
23 #include "TDataType.h"
24 
26 
27 ////////////////////////////////////////////////////////////////////////////////
28 /// Default constructor for LeafObject.
29 
31 {
32  fClass = 0;
33  fObjAddress = 0;
34  fVirtual = kTRUE;
35 }
36 
37 ////////////////////////////////////////////////////////////////////////////////
38 /// Create a LeafObject.
39 
40 TLeafObject::TLeafObject(TBranch *parent, const char *name, const char *type)
41  :TLeaf(parent, name,type)
42 {
43  SetTitle(type);
45  fObjAddress = 0;
46  fVirtual = kTRUE;
47 }
48 
49 ////////////////////////////////////////////////////////////////////////////////
50 /// Default destructor for a LeafObject.
51 
53 {
54 }
55 
56 ////////////////////////////////////////////////////////////////////////////////
57 /// Pack leaf elements in Basket output buffer.
58 
60 {
61  if (!fObjAddress) return;
62  TObject *object = GetObject();
63  if (object) {
64  if (fVirtual) {
65  UChar_t n = (UChar_t) strlen(object->ClassName());
66  b << n;
67  b.WriteFastArray(object->ClassName(),n+1);
68  }
69  object->Streamer(b);
70  } else {
71  if (fClass) {
72  if (fClass->Property() & kIsAbstract) object = new TObject;
73  else object = (TObject *)fClass->New();
74  object->SetBit(kInvalidObject);
75  object->SetUniqueID(123456789);
76  object->Streamer(b);
77  if (fClass->Property() & kIsAbstract) delete object;
78  else fClass->Destructor(object);
79  } else {
80  Error("FillBasket","Attempt to write a NULL object in leaf:%s",GetName());
81  }
82  }
83 }
84 
85 ////////////////////////////////////////////////////////////////////////////////
86 /// Returns pointer to method corresponding to name.
87 ///
88 /// name is a string with the general form "method(list of params)"
89 /// If list of params is omitted, () is assumed;
90 
92 {
93  char *namecpy = new char[strlen(name)+1];
94  strcpy(namecpy,name);
95  char *params = strchr(namecpy,'(');
96  if (params) { *params = 0; params++; }
97  else params = (char *) ")";
98 
100  TMethodCall *m = new TMethodCall(fClass, namecpy, params);
101  delete [] namecpy;
102  if (m->GetMethod()) return m;
103  Error("GetMethodCall","Unknown method:%s",name);
104  delete m;
105  return 0;
106 }
107 
108 ////////////////////////////////////////////////////////////////////////////////
109 /// Returns name of leaf type.
110 
111 const char *TLeafObject::GetTypeName() const
112 {
113  return fTitle.Data();
114 }
115 
116 ////////////////////////////////////////////////////////////////////////////////
117 /// This method must be overridden to handle object notifcation.
118 
120 {
122  return kFALSE;
123 }
124 
125 ////////////////////////////////////////////////////////////////////////////////
126 /// Prints leaf value.
127 
129 {
130  printf("%lx\n",(Long_t)GetValuePointer());
131 }
132 
133 ////////////////////////////////////////////////////////////////////////////////
134 /// Read leaf elements from Basket input buffer.
135 
137 {
138  char classname[128];
139  UChar_t n;
140  if (fVirtual) {
141  b >> n;
142  b.ReadFastArray(classname,n+1);
143  fClass = TClass::GetClass(classname);
144  }
145  if (fClass) {
146  TObject *object;
147  if (!fObjAddress) {
148  Long_t *voidobj = new Long_t[1];
149  fObjAddress = (void **)voidobj;
150  *fObjAddress = (TObject *)fClass->New();
151  }
152  object = (TObject*)(*fObjAddress);
153  if (fBranch->IsAutoDelete()) {
154  fClass->Destructor(object);
155  object = (TObject *)fClass->New();
156  }
157  if (!object) return;
158 
159  if (fClass->GetState() > TClass::kEmulated) {
160  object->Streamer(b);
161  } else {
162  //emulated class has no Streamer
163  if (!TestBit(kWarn)) {
164  Warning("ReadBasket","%s::Streamer not available, using TClass::ReadBuffer instead",fClass->GetName());
165  SetBit(kWarn);
166  }
167  fClass->ReadBuffer(b,object);
168  }
169  // in case we had written a null pointer a Zombie object was created
170  // we must delete it
171  if (object->TestBit(kInvalidObject)) {
172  if (object->GetUniqueID() == 123456789) {
173  fClass->Destructor(object);
174  object = 0;
175  }
176  }
177  *fObjAddress = object;
178  } else GetBranch()->SetAddress(0);
179 }
180 
181 ////////////////////////////////////////////////////////////////////////////////
182 /// Set leaf buffer data address.
183 
184 void TLeafObject::SetAddress(void *add)
185 {
186  fObjAddress = (void **)add;
187 }
188 
189 ////////////////////////////////////////////////////////////////////////////////
190 /// Stream an object of class TLeafObject.
191 
192 void TLeafObject::Streamer(TBuffer &b)
193 {
194  if (b.IsReading()) {
195  UInt_t R__s, R__c;
196  Version_t R__v = b.ReadVersion(&R__s, &R__c);
197  if (R__v > 3 || R__v == 2) {
198  b.ReadClassBuffer(TLeafObject::Class(), this, R__v, R__s, R__c);
199  if (R__v == 2) fVirtual = kTRUE;
200  fObjAddress = 0;
202  if (!fClass) Warning("Streamer","Cannot find class:%s",fTitle.Data());
203 
204  // We should rewarn in this process.
205  ResetBit(kWarn);
207 
208  return;
209  }
210  //====process old versions before automatic schema evolution
211  TLeaf::Streamer(b);
212  fObjAddress = 0;
214  if (!fClass) Warning("Streamer","Cannot find class:%s",fTitle.Data());
215  if (R__v < 1) fVirtual = kFALSE;
216  if (R__v == 1) fVirtual = kTRUE;
217  if (R__v == 3) b >> fVirtual;
218  // We should rewarn in this process.
220  //====end of old versions
221 
222  } else {
223  b.WriteClassBuffer(TLeafObject::Class(),this);
224  }
225 }
226 
227 ////////////////////////////////////////////////////////////////////////////////
228 /// Return true if this leaf is does not have any sub-branch/leaf.
229 
231 {
233  return kTRUE;
234 }
m
auto * m
Definition: textangle.C:8
n
const Int_t n
Definition: legend1.C:16
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
TObject::TestBit
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
Definition: TObject.h:172
Version_t
short Version_t
Definition: RtypesCore.h:65
TBranch::IsAutoDelete
Bool_t IsAutoDelete() const
Return kTRUE if an existing object in a TBranchObject must be deleted.
Definition: TBranch.cxx:2095
TLeafObject.h
TLeafObject::fClass
TClassRef fClass
! pointer to class
Definition: TLeafObject.h:34
TLeafObject::kWarn
@ kWarn
Definition: TLeafObject.h:40
TString::Data
const char * Data() const
Definition: TString.h:369
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
TNamed::GetTitle
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:54
TClass::Property
Long_t Property() const
Set TObject::fBits and fStreamerType to cache information about the class.
Definition: TClass.cxx:5857
TBranch::GetListOfBranches
TObjArray * GetListOfBranches()
Definition: TBranch.h:242
TLeafObject::IsOnTerminalBranch
Bool_t IsOnTerminalBranch() const
Return true if this leaf is does not have any sub-branch/leaf.
Definition: TLeafObject.cxx:230
TBranch.h
TObject::Error
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:890
TLeafObject::GetObject
TObject * GetObject() const
Definition: TLeafObject.h:61
TClass.h
TBuffer
Definition: TBuffer.h:43
TLeafObject::GetMethodCall
TMethodCall * GetMethodCall(const char *name)
Returns pointer to method corresponding to name.
Definition: TLeafObject.cxx:91
b
#define b(i)
Definition: RSha256.hxx:118
bool
object
TNamed::fTitle
TString fTitle
Definition: TNamed.h:39
TLeafObject::GetValuePointer
virtual void * GetValuePointer() const
Definition: TLeafObject.h:63
TLeafObject::ReadBasket
virtual void ReadBasket(TBuffer &b)
Read leaf elements from Basket input buffer.
Definition: TLeafObject.cxx:136
TClass::kEmulated
@ kEmulated
Definition: TClass.h:124
TBranch
Definition: TBranch.h:89
TDataType.h
TClass::ReadBuffer
Int_t ReadBuffer(TBuffer &b, void *pointer, Int_t version, UInt_t start, UInt_t count)
Function called by the Streamer functions to deserialize information from buffer b into object at p.
Definition: TClass.cxx:6523
TBuffer.h
TObject::ResetBit
void ResetBit(UInt_t f)
Definition: TObject.h:171
TLeafObject::~TLeafObject
virtual ~TLeafObject()
Default destructor for a LeafObject.
Definition: TLeafObject.cxx:52
TClass::New
void * New(ENewType defConstructor=kClassNew, Bool_t quiet=kFALSE) const
Return a pointer to a newly allocated object of this class.
Definition: TClass.cxx:4881
TLeaf
Definition: TLeaf.h:57
TObject::SetBit
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition: TObject.cxx:696
TLeafObject::fObjAddress
void ** fObjAddress
! Address of Pointer to object
Definition: TLeafObject.h:35
TObjArray::GetEntriesFast
Int_t GetEntriesFast() const
Definition: TObjArray.h:64
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
Long_t
long Long_t
Definition: RtypesCore.h:54
TLeafObject::Notify
virtual Bool_t Notify()
This method must be overridden to handle object notifcation.
Definition: TLeafObject.cxx:119
TLeafObject
Definition: TLeafObject.h:31
UInt_t
unsigned int UInt_t
Definition: RtypesCore.h:46
TClass::GetClass
static TClass * GetClass(const char *name, Bool_t load=kTRUE, Bool_t silent=kFALSE)
Static method returning pointer to TClass of the specified class name.
Definition: TClass.cxx:2925
TLeafObject::FillBasket
virtual void FillBasket(TBuffer &b)
Pack leaf elements in Basket output buffer.
Definition: TLeafObject.cxx:59
TBranch::SetAddress
virtual void SetAddress(void *add)
Set address of this branch.
Definition: TBranch.cxx:2514
TLeafObject::kOldWarn
@ kOldWarn
Definition: TLeafObject.h:49
TNamed::SetTitle
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition: TNamed.cxx:164
TLeafObject::SetAddress
virtual void SetAddress(void *add=0)
Set leaf buffer data address.
Definition: TLeafObject.cxx:184
TObject::Warning
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition: TObject.cxx:876
TObject::kInvalidObject
@ kInvalidObject
if object ctor succeeded but object should not be used
Definition: TObject.h:68
TLeaf::GetBranch
TBranch * GetBranch() const
Definition: TLeaf.h:115
UChar_t
unsigned char UChar_t
Definition: RtypesCore.h:38
TClass::Destructor
void Destructor(void *obj, Bool_t dtorOnly=kFALSE)
Explicitly call destructor for object.
Definition: TClass.cxx:5238
TObject
Definition: TObject.h:37
name
char name[80]
Definition: TGX11.cxx:110
TClass::GetState
EState GetState() const
Definition: TClass.h:439
TMethodCall
Definition: TMethodCall.h:37
TLeafObject::PrintValue
virtual void PrintValue(Int_t i=0) const
Prints leaf value.
Definition: TLeafObject.cxx:128
TLeafObject::fVirtual
Bool_t fVirtual
Support for polymorphism, when set classname is written with object.
Definition: TLeafObject.h:36
TNamed::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:53
type
int type
Definition: TGX11.cxx:121
Class
void Class()
Definition: Class.C:29
TLeafObject::TLeafObject
TLeafObject()
Default constructor for LeafObject.
Definition: TLeafObject.cxx:30
TLeaf::fBranch
TBranch * fBranch
! Pointer to supporting branch (we do not own the branch)
Definition: TLeaf.h:78
TLeafObject::GetTypeName
const char * GetTypeName() const
Returns name of leaf type.
Definition: TLeafObject.cxx:111
int
kIsAbstract
@ kIsAbstract
Definition: TDictionary.h:71
TMethodCall.h