source: tags/Mars-V0.7/mbase/MArrayB.h

Last change on this file was 1014, checked in by tbretz, 23 years ago
*** empty log message ***
File size: 3.6 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)
49 {
50 // Copy constructor.
51 fArray = NULL;
52 Set(array.fN, array.fArray);
53 }
54
55 UInt_t GetSize() const
56 {
57 return fN;
58 }
59
60 MArrayB &operator=(const MArrayB &rhs)
61 {
62 // TArrayC assignment operator.
63 if (this != &rhs)
64 Set(rhs.fN, rhs.fArray);
65 return *this;
66 }
67
68 virtual ~MArrayB()
69 {
70 // Delete TArrayC object.
71 delete [] fArray;
72 fArray = NULL;
73 }
74
75 void Adopt(UInt_t n, Byte_t *array)
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 if (fArray)
80 delete [] fArray;
81
82 fN = n;
83 fArray = array;
84 }
85
86 void AddAt(Byte_t c, UInt_t i)
87 {
88 // Add char c at position i. Check for out of bounds.
89 fArray[i] = c;
90 }
91
92 void AddAt(Byte_t *array, UInt_t i, UInt_t n)
93 {
94 // Add char c at position i. Check for out of bounds.
95 memcpy(fArray+i, array, n*sizeof(Byte_t));
96 }
97
98 Byte_t At(UInt_t i)
99 {
100 return fArray[i];
101 }
102
103 Byte_t *GetArray() const
104 {
105 return fArray;
106 }
107
108 void Reset()
109 {
110 memset(fArray, 0, fN*sizeof(Byte_t));
111 }
112
113 void Set(UInt_t n)
114 {
115 // Set size of this array to n chars.
116 // A new array is created, the old contents copied to the new array,
117 // then the old array is deleted.
118
119 if (n==fN)
120 return;
121
122 Byte_t *temp = fArray;
123 if (n == 0)
124 fArray = 0;
125 else
126 {
127 fArray = new Byte_t[n];
128 if (n < fN)
129 memcpy(fArray,temp, n*sizeof(Byte_t));
130 else
131 {
132 memcpy(fArray,temp,fN*sizeof(Byte_t));
133 memset(&fArray[fN],0,(n-fN)*sizeof(Byte_t));
134 }
135 }
136
137 if (fN)
138 delete [] temp;
139
140 fN = n;
141 }
142
143 void Set(UInt_t n, Byte_t *array)
144 {
145 // Set size of this array to n chars and set the contents.
146 if (!array)
147 return;
148
149 if (fArray && fN != n)
150 {
151 delete [] fArray;
152 fArray = 0;
153 }
154
155 fN = n;
156
157 if (fN == 0)
158 return;
159
160 if (!fArray)
161 fArray = new Byte_t[fN];
162
163 memcpy(fArray,array, n*sizeof(Byte_t));
164 }
165
166 Byte_t &operator[](UInt_t i)
167 {
168 return fArray[i];
169 }
170
171 ClassDef(MArrayB, 1) //Array of Byte_t
172};
173
174#endif
Note: See TracBrowser for help on using the repository browser.