Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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 = nullptr;
39 fPointer = nullptr;
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 = nullptr;
52 fPointer = nullptr;
53}
54
55////////////////////////////////////////////////////////////////////////////////
56/// Default destructor for a LeafC.
57
59{
60 if (ResetAddress(nullptr,true)) 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 true;
107 } else {
108 return false;
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 == nullptr) {
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:37
unsigned int UInt_t
Definition RtypesCore.h:46
long long Long64_t
Definition RtypesCore.h:80
#define ClassImp(name)
Definition Rtypes.h:377
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void input
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
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 UChar_t len
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
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:93
TBasket * GetBasket(Int_t basket)
Definition TBranch.h:213
Long64_t GetReadEntry() const
Definition TBranch.h:237
Int_t GetReadBasket() const
Definition TBranch.h:236
Long64_t * GetBasketEntry() const
Definition TBranch.h:215
Buffer base class used for serializing objects.
Definition TBuffer.h:43
An array of clone (identical) objects.
A TLeaf for a variable length string.
Definition TLeafC.h:26
void * GetValuePointer() const override
Definition TLeafC.h:45
Int_t fMinimum
Minimum value if leaf range is specified.
Definition TLeafC.h:29
TLeafC()
Default constructor for LeafC.
Definition TLeafC.cxx:33
Int_t fMaximum
Maximum value if leaf range is specified.
Definition TLeafC.h:30
void PrintValue(Int_t i=0) const override
Prints leaf value.
Definition TLeafC.cxx:127
void ReadBasket(TBuffer &b) override
Read leaf elements from Basket input buffer.
Definition TLeafC.cxx:136
void Import(TClonesArray *list, Int_t n) override
Import element from ClonesArray into local leaf buffer.
Definition TLeafC.cxx:115
Char_t * fValue
! Pointer to data buffer
Definition TLeafC.h:31
bool IncludeRange(TLeaf *) override
Copy/set fMinimum and fMaximum to include/be wide than those of the parameter.
Definition TLeafC.cxx:99
const char * GetTypeName() const override
Returns name of leaf type.
Definition TLeafC.cxx:90
Char_t ** fPointer
! Address of pointer to data buffer
Definition TLeafC.h:32
void Export(TClonesArray *list, Int_t n) override
Export element from local leaf buffer to ClonesArray.
Definition TLeafC.cxx:66
void ReadBasketExport(TBuffer &b, TClonesArray *list, Int_t n) override
Read leaf elements from Basket input buffer and export buffer to TClonesArray objects.
Definition TLeafC.cxx:176
void FillBasket(TBuffer &b) override
Pack leaf elements in Basket output buffer.
Definition TLeafC.cxx:78
void SetAddress(void *add=nullptr) override
Set leaf buffer data address.
Definition TLeafC.cxx:220
void ReadValue(std::istream &s, Char_t delim=' ') override
Read a string from std::istream s up to delimiter and store it into the branch buffer.
Definition TLeafC.cxx:199
virtual void SetMinimum(Int_t min)
Definition TLeafC.h:55
virtual void SetMaximum(Int_t max)
Definition TLeafC.h:54
~TLeafC() override
Default destructor for a LeafC.
Definition TLeafC.cxx:58
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
Int_t ResetAddress(void *add, bool calledFromDestructor=false)
Helper routine for TLeafX::SetAddress.
Definition TLeaf.cxx:429
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
bool fIsUnsigned
(=true if unsigned, false otherwise)
Definition TLeaf.h:76
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:84
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
Definition TObject.h:201
const Int_t n
Definition legend1.C:16