xoreos  0.0.5
memwritestream.h
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 /* Based on ScummVM (<http://scummvm.org>) code, which is released
26  * under the terms of version 2 or later of the GNU General Public
27  * License.
28  *
29  * The original copyright note in ScummVM reads as follows:
30  *
31  * ScummVM is the legal property of its developers, whose names
32  * are too numerous to list here. Please refer to the COPYRIGHT
33  * file distributed with this source distribution.
34  *
35  * This program is free software; you can redistribute it and/or
36  * modify it under the terms of the GNU General Public License
37  * as published by the Free Software Foundation; either version 2
38  * of the License, or (at your option) any later version.
39  *
40  * This program is distributed in the hope that it will be useful,
41  * but WITHOUT ANY WARRANTY; without even the implied warranty of
42  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
43  * GNU General Public License for more details.
44  *
45  * You should have received a copy of the GNU General Public License
46  * along with this program; if not, write to the Free Software
47  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
48  */
49 
50 #ifndef COMMON_MEMWRITESTREAM_H
51 #define COMMON_MEMWRITESTREAM_H
52 
53 #include <boost/noncopyable.hpp>
54 
55 #include "src/common/types.h"
57 #include "src/common/writestream.h"
58 
59 namespace Common {
60 
66 class MemoryWriteStream : boost::noncopyable, public SeekableWriteStream {
67 public:
68  MemoryWriteStream(byte *buf, size_t len) : _ptr(buf), _bufSize(len), _pos(0) { }
70 
72  template<size_t N>
73  MemoryWriteStream(byte (&array)[N]) : _ptr(array), _bufSize(N), _pos(0) { }
74 
75  size_t write(const void *dataPtr, size_t dataSize);
76 
78  size_t pos() const;
80  size_t size() const;
81 
83  size_t seek(ptrdiff_t offset, Origin whence = kOriginBegin);
84 
85 private:
87 
88  const size_t _bufSize;
89  size_t _pos;
90 };
91 
96 class MemoryWriteStreamDynamic : boost::noncopyable, public SeekableWriteStream {
97 public:
98  MemoryWriteStreamDynamic(bool disposeMemory = false, size_t capacity = 0);
100 
101  void reserve(size_t s);
102 
103  size_t write(const void *dataPtr, size_t dataSize);
104 
105  void setDisposable(bool disposeMemory);
106  void dispose();
107 
109  size_t pos() const;
111  size_t size() const;
112 
114  size_t seek(ptrdiff_t offset, Origin whence = kOriginBegin);
115 
116  byte *getData();
117 
118 private:
120 
122 
123  size_t _capacity;
124  size_t _size;
125 
126  void ensureCapacity(size_t newLen);
127 };
128 
129 } // End of namespace Common
130 
131 #endif // COMMON_MEMWRITESTREAM_H
MemoryWriteStream(byte(&array)[N])
Template constructor to create a MemoryWriteStream around an array buffer.
Definition: 2dafile.h:39
A stream that dynamically grows as it&#39;s written to.
size_t seek(ptrdiff_t offset, Origin whence=kOriginBegin)
Seek offset bytes from the origin whence.
MemoryWriteStreamDynamic(bool disposeMemory=false, size_t capacity=0)
size_t seek(ptrdiff_t offset, Origin whence=kOriginBegin)
Seek offset bytes from the origin whence.
DisposableArray< byte > _data
size_t size() const
Return the number of bytes written to this stream in total.
Basic writing stream interfaces.
Low-level type definitions to handle fixed width types portably.
void setDisposable(bool disposeMemory)
size_t write(const void *dataPtr, size_t dataSize)
Write data into the stream.
size_t size() const
Return the total size of the memory block.
A smart pointer with a deletion flag.
size_t write(const void *dataPtr, size_t dataSize)
Write data into the stream.
Origin
The position a seeking offset takes as a base.
Definition: writestream.h:218
MemoryWriteStream(byte *buf, size_t len)
size_t pos() const
Return the current writing position within the stream.
size_t pos() const
Return the current writing position within the memory block.
Simple memory based &#39;stream&#39;, which implements the SeekableWriteStream interface for a plain memory b...
uint8 byte
Definition: types.h:209
Seek from the begin of the stream.
Definition: writestream.h:219