ROOT  6.06/09
Reference Guide
TArrayC.cxx
Go to the documentation of this file.
1 // @(#)root/cont:$Id$
2 // Author: Rene Brun 06/03/95
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 TArrayC
13 Array of chars or bytes (8 bits per element).
14 */
15 
16 #include "TArrayC.h"
17 #include "TBuffer.h"
18 
19 
21 
22 ////////////////////////////////////////////////////////////////////////////////
23 /// Default TArrayC ctor.
24 
26 {
27  fArray = 0;
28 }
29 
30 ////////////////////////////////////////////////////////////////////////////////
31 /// Create TArrayC object and set array size to n chars.
32 
34 {
35  fArray = 0;
36  if (n > 0) Set(n);
37 }
38 
39 ////////////////////////////////////////////////////////////////////////////////
40 /// Create TArrayC object and initialize it with values of array.
41 
42 TArrayC::TArrayC(Int_t n, const Char_t *array)
43 {
44  fArray = 0;
45  Set(n, array);
46 }
47 
48 ////////////////////////////////////////////////////////////////////////////////
49 /// Copy constructor.
50 
51 TArrayC::TArrayC(const TArrayC &array) : TArray(array)
52 {
53  fArray = 0;
54  Set(array.fN, array.fArray);
55 }
56 
57 ////////////////////////////////////////////////////////////////////////////////
58 /// TArrayC assignment operator.
59 
61 {
62  if (this != &rhs)
63  Set(rhs.fN, rhs.fArray);
64  return *this;
65 }
66 
67 ////////////////////////////////////////////////////////////////////////////////
68 /// Delete TArrayC object.
69 
71 {
72  delete [] fArray;
73  fArray = 0;
74 }
75 
76 ////////////////////////////////////////////////////////////////////////////////
77 /// Adopt array arr into TArrayC, i.e. don't copy arr but use it directly
78 /// in TArrayC. User may not delete arr, TArrayC dtor will do it.
79 
81 {
82  if (fArray)
83  delete [] fArray;
84 
85  fN = n;
86  fArray = arr;
87 }
88 
89 ////////////////////////////////////////////////////////////////////////////////
90 /// Add char c at position i. Check for out of bounds.
91 
93 {
94  if (!BoundsOk("TArrayC::AddAt", i)) return;
95  fArray[i] = c;
96 }
97 
98 ////////////////////////////////////////////////////////////////////////////////
99 /// Set size of this array to n chars.
100 /// A new array is created, the old contents copied to the new array,
101 /// then the old array is deleted.
102 /// This function should not be called if the array was declared via Adopt.
103 
105 {
106  if (n < 0) return;
107  if (n != fN) {
108  Char_t *temp = fArray;
109  if (n != 0) {
110  fArray = new Char_t[n];
111  if (n < fN) memcpy(fArray,temp, n*sizeof(Char_t));
112  else {
113  memcpy(fArray,temp,fN*sizeof(Char_t));
114  memset(&fArray[fN],0,(n-fN)*sizeof(Char_t));
115  }
116  } else {
117  fArray = 0;
118  }
119  if (fN) delete [] temp;
120  fN = n;
121  }
122 }
123 
124 ////////////////////////////////////////////////////////////////////////////////
125 /// Set size of this array to n chars and set the contents.
126 /// This function should not be called if the array was declared via Adopt.
127 
128 void TArrayC::Set(Int_t n, const Char_t *array)
129 {
130  if (fArray && fN != n) {
131  delete [] fArray;
132  fArray = 0;
133  }
134  fN = n;
135  if (fN == 0) return;
136  if (array == 0) return;
137  if (!fArray) fArray = new Char_t[fN];
138  memmove(fArray, array, n*sizeof(Char_t));
139 }
140 
141 ////////////////////////////////////////////////////////////////////////////////
142 /// Stream a TArrayC object.
143 
144 void TArrayC::Streamer(TBuffer &b)
145 {
146  if (b.IsReading()) {
147  Int_t n;
148  b >> n;
149  Set(n);
150  b.ReadFastArray(fArray,n);
151  } else {
152  b << fN;
153  b.WriteFastArray(fArray, fN);
154  }
155 }
156 
Abstract array base class.
Definition: TArray.h:33
virtual ~TArrayC()
Delete TArrayC object.
Definition: TArrayC.cxx:70
void Set(Int_t n)
Set size of this array to n chars.
Definition: TArrayC.cxx:104
Bool_t IsReading() const
Definition: TBuffer.h:81
void Adopt(Int_t n, Char_t *array)
Adopt array arr into TArrayC, i.e.
Definition: TArrayC.cxx:80
ClassImp(TArrayC) TArrayC
Default TArrayC ctor.
Definition: TArrayC.cxx:20
Buffer base class used for serializing objects.
Definition: TBuffer.h:40
TArrayC & operator=(const TArrayC &rhs)
TArrayC assignment operator.
Definition: TArrayC.cxx:60
int Int_t
Definition: RtypesCore.h:41
Int_t fN
Definition: TArray.h:40
Bool_t BoundsOk(const char *where, Int_t at) const
Definition: TArray.h:79
virtual void ReadFastArray(Bool_t *b, Int_t n)=0
virtual void WriteFastArray(const Bool_t *b, Int_t n)=0
char Char_t
Definition: RtypesCore.h:29
void AddAt(Char_t c, Int_t i)
Add char c at position i. Check for out of bounds.
Definition: TArrayC.cxx:92
Char_t * fArray
Definition: TArrayC.h:32
const Int_t n
Definition: legend1.C:16
Array of chars or bytes (8 bits per element).
Definition: TArrayC.h:29