ROOT logo
// @(#)root/tree:$Id: TLeafB.cxx 20882 2007-11-19 11:31:26Z rdm $
// Author: Rene Brun   12/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 an 8 bit Integer data type.                              //
//////////////////////////////////////////////////////////////////////////

#include "TLeafB.h"
#include "TBranch.h"
#include "TClonesArray.h"
#include "Riostream.h"

ClassImp(TLeafB)

//______________________________________________________________________________
TLeafB::TLeafB()
: TLeaf()
, fMinimum(0)
, fMaximum(0)
, fValue(0)
, fPointer(0)
{
   // -- Default constructor.
}

//______________________________________________________________________________
TLeafB::TLeafB(TBranch *parent, const char* name, const char* type)
   : TLeaf(parent, name, type)
   , fMinimum(0)
   , fMaximum(0)
   , fValue(0)
   , fPointer(0)
{
   // -- Create a LeafB.
   fLenType = 1;
}

//______________________________________________________________________________
TLeafB::~TLeafB()
{
   // -- Destructor.
   if (ResetAddress(0, kTRUE)) {
      delete[] fValue;
      fValue = 0;
   }
   // Note: We do not own this, the user's object does.
   fPointer = 0;
}

//______________________________________________________________________________
void TLeafB::Export(TClonesArray* list, Int_t n)
{
   // -- Export element from local leaf buffer to a ClonesArray.

   for (Int_t i = 0, j = 0; i < n; i++, j += fLen) {
      memcpy(((char*) list->UncheckedAt(i)) + fOffset, &fValue[j], fLen);
   }
}

//______________________________________________________________________________
void TLeafB::FillBasket(TBuffer& b)
{
   // -- Pack leaf elements into Basket output buffer.

   Int_t len = GetLen();
   if (fPointer) {
      fValue = *fPointer;
   }
   if (IsRange()) {
      if (fValue[0] > fMaximum) {
         fMaximum = fValue[0];
      }
   }
   if (IsUnsigned()) {
      for (Int_t i = 0; i < len; i++) {
         b << (UChar_t) fValue[i];
      }
   } else {
      b.WriteFastArray(fValue, len);
   }
}

//______________________________________________________________________________
const char *TLeafB::GetTypeName() const
{
   // -- Returns name of leaf type.

   if (fIsUnsigned) {
      return "UChar_t";
   }
   return "Char_t";
}

//______________________________________________________________________________
void TLeafB::Import(TClonesArray *list, Int_t n)
{
   // -- Import element from ClonesArray into local leaf buffer.

   for (Int_t i = 0, j = 0; i < n; i++, j+= fLen) {
      memcpy(&fValue[j], ((char*) list->UncheckedAt(i)) + fOffset, fLen);
   }
}

//______________________________________________________________________________
void TLeafB::PrintValue(Int_t l) const
{
   // -- Prints leaf value.

   char* value = (char*) GetValuePointer();
   printf("%d", (Int_t) value[l]);
}

//______________________________________________________________________________
void TLeafB::ReadBasket(TBuffer &b)
{
   // -- Read leaf elements from Basket input buffer.

   if (!fLeafCount && (fNdata == 1)) {
      b >> fValue[0];
   } else {
      if (fLeafCount) {
         Int_t len = Int_t(fLeafCount->GetValue());
         if (len > fLeafCount->GetMaximum()) {
            Error("ReadBasket", "leaf: '%s' len: %d max: %d", GetName(), len, fLeafCount->GetMaximum());
            len = fLeafCount->GetMaximum();
         }
         fNdata = len * fLen;
         b.ReadFastArray(fValue, len*fLen);
      } else {
         b.ReadFastArray(fValue, fLen);
      }
   }
}

//______________________________________________________________________________
void TLeafB::ReadBasketExport(TBuffer& b, TClonesArray* list, Int_t n)
{
   // -- Read leaf elements from Basket input buffer and export buffer to TClonesArray objects.

   b.ReadFastArray(fValue, n*fLen);

   for (Int_t i = 0, j = 0; i < n; i++, j += fLen) {
      memcpy(((char*) list->UncheckedAt(i)) + fOffset, &fValue[j], fLen);
   }
}

//______________________________________________________________________________
void TLeafB::ReadValue(ifstream &s)
{
   // -- Read a string from ifstream s and store it into the branch buffer.
   char* value = (char*) GetValuePointer();
   s >> value;
}

//______________________________________________________________________________
void TLeafB::SetAddress(void *addr)
{
   // -- Set value buffer address.

   // Check ownership of the value buffer and
   // calculate a new size for it.
   if (ResetAddress(addr)) {
      // -- We owned the old value buffer, delete it.
      delete[] fValue;
      fValue = 0;
   }
   if (addr) {
      // -- We have been provided a new value buffer.
      if (TestBit(kIndirectAddress)) {
         // -- The data member is a pointer to an array.
         fPointer = (Char_t**) addr;
         // Calculate the maximum size we have ever needed
         // for the value buffer.
         Int_t ncountmax = fLen;
         if (fLeafCount) {
            ncountmax = (fLeafCount->GetMaximum() + 1) * fLen;
         }
         // Reallocate the value buffer if needed.
         if ((fLeafCount && (Int_t(fLeafCount->GetValue()) < ncountmax)) ||
             (fNdata < ncountmax) ||
             (*fPointer == 0)) {
            // -- Reallocate.
            // Note:
            //      1) For a varying length array we do this based on
            //         an indirect estimator of the size of the value
            //         buffer since we have no record of how large it
            //         actually is.  If the current length of the
            //         varying length array is less than it has been
            //         in the past, then reallocate the value buffer
            //         to the larger of either the calculated new size
            //         or the maximum size it has ever been.
            //
            //      2) The second condition checks if the new value
            //         buffer size calculated by ResetAddress() is
            //         smaller than the most we have ever used, and
            //         if it is, then we increase the new size and
            //         reallocate.
            //
            //      3) The third condition is checking to see if we
            //         have been given a value buffer, if not then
            //         we must allocate one.
            //
            if (fNdata < ncountmax) {
               fNdata = ncountmax;
            }
            delete[] *fPointer;
            *fPointer = 0;
            *fPointer = new Char_t[fNdata];
         }
         fValue = *fPointer;
      } else {
         // -- The data member is fixed size.
         // FIXME: What about fPointer???
         fValue = (char*) addr;
      }
   } else {
      // -- We must create the value buffer ourselves.
      // Note: We are using the size calculated by ResetAddress().
      fValue = new char[fNdata];
      // FIXME: Why initialize at all?
      fValue[0] = 0;
   }
}

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