1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
-
!
-
|
!
-
|
!
-
|
!
-
|
|
|
|
!
-
|
|
|
|
|
|
|
|
|
|
|
|
|
-
|
-
|
|
!
|
-
|
!
!
|
|
-
|
!
|
|
|
|
!
-
|
|
|
!
-
|
|
|
!
-
|
|
|
!
-
|
|
!
-
|
|
!
-
|
!
-
|
!
-
|
!
-
|
|
!
-
|
!
-
|
!
-
|
!
-
|
|
!
-
|
-
|
|
|
!
!
|
#include <apcl/util/Data.hpp>
#include <cassert>
#include <cstring>
using namespace ::apcl::util;
using namespace ::std;
Data::Data(void):
bytes_(0),
length_(0)
{
}
Data::Data( const Data& aData ):
bytes_(0),
length_(0)
{
init( aData.bytes() , aData.length() );
}
Data::Data( const void* const aBytes , const u32 aLength ):
bytes_(0),
length_(0)
{
init( aBytes , aLength );
}
Data::~Data()
{
_releaseBytes();
}
void Data::init( const void* const aBytes , const u32 aLength )
{
_releaseBytes();
bytes_ = new byte[aLength];
length_ = aLength;
memcpy( bytes_ , aBytes , aLength );
}
void Data::resize( const u32 aSize )
{
byte* preBytes = bytes_;
u32 preLength = length_;
if ( aSize > 0 )
bytes_ = new byte[aSize];
else
bytes_ = 0;
length_ = aSize;
if ( length_ > 0 )
{
if ( preLength > 0 )
{ const u32 size = preLength < length_ ? preLength : length_;
memcpy( bytes_ , preBytes, size );
}
else
{ memset( bytes_ , 0 , length_ );
}
}
if ( length_ > preLength )
{ memset( &bytes_[preLength] , 0 , length_ - preLength );
}
delete[] preBytes;
}
void Data::push( const Data& aData )
{
assert( aData.length() > 0 );
resize( length_ + aData.length() );
memcpy( &bytes_[length_] , aData.bytes() , aData.length() );
}
void Data::pop( const u32 aLength )
{
assert( aLength > 0 );
assert( aLength <= length_ );
resize( length_ - aLength );
}
bool Data::equals( const Data& aData )const
{
if ( length_ == aData.length() )
return ( memcmp( bytes_ , aData.bytes() , length_ ) == 0 );
return false;
}
byte& Data::at( const u32 aIndex )
{
assert( aIndex > 0 && aIndex <= length_ );
return bytes_[aIndex];
}
byte Data::at( const u32 aIndex )const
{
assert( aIndex > 0 && aIndex <= length_ );
return bytes_[aIndex];
}
byte* Data::bytes()
{
return bytes_;
}
const byte* Data::bytes()const
{
return bytes_;
}
u32 Data::length()const
{
return length_;
}
const Data& Data::operator +=( const Data& aData )
{
push( aData );
return *this;
}
byte& Data::operator []( const u32 aIndex )
{
return at( aIndex );
}
byte Data::operator []( const u32 aIndex )const
{
return at( aIndex );
}
bool Data::operator ==( const Data& aData )const
{
return equals( aData );
}
const Data& Data::operator =( const Data& aData )
{
init( aData.bytes() , aData.length() );
return *this;
}
void Data::_releaseBytes()
{
if ( bytes_ )
{
delete[] bytes_;
bytes_ = 0;
length_ = 0;
}
}
|