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
28
29////////////////////////////////////////////////////////////////////////////////
30/// Default constructor for LeafC.
31
33{
34 fLenType = 1;
35 fMinimum = 0;
36 fMaximum = 0;
37 fValue = nullptr;
38 fPointer = nullptr;
39}
40
41////////////////////////////////////////////////////////////////////////////////
42/// Create a LeafC.
43
44TLeafC::TLeafC(TBranch *parent, const char *name, const char *type)
45 :TLeaf(parent, name,type)
46{
47 fLenType = 1;
48 fMinimum = 0;
49 fMaximum = 0;
50 fValue = nullptr;
51 fPointer = nullptr;
52}
53
54////////////////////////////////////////////////////////////////////////////////
55/// Default destructor for a LeafC.
56
58{
59 if (ResetAddress(nullptr,true)) delete [] fValue;
60}
61
62////////////////////////////////////////////////////////////////////////////////
63/// Export element from local leaf buffer to ClonesArray.
64
66{
67 Int_t j = 0;
68 for (Int_t i=0;i<n;i++) {
69 memcpy((char*)list->UncheckedAt(i) + fOffset,&fValue[j], 1);
70 j += fLen;
71 }
72}
73
74////////////////////////////////////////////////////////////////////////////////
75/// Pack leaf elements in Basket output buffer.
76
78{
79 if (fPointer) fValue = *fPointer;
81 if (len >= fMaximum) fMaximum = len+1;
82 if (len >= fLen) fLen = len+1;
83 b.WriteFastArrayString(fValue,len);
84}
85
86////////////////////////////////////////////////////////////////////////////////
87/// Returns name of leaf type.
88
89const char *TLeafC::GetTypeName() const
90{
91 if (fIsUnsigned) return "UChar_t";
92 return "Char_t";
93}
94
95////////////////////////////////////////////////////////////////////////////////
96/// Copy/set fMinimum and fMaximum to include/be wide than those of the parameter
97
99{
100 if (input) {
101 if (input->GetMaximum() > this->GetMaximum())
102 this->SetMaximum( input->GetMaximum() );
103 if (input->GetMinimum() < this->GetMinimum())
104 this->SetMinimum( input->GetMinimum() );
105 return true;
106 } else {
107 return false;
108 }
109}
110
111////////////////////////////////////////////////////////////////////////////////
112/// Import element from ClonesArray into local leaf buffer.
113
115{
116 Int_t j = 0;
117 for (Int_t i=0;i<n;i++) {
118 memcpy(&fValue[j],(char*)list->UncheckedAt(i) + fOffset, 1);
119 j += fLen;
120 }
121}
122
123////////////////////////////////////////////////////////////////////////////////
124/// Prints leaf value.
125
127{
128 char *value = (char*)GetValuePointer();
129 printf("%s",value);
130}
131
132////////////////////////////////////////////////////////////////////////////////
133/// Read leaf elements from Basket input buffer.
134
136{
137 // Try to deal with the file written during the time where len was not
138 // written to disk when len was == 0.
141 if (!basket) {
142 fValue[0] = '\0';
143 return;
144 }
145 Int_t* entryOffset = basket->GetEntryOffset();
146 if (entryOffset) {
149 if ( (readbasket == GetBranch()->GetWriteBasket() && (entry+1) == GetBranch()->GetEntries()) /* Very last entry */
150 ||
151 (readbasket < GetBranch()->GetWriteBasket() && (entry+1) == GetBranch()->GetBasketEntry()[readbasket+1] ) /* Last entry of the basket */
152 )
153 {
154 if ( entryOffset[entry-first] == basket->GetLast() ) /* The 'read' point is at the end of the basket */
155 {
156 // Empty string
157 fValue[0] = '\0';
158 return;
159 }
160 }
161 else if ( entryOffset[entry-first] == entryOffset[entry-first+1] ) /* This string did not use up any space in the buffer */
162 {
163 // Empty string
164 fValue[0] = '\0';
165 return;
166 }
167 }
168 b.ReadFastArrayString(fValue,fLen);
169}
170
171////////////////////////////////////////////////////////////////////////////////
172/// Read leaf elements from Basket input buffer and export buffer to
173/// TClonesArray objects.
174
176{
177 UChar_t len;
178 b >> len;
179 if (len) {
180 if (len >= fLen) len = fLen-1;
181 b.ReadFastArray(fValue,len);
182 fValue[len] = 0;
183 } else {
184 fValue[0] = 0;
185 }
186
187 Int_t j = 0;
188 for (Int_t i=0;i<n;i++) {
189 memcpy((char*)list->UncheckedAt(i) + fOffset,&fValue[j], 1);
190 j += fLen;
191 }
192}
193
194////////////////////////////////////////////////////////////////////////////////
195/// Read a string from std::istream s up to delimiter and store it into the branch
196/// buffer.
197
198void TLeafC::ReadValue(std::istream &s, Char_t delim /*= ' '*/)
199{
200 std::string temp;
201 std::getline(s, temp, delim);
202 if (TestBit(kNewValue) &&
203 (temp.length()+1 > ((UInt_t)fNdata))) {
204 // Grow buffer if needed and we created the buffer.
205 fNdata = ((UInt_t)temp.size()) + 1;
207 delete [] *fPointer;
208 *fPointer = new char[fNdata];
209 } else {
210 fValue = new char[fNdata];
211 }
212 }
213 strlcpy(fValue,temp.c_str(),fNdata);
214}
215
216////////////////////////////////////////////////////////////////////////////////
217/// Set leaf buffer data address.
218
219void TLeafC::SetAddress(void *add)
220{
221 if (ResetAddress(add)) {
222 delete [] fValue;
223 }
224 if (add) {
226 fPointer = (char**)add;
230 ncountmax > fNdata || *fPointer == nullptr) {
231 if (*fPointer) delete [] *fPointer;
233 *fPointer = new char[fNdata];
234 }
235 fValue = *fPointer;
236 } else {
237 fValue = (char*)add;
238 }
239 }
240 else {
241 fValue = new char[fNdata];
242 fValue[0] = 0;
243 }
244}
#define b(i)
Definition RSha256.hxx:100
int Int_t
Signed integer 4 bytes (int)
Definition RtypesCore.h:59
char Char_t
Character 1 byte (char)
Definition RtypesCore.h:51
unsigned int UInt_t
Unsigned integer 4 bytes (unsigned int)
Definition RtypesCore.h:60
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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
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.
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:32
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:126
void ReadBasket(TBuffer &b) override
Read leaf elements from Basket input buffer.
Definition TLeafC.cxx:135
void Import(TClonesArray *list, Int_t n) override
Import element from ClonesArray into local leaf buffer.
Definition TLeafC.cxx:114
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:98
const char * GetTypeName() const override
Returns name of leaf type.
Definition TLeafC.cxx:89
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:65
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:175
void FillBasket(TBuffer &b) override
Pack leaf elements in Basket output buffer.
Definition TLeafC.cxx:77
void SetAddress(void *add=nullptr) override
Set leaf buffer data address.
Definition TLeafC.cxx:219
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:198
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:57
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:430
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
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
Definition TObject.h:202
const Int_t n
Definition legend1.C:16