ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 "TBuffer.h"
19 #include "TClass.h"
20 #include "TMethodCall.h"
21 #include "TDataType.h"
22 
24 
25 ////////////////////////////////////////////////////////////////////////////////
26 /// Default constructor for LeafObject.
27 
29 {
30  fClass = 0;
31  fObjAddress = 0;
32  fVirtual = kTRUE;
33 }
34 
35 ////////////////////////////////////////////////////////////////////////////////
36 /// Create a LeafObject.
37 
38 TLeafObject::TLeafObject(TBranch *parent, const char *name, const char *type)
39  :TLeaf(parent, name,type)
40 {
41  SetTitle(type);
42  fClass = TClass::GetClass(type);
43  fObjAddress = 0;
44  fVirtual = kTRUE;
45 }
46 
47 ////////////////////////////////////////////////////////////////////////////////
48 /// Default destructor for a LeafObject.
49 
51 {
52 }
53 
54 ////////////////////////////////////////////////////////////////////////////////
55 /// Pack leaf elements in Basket output buffer.
56 
58 {
59  if (!fObjAddress) return;
60  TObject *object = GetObject();
61  if (object) {
62  if (fVirtual) {
63  UChar_t n = strlen(object->ClassName());
64  b << n;
65  b.WriteFastArray(object->ClassName(),n+1);
66  }
67  object->Streamer(b);
68  } else {
69  if (fClass) {
70  if (fClass->Property() & kIsAbstract) object = new TObject;
71  else object = (TObject *)fClass->New();
72  object->SetBit(kInvalidObject);
73  object->SetUniqueID(123456789);
74  object->Streamer(b);
75  if (fClass->Property() & kIsAbstract) delete object;
76  else fClass->Destructor(object);
77  } else {
78  Error("FillBasket","Attempt to write a NULL object in leaf:%s",GetName());
79  }
80  }
81 }
82 
83 ////////////////////////////////////////////////////////////////////////////////
84 /// Returns pointer to method corresponding to name.
85 ///
86 /// name is a string with the general form "method(list of params)"
87 /// If list of params is omitted, () is assumed;
88 
90 {
91  char *namecpy = new char[strlen(name)+1];
92  strcpy(namecpy,name);
93  char *params = strchr(namecpy,'(');
94  if (params) { *params = 0; params++; }
95  else params = (char *) ")";
96 
98  TMethodCall *m = new TMethodCall(fClass, namecpy, params);
99  delete [] namecpy;
100  if (m->GetMethod()) return m;
101  Error("GetMethodCall","Unknown method:%s",name);
102  delete m;
103  return 0;
104 }
105 
106 ////////////////////////////////////////////////////////////////////////////////
107 /// Returns name of leaf type.
108 
109 const char *TLeafObject::GetTypeName() const
110 {
111  return fTitle.Data();
112 }
113 
114 ////////////////////////////////////////////////////////////////////////////////
115 /// This method must be overridden to handle object notifcation.
116 
118 {
120  return kFALSE;
121 }
122 
123 ////////////////////////////////////////////////////////////////////////////////
124 /// Prints leaf value.
125 
127 {
128  printf("%lx\n",(Long_t)GetValuePointer());
129 }
130 
131 ////////////////////////////////////////////////////////////////////////////////
132 /// Read leaf elements from Basket input buffer.
133 
135 {
136  char classname[128];
137  UChar_t n;
138  if (fVirtual) {
139  b >> n;
140  b.ReadFastArray(classname,n+1);
141  fClass = TClass::GetClass(classname);
142  }
143  if (fClass) {
144  TObject *object;
145  if (!fObjAddress) {
146  Long_t *voidobj = new Long_t[1];
147  fObjAddress = (void **)voidobj;
148  *fObjAddress = (TObject *)fClass->New();
149  }
150  object = (TObject*)(*fObjAddress);
151  if (fBranch->IsAutoDelete()) {
152  fClass->Destructor(object);
153  object = (TObject *)fClass->New();
154  }
155  if (!object) return;
156 
157  if (fClass->GetState() > TClass::kEmulated) {
158  object->Streamer(b);
159  } else {
160  //emulated class has no Streamer
161  if (!TestBit(kWarn)) {
162  Warning("ReadBasket","%s::Streamer not available, using TClass::ReadBuffer instead",fClass->GetName());
163  SetBit(kWarn);
164  }
165  fClass->ReadBuffer(b,object);
166  }
167  // in case we had written a null pointer a Zombie object was created
168  // we must delete it
169  if (object->TestBit(kInvalidObject)) {
170  if (object->GetUniqueID() == 123456789) {
171  fClass->Destructor(object);
172  object = 0;
173  }
174  }
175  *fObjAddress = object;
176  } else GetBranch()->SetAddress(0);
177 }
178 
179 ////////////////////////////////////////////////////////////////////////////////
180 /// Set leaf buffer data address.
181 
182 void TLeafObject::SetAddress(void *add)
183 {
184  fObjAddress = (void **)add;
185 }
186 
187 ////////////////////////////////////////////////////////////////////////////////
188 /// Stream an object of class TLeafObject.
189 
190 void TLeafObject::Streamer(TBuffer &b)
191 {
192  if (b.IsReading()) {
193  UInt_t R__s, R__c;
194  Version_t R__v = b.ReadVersion(&R__s, &R__c);
195  if (R__v > 3 || R__v == 2) {
196  b.ReadClassBuffer(TLeafObject::Class(), this, R__v, R__s, R__c);
197  if (R__v == 2) fVirtual = kTRUE;
198  fObjAddress = 0;
200  if (!fClass) Warning("Streamer","Cannot find class:%s",fTitle.Data());
201  return;
202  }
203  //====process old versions before automatic schema evolution
204  TLeaf::Streamer(b);
205  fObjAddress = 0;
207  if (!fClass) Warning("Streamer","Cannot find class:%s",fTitle.Data());
208  if (R__v < 1) fVirtual = kFALSE;
209  if (R__v == 1) fVirtual = kTRUE;
210  if (R__v == 3) b >> fVirtual;
211  //====end of old versions
212 
213  } else {
215  }
216 }
217 
218 ////////////////////////////////////////////////////////////////////////////////
219 /// Return true if this leaf is does not have any sub-branch/leaf.
220 
222 {
224  return kTRUE;
225 }
EState GetState() const
Definition: TClass.h:442
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:2049
Bool_t IsReading() const
Definition: TBuffer.h:83
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:5560
Buffer base class used for serializing objects.
Definition: TBuffer.h:42
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:6196
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:4602
TMethodCall * GetMethodCall(const char *name)
Returns pointer to method corresponding to name.
Definition: TLeafObject.cxx:89
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:23
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:4959
virtual ~TLeafObject()
Default destructor for a LeafObject.
Definition: TLeafObject.cxx:50
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:57
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:1653
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:2801
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