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

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