// @(#)root/tree:$Id$
// Author: Rene Brun   27/01/96

/*************************************************************************
 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

//////////////////////////////////////////////////////////////////////////
//                                                                      //
// A TLeaf for a general object derived from TObject.                   //
//////////////////////////////////////////////////////////////////////////

#include "TLeafObject.h"
#include "TBranch.h"
#include "TClass.h"
#include "TMethodCall.h"
#include "TDataType.h"


ClassImp(TLeafObject)

//______________________________________________________________________________
TLeafObject::TLeafObject(): TLeaf()
{
//*-*-*-*-*-*Default constructor for LeafObject*-*-*-*-*-*-*-*-*-*-*-*-*-*
//*-*        =================================
   fClass      = 0;
   fObjAddress = 0;
   fVirtual    = kTRUE;
}

//______________________________________________________________________________
TLeafObject::TLeafObject(TBranch *parent, const char *name, const char *type)
   :TLeaf(parent, name,type)
{
//*-*-*-*-*-*-*-*-*-*-*-*-*Create a LeafObject*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
//*-*                      ==================
//*-*

   SetTitle(type);
   fClass      = TClass::GetClass(type);
   fObjAddress = 0;
   fVirtual    = kTRUE;
}

//______________________________________________________________________________
TLeafObject::~TLeafObject()
{
//*-*-*-*-*-*Default destructor for a LeafObject*-*-*-*-*-*-*-*-*-*-*-*
//*-*        ==================================

}


//______________________________________________________________________________
void TLeafObject::FillBasket(TBuffer &b)
{
//*-*-*-*-*-*-*-*-*-*-*Pack leaf elements in Basket output buffer*-*-*-*-*-*-*
//*-*                  =========================================

   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)
{
//*-*-*-*-*-*-*-*Returns pointer to method corresponding to name*-*-*-*-*-*-*
//*-*            ============================================
//*-*
//*-*    name is a string with the general form  "method(list of params)"
//*-*   If list of params is omitted, () is assumed;
//*-*

   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
{
//*-*-*-*-*-*-*-*Returns name of leaf type*-*-*-*-*-*-*-*-*-*-*-*
//*-*            =========================

   return fTitle.Data();
}

//______________________________________________________________________________
Bool_t TLeafObject::Notify()
{
   // This method must be overridden to handle object notifcation.

   fClass      = TClass::GetClass(GetTitle());
   return kFALSE;
}

//______________________________________________________________________________
void TLeafObject::PrintValue(Int_t) const
{
// Prints leaf value

   printf("%lx\n",(Long_t)GetValuePointer());
}

//______________________________________________________________________________
void TLeafObject::ReadBasket(TBuffer &b)
{
//*-*-*-*-*-*-*-*-*-*-*Read leaf elements from Basket input buffer*-*-*-*-*-*
//*-*                  ===========================================

   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->GetState() > TClass::kEmulated) {
         object->Streamer(b);
      } else {
         //emulated class has no Streamer
         if (!TestBit(kWarn)) {
            Warning("ReadBasket","%s::Streamer not available, using TClass::ReadBuffer instead",fClass->GetName());
            SetBit(kWarn);
         }
         fClass->ReadBuffer(b,object);
      }
      // in case we had written a null pointer a Zombie object was created
      // we must delete it
      if (object->TestBit(kInvalidObject)) {
         if (object->GetUniqueID() == 123456789) {
            fClass->Destructor(object);
            object = 0;
         }
      }
      *fObjAddress = object;
   } else GetBranch()->SetAddress(0);
}

//______________________________________________________________________________
void TLeafObject::SetAddress(void *add)
{
//*-*-*-*-*-*-*-*-*-*-*Set leaf buffer data address*-*-*-*-*-*
//*-*                  ============================

   fObjAddress = (void **)add;
}

//______________________________________________________________________________
void TLeafObject::Streamer(TBuffer &b)
{
   // Stream an object of class TLeafObject.

   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;
      }
      //====process old versions before automatic schema evolution
      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;
      //====end of old versions

   } else {
      b.WriteClassBuffer(TLeafObject::Class(),this);
   }
}

//______________________________________________________________________________
Bool_t TLeafObject::IsOnTerminalBranch() const
{
   // Return true if this leaf is does not have any sub-branch/leaf.

   if (fBranch->GetListOfBranches()->GetEntriesFast()) return kFALSE;
   return kTRUE;
}
 TLeafObject.cxx:1
 TLeafObject.cxx:2
 TLeafObject.cxx:3
 TLeafObject.cxx:4
 TLeafObject.cxx:5
 TLeafObject.cxx:6
 TLeafObject.cxx:7
 TLeafObject.cxx:8
 TLeafObject.cxx:9
 TLeafObject.cxx:10
 TLeafObject.cxx:11
 TLeafObject.cxx:12
 TLeafObject.cxx:13
 TLeafObject.cxx:14
 TLeafObject.cxx:15
 TLeafObject.cxx:16
 TLeafObject.cxx:17
 TLeafObject.cxx:18
 TLeafObject.cxx:19
 TLeafObject.cxx:20
 TLeafObject.cxx:21
 TLeafObject.cxx:22
 TLeafObject.cxx:23
 TLeafObject.cxx:24
 TLeafObject.cxx:25
 TLeafObject.cxx:26
 TLeafObject.cxx:27
 TLeafObject.cxx:28
 TLeafObject.cxx:29
 TLeafObject.cxx:30
 TLeafObject.cxx:31
 TLeafObject.cxx:32
 TLeafObject.cxx:33
 TLeafObject.cxx:34
 TLeafObject.cxx:35
 TLeafObject.cxx:36
 TLeafObject.cxx:37
 TLeafObject.cxx:38
 TLeafObject.cxx:39
 TLeafObject.cxx:40
 TLeafObject.cxx:41
 TLeafObject.cxx:42
 TLeafObject.cxx:43
 TLeafObject.cxx:44
 TLeafObject.cxx:45
 TLeafObject.cxx:46
 TLeafObject.cxx:47
 TLeafObject.cxx:48
 TLeafObject.cxx:49
 TLeafObject.cxx:50
 TLeafObject.cxx:51
 TLeafObject.cxx:52
 TLeafObject.cxx:53
 TLeafObject.cxx:54
 TLeafObject.cxx:55
 TLeafObject.cxx:56
 TLeafObject.cxx:57
 TLeafObject.cxx:58
 TLeafObject.cxx:59
 TLeafObject.cxx:60
 TLeafObject.cxx:61
 TLeafObject.cxx:62
 TLeafObject.cxx:63
 TLeafObject.cxx:64
 TLeafObject.cxx:65
 TLeafObject.cxx:66
 TLeafObject.cxx:67
 TLeafObject.cxx:68
 TLeafObject.cxx:69
 TLeafObject.cxx:70
 TLeafObject.cxx:71
 TLeafObject.cxx:72
 TLeafObject.cxx:73
 TLeafObject.cxx:74
 TLeafObject.cxx:75
 TLeafObject.cxx:76
 TLeafObject.cxx:77
 TLeafObject.cxx:78
 TLeafObject.cxx:79
 TLeafObject.cxx:80
 TLeafObject.cxx:81
 TLeafObject.cxx:82
 TLeafObject.cxx:83
 TLeafObject.cxx:84
 TLeafObject.cxx:85
 TLeafObject.cxx:86
 TLeafObject.cxx:87
 TLeafObject.cxx:88
 TLeafObject.cxx:89
 TLeafObject.cxx:90
 TLeafObject.cxx:91
 TLeafObject.cxx:92
 TLeafObject.cxx:93
 TLeafObject.cxx:94
 TLeafObject.cxx:95
 TLeafObject.cxx:96
 TLeafObject.cxx:97
 TLeafObject.cxx:98
 TLeafObject.cxx:99
 TLeafObject.cxx:100
 TLeafObject.cxx:101
 TLeafObject.cxx:102
 TLeafObject.cxx:103
 TLeafObject.cxx:104
 TLeafObject.cxx:105
 TLeafObject.cxx:106
 TLeafObject.cxx:107
 TLeafObject.cxx:108
 TLeafObject.cxx:109
 TLeafObject.cxx:110
 TLeafObject.cxx:111
 TLeafObject.cxx:112
 TLeafObject.cxx:113
 TLeafObject.cxx:114
 TLeafObject.cxx:115
 TLeafObject.cxx:116
 TLeafObject.cxx:117
 TLeafObject.cxx:118
 TLeafObject.cxx:119
 TLeafObject.cxx:120
 TLeafObject.cxx:121
 TLeafObject.cxx:122
 TLeafObject.cxx:123
 TLeafObject.cxx:124
 TLeafObject.cxx:125
 TLeafObject.cxx:126
 TLeafObject.cxx:127
 TLeafObject.cxx:128
 TLeafObject.cxx:129
 TLeafObject.cxx:130
 TLeafObject.cxx:131
 TLeafObject.cxx:132
 TLeafObject.cxx:133
 TLeafObject.cxx:134
 TLeafObject.cxx:135
 TLeafObject.cxx:136
 TLeafObject.cxx:137
 TLeafObject.cxx:138
 TLeafObject.cxx:139
 TLeafObject.cxx:140
 TLeafObject.cxx:141
 TLeafObject.cxx:142
 TLeafObject.cxx:143
 TLeafObject.cxx:144
 TLeafObject.cxx:145
 TLeafObject.cxx:146
 TLeafObject.cxx:147
 TLeafObject.cxx:148
 TLeafObject.cxx:149
 TLeafObject.cxx:150
 TLeafObject.cxx:151
 TLeafObject.cxx:152
 TLeafObject.cxx:153
 TLeafObject.cxx:154
 TLeafObject.cxx:155
 TLeafObject.cxx:156
 TLeafObject.cxx:157
 TLeafObject.cxx:158
 TLeafObject.cxx:159
 TLeafObject.cxx:160
 TLeafObject.cxx:161
 TLeafObject.cxx:162
 TLeafObject.cxx:163
 TLeafObject.cxx:164
 TLeafObject.cxx:165
 TLeafObject.cxx:166
 TLeafObject.cxx:167
 TLeafObject.cxx:168
 TLeafObject.cxx:169
 TLeafObject.cxx:170
 TLeafObject.cxx:171
 TLeafObject.cxx:172
 TLeafObject.cxx:173
 TLeafObject.cxx:174
 TLeafObject.cxx:175
 TLeafObject.cxx:176
 TLeafObject.cxx:177
 TLeafObject.cxx:178
 TLeafObject.cxx:179
 TLeafObject.cxx:180
 TLeafObject.cxx:181
 TLeafObject.cxx:182
 TLeafObject.cxx:183
 TLeafObject.cxx:184
 TLeafObject.cxx:185
 TLeafObject.cxx:186
 TLeafObject.cxx:187
 TLeafObject.cxx:188
 TLeafObject.cxx:189
 TLeafObject.cxx:190
 TLeafObject.cxx:191
 TLeafObject.cxx:192
 TLeafObject.cxx:193
 TLeafObject.cxx:194
 TLeafObject.cxx:195
 TLeafObject.cxx:196
 TLeafObject.cxx:197
 TLeafObject.cxx:198
 TLeafObject.cxx:199
 TLeafObject.cxx:200
 TLeafObject.cxx:201
 TLeafObject.cxx:202
 TLeafObject.cxx:203
 TLeafObject.cxx:204
 TLeafObject.cxx:205
 TLeafObject.cxx:206
 TLeafObject.cxx:207
 TLeafObject.cxx:208
 TLeafObject.cxx:209
 TLeafObject.cxx:210
 TLeafObject.cxx:211
 TLeafObject.cxx:212
 TLeafObject.cxx:213
 TLeafObject.cxx:214
 TLeafObject.cxx:215
 TLeafObject.cxx:216
 TLeafObject.cxx:217
 TLeafObject.cxx:218
 TLeafObject.cxx:219
 TLeafObject.cxx:220
 TLeafObject.cxx:221
 TLeafObject.cxx:222
 TLeafObject.cxx:223
 TLeafObject.cxx:224
 TLeafObject.cxx:225
 TLeafObject.cxx:226
 TLeafObject.cxx:227
 TLeafObject.cxx:228
 TLeafObject.cxx:229
 TLeafObject.cxx:230
 TLeafObject.cxx:231
 TLeafObject.cxx:232
 TLeafObject.cxx:233
 TLeafObject.cxx:234
 TLeafObject.cxx:235
 TLeafObject.cxx:236