source: trunk/MagicSoft/Mars/mbase/MArrayB.h@ 587

Last change on this file since 587 was 458, 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(Int_t n)
33 {
34 fN = 0;
35 fArray = NULL;
36 if (n > 0)
37 Set(n);
38 }
39
40 MArrayB(Int_t n, Byte_t *array)
41 {
42 // Create TArrayC object and initialize it with values of array.
43 fN = 0;
44 fArray = NULL;
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 Int_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(Int_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, Int_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, Int_t i, Int_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(Int_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(Int_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 < 0 || n==fN)
120 return;
121
122 Byte_t *temp = fArray;
123 if (n != 0)
124 {
125 fArray = new Byte_t[n];
126 if (n < fN)
127 memcpy(fArray,temp, n*sizeof(Byte_t));
128 else
129 {
130 memcpy(fArray,temp,fN*sizeof(Byte_t));
131 memset(&fArray[fN],0,(n-fN)*sizeof(Byte_t));
132 }
133 }
134 else
135 {
136 fArray = 0;
137 }
138
139 if (fN)
140 delete [] temp;
141
142 fN = n;
143 }
144
145 void Set(Int_t n, Byte_t *array)
146 {
147 // Set size of this array to n chars and set the contents.
148 if (n < 0 || array == 0)
149 return;
150
151 if (fArray && fN != n)
152 {
153 delete [] fArray;
154 fArray = 0;
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[](Int_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.