#include "TLeafObject.h"
#include "TBranch.h"
#include "TClass.h"
#include "TMethodCall.h"
#include "TDataType.h"
ClassImp(TLeafObject)
TLeafObject::TLeafObject(): TLeaf()
{
fClass = 0;
fObjAddress = 0;
fVirtual = kTRUE;
}
TLeafObject::TLeafObject(TBranch *parent, const char *name, const char *type)
:TLeaf(parent, name,type)
{
SetTitle(type);
fClass = TClass::GetClass(type);
fObjAddress = 0;
fVirtual = kTRUE;
}
TLeafObject::~TLeafObject()
{
}
void TLeafObject::FillBasket(TBuffer &b)
{
if (!fObjAddress) return;
TObject *object = GetObject();
if (object) {
if (fVirtual) {
UChar_t n = strlen(object->ClassName());
b << n;
b.WriteFastArray(object->ClassName(),n+1);
}
object->Streamer(b);
} else {
if (fClass) {
if (fClass->Property() & kIsAbstract) object = new TObject;
else object = (TObject *)fClass->New();
object->SetBit(kInvalidObject);
object->SetUniqueID(123456789);
object->Streamer(b);
if (fClass->Property() & kIsAbstract) delete object;
else fClass->Destructor(object);
} else {
Error("FillBasket","Attempt to write a NULL object in leaf:%s",GetName());
}
}
}
TMethodCall *TLeafObject::GetMethodCall(const char *name)
{
char *namecpy = new char[strlen(name)+1];
strcpy(namecpy,name);
char *params = strchr(namecpy,'(');
if (params) { *params = 0; params++; }
else params = (char *) ")";
if (!fClass) fClass = TClass::GetClass(GetTitle());
TMethodCall *m = new TMethodCall(fClass, namecpy, params);
delete [] namecpy;
if (m->GetMethod()) return m;
Error("GetMethodCall","Unknown method:%s",name);
delete m;
return 0;
}
const char *TLeafObject::GetTypeName() const
{
return fTitle.Data();
}
Bool_t TLeafObject::Notify()
{
fClass = TClass::GetClass(GetTitle());
return kFALSE;
}
void TLeafObject::PrintValue(Int_t) const
{
printf("%lx\n",(Long_t)GetValuePointer());
}
void TLeafObject::ReadBasket(TBuffer &b)
{
char classname[128];
UChar_t n;
if (fVirtual) {
b >> n;
b.ReadFastArray(classname,n+1);
fClass = TClass::GetClass(classname);
}
if (fClass) {
TObject *object;
if (!fObjAddress) {
Long_t *voidobj = new Long_t[1];
fObjAddress = (void **)voidobj;
*fObjAddress = (TObject *)fClass->New();
}
object = (TObject*)(*fObjAddress);
if (fBranch->IsAutoDelete()) {
fClass->Destructor(object);
object = (TObject *)fClass->New();
}
if (!object) return;
if (fClass->GetClassInfo()) {
object->Streamer(b);
} else {
if (!TestBit(kWarn)) {
Warning("ReadBasket","%s::Streamer not available, using TClass::ReadBuffer instead",fClass->GetName());
SetBit(kWarn);
}
fClass->ReadBuffer(b,object);
}
if (object->TestBit(kInvalidObject)) {
if (object->GetUniqueID() == 123456789) {
fClass->Destructor(object);
object = 0;
}
}
*fObjAddress = object;
} else GetBranch()->SetAddress(0);
}
void TLeafObject::SetAddress(void *add)
{
fObjAddress = (void **)add;
}
void TLeafObject::Streamer(TBuffer &b)
{
if (b.IsReading()) {
UInt_t R__s, R__c;
Version_t R__v = b.ReadVersion(&R__s, &R__c);
if (R__v > 3 || R__v == 2) {
b.ReadClassBuffer(TLeafObject::Class(), this, R__v, R__s, R__c);
if (R__v == 2) fVirtual = kTRUE;
fObjAddress = 0;
fClass = TClass::GetClass(fTitle.Data());
if (!fClass) Warning("Streamer","Cannot find class:%s",fTitle.Data());
return;
}
TLeaf::Streamer(b);
fObjAddress = 0;
fClass = TClass::GetClass(fTitle.Data());
if (!fClass) Warning("Streamer","Cannot find class:%s",fTitle.Data());
if (R__v < 1) fVirtual = kFALSE;
if (R__v == 1) fVirtual = kTRUE;
if (R__v == 3) b >> fVirtual;
} else {
b.WriteClassBuffer(TLeafObject::Class(),this);
}
}
Bool_t TLeafObject::IsOnTerminalBranch() const
{
if (fBranch->GetListOfBranches()->GetEntriesFast()) return kFALSE;
return kTRUE;
}