Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
15A 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
25
26////////////////////////////////////////////////////////////////////////////////
27/// Default constructor for LeafObject.
28
30{
31 fClass = nullptr;
32 fObjAddress = nullptr;
33 fVirtual = true;
34}
35
36////////////////////////////////////////////////////////////////////////////////
37/// Create a LeafObject.
38
39TLeafObject::TLeafObject(TBranch *parent, const char *name, const char *type)
40 :TLeaf(parent, name,type)
41{
44 fObjAddress = nullptr;
45 fVirtual = true;
46}
47
48////////////////////////////////////////////////////////////////////////////////
49/// Default destructor for a LeafObject.
50
54
55////////////////////////////////////////////////////////////////////////////////
56/// Pack leaf elements in Basket output buffer.
57
59{
60 if (!fObjAddress) return;
61 TObject *object = GetObject();
62 if (object) {
63 if (fVirtual) {
64 UChar_t n = (UChar_t) strlen(object->ClassName());
65 b << n;
66 b.WriteFastArray(object->ClassName(),n+1);
67 }
68 object->Streamer(b);
69 } else {
70 if (fClass) {
71 if (fClass->Property() & kIsAbstract) object = new TObject;
72 else object = (TObject *)fClass->New();
73 object->SetBit(kInvalidObject);
74 object->SetUniqueID(123456789);
75 object->Streamer(b);
76 if (fClass->Property() & kIsAbstract) delete object;
77 else fClass->Destructor(object);
78 } else {
79 Error("FillBasket","Attempt to write a NULL object in leaf:%s",GetName());
80 }
81 }
82}
83
84////////////////////////////////////////////////////////////////////////////////
85/// Returns pointer to method corresponding to name.
86///
87/// name is a string with the general form "method(list of params)"
88/// If list of params is omitted, () is assumed;
89
91{
92 char *namecpy = new char[strlen(name)+1];
94 char *params = strchr(namecpy,'(');
95 if (params) { *params = 0; params++; }
96 else params = (char *) ")";
97
99 TMethodCall *m = new TMethodCall(fClass, namecpy, params);
100 delete [] namecpy;
101 if (m->GetMethod()) return m;
102 Error("GetMethodCall","Unknown method:%s",name);
103 delete m;
104 return nullptr;
105}
106
107////////////////////////////////////////////////////////////////////////////////
108/// Returns name of leaf type.
109
110const char *TLeafObject::GetTypeName() const
111{
112 return fTitle.Data();
113}
114
115////////////////////////////////////////////////////////////////////////////////
116/// This method must be overridden to handle object notification.
117
119{
121 return false;
122}
123
124////////////////////////////////////////////////////////////////////////////////
125/// Prints leaf value.
126
128{
129 printf("%zx\n",(size_t)GetValuePointer());
130}
131
132////////////////////////////////////////////////////////////////////////////////
133/// Read leaf elements from Basket input buffer.
134
136{
137 char classname[128];
138 UChar_t n;
139 if (fVirtual) {
140 b >> n;
141 b.ReadFastArray(classname,n+1);
142 fClass = TClass::GetClass(classname);
143 }
144 if (fClass) {
146 if (!fObjAddress) {
147 Longptr_t *voidobj = new Longptr_t[1];
148 fObjAddress = (void **)voidobj;
149 *fObjAddress = (TObject *)fClass->New();
150 }
151 object = (TObject*)(*fObjAddress);
152 if (fBranch->IsAutoDelete()) {
153 fClass->Destructor(object);
154 object = (TObject *)fClass->New();
155 }
156 if (!object) return;
157
159 object->Streamer(b);
160 } else {
161 //emulated class has no Streamer
162 if (!TestBit(kWarn)) {
163 Warning("ReadBasket","%s::Streamer not available, using TClass::ReadBuffer instead",fClass->GetName());
164 SetBit(kWarn);
165 }
166 fClass->ReadBuffer(b,object);
167 }
168 // in case we had written a null pointer a Zombie object was created
169 // we must delete it
170 if (object->TestBit(kInvalidObject)) {
171 if (object->GetUniqueID() == 123456789) {
172 fClass->Destructor(object);
173 object = nullptr;
174 }
175 }
177 } else GetBranch()->SetAddress(nullptr);
178}
179
180////////////////////////////////////////////////////////////////////////////////
181/// Set leaf buffer data address.
182
184{
185 fObjAddress = (void **)add;
186}
187
188////////////////////////////////////////////////////////////////////////////////
189/// Stream an object of class TLeafObject.
190
192{
193 if (b.IsReading()) {
195 Version_t R__v = b.ReadVersion(&R__s, &R__c);
196 if (R__v > 3 || R__v == 2) {
197 b.ReadClassBuffer(TLeafObject::Class(), this, R__v, R__s, R__c);
198 if (R__v == 2) fVirtual = true;
199 fObjAddress = nullptr;
201 if (!fClass) Warning("Streamer","Cannot find class:%s",fTitle.Data());
202
203 // We should rewarn in this process.
206
207 return;
208 }
209 //====process old versions before automatic schema evolution
211 fObjAddress = nullptr;
213 if (!fClass) Warning("Streamer","Cannot find class:%s",fTitle.Data());
214 if (R__v < 1) fVirtual = false;
215 if (R__v == 1) fVirtual = true;
216 if (R__v == 3) b >> fVirtual;
217 // We should rewarn in this process.
219 //====end of old versions
220
221 } else {
222 b.WriteClassBuffer(TLeafObject::Class(),this);
223 }
224}
225
226////////////////////////////////////////////////////////////////////////////////
227/// Return true if this leaf is does not have any sub-branch/leaf.
228
230{
231 if (fBranch->GetListOfBranches()->GetEntriesFast()) return false;
232 return true;
233}
#define b(i)
Definition RSha256.hxx:100
long Longptr_t
Integer large enough to hold a pointer (platform-dependent)
Definition RtypesCore.h:89
short Version_t
Class version identifier (short)
Definition RtypesCore.h:79
unsigned char UChar_t
Unsigned Character 1 byte (unsigned char)
Definition RtypesCore.h:52
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
@ kIsAbstract
Definition TDictionary.h:71
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
char name[80]
Definition TGX11.cxx:110
A TTree is a list of TBranches.
Definition TBranch.h:93
TObjArray * GetListOfBranches()
Definition TBranch.h:246
virtual void SetAddress(void *add)
Set address of this branch.
Definition TBranch.cxx:2694
bool IsAutoDelete() const
Return true if an existing object in a TBranchObject must be deleted.
Definition TBranch.cxx:2262
Buffer base class used for serializing objects.
Definition TBuffer.h:43
EState GetState() const
Definition TClass.h:501
void * New(ENewType defConstructor=kClassNew, Bool_t quiet=kFALSE) const
Return a pointer to a newly allocated object of this class.
Definition TClass.cxx:5017
void Destructor(void *obj, Bool_t dtorOnly=kFALSE)
Explicitly call destructor for object.
Definition TClass.cxx:5439
Long_t Property() const override
Returns the properties of the TClass as a bit field stored as a Long_t value.
Definition TClass.cxx:6128
@ kEmulated
Definition TClass.h:128
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:6806
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:2973
void ReadBasket(TBuffer &b) override
Read leaf elements from Basket input buffer.
bool IsOnTerminalBranch() const override
Return true if this leaf is does not have any sub-branch/leaf.
void Streamer(TBuffer &) override
Stream an object of class TLeafObject.
void FillBasket(TBuffer &b) override
Pack leaf elements in Basket output buffer.
TLeafObject()
Default constructor for LeafObject.
TObject * GetObject() const
Definition TLeafObject.h:61
bool Notify() override
This method must be overridden to handle object notification.
bool fVirtual
Support for polymorphism, when set classname is written with object.
Definition TLeafObject.h:36
const char * GetTypeName() const override
Returns name of leaf type.
~TLeafObject() override
Default destructor for a LeafObject.
void PrintValue(Int_t i=0) const override
Prints leaf value.
static TClass * Class()
void * GetValuePointer() const override
Definition TLeafObject.h:63
void ** fObjAddress
! Address of Pointer to object
Definition TLeafObject.h:35
TClassRef fClass
! pointer to class
Definition TLeafObject.h:34
void SetAddress(void *add=nullptr) override
Set leaf buffer data address.
TMethodCall * GetMethodCall(const char *name)
Returns pointer to method corresponding to name.
A TLeaf describes individual elements of a TBranch See TBranch structure in TTree.
Definition TLeaf.h:57
void Streamer(TBuffer &) override
Stream a class object.
Definition TLeaf.cxx:481
TBranch * GetBranch() const
Definition TLeaf.h:116
TBranch * fBranch
! Pointer to supporting branch (we do not own the branch)
Definition TLeaf.h:78
Method or function calling interface.
Definition TMethodCall.h:37
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition TNamed.cxx:173
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
const char * GetTitle() const override
Returns title of object.
Definition TNamed.h:50
TString fTitle
Definition TNamed.h:33
Int_t GetEntriesFast() const
Definition TObjArray.h:58
Mother of all ROOT objects.
Definition TObject.h:41
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
Definition TObject.h:202
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition TObject.cxx:1057
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition TObject.cxx:864
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:1071
void ResetBit(UInt_t f)
Definition TObject.h:201
@ kInvalidObject
if object ctor succeeded but object should not be used
Definition TObject.h:78
const char * Data() const
Definition TString.h:384
const Int_t n
Definition legend1.C:16
TMarker m
Definition textangle.C:8