ROOT  6.06/09
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 A TLeaf for a general object derived from TObject.
14 */
15 
16 #include "TLeafObject.h"
17 #include "TBranch.h"
18 #include "TClass.h"
19 #include "TMethodCall.h"
20 #include "TDataType.h"
21 
23 
24 ////////////////////////////////////////////////////////////////////////////////
25 /// Default constructor for LeafObject.
26 
28 {
29  fClass = 0;
30  fObjAddress = 0;
31  fVirtual = kTRUE;
32 }
33 
34 ////////////////////////////////////////////////////////////////////////////////
35 /// Create a LeafObject.
36 
37 TLeafObject::TLeafObject(TBranch *parent, const char *name, const char *type)
38  :TLeaf(parent, name,type)
39 {
40  SetTitle(type);
41  fClass = TClass::GetClass(type);
42  fObjAddress = 0;
43  fVirtual = kTRUE;
44 }
45 
46 ////////////////////////////////////////////////////////////////////////////////
47 /// Default destructor for a LeafObject.
48 
50 {
51 }
52 
53 ////////////////////////////////////////////////////////////////////////////////
54 /// Pack leaf elements in Basket output buffer.
55 
57 {
58  if (!fObjAddress) return;
59  TObject *object = GetObject();
60  if (object) {
61  if (fVirtual) {
62  UChar_t n = strlen(object->ClassName());
63  b << n;
64  b.WriteFastArray(object->ClassName(),n+1);
65  }
66  object->Streamer(b);
67  } else {
68  if (fClass) {
69  if (fClass->Property() & kIsAbstract) object = new TObject;
70  else object = (TObject *)fClass->New();
71  object->SetBit(kInvalidObject);
72  object->SetUniqueID(123456789);
73  object->Streamer(b);
74  if (fClass->Property() & kIsAbstract) delete object;
75  else fClass->Destructor(object);
76  } else {
77  Error("FillBasket","Attempt to write a NULL object in leaf:%s",GetName());
78  }
79  }
80 }
81 
82 ////////////////////////////////////////////////////////////////////////////////
83 /// Returns pointer to method corresponding to name.
84 ///
85 /// name is a string with the general form "method(list of params)"
86 /// If list of params is omitted, () is assumed;
87 
89 {
90  char *namecpy = new char[strlen(name)+1];
91  strcpy(namecpy,name);
92  char *params = strchr(namecpy,'(');
93  if (params) { *params = 0; params++; }
94  else params = (char *) ")";
95 
97  TMethodCall *m = new TMethodCall(fClass, namecpy, params);
98  delete [] namecpy;
99  if (m->GetMethod()) return m;
100  Error("GetMethodCall","Unknown method:%s",name);
101  delete m;
102  return 0;
103 }
104 
105 ////////////////////////////////////////////////////////////////////////////////
106 /// Returns name of leaf type.
107 
108 const char *TLeafObject::GetTypeName() const
109 {
110  return fTitle.Data();
111 }
112 
113 ////////////////////////////////////////////////////////////////////////////////
114 /// This method must be overridden to handle object notifcation.
115 
117 {
119  return kFALSE;
120 }
121 
122 ////////////////////////////////////////////////////////////////////////////////
123 /// Prints leaf value.
124 
126 {
127  printf("%lx\n",(Long_t)GetValuePointer());
128 }
129 
130 ////////////////////////////////////////////////////////////////////////////////
131 /// Read leaf elements from Basket input buffer.
132 
134 {
135  char classname[128];
136  UChar_t n;
137  if (fVirtual) {
138  b >> n;
139  b.ReadFastArray(classname,n+1);
140  fClass = TClass::GetClass(classname);
141  }
142  if (fClass) {
143  TObject *object;
144  if (!fObjAddress) {
145  Long_t *voidobj = new Long_t[1];
146  fObjAddress = (void **)voidobj;
147  *fObjAddress = (TObject *)fClass->New();
148  }
149  object = (TObject*)(*fObjAddress);
150  if (fBranch->IsAutoDelete()) {
151  fClass->Destructor(object);
152  object = (TObject *)fClass->New();
153  }
154  if (!object) return;
155 
156  if (fClass->GetState() > TClass::kEmulated) {
157  object->Streamer(b);
158  } else {
159  //emulated class has no Streamer
160  if (!TestBit(kWarn)) {
161  Warning("ReadBasket","%s::Streamer not available, using TClass::ReadBuffer instead",fClass->GetName());
162  SetBit(kWarn);
163  }
164  fClass->ReadBuffer(b,object);
165  }
166  // in case we had written a null pointer a Zombie object was created
167  // we must delete it
168  if (object->TestBit(kInvalidObject)) {
169  if (object->GetUniqueID() == 123456789) {
170  fClass->Destructor(object);
171  object = 0;
172  }
173  }
174  *fObjAddress = object;
175  } else GetBranch()->SetAddress(0);
176 }
177 
178 ////////////////////////////////////////////////////////////////////////////////
179 /// Set leaf buffer data address.
180 
181 void TLeafObject::SetAddress(void *add)
182 {
183  fObjAddress = (void **)add;
184 }
185 
186 ////////////////////////////////////////////////////////////////////////////////
187 /// Stream an object of class TLeafObject.
188 
189 void TLeafObject::Streamer(TBuffer &b)
190 {
191  if (b.IsReading()) {
192  UInt_t R__s, R__c;
193  Version_t R__v = b.ReadVersion(&R__s, &R__c);
194  if (R__v > 3 || R__v == 2) {
195  b.ReadClassBuffer(TLeafObject::Class(), this, R__v, R__s, R__c);
196  if (R__v == 2) fVirtual = kTRUE;
197  fObjAddress = 0;
199  if (!fClass) Warning("Streamer","Cannot find class:%s",fTitle.Data());
200  return;
201  }
202  //====process old versions before automatic schema evolution
203  TLeaf::Streamer(b);
204  fObjAddress = 0;
206  if (!fClass) Warning("Streamer","Cannot find class:%s",fTitle.Data());
207  if (R__v < 1) fVirtual = kFALSE;
208  if (R__v == 1) fVirtual = kTRUE;
209  if (R__v == 3) b >> fVirtual;
210  //====end of old versions
211 
212  } else {
214  }
215 }
216 
217 ////////////////////////////////////////////////////////////////////////////////
218 /// Return true if this leaf is does not have any sub-branch/leaf.
219 
221 {
223  return kTRUE;
224 }
EState GetState() const
Definition: TClass.h:443
TString fTitle
Definition: TNamed.h:37
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:52
A TLeaf describes individual elements of a TBranch See TBranch structure in TTree.
Definition: TLeaf.h:37
Bool_t IsOnTerminalBranch() const
Return true if this leaf is does not have any sub-branch/leaf.
virtual Int_t WriteClassBuffer(const TClass *cl, void *pointer)=0
Bool_t fVirtual
Address of Pointer to object.
Definition: TLeafObject.h:40
virtual void SetAddress(void *add)
Set address of this branch.
Definition: TBranch.cxx:2048
Bool_t IsReading() const
Definition: TBuffer.h:81
short Version_t
Definition: RtypesCore.h:61
A TLeaf for a general object derived from TObject.
Definition: TLeafObject.h:35
Long_t Property() const
Set TObject::fBits and fStreamerType to cache information about the class.
Definition: TClass.cxx:5652
Buffer base class used for serializing objects.
Definition: TBuffer.h:40
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
TBranch * GetBranch() const
Definition: TLeaf.h:70
TClassRef fClass
Definition: TLeafObject.h:38
Int_t GetEntriesFast() const
Definition: TObjArray.h:66
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition: TObject.cxx:732
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:6288
const char * Data() const
Definition: TString.h:349
void Class()
Definition: Class.C:29
void * New(ENewType defConstructor=kClassNew, Bool_t quiet=kFALSE) const
Return a pointer to a newly allocated object of this class.
Definition: TClass.cxx:4683
TMethodCall * GetMethodCall(const char *name)
Returns pointer to method corresponding to name.
Definition: TLeafObject.cxx:88
TClass * fClass
pointer to the foreign object
const char * GetTypeName() const
Returns name of leaf type.
TObjArray * GetListOfBranches()
Definition: TBranch.h:177
virtual void ReadBasket(TBuffer &b)
Read leaf elements from Basket input buffer.
Method or function calling interface.
Definition: TMethodCall.h:41
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:918
virtual void * GetValuePointer() const
Definition: TLeafObject.h:54
ClassImp(TLeafObject) TLeafObject
Default constructor for LeafObject.
Definition: TLeafObject.cxx:22
void ** fObjAddress
pointer to class
Definition: TLeafObject.h:39
virtual const char * ClassName() const
Returns name of class to which the object belongs.
Definition: TObject.cxx:187
virtual void SetAddress(void *add=0)
Set leaf buffer data address.
void Destructor(void *obj, Bool_t dtorOnly=kFALSE)
Explicitly call destructor for object.
Definition: TClass.cxx:5040
virtual ~TLeafObject()
Default destructor for a LeafObject.
Definition: TLeafObject.cxx:49
unsigned int UInt_t
Definition: RtypesCore.h:42
Bool_t TestBit(UInt_t f) const
Definition: TObject.h:173
TMarker * m
Definition: textangle.C:8
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
virtual void ReadFastArray(Bool_t *b, Int_t n)=0
virtual void WriteFastArray(const Bool_t *b, Int_t n)=0
virtual void FillBasket(TBuffer &b)
Pack leaf elements in Basket output buffer.
Definition: TLeafObject.cxx:56
long Long_t
Definition: RtypesCore.h:50
virtual Int_t ReadClassBuffer(const TClass *cl, void *pointer, const TClass *onfile_class=0)=0
TFunction * GetMethod()
Returns the TMethod describing the method to be executed.
virtual Bool_t Notify()
This method must be overridden to handle object notifcation.
int type
Definition: TGX11.cxx:120
ClassImp(TMCParticle) void TMCParticle printf(": p=(%7.3f,%7.3f,%9.3f) ;", fPx, fPy, fPz)
Bool_t IsAutoDelete() const
Return kTRUE if an existing object in a TBranchObject must be deleted.
Definition: TBranch.cxx:1652
TObject * GetObject() const
Definition: TLeafObject.h:52
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:2881
virtual UInt_t GetUniqueID() const
Return the unique object id.
Definition: TObject.cxx:433
#define name(a, b)
Definition: linkTestLib0.cpp:5
Mother of all ROOT objects.
Definition: TObject.h:58
TBranch * fBranch
Definition: TLeaf.h:48
unsigned char UChar_t
Definition: RtypesCore.h:34
A TTree is a list of TBranches.
Definition: TBranch.h:58
const Bool_t kTRUE
Definition: Rtypes.h:91
virtual void SetTitle(const char *title="")
Change (i.e. set) the title of the TNamed.
Definition: TNamed.cxx:152
virtual void PrintValue(Int_t i=0) const
Prints leaf value.
const Int_t n
Definition: legend1.C:16
virtual Version_t ReadVersion(UInt_t *start=0, UInt_t *bcnt=0, const TClass *cl=0)=0
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition: TObject.cxx:904