source: branches/MarsISDCBranchBasedOn17887/mbase/MArrayB.h@ 19921

Last change on this file since 19921 was 9211, checked in by tbretz, 16 years ago
*** empty log message ***
File size: 3.7 KB
Line 
1#ifndef MARS_MArrayB
2#define MARS_MArrayB
3
4/////////////////////////////////////////////////////////////////////////////
5// //
6// MArrayB //
7// //
8// Array of Byte_t //
9// //
10/////////////////////////////////////////////////////////////////////////////
11
12#ifndef MARS_MArray
13#include "MArray.h"
14#endif
15
16#include <string.h>
17
18class MArrayB : public MArray
19{
20private:
21 Byte_t *fArray; //[fN] Array of fN chars
22
23public:
24
25 MArrayB()
26 {
27 fN = 0;
28 fArray = NULL;
29 }
30
31 MArrayB(UInt_t n)
32 {
33 fN = 0;
34 fArray = NULL;
35
36 Set(n);
37 }
38
39 MArrayB(UInt_t n, Byte_t *array)
40 {
41 // Create TArrayC object and initialize it with values of array.
42 fN = 0;
43 fArray = NULL;
44
45 Set(n, array);
46 }
47
48 MArrayB(const MArrayB &array) : MArray()
49 {
50 // Copy constructor.
51 fArray = NULL;
52 Set(array.fN, array.fArray);
53 }
54
55 MArrayB &operator=(const MArrayB &rhs)
56 {
57 // TArrayC assignment operator.
58 if (this != &rhs)
59 Set(rhs.fN, rhs.fArray);
60 return *this;
61 }
62
63 virtual ~MArrayB()
64 {
65 // Delete TArrayC object.
66 delete [] fArray;
67 fArray = NULL;
68 }
69
70 void Adopt(UInt_t n, Byte_t *array)
71 {
72 // Adopt array arr into TArrayC, i.e. don't copy arr but use it directly
73 // in TArrayC. User may not delete arr, TArrayC dtor will do it.
74 if (fArray)
75 delete [] fArray;
76
77 fN = n;
78 fArray = array;
79 }
80
81 void AddAt(Byte_t c, UInt_t i)
82 {
83 // Add char c at position i. Check for out of bounds.
84 fArray[i] = c;
85 }
86
87 void AddAt(Byte_t *array, UInt_t i, UInt_t n)
88 {
89 // Add char c at position i. Check for out of bounds.
90 memcpy(fArray+i, array, n*sizeof(Byte_t));
91 }
92
93 Byte_t At(UInt_t i)
94 {
95 return fArray[i];
96 }
97
98 Byte_t *GetArray() const
99 {
100 return fArray;
101 }
102
103 void Reset()
104 {
105 memset(fArray, 0, fN*sizeof(Byte_t));
106 }
107
108 void Reset(Byte_t v)
109 {
110 for (Byte_t *b=fArray; b<fArray+fN; b++)
111 *b = v;
112 }
113
114
115 void Set(UInt_t n)
116 {
117 // Set size of this array to n chars.
118 // A new array is created, the old contents copied to the new array,
119 // then the old array is deleted.
120
121 if (n==fN)
122 return;
123
124 Byte_t *temp = fArray;
125 if (n == 0)
126 fArray = 0;
127 else
128 {
129 fArray = new Byte_t[n];
130 if (n < fN)
131 memcpy(fArray,temp, n*sizeof(Byte_t));
132 else
133 {
134 memcpy(fArray,temp,fN*sizeof(Byte_t));
135 memset(&fArray[fN],0,(n-fN)*sizeof(Byte_t));
136 }
137 }
138
139 if (fN)
140 delete [] temp;
141
142 fN = n;
143 }
144
145 void Set(UInt_t n, Byte_t *array)
146 {
147 // Set size of this array to n chars and set the contents.
148 if (!array)
149 return;
150
151 if (fArray && fN != n)
152 {
153 delete [] fArray;
154 fArray = 0;
155 }
156
157 fN = n;
158
159 if (fN == 0)
160 return;
161
162 if (!fArray)
163 fArray = new Byte_t[fN];
164
165 memcpy(fArray,array, n*sizeof(Byte_t));
166 }
167
168 Byte_t &operator[](UInt_t i)
169 {
170 return fArray[i];
171 }
172 const Byte_t &operator[](UInt_t i) const
173 {
174 return fArray[i];
175 }
176
177 ClassDef(MArrayB, 1) //Array of Byte_t
178};
179
180#endif
Note: See TracBrowser for help on using the repository browser.