comparison deps/Platinum/ThirdParty/Neptune/Source/Core/NptDataBuffer.cpp @ 0:3425707ddbf6

Initial import (hopefully this mercurial stuff works...)
author fraserofthenight
date Mon, 06 Jul 2009 08:06:28 -0700
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:3425707ddbf6
1 /*****************************************************************
2 |
3 | Neptune - Data Buffer
4 |
5 | Copyright (c) 2002-2008, Axiomatic Systems, LLC.
6 | All rights reserved.
7 |
8 | Redistribution and use in source and binary forms, with or without
9 | modification, are permitted provided that the following conditions are met:
10 | * Redistributions of source code must retain the above copyright
11 | notice, this list of conditions and the following disclaimer.
12 | * Redistributions in binary form must reproduce the above copyright
13 | notice, this list of conditions and the following disclaimer in the
14 | documentation and/or other materials provided with the distribution.
15 | * Neither the name of Axiomatic Systems nor the
16 | names of its contributors may be used to endorse or promote products
17 | derived from this software without specific prior written permission.
18 |
19 | THIS SOFTWARE IS PROVIDED BY AXIOMATIC SYSTEMS ''AS IS'' AND ANY
20 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 | DISCLAIMED. IN NO EVENT SHALL AXIOMATIC SYSTEMS BE LIABLE FOR ANY
23 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 |
30 ****************************************************************/
31
32 /*----------------------------------------------------------------------
33 | includes
34 +---------------------------------------------------------------------*/
35 #include "NptDataBuffer.h"
36 #include "NptUtils.h"
37 #include "NptResults.h"
38
39 /*----------------------------------------------------------------------
40 | NPT_DataBuffer::NPT_DataBuffer
41 +---------------------------------------------------------------------*/
42 NPT_DataBuffer::NPT_DataBuffer() :
43 m_BufferIsLocal(true),
44 m_Buffer(NULL),
45 m_BufferSize(0),
46 m_DataSize(0)
47 {
48 }
49
50 /*----------------------------------------------------------------------
51 | NPT_DataBuffer::NPT_DataBuffer
52 +---------------------------------------------------------------------*/
53 NPT_DataBuffer::NPT_DataBuffer(NPT_Size bufferSize) :
54 m_BufferIsLocal(true),
55 m_Buffer(bufferSize?new NPT_Byte[bufferSize]:NULL),
56 m_BufferSize(bufferSize),
57 m_DataSize(0)
58 {
59 }
60
61 /*----------------------------------------------------------------------
62 | NPT_DataBuffer::NPT_DataBuffer
63 +---------------------------------------------------------------------*/
64 NPT_DataBuffer::NPT_DataBuffer(const void* data, NPT_Size data_size, bool copy) :
65 m_BufferIsLocal(copy),
66 m_Buffer(copy?(data_size?new NPT_Byte[data_size]:NULL):reinterpret_cast<NPT_Byte*>(const_cast<void*>(data))),
67 m_BufferSize(data_size),
68 m_DataSize(data_size)
69 {
70 if (copy && data_size) NPT_CopyMemory(m_Buffer, data, data_size);
71 }
72
73 /*----------------------------------------------------------------------
74 | NPT_DataBuffer::NPT_DataBuffer
75 +---------------------------------------------------------------------*/
76 NPT_DataBuffer::NPT_DataBuffer(const NPT_DataBuffer& other) :
77 m_BufferIsLocal(true),
78 m_Buffer(NULL),
79 m_BufferSize(other.m_DataSize),
80 m_DataSize(other.m_DataSize)
81 {
82 if (m_BufferSize) {
83 m_Buffer = new NPT_Byte[m_BufferSize];
84 NPT_CopyMemory(m_Buffer, other.m_Buffer, m_BufferSize);
85 }
86 }
87
88 /*----------------------------------------------------------------------
89 | NPT_DataBuffer::~NPT_DataBuffer
90 +---------------------------------------------------------------------*/
91 NPT_DataBuffer::~NPT_DataBuffer()
92 {
93 Clear();
94 }
95
96 /*----------------------------------------------------------------------
97 | NPT_DataBuffer::Clear
98 +---------------------------------------------------------------------*/
99 NPT_Result
100 NPT_DataBuffer::Clear()
101 {
102 if (m_BufferIsLocal) {
103 delete[] m_Buffer;
104 }
105 m_Buffer = NULL;
106 m_DataSize = 0;
107 m_BufferSize = 0;
108
109 return NPT_SUCCESS;
110 }
111
112 /*----------------------------------------------------------------------
113 | NPT_DataBuffer::operator=
114 +---------------------------------------------------------------------*/
115 NPT_DataBuffer&
116 NPT_DataBuffer::operator=(const NPT_DataBuffer& copy)
117 {
118 // do nothing if we're assigning to ourselves
119 if (this != &copy) {
120 Clear();
121
122 m_BufferIsLocal = true;
123 m_BufferSize = copy.m_BufferSize;
124 m_DataSize = copy.m_DataSize;
125
126 if (m_BufferSize) {
127 m_Buffer = new NPT_Byte[m_BufferSize];
128 NPT_CopyMemory(m_Buffer, copy.m_Buffer, m_BufferSize);
129 }
130 }
131 return *this;
132 }
133
134 /*----------------------------------------------------------------------
135 | NPT_DataBuffer::operator==
136 +---------------------------------------------------------------------*/
137 bool
138 NPT_DataBuffer::operator==(const NPT_DataBuffer& other) const
139 {
140 // check that the sizes match
141 if (m_DataSize != other.m_DataSize) return false;
142
143 return NPT_MemoryEqual(m_Buffer,
144 other.m_Buffer,
145 m_DataSize);
146 }
147
148 /*----------------------------------------------------------------------
149 | NPT_DataBuffer::SetBuffer
150 +---------------------------------------------------------------------*/
151 NPT_Result
152 NPT_DataBuffer::SetBuffer(NPT_Byte* buffer, NPT_Size buffer_size)
153 {
154 Clear();
155
156 // we're now using an external buffer
157 m_BufferIsLocal = false;
158 m_Buffer = buffer;
159 m_BufferSize = buffer_size;
160 m_DataSize = 0;
161
162 return NPT_SUCCESS;
163 }
164
165 /*----------------------------------------------------------------------
166 | NPT_DataBuffer::SetBufferSize
167 +---------------------------------------------------------------------*/
168 NPT_Result
169 NPT_DataBuffer::SetBufferSize(NPT_Size buffer_size)
170 {
171 if (m_BufferIsLocal) {
172 return ReallocateBuffer(buffer_size);
173 } else {
174 return NPT_ERROR_NOT_SUPPORTED; // you cannot change the
175 // buffer management mode
176 }
177 }
178
179 /*----------------------------------------------------------------------
180 | NPT_DataBuffer::Reserve
181 +---------------------------------------------------------------------*/
182 NPT_Result
183 NPT_DataBuffer::Reserve(NPT_Size size)
184 {
185 if (size <= m_BufferSize) return NPT_SUCCESS;
186
187 // try doubling the buffer to accomodate for the new size
188 NPT_Size new_size = m_BufferSize*2;
189 if (new_size < size) new_size = size;
190 return SetBufferSize(new_size);
191 }
192
193 /*----------------------------------------------------------------------
194 | NPT_DataBuffer::SetDataSize
195 +---------------------------------------------------------------------*/
196 NPT_Result
197 NPT_DataBuffer::SetDataSize(NPT_Size size)
198 {
199 if (size > m_BufferSize) {
200 // the buffer is too small, we need to reallocate it
201 if (m_BufferIsLocal) {
202 NPT_CHECK(ReallocateBuffer(size));
203 } else {
204 // we cannot reallocate an external buffer
205 return NPT_ERROR_NOT_SUPPORTED;
206 }
207 }
208 m_DataSize = size;
209 return NPT_SUCCESS;
210 }
211
212 /*----------------------------------------------------------------------
213 | NPT_DataBuffer::SetData
214 +---------------------------------------------------------------------*/
215 NPT_Result
216 NPT_DataBuffer::SetData(const NPT_Byte* data, NPT_Size size)
217 {
218 if (size > m_BufferSize) {
219 if (m_BufferIsLocal) {
220 NPT_CHECK(ReallocateBuffer(size));
221 } else {
222 return NPT_ERROR_INVALID_STATE;
223 }
224 }
225 if (data) NPT_CopyMemory(m_Buffer, data, size);
226 m_DataSize = size;
227
228 return NPT_SUCCESS;
229 }
230
231 /*----------------------------------------------------------------------
232 | NPT_DataBuffer::ReallocateBuffer
233 +---------------------------------------------------------------------*/
234 NPT_Result
235 NPT_DataBuffer::ReallocateBuffer(NPT_Size size)
236 {
237 // check that the existing data fits
238 if (m_DataSize > size) return NPT_ERROR_INVALID_PARAMETERS;
239
240 // allocate a new buffer
241 NPT_Byte* newBuffer = new NPT_Byte[size];
242
243 // copy the contents of the previous buffer, if any
244 if (m_Buffer && m_DataSize) {
245 NPT_CopyMemory(newBuffer, m_Buffer, m_DataSize);
246 }
247
248 // destroy the previous buffer
249 delete[] m_Buffer;
250
251 // use the new buffer
252 m_Buffer = newBuffer;
253 m_BufferSize = size;
254
255 return NPT_SUCCESS;
256 }