Logo ROOT  
Reference Guide
TLeafC.cxx
Go to the documentation of this file.
1// @(#)root/tree:$Id$
2// Author: Rene Brun 17/03/97
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 TLeafC
13\ingroup tree
14
15A TLeaf for a variable length string.
16*/
17
18#include "TLeafC.h"
19#include "TBuffer.h"
20#include "TBranch.h"
21#include "TBasket.h"
22#include "TClonesArray.h"
23#include "strlcpy.h"
24
25#include <string>
26#include <iostream>
27
29
30////////////////////////////////////////////////////////////////////////////////
31/// Default constructor for LeafC.
32
34{
35 fLenType = 1;
36 fMinimum = 0;
37 fMaximum = 0;
38 fValue = 0;
39 fPointer = 0;
40}
41
42////////////////////////////////////////////////////////////////////////////////
43/// Create a LeafC.
44
45TLeafC::TLeafC(TBranch *parent, const char *name, const char *type)
46 :TLeaf(parent, name,type)
47{
48 fLenType = 1;
49 fMinimum = 0;
50 fMaximum = 0;
51 fValue = 0;
52 fPointer = 0;
53}
54
55////////////////////////////////////////////////////////////////////////////////
56/// Default destructor for a LeafC.
57
59{
60 if (ResetAddress(0,kTRUE)) delete [] fValue;
61}
62
63////////////////////////////////////////////////////////////////////////////////
64/// Export element from local leaf buffer to ClonesArray.
65
67{
68 Int_t j = 0;
69 for (Int_t i=0;i<n;i++) {
70 memcpy((char*)list->UncheckedAt(i) + fOffset,&fValue[j], 1);
71 j += fLen;
72 }
73}
74
75////////////////////////////////////////////////////////////////////////////////
76/// Pack leaf elements in Basket output buffer.
77
79{
80 if (fPointer) fValue = *fPointer;
81 Int_t len = strlen(fValue);
82 if (len >= fMaximum) fMaximum = len+1;
83 if (len >= fLen) fLen = len+1;
84 b.WriteFastArrayString(fValue,len);
85}
86
87////////////////////////////////////////////////////////////////////////////////
88/// Returns name of leaf type.
89
90const char *TLeafC::GetTypeName() const
91{
92 if (fIsUnsigned) return "UChar_t";
93 return "Char_t";
94}
95
96////////////////////////////////////////////////////////////////////////////////
97/// Copy/set fMinimum and fMaximum to include/be wide than those of the parameter
98
100{
101 if (input) {
102 if (input->GetMaximum() > this->GetMaximum())
103 this->SetMaximum( input->GetMaximum() );
104 if (input->GetMinimum() < this->GetMinimum())
105 this->SetMinimum( input->GetMinimum() );
106 return kTRUE;
107 } else {
108 return kFALSE;
109 }
110}
111
112////////////////////////////////////////////////////////////////////////////////
113/// Import element from ClonesArray into local leaf buffer.
114
116{
117 Int_t j = 0;
118 for (Int_t i=0;i<n;i++) {
119 memcpy(&fValue[j],(char*)list->UncheckedAt(i) + fOffset, 1);
120 j += fLen;
121 }
122}
123
124////////////////////////////////////////////////////////////////////////////////
125/// Prints leaf value.
126
128{
129 char *value = (char*)GetValuePointer();
130 printf("%s",value);
131}
132
133////////////////////////////////////////////////////////////////////////////////
134/// Read leaf elements from Basket input buffer.
135
137{
138 // Try to deal with the file written during the time where len was not
139 // written to disk when len was == 0.
140 Int_t readbasket = GetBranch()->GetReadBasket();
141 TBasket *basket = GetBranch()->GetBasket(readbasket);
142 if (!basket) {
143 fValue[0] = '\0';
144 return;
145 }
146 Int_t* entryOffset = basket->GetEntryOffset();
147 if (entryOffset) {
148 Long64_t first = GetBranch()->GetBasketEntry()[readbasket];
149 Long64_t entry = GetBranch()->GetReadEntry();
150 if ( (readbasket == GetBranch()->GetWriteBasket() && (entry+1) == GetBranch()->GetEntries()) /* Very last entry */
151 ||
152 (readbasket < GetBranch()->GetWriteBasket() && (entry+1) == GetBranch()->GetBasketEntry()[readbasket+1] ) /* Last entry of the basket */
153 )
154 {
155 if ( entryOffset[entry-first] == basket->GetLast() ) /* The 'read' point is at the end of the basket */
156 {
157 // Empty string
158 fValue[0] = '\0';
159 return;
160 }
161 }
162 else if ( entryOffset[entry-first] == entryOffset[entry-first+1] ) /* This string did not use up any space in the buffer */
163 {
164 // Empty string
165 fValue[0] = '\0';
166 return;
167 }
168 }
169 b.ReadFastArrayString(fValue,fLen);
170}
171
172////////////////////////////////////////////////////////////////////////////////
173/// Read leaf elements from Basket input buffer and export buffer to
174/// TClonesArray objects.
175
177{
178 UChar_t len;
179 b >> len;
180 if (len) {
181 if (len >= fLen) len = fLen-1;
182 b.ReadFastArray(fValue,len);
183 fValue[len] = 0;
184 } else {
185 fValue[0] = 0;
186 }
187
188 Int_t j = 0;
189 for (Int_t i=0;i<n;i++) {
190 memcpy((char*)list->UncheckedAt(i) + fOffset,&fValue[j], 1);
191 j += fLen;
192 }
193}
194
195////////////////////////////////////////////////////////////////////////////////
196/// Read a string from std::istream s up to delimiter and store it into the branch
197/// buffer.
198
199void TLeafC::ReadValue(std::istream &s, Char_t delim /*= ' '*/)
200{
201 std::string temp;
202 std::getline(s, temp, delim);
203 if (TestBit(kNewValue) &&
204 (temp.length()+1 > ((UInt_t)fNdata))) {
205 // Grow buffer if needed and we created the buffer.
206 fNdata = ((UInt_t)temp.size()) + 1;
208 delete [] *fPointer;
209 *fPointer = new char[fNdata];
210 } else {
211 fValue = new char[fNdata];
212 }
213 }
214 strlcpy(fValue,temp.c_str(),fNdata);
215}
216
217////////////////////////////////////////////////////////////////////////////////
218/// Set leaf buffer data address.
219
220void TLeafC::SetAddress(void *add)
221{
222 if (ResetAddress(add)) {
223 delete [] fValue;
224 }
225 if (add) {
227 fPointer = (char**)add;
228 Int_t ncountmax = fLen;
229 if (fLeafCount) ncountmax = fLen*(fLeafCount->GetMaximum() + 1);
230 if ((fLeafCount && ncountmax > Int_t(fLeafCount->GetValue())) ||
231 ncountmax > fNdata || *fPointer == 0) {
232 if (*fPointer) delete [] *fPointer;
233 if (ncountmax > fNdata) fNdata = ncountmax;
234 *fPointer = new char[fNdata];
235 }
236 fValue = *fPointer;
237 } else {
238 fValue = (char*)add;
239 }
240 }
241 else {
242 fValue = new char[fNdata];
243 fValue[0] = 0;
244 }
245}
#define b(i)
Definition: RSha256.hxx:100
int Int_t
Definition: RtypesCore.h:45
unsigned char UChar_t
Definition: RtypesCore.h:38
char Char_t
Definition: RtypesCore.h:33
unsigned int UInt_t
Definition: RtypesCore.h:46
const Bool_t kFALSE
Definition: RtypesCore.h:101
bool Bool_t
Definition: RtypesCore.h:63
long long Long64_t
Definition: RtypesCore.h:80
const Bool_t kTRUE
Definition: RtypesCore.h:100
#define ClassImp(name)
Definition: Rtypes.h:364
char name[80]
Definition: TGX11.cxx:110
int type
Definition: TGX11.cxx:121
Manages buffers for branches of a Tree.
Definition: TBasket.h:34
Int_t * GetEntryOffset()
Definition: TBasket.h:124
Int_t GetLast() const
Definition: TBasket.h:131
A TTree is a list of TBranches.
Definition: TBranch.h:89
TBasket * GetBasket(Int_t basket)
Definition: TBranch.h:209
Long64_t GetReadEntry() const
Definition: TBranch.h:233
Int_t GetReadBasket() const
Definition: TBranch.h:232
Long64_t * GetBasketEntry() const
Definition: TBranch.h:211
Buffer base class used for serializing objects.
Definition: TBuffer.h:43
An array of clone (identical) objects.
Definition: TClonesArray.h:29
A TLeaf for a variable length string.
Definition: TLeafC.h:26
virtual void PrintValue(Int_t i=0) const
Prints leaf value.
Definition: TLeafC.cxx:127
Int_t fMinimum
Minimum value if leaf range is specified.
Definition: TLeafC.h:29
TLeafC()
Default constructor for LeafC.
Definition: TLeafC.cxx:33
virtual void SetAddress(void *add=0)
Set leaf buffer data address.
Definition: TLeafC.cxx:220
virtual void Import(TClonesArray *list, Int_t n)
Import element from ClonesArray into local leaf buffer.
Definition: TLeafC.cxx:115
Int_t fMaximum
Maximum value if leaf range is specified.
Definition: TLeafC.h:30
virtual void Export(TClonesArray *list, Int_t n)
Export element from local leaf buffer to ClonesArray.
Definition: TLeafC.cxx:66
virtual Bool_t IncludeRange(TLeaf *)
Copy/set fMinimum and fMaximum to include/be wide than those of the parameter.
Definition: TLeafC.cxx:99
Char_t * fValue
! Pointer to data buffer
Definition: TLeafC.h:31
virtual void ReadBasketExport(TBuffer &b, TClonesArray *list, Int_t n)
Read leaf elements from Basket input buffer and export buffer to TClonesArray objects.
Definition: TLeafC.cxx:176
Char_t ** fPointer
! Address of pointer to data buffer
Definition: TLeafC.h:32
virtual void ReadValue(std::istream &s, Char_t delim=' ')
Read a string from std::istream s up to delimiter and store it into the branch buffer.
Definition: TLeafC.cxx:199
virtual void * GetValuePointer() const
Definition: TLeafC.h:45
virtual ~TLeafC()
Default destructor for a LeafC.
Definition: TLeafC.cxx:58
virtual void FillBasket(TBuffer &b)
Pack leaf elements in Basket output buffer.
Definition: TLeafC.cxx:78
virtual void ReadBasket(TBuffer &b)
Read leaf elements from Basket input buffer.
Definition: TLeafC.cxx:136
virtual void SetMinimum(Int_t min)
Definition: TLeafC.h:55
const char * GetTypeName() const
Returns name of leaf type.
Definition: TLeafC.cxx:90
virtual void SetMaximum(Int_t max)
Definition: TLeafC.h:54
A TLeaf describes individual elements of a TBranch See TBranch structure in TTree.
Definition: TLeaf.h:57
virtual Double_t GetValue(Int_t i=0) const
Definition: TLeaf.h:183
Int_t fLenType
Number of bytes for this data type.
Definition: TLeaf.h:73
virtual Int_t GetMaximum() const
Definition: TLeaf.h:134
Int_t fLen
Number of fixed length elements in the leaf's data.
Definition: TLeaf.h:72
Int_t fNdata
! Number of elements in fAddress data buffer.
Definition: TLeaf.h:71
Bool_t fIsUnsigned
(=kTRUE if unsigned, kFALSE otherwise)
Definition: TLeaf.h:76
TBranch * GetBranch() const
Definition: TLeaf.h:116
Int_t fOffset
Offset in ClonesArray object (if one)
Definition: TLeaf.h:74
virtual Int_t GetMinimum() const
Definition: TLeaf.h:135
Int_t ResetAddress(void *add, Bool_t calledFromDestructor=kFALSE)
Helper routine for TLeafX::SetAddress.
Definition: TLeaf.cxx:429
TLeaf * fLeafCount
Pointer to Leaf count if variable length (we do not own the counter)
Definition: TLeaf.h:77
@ kNewValue
Set if we own the value buffer and so must delete it ourselves.
Definition: TLeaf.h:96
@ kIndirectAddress
Data member is a pointer to an array of basic types.
Definition: TLeaf.h:95
TObject * UncheckedAt(Int_t i) const
Definition: TObjArray.h:90
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
Definition: TObject.h:187
const Int_t n
Definition: legend1.C:16
static constexpr double s
Definition: first.py:1